Edwin BradySome buffer updates2465e5a6 years agoHistory
136 lines · 4.3 KB · Text
RawDownload
module Data.Buffer import System.File exportdata Buffer : Type where     MkBuffer : AnyPtr -> (size : Int) -> (loc : Int) -> Buffer exportrawSize : Buffer -> IO IntrawSize (MkBuffer buf _ _)    = schemeCall Int "blodwen-buffer-size" [buf] exportnewBuffer : Int -> IO (Maybe Buffer)newBuffer size    = do buf <- schemeCall AnyPtr "blodwen-new-buffer" [size]         sz <- schemeCall Int "blodwen-buffer-size" [buf]         if sz == 0            then pure Nothing            else pure $ Just $ MkBuffer buf size 0 exportresetBuffer : Buffer -> BufferresetBuffer (MkBuffer ptr s l) = MkBuffer ptr s 0 exportsize : Buffer -> Intsize (MkBuffer _ s _) = s -- Assumes val is in the range 0-255exportsetByte : Buffer -> (loc : Int) -> (val : Int) -> IO ()setByte (MkBuffer buf _ _) loc val    = schemeCall () "blodwen-buffer-setbyte" [buf, loc, val] exportgetByte : Buffer -> (loc : Int) -> IO IntgetByte (MkBuffer buf _ _) loc    = schemeCall Int "blodwen-buffer-getbyte" [buf, loc] exportsetInt : Buffer -> (loc : Int) -> (val : Int) -> IO ()setInt (MkBuffer buf _ _) loc val    = schemeCall () "blodwen-buffer-setint" [buf, loc, val] exportgetInt : Buffer -> (loc : Int) -> IO IntgetInt (MkBuffer buf _ _) loc    = schemeCall Int "blodwen-buffer-getint" [buf, loc] exportsetDouble : Buffer -> (loc : Int) -> (val : Double) -> IO ()setDouble (MkBuffer buf _ _) loc val    = schemeCall () "blodwen-buffer-setdouble" [buf, loc, val] exportgetDouble : Buffer -> (loc : Int) -> IO DoublegetDouble (MkBuffer buf _ _) loc    = schemeCall Double "blodwen-buffer-getdouble" [buf, loc] -- Get the length of a string in bytes, rather than charactersexport%foreign "scheme:blodwen-stringbytelen"stringByteLength : String -> Int exportsetString : Buffer -> (loc : Int) -> (val : String) -> IO ()setString (MkBuffer buf _ _) loc val    = schemeCall () "blodwen-buffer-setstring" [buf, loc, val] exportgetString : Buffer -> (loc : Int) -> (len : Int) -> IO StringgetString (MkBuffer buf _ _) loc len    = schemeCall String "blodwen-buffer-getstring" [buf, loc, len] exportbufferData : 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) exportcopyData : (src : Buffer) -> (start, len : Int) ->           (dest : Buffer) -> (loc : Int) -> IO ()copyData (MkBuffer src _ _) start len (MkBuffer dest _ _) loc    = schemeCall () "blodwen-buffer-copydata" [src,start,len,dest,loc] exportreadBufferFromFile : BinaryFile -> Buffer -> (maxbytes : Int) ->                     IO (Either FileError Buffer)readBufferFromFile (FHandle h) (MkBuffer buf size loc) max    = do read <- schemeCall Int "blodwen-readbuffer-bytes" [h, buf, loc, max]         if read >= 0            then pure (Right (MkBuffer buf size (loc + read)))            else pure (Left FileReadError) -- Create a new buffer by reading all the contents from the given file-- Fails if no bytes can be read or buffer can't be createdexportcreateBufferFromFile : BinaryFile -> IO (Either FileError Buffer)createBufferFromFile (FHandle h)    = do buf <- schemeCall AnyPtr "blodwen-readbuffer" [h]         sz <- schemeCall Int "blodwen-buffer-size" [buf]         if sz >= 0            then pure (Right (MkBuffer buf sz sz))            else pure (Left FileReadError) exportwriteBufferToFile : BinaryFile -> Buffer -> (maxbytes : Int) ->                    IO (Either FileError Buffer)writeBufferToFile (FHandle h) (MkBuffer buf size loc) max    = do let maxwrite = size - loc         let max' = if maxwrite < max then maxwrite else max         written <- schemeCall Int "blodwen-writebuffer" [h, buf, loc, max']         if written == max'            then pure (Right (MkBuffer buf size (loc + max')))            else pure (Left FileWriteError) exportresizeBuffer : Buffer -> Int -> IO (Maybe Buffer)resizeBuffer old newsize    = do Just buf <- newBuffer newsize              | Nothing => pure Nothing         -- If the new buffer is smaller than the old one, just copy what         -- fits         let oldsize = size old         let len = if newsize < oldsize then newsize else oldsize         copyData old 0 len buf 0         pure (Just buf)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t