on master help sign in
Get and set Double in Data.Buffer
578134cEdwin Brady
libs/base/Data/Buffer.idr+18
@@ -60,6 +60,15 @@ setInt b loc val
60 = foreign FFI_C "idris_setBufferInt" (ManagedPtr -> Int -> Int -> IO ())
60 = foreign FFI_C "idris_setBufferInt" (ManagedPtr -> Int -> Int -> IO ())
61 (rawdata b) loc val
61 (rawdata b) loc val
62
62
63+||| Set the double at position 'loc' to 'val'.
64+||| Uses 8 bytes (assumes 64 bit double).
65+||| Does nothing if the location is outside the bounds of the buffer
66+export
67+setDouble : Buffer -> (loc : Int) -> (val : Double) -> IO ()
68+setDouble b loc val
69+ = foreign FFI_C "idris_setBufferDouble" (ManagedPtr -> Int -> Double -> IO ())
70+ (rawdata b) loc val
71+
63||| Set the byte at position 'loc' to 'val'.
72||| Set the byte at position 'loc' to 'val'.
64||| Does nothing if the location is out of bounds of the buffer, or the string
73||| Does nothing if the location is out of bounds of the buffer, or the string
65||| is too long for the location
74||| is too long for the location
@@ -96,6 +105,15 @@ getInt b loc
96 = foreign FFI_C "idris_getBufferInt" (ManagedPtr -> Int -> IO Int)
105 = foreign FFI_C "idris_getBufferInt" (ManagedPtr -> Int -> IO Int)
97 (rawdata b) loc
106 (rawdata b) loc
98
107
108+||| Return the value at the given location in the buffer, assuming 8
109+||| bytes to store the Double.
110+||| Returns 0 if out of bounds.
111+export
112+getDouble : Buffer -> (loc : Int) -> IO Double
113+getDouble b loc
114+ = foreign FFI_C "idris_getBufferDouble" (ManagedPtr -> Int -> IO Double)
115+ (rawdata b) loc
116+
99||| Return the string at the given location in the buffer, with the given
117||| Return the string at the given location in the buffer, with the given
100||| length. Returns "" if out of bounds.
118||| length. Returns "" if out of bounds.
101export
119export
rts/idris_buffer.c+27
@@ -49,6 +49,17 @@ void idris_setBufferInt(void* buffer, int loc, int val) {
49 }
49 }
50}
50}
51
51
52+void idris_setBufferDouble(void* buffer, int loc, double val) {
53+ Buffer* b = buffer;
54+ // I am not proud of this
55+ if (loc >= 0 && loc + sizeof(double) < b->size) {
56+ unsigned char* c = (unsigned char*)(& val);
57+ for (int i = 0; i < sizeof(double); ++i) {
58+ b->data[loc+i] = c[i];
59+ }
60+ }
61+}
62+
52void idris_setBufferString(void* buffer, int loc, char* str) {
63void idris_setBufferString(void* buffer, int loc, char* str) {
53 Buffer* b = buffer;
64 Buffer* b = buffer;
54 int len = strlen(str);
65 int len = strlen(str);
@@ -79,6 +90,22 @@ int idris_getBufferInt(void* buffer, int loc) {
79 }
90 }
80}
91}
81
92
93+double idris_getBufferDouble(void* buffer, int loc) {
94+ Buffer* b = buffer;
95+ double d;
96+ // I am even less proud of this
97+ unsigned char *c = (unsigned char*)(& d);
98+ if (loc >= 0 && loc + sizeof(double) < b->size) {
99+ for (int i = 0; i < sizeof(double); ++i) {
100+ c[i] = b->data[loc+i];
101+ }
102+ return d;
103+ }
104+ else {
105+ return 0;
106+ }
107+}
108+
82VAL idris_getBufferString(void* buffer, int loc, int len) {
109VAL idris_getBufferString(void* buffer, int loc, int len) {
83 Buffer* b = buffer;
110 Buffer* b = buffer;
84
111
rts/idris_buffer.h+3−1
@@ -11,7 +11,8 @@ void* idris_newBuffer(int bytes);
11int idris_getBufferSize(void* buffer);
11int idris_getBufferSize(void* buffer);
12
12
13void idris_setBufferByte(void* buffer, int loc, uint8_t byte);
13void idris_setBufferByte(void* buffer, int loc, uint8_t byte);
14−void idris_setBufferInt(void* buffer, int loc, int byte);
14+void idris_setBufferInt(void* buffer, int loc, int val);
15+void idris_setBufferDouble(void* buffer, int loc, double val);
15void idris_setBufferString(void* buffer, int loc, char* str);
16void idris_setBufferString(void* buffer, int loc, char* str);
16
17
17void idris_copyBuffer(void* from, int start, int len,
18void idris_copyBuffer(void* from, int start, int len,
@@ -22,6 +23,7 @@ void idris_writeBuffer(FILE* h, void* buffer, int loc, int len);
22
23
23uint8_t idris_getBufferByte(void* buffer, int loc);
24uint8_t idris_getBufferByte(void* buffer, int loc);
24int idris_getBufferInt(void* buffer, int loc);
25int idris_getBufferInt(void* buffer, int loc);
26+double idris_getBufferDouble(void* buffer, int loc);
25VAL idris_getBufferString(void* buffer, int loc, int len);
27VAL idris_getBufferString(void* buffer, int loc, int len);
26
28
27#endif
29#endif
test/buffer002/buffer002.idr+7
@@ -6,12 +6,19 @@ main = do Just buf <- newBuffer 40
6 setInt buf 5 1234567890
6 setInt buf 5 1234567890
7 setInt buf 36 1234567890
7 setInt buf 36 1234567890
8 setString buf 20 "Hello world!"
8 setString buf 20 "Hello world!"
9+
10+ setDouble buf 10 123.456
11+
9 printLn !(bufferData buf)
12 printLn !(bufferData buf)
10
13
11 val <- getInt buf 5
14 val <- getInt buf 5
12 printLn val
15 printLn val
13 val <- getInt buf 6
16 val <- getInt buf 6
14 printLn val
17 printLn val
18+
19+ val <- getDouble buf 10
20+ printLn val
21+
15 val <- getInt buf 36
22 val <- getInt buf 36
16 printLn val
23 printLn val
17 val <- getInt buf 37 -- out of bounds, expect 0
24 val <- getInt buf 37 -- out of bounds, expect 0
test/buffer002/expected+2−1
@@ -1,7 +1,8 @@
140
140
2−[00, 00, 00, 00, 00, D2, 02, 96, 49, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 48, 65, 6C, 6C, 6F, 20, 77, 6F, 72, 6C, 64, 21, 00, 00, 00, 00, D2, 02, 96, 49]
2+[00, 00, 00, 00, 00, D2, 02, 96, 49, 00, 77, BE, 9F, 1A, 2F, DD, 5E, 40, 00, 00, 48, 65, 6C, 6C, 6F, 20, 77, 6F, 72, 6C, 64, 21, 00, 00, 00, 00, D2, 02, 96, 49]
31234567890
31234567890
44822530
44822530
5+123.456
51234567890
61234567890
60
70
7Hello worl
8Hello worl