@@ -2,7 +2,11 @@ module Data.Buffer
22
33%include C "idris_buffer.h"
44
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
9+||| advance.
610export
711record Buffer where
812 constructor MkBuffer
@@ -47,6 +51,15 @@ setByte b loc val
4751 = foreign FFI_C "idris_setBufferByte" (ManagedPtr -> Int -> Bits8 -> IO ())
4852 (rawdata b) loc val
4953
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
57+export
58+setInt : Buffer -> (loc : Int) -> (val : Int) -> IO ()
59+setInt b loc val
60+ = foreign FFI_C "idris_setBufferInt" (ManagedPtr -> Int -> Int -> IO ())
61+ (rawdata b) loc val
62+
5063||| Set the byte at position 'loc' to 'val'.
5164||| Does nothing if the location is out of bounds of the buffer, or the string
5265||| is too long for the location
@@ -66,13 +79,32 @@ copyData src start len dest loc
6679 = foreign FFI_C "idris_copyBuffer" (ManagedPtr -> Int -> Int -> ManagedPtr -> Int -> IO ())
6780 (rawdata src) start len (rawdata dest) loc
6881
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.
7084export
7185getByte : Buffer -> (loc : Int) -> IO Bits8
7286getByte b loc
7387 = foreign FFI_C "idris_getBufferByte" (ManagedPtr -> Int -> IO Bits8)
7488 (rawdata b) loc
7589
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.
93+export
94+getInt : Buffer -> (loc : Int) -> IO Int
95+getInt b loc
96+ = foreign FFI_C "idris_getBufferInt" (ManagedPtr -> Int -> IO Int)
97+ (rawdata b) loc
98+
99+||| Return the string at the given location in the buffer, with the given
100+||| length. Returns "" if out of bounds.
101+export
102+getString : Buffer -> (loc : Int) -> (len : Int) -> IO String
103+getString b loc len
104+ = do MkRaw str <- foreign FFI_C "idris_getBufferString" (ManagedPtr -> Int -> Int -> IO (Raw String))
105+ (rawdata b) loc len
106+ pure str
107+
76108||| Read 'maxbytes' into the buffer from a file, returning a new
77109||| buffer with the 'locaton' pointer moved along
78110export