-
Notifications
You must be signed in to change notification settings - Fork 9
/
fwk_data.c
226 lines (204 loc) Β· 6.95 KB
/
fwk_data.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
static array(json5) roots;
static array(char*) sources;
bool json_push(const char *source) {
char *source_rw = STRDUP(source);
json5 root = {0};
char *error = json5_parse(&root, source_rw, 0);
if( error ) {
FREE(source_rw);
return false;
} else {
array_push(sources, source_rw);
array_push(roots, root);
return true;
}
}
bool json_pop() {
if( array_count(roots) > 0 ) {
FREE(*array_back(sources));
array_pop(sources);
json5_free(array_back(roots));
array_pop(roots);
return true;
}
return false;
}
json5* json_node(const char *keypath) {
json5 *j = array_back(roots), *r = j;
for each_substring( keypath, "/[.]", key ) {
r = 0;
/**/ if( j->type == JSON5_ARRAY ) r = j = &j->array[atoi(key)];
else if( j->type == JSON5_OBJECT && isdigit(key[0]) )
for( int i = 0, seq = atoi(key); !r && i < j->count; ++i ) {
if( i == seq ) {
r = j = &j->nodes[i];
break;
}
}
else if( j->type == JSON5_OBJECT )
for( int i = 0; !r && i < j->count; ++i ) {
if( j->nodes[i].name && !strcmp(j->nodes[i].name, key) ) {
r = j = &j->nodes[i];
break;
}
}
if( !j ) break;
}
return r;
}
int (json_count)(const char *keypath) {
json5* j = json_node(keypath);
return j ? j->count : 0;
}
json_t *json_find(const char *type_keypath) {
char type = type_keypath[0];
const char *key = type_keypath+1;
json5 *j = json_node(key);
if( !j ) return NULL;
static __thread int slot = 0;
static __thread json_t buf[128] = {0};
slot = (slot+1) % 128;
json_t *v = &buf[slot];
v->i = j ? j->integer : 0;
if(type == 's' && (!v->p || j->type == JSON5_NULL)) v->s = ""; // if_null_string
if(type == 'f' && j && j->type == JSON5_INTEGER) v->f = j->integer;
return v;
}
json_t json_get(const char *type_keypath) {
char type = type_keypath[0];
const char *key = type_keypath+1;
json5 *j = json_node(key);
json_t v = {0};
v.i = j ? j->integer : 0;
if(type == 's' && (!v.p || j->type == JSON5_NULL)) v.s = ""; // if_null_string
if(type == 'f' && j && j->type == JSON5_INTEGER) v.f = j->integer;
return v;
}
const char *(json_key)(const char *keypath) {
json5 *j = json_node(keypath);
if( !j ) return "";
return j->name;
}
// xml impl
static __thread array(char *) xml_sources;
static __thread array(struct xml *) xml_docs;
int xml_push(const char *xml_source) {
if( xml_source ) {
char *src = STRDUP(xml_source), *error = 0;
for( struct xml *doc = xml_parse(src, 0, &error); doc && !error; ) {
array_push(xml_docs, doc);
array_push(xml_sources, src);
return 1;
}
if( error ) PRINTF("%s\n", error);
FREE(src);
}
return 0;
}
void xml_pop() {
if( array_count(xml_docs) ) {
xml_free( *array_back(xml_docs) );
array_pop(xml_docs);
FREE( *array_back(xml_sources) );
array_pop(xml_sources);
}
}
static void *xml_path(struct xml *node, char *path, int down) {
if( !path || !path[0] ) return node;
if( node ) {
char type = path[0];
if( type == '/' ) {
int sep = strcspn(++path, "/[@$");
if( !sep ) type = path[0];
else
if( 1 ) { // path[ sep ] ) {
char tag[32]; snprintf(tag, 32, "%.*s", sep, path);
// Find the first sibling with the given tag name (may be the same node)
struct xml *next = down ? xml_find_down(node, tag) : xml_find(node, tag);
return xml_path(next, &path[ sep ], 1);
}
}
if( type == '$' ) {
return (void*)( node->down ? xml_text( node->down ) : xml_tag( node ) );
}
if( type == '@' ) {
return (void*)xml_att(node, ++path);
}
if( type == '[' ) {
for( int i = 0, end = atoi(++path); i < end; ++i ) { node = xml_find_next(node, xml_tag(node)); if(!node) return NULL; }
while( isdigit(path[0]) ) ++path;
return xml_path(node, ++path, 1);
}
}
return NULL;
}
const char *(xml_string)(char *key) {
struct xml *node = xml_path(*array_back(xml_docs), key, 0);
if( node && strchr(key, '@') ) return (const char *)node;
if( node && strchr(key, '$') ) return (const char *)node;
return "";
}
unsigned (xml_count)(char *key) {
struct xml *node = xml_path(*array_back(xml_docs), key, 0);
if( !node ) return 0;
const char *tag = xml_tag(node);
unsigned count = 1;
while( (node = xml_find_next(node, tag)) != 0) ++count;
return count;
}
array(char) (xml_blob)(char *key) { // base64 blob
struct xml *node = xml_path(*array_back(xml_docs), key, 0);
if( !node ) return 0;
if( !strchr(key, '$') ) return 0;
const char *data = (const char*)node;
array(char) out = base64_decode(data, strlen(data)); // either array of chars (ok) or null (error)
return out;
}
bool data_tests() {
// data tests (json5)
const char json5[] =
" /* json5 */ // comment\n"
" abc: 42.67, def: true, integer:0x100 \n"
" huge: 2.2239333e5, \n"
" hello: 'world /*comment in string*/ //again', \n"
" children : { a: 1, b: 2, c: 3 },\n"
" array: [+1,2,-3,4,5], \n"
" invalids : [ nan, NaN, -nan, -NaN, inf, Infinity, -inf, -Infinity ],";
if( json_push(json5) ) {
assert( json_float("/abc") == 42.67 );
assert( json_int("/def") == 1 );
assert( json_int("/integer") == 0x100 );
assert( json_float("/huge") > 2.22e5 );
assert( strlen(json_string("/hello")) == 35 );
assert( json_int("/children/a") == 1 );
assert( json_int("/children.b") == 2 );
assert( json_int("/children[c]") == 3 );
assert( json_int("/array[%d]", 2) == -3 );
assert( json_count("/invalids") == 8 );
assert( isnan(json_float("/invalids[0]")) );
assert( !json_find("/non_existing") );
assert( PRINTF("json5 tests OK\n") );
json_pop();
}
// data tests (xml)
const char *xml = // vfs_read("test1.xml");
"<!-- XML representation of a person record -->"
"<person created=\"2006-11-11T19:23\" modified=\"2006-12-31T23:59\">"
" <firstName>Robert</firstName>"
" <lastName>Smith</lastName>"
" <address type=\"home\">"
" <street>12345 Sixth Ave</street>"
" <city>Anytown</city>"
" <state>CA</state>"
" <postalCode>98765-4321</postalCode>"
" </address>"
"</person>";
if( xml_push(xml) ) {
assert( !strcmp("Robert", xml_string("/person/firstName/$")) );
assert( !strcmp("Smith", xml_string("/person/lastName/$")) );
assert( !strcmp("home", xml_string("/person/address/@type")) );
assert( PRINTF("xml tests OK\n") );
xml_pop();
}
return true;
}