-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathreading.c
77 lines (56 loc) · 1.51 KB
/
reading.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
This file shows the basics of using binn objects. It does not show lists and maps.
For more examples check https://github.com/liteserver/binn/blob/master/usage.md
*/
#include <binn.h>
/*
** option 1: use the raw buffer pointer
*/
void read_example_1(void *buf) {
int id;
char *name;
double price;
id = binn_object_int32(buf, "id");
name = binn_object_str(buf, "name");
price = binn_object_double(buf, "price");
}
/*
** option 2: load the raw buffer to a binn pointer
*/
void read_example_2(void *buf) {
int id;
char *name;
double price;
binn *obj;
obj = binn_open(buf);
if (obj == 0) return;
id = binn_object_int32(obj, "id");
name = binn_object_str(obj, "name");
price = binn_object_double(obj, "price");
binn_free(obj); /* releases the binn pointer but NOT the received buf */
}
/*
** option 3: use the current created object
**
** this is used when both the writer and the reader are in the same app
*/
void read_example_3a(binn *obj) {
int id;
char *name;
double price;
id = binn_object_int32(obj, "id");
name = binn_object_str(obj, "name");
price = binn_object_double(obj, "price");
}
/* almost the same but in this case the binn pointer is returned from another function */
void read_example_3b() {
int id;
char *name;
double price;
binn *obj;
obj = some_function(); /* this function should return a binn pointer */
id = binn_object_int32(obj, "id");
name = binn_object_str(obj, "name");
price = binn_object_double(obj, "price");
binn_free(obj);
}