2019-06-15 13:54:22 +03:00
|
|
|
module Data.Buffer
|
|
|
|
|
|
|
|
import System.File
|
|
|
|
|
|
|
|
export
|
|
|
|
data Buffer : Type where
|
2019-09-04 12:25:45 +03:00
|
|
|
MkBuffer : AnyPtr -> (size : Int) -> (loc : Int) -> Buffer
|
2019-06-15 13:54:22 +03:00
|
|
|
|
|
|
|
export
|
|
|
|
newBuffer : Int -> IO Buffer
|
|
|
|
newBuffer size
|
2019-09-04 12:25:45 +03:00
|
|
|
= do buf <- schemeCall AnyPtr "blodwen-new-buffer" [size]
|
2019-06-15 13:54:22 +03:00
|
|
|
pure (MkBuffer buf size 0)
|
|
|
|
|
|
|
|
export
|
|
|
|
rawSize : Buffer -> IO Int
|
|
|
|
rawSize (MkBuffer buf _ _)
|
|
|
|
= schemeCall Int "blodwen-buffer-size" [buf]
|
|
|
|
|
|
|
|
export
|
|
|
|
size : Buffer -> Int
|
|
|
|
size (MkBuffer _ s _) = s
|
|
|
|
|
|
|
|
-- Assumes val is in the range 0-255
|
|
|
|
export
|
|
|
|
setByte : Buffer -> (loc : Int) -> (val : Int) -> IO ()
|
|
|
|
setByte (MkBuffer buf _ _) loc val
|
|
|
|
= schemeCall () "blodwen-buffer-setbyte" [buf, loc, val]
|
|
|
|
|
|
|
|
export
|
|
|
|
getByte : Buffer -> (loc : Int) -> IO Int
|
|
|
|
getByte (MkBuffer buf _ _) loc
|
|
|
|
= schemeCall Int "blodwen-buffer-getbyte" [buf, loc]
|
|
|
|
|
|
|
|
export
|
|
|
|
setInt : Buffer -> (loc : Int) -> (val : Int) -> IO ()
|
|
|
|
setInt (MkBuffer buf _ _) loc val
|
|
|
|
= schemeCall () "blodwen-buffer-setint" [buf, loc, val]
|
|
|
|
|
|
|
|
export
|
|
|
|
getInt : Buffer -> (loc : Int) -> IO Int
|
|
|
|
getInt (MkBuffer buf _ _) loc
|
|
|
|
= schemeCall Int "blodwen-buffer-getint" [buf, loc]
|
|
|
|
|
|
|
|
export
|
|
|
|
setDouble : Buffer -> (loc : Int) -> (val : Double) -> IO ()
|
|
|
|
setDouble (MkBuffer buf _ _) loc val
|
|
|
|
= schemeCall () "blodwen-buffer-setdouble" [buf, loc, val]
|
|
|
|
|
|
|
|
export
|
|
|
|
getDouble : Buffer -> (loc : Int) -> IO Double
|
|
|
|
getDouble (MkBuffer buf _ _) loc
|
|
|
|
= schemeCall Double "blodwen-buffer-getdouble" [buf, loc]
|
|
|
|
|
|
|
|
export
|
|
|
|
setString : Buffer -> (loc : Int) -> (val : String) -> IO ()
|
|
|
|
setString (MkBuffer buf _ _) loc val
|
|
|
|
= schemeCall () "blodwen-buffer-setstring" [buf, loc, val]
|
|
|
|
|
|
|
|
export
|
|
|
|
getString : Buffer -> (loc : Int) -> (len : Int) -> IO String
|
|
|
|
getString (MkBuffer buf _ _) loc len
|
|
|
|
= schemeCall String "blodwen-buffer-getstring" [buf, loc, len]
|
|
|
|
|
|
|
|
export
|
|
|
|
bufferData : Buffer -> IO (List Int)
|
|
|
|
bufferData buf
|
|
|
|
= do len <- rawSize buf
|
|
|
|
unpackTo [] len
|
|
|
|
where
|
|
|
|
unpackTo : List Int -> Int -> IO (List Int)
|
|
|
|
unpackTo acc 0 = pure acc
|
|
|
|
unpackTo acc loc
|
|
|
|
= do val <- getByte buf (loc - 1)
|
|
|
|
unpackTo (val :: acc) (loc - 1)
|
|
|
|
|
|
|
|
export
|
2019-09-28 20:33:46 +03:00
|
|
|
readBufferFromFile : BinaryFile -> Buffer -> (maxbytes : Int) ->
|
|
|
|
IO (Either FileError Buffer)
|
2019-06-15 13:54:22 +03:00
|
|
|
readBufferFromFile (FHandle h) (MkBuffer buf size loc) max
|
|
|
|
= do read <- schemeCall Int "blodwen-readbuffer" [h, buf, loc, max]
|
2019-09-28 20:33:46 +03:00
|
|
|
if read >= 0
|
|
|
|
then pure (Right (MkBuffer buf size (loc + read)))
|
|
|
|
else pure (Left FileReadError)
|
2019-06-15 13:54:22 +03:00
|
|
|
|
|
|
|
export
|
2019-09-28 20:33:46 +03:00
|
|
|
writeBufferToFile : BinaryFile -> Buffer -> (maxbytes : Int) ->
|
|
|
|
IO (Either FileError Buffer)
|
2019-06-15 13:54:22 +03:00
|
|
|
writeBufferToFile (FHandle h) (MkBuffer buf size loc) max
|
|
|
|
= do let maxwrite = size - loc
|
|
|
|
let max' = if maxwrite < max then maxwrite else max
|
2019-09-28 20:33:46 +03:00
|
|
|
written <- schemeCall Int "blodwen-writebuffer" [h, buf, loc, max']
|
|
|
|
if written == max'
|
|
|
|
then pure (Right (MkBuffer buf size (loc + max')))
|
|
|
|
else pure (Left FileWriteError)
|