@@ -2,7 +2,11 @@ module Data.Buffer
3%include C "idris_buffer.h"
3%include C "idris_buffer.h"
5−||| A buffer is a pointer to a sized, unstructured, mutable chunk of memory
5+||| A buffer is a pointer to a sized, unstructured, mutable chunk of memory.
6+||| There are primitive operations for gettng and setting bytes, ints (32 bit)
7+||| and strings at a location in the buffer. These operations silently fail
8+||| if the location is out of bounds, so bounds checking should be done in
7record Buffer where
11record Buffer where
8 constructor MkBuffer
12 constructor MkBuffer
@@ -47,6 +51,15 @@ setByte b loc val
47 = foreign FFI_C "idris_setBufferByte" (ManagedPtr -> Int -> Bits8 -> IO ())
51 = foreign FFI_C "idris_setBufferByte" (ManagedPtr -> Int -> Bits8 -> IO ())
48 (rawdata b) loc val
52 (rawdata b) loc val
54+||| Set the int at position 'loc' to 'val'.
55+||| Uses 4 bytes (assumes up to 32 bit Int).
56+||| Does nothing if the location is outside the bounds of the buffer
58+setInt : Buffer -> (loc : Int) -> (val : Int) -> IO ()
60+ = foreign FFI_C "idris_setBufferInt" (ManagedPtr -> Int -> Int -> IO ())
50||| Set the byte at position 'loc' to 'val'.
63||| Set the byte at position 'loc' to 'val'.
51||| Does nothing if the location is out of bounds of the buffer, or the string
64||| Does nothing if the location is out of bounds of the buffer, or the string
52||| is too long for the location
65||| is too long for the location
@@ -66,13 +79,32 @@ copyData src start len dest loc
66 = foreign FFI_C "idris_copyBuffer" (ManagedPtr -> Int -> Int -> ManagedPtr -> Int -> IO ())
79 = foreign FFI_C "idris_copyBuffer" (ManagedPtr -> Int -> Int -> ManagedPtr -> Int -> IO ())
67 (rawdata src) start len (rawdata dest) loc
80 (rawdata src) start len (rawdata dest) loc
69−||| Return the value at the given location in the buffer
82+||| Return the value at the given location in the buffer.
83+||| Returns 0 if out of bounds.
71getByte : Buffer -> (loc : Int) -> IO Bits8
85getByte : Buffer -> (loc : Int) -> IO Bits8
72getByte b loc
86getByte b loc
73 = foreign FFI_C "idris_getBufferByte" (ManagedPtr -> Int -> IO Bits8)
87 = foreign FFI_C "idris_getBufferByte" (ManagedPtr -> Int -> IO Bits8)
74 (rawdata b) loc
88 (rawdata b) loc
90+||| Return the value at the given location in the buffer, assuming 4
91+||| bytes to store the Int.
92+||| Returns 0 if out of bounds.
94+getInt : Buffer -> (loc : Int) -> IO Int
96+ = foreign FFI_C "idris_getBufferInt" (ManagedPtr -> Int -> IO Int)
99+||| Return the string at the given location in the buffer, with the given
100+||| length. Returns "" if out of bounds.
102+getString : Buffer -> (loc : Int) -> (len : Int) -> IO String
104+ = do MkRaw str <- foreign FFI_C "idris_getBufferString" (ManagedPtr -> Int -> Int -> IO (Raw String))
76||| Read 'maxbytes' into the buffer from a file, returning a new
108||| Read 'maxbytes' into the buffer from a file, returning a new
77||| buffer with the 'locaton' pointer moved along
109||| buffer with the 'locaton' pointer moved along