-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcsonpath_do.h
More file actions
419 lines (363 loc) · 11 KB
/
csonpath_do.h
File metadata and controls
419 lines (363 loc) · 11 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#ifndef CSONPATH_DO_GET_NOTFOUND
#define CSONPATH_DO_GET_NOTFOUND(this_idx) \
do { \
walker += cjp->inst_lst[idx].next; \
while (cjp->inst_lst[++idx].inst != CSONPATH_INST_END) { \
if (cjp->inst_lst[idx].inst == CSONPATH_INST_OR) { \
walker += cjp->inst_lst[idx].next; \
goto next_inst; \
} \
walker += cjp->inst_lst[idx].next; \
} \
return CSONPATH_NONE_FOUND_RET; \
} while (0)
#endif
#ifndef CSONPATH_PRE_GET
#define CSONPATH_PRE_GET(this_idx)
#endif
#ifndef CSONPATH_PRE_GET_ROOT
#define CSONPATH_PRE_GET_ROOT
#endif
#ifndef CSONPATH_DO_DECLARATION
#define CSONPATH_DO_DECLARATION
#endif
#ifndef CSONPATH_DO_POST_FIND_OBJ
#define CSONPATH_DO_POST_FIND_OBJ
#endif
#ifndef CSONPATH_DO_POST_FIND_ARRAY
#define CSONPATH_DO_POST_FIND_ARRAY
#endif
#ifndef CSONPATH_DO_EXTRA_DECLATION
#define CSONPATH_DO_EXTRA_DECLATION
#endif
#ifndef CSONPATH_DO_FIND_ALL_CLEAUP
#define CSONPATH_DO_FIND_ALL_CLEAUP
#endif
#ifndef CSONPATH_DO_EXTRA_ARGS
#define CSONPATH_DO_EXTRA_ARGS
#endif
#ifndef CSONPATH_DO_EXTRA_ARGS_NEESTED
# ifdef CSONPATH_DO_EXTRA_ARGS_IN
# define CSONPATH_DO_EXTRA_ARGS_NEESTED CSONPATH_DO_EXTRA_ARGS_IN
# else
# define CSONPATH_DO_EXTRA_ARGS_NEESTED
# endif
#endif
#ifndef CSONPATH_DO_EXTRA_ARGS_FIND_ALL
#define CSONPATH_DO_EXTRA_ARGS_FIND_ALL CSONPATH_DO_EXTRA_ARGS_NEESTED
#endif
#ifndef CSONPATH_DO_EXTRA_ARGS_IN
#define CSONPATH_DO_EXTRA_ARGS_IN
#endif
#ifndef CSONPATH_DO_POST_OPERATION
#define CSONPATH_DO_POST_OPERATION
#endif
#ifndef CSONPATH_DO_PRE_OPERATION
#define CSONPATH_DO_PRE_OPERATION
#endif
#ifndef CSONPATH_DO_FIND_ALL_PRE_LOOP
#define CSONPATH_DO_FIND_ALL_PRE_LOOP
#endif
#ifndef CSONPATH_DO_FOREACH_PRE_SET
#define CSONPATH_DO_FOREACH_PRE_SET
#endif
#ifndef CSONPATH_DO_GET_ALL_OUT
#define CSONPATH_DO_GET_ALL_OUT CSONPATH_DO_FIND_ALL_OUT
#endif
#ifndef CSONPATH_DO_FILTER_OUT
#define CSONPATH_DO_FILTER_OUT CSONPATH_DO_FIND_ALL_OUT
#endif
#ifndef CSONPATH_DO_FILTER_PRE_LOOP
#define CSONPATH_DO_FILTER_PRE_LOOP
#endif
#ifndef CSONPATH_DO_FILTER_LOOP_PRE_SET
#define CSONPATH_DO_FILTER_LOOP_PRE_SET
#endif
#ifndef CSONPATH_DO_RANGE
#define CSONPATH_DO_RANGE CSONPATH_DO_FIND_ALL
#endif
#ifndef CSONPATH_DO_RANGE
#define CSONPATH_DO_RANGE CSONPATH_DO_FIND_ALL
#endif
#ifndef CSONPATH_DO_RANGE_PRE_LOOP
#define CSONPATH_DO_RANGE_PRE_LOOP CSONPATH_DO_FIND_ALL_PRE_LOOP
#endif
#ifndef CAT
# define CATCAT(a, b, c) a ## b ## c
# define CAT(a, b) a ## b
#endif
#define csonpath_do_internal__(name) CATCAT(csonpath_, name, _internal)
#define csonpath_do_internal csonpath_do_internal__(CSONPATH_DO_FUNC_NAME)
static CSONPATH_DO_RET_TYPE csonpath_do_internal(struct csonpath cjp[static 1],
CSONPATH_JSON origin,
CSONPATH_JSON value,
CSONPATH_JSON ctx,
int idx,
char *walker CSONPATH_DO_EXTRA_DECLATION);
#define csonpath_do_dotdot__(name) CATCAT(csonpath_, name, _dotdot)
#define csonpath_do_dotdot csonpath_do_dotdot__(CSONPATH_DO_FUNC_NAME)
static CSONPATH_DO_RET_TYPE csonpath_do_dotdot(struct csonpath cjp[static 1],
CSONPATH_JSON origin,
CSONPATH_JSON tmp,
CSONPATH_JSON ctx,
int idx,
char *walker CSONPATH_DO_EXTRA_DECLATION)
{
CSONPATH_JSON el;
CSONPATH_DO_DECLARATION;
const int is_obj = CSONPATH_IS_OBJ(tmp);
CSONPATH_DO_RET_TYPE tret = CSONPATH_NONE_FOUND_RET;
CSONPATH_DO_FIND_ALL_PRE_LOOP;
CSONPATH_FOREACH(tmp, el, {
CSONPATH_DO_FOREACH_PRE_SET;
if (is_obj && !strcmp((char *)key_idx, walker)) {
tret = csonpath_do_internal(cjp, origin, el, tmp, idx,
walker + cjp->inst_lst[idx].next
CSONPATH_DO_EXTRA_ARGS_NEESTED);
CSONPATH_DO_FIND_ALL;
}
if (CSONPATH_IS_OBJ(el) || CSONPATH_IS_ARRAY(el)) {
tret = csonpath_do_dotdot(cjp, origin, el, tmp, idx, walker
CSONPATH_DO_EXTRA_ARGS_NEESTED);
CSONPATH_DO_FIND_ALL;
}
})
CSONPATH_DO_FIND_ALL_OUT;
return tret;
}
static CSONPATH_DO_RET_TYPE csonpath_do_internal(struct csonpath cjp[static 1],
CSONPATH_JSON origin,
CSONPATH_JSON value,
CSONPATH_JSON ctx,
int idx,
char *walker CSONPATH_DO_EXTRA_DECLATION)
{
CSONPATH_JSON tmp = value;
CSONPATH_DO_DECLARATION;
(void)ctx; /* maybe unused */
while (cjp->inst_lst[idx].inst != CSONPATH_INST_END &&
cjp->inst_lst[idx].inst != CSONPATH_INST_OR) {
switch (cjp->inst_lst[idx].inst) {
case CSONPATH_INST_ROOT:
{
CSONPATH_PRE_GET_ROOT
value = origin;
tmp = value;
ctx = CSONPATH_NULL;
walker += cjp->inst_lst[idx].next;
}
break;
case CSONPATH_INST_FILTER_KEY_EQ:
case CSONPATH_INST_FILTER_KEY_REG_EQ:
case CSONPATH_INST_FILTER_KEY_NOT_EQ:
case CSONPATH_INST_FILTER_KEY_SUPERIOR:
case CSONPATH_INST_FILTER_KEY_INFERIOR:
{
CSONPATH_JSON el;
int operation_in = cjp->inst_lst[idx].inst, operation = cjp->inst_lst[idx].inst;
walker += cjp->inst_lst[idx].next;
char *owalker;
int filter_next_in = cjp->inst_lst[idx].filter_next, filter_next = cjp->inst_lst[idx].filter_next;
int walker_idx = idx + 1, in_idx = idx + 1;
int foreach_idx;
(void) foreach_idx;
CSONPATH_DO_FILTER_PRE_LOOP;
CSONPATH_FOREACH_ARRAY(tmp, el, foreach_idx)
{
intptr_t key_idx = foreach_idx;
(void)key_idx;
owalker = walker;
match_and_or_or:
if (CSONPATH_IS_OBJ(el)) {
CSONPATH_JSON el2 = el;
idx = walker_idx;
CSONPATH_DO_FILTER_LOOP_PRE_SET;
el2 = cosnpath_crawl_filter_el(cjp, &idx, &owalker, el2, filter_next);
if (csonpath_make_match(cjp, &cjp->inst_lst[idx], el2, owalker,
operation)) {
if (cjp->inst_lst[idx + 1].inst == CSONPATH_INST_FILTER_AND) {
++idx; /* skip next */
if (csonpath_is_endish_inst(cjp->inst_lst[idx + 1].inst) ||
csonpath_is_endish_inst(cjp->inst_lst[idx + 2].inst))
goto fail_match;
operation = cjp->inst_lst[idx +1].inst;
filter_next = cjp->inst_lst[idx +1].filter_next;
owalker += cjp->inst_lst[idx].next + cjp->inst_lst[idx-1].next;
walker_idx = idx + 2;
goto match_and_or_or;
}
CSONPATH_DO_RET_TYPE tret =
csonpath_do_internal(cjp, origin, el, tmp, idx + 1,
owalker + cjp->inst_lst[idx].next
CSONPATH_DO_EXTRA_ARGS_NEESTED);
CSONPATH_DO_FILTER_FIND;
} else {
fail_match:
operation = operation_in;
walker_idx = in_idx;
filter_next = filter_next_in;
}
}
}
CSONPATH_DO_FILTER_OUT;
break;
}
case CSONPATH_INST_FIND_ALL:
{
CSONPATH_DO_RET_TYPE tret =
csonpath_do_dotdot(cjp, origin, tmp, ctx, idx + 1,
walker CSONPATH_DO_EXTRA_ARGS_FIND_ALL);
CSONPATH_DO_FIND_ALL_CLEAUP;
return tret;
}
case CSONPATH_INST_RANGE:
{
if (!CSONPATH_IS_ARRAY(tmp)) {
return CSONPATH_NONE_FOUND_RET;
}
CSONPATH_JSON el;
intptr_t key_idx;
(void)key_idx;
int next_0 = cjp->inst_lst[idx].next;
int next_1 = cjp->inst_lst[idx + 1].next;
int next_2 = cjp->inst_lst[idx + 2].next;
unsigned int beg = csonpath_int_from_walker(cjp->inst_lst[idx + 1].inst, walker + next_0);
unsigned int end = csonpath_int_from_walker(cjp->inst_lst[idx + 2].inst,
walker + next_0 + next_1);
if (!end)
end = -1;
CSONPATH_DO_RANGE_PRE_LOOP;
CSONPATH_FOREACH_ARRAY(tmp, el, key_idx) {
if (key_idx < beg) {
key_idx = beg - 1;
continue;
}
if (key_idx >= end)
break;
CSONPATH_DO_FOREACH_PRE_SET
CSONPATH_DO_RET_TYPE tret =
csonpath_do_internal(cjp, origin, el, tmp, idx + 3,
walker + next_0 + next_1 + next_2
CSONPATH_DO_EXTRA_ARGS_NEESTED);
CSONPATH_DO_RANGE;
}
CSONPATH_DO_GET_ALL_OUT;
break;
}
case CSONPATH_INST_GET_ALL:
{
CSONPATH_JSON el;
CSONPATH_DO_FIND_ALL_PRE_LOOP;
CSONPATH_FOREACH(tmp, el, {
CSONPATH_DO_FOREACH_PRE_SET
CSONPATH_DO_RET_TYPE tret =
csonpath_do_internal(cjp, origin, el, tmp, idx + 1,
walker + cjp->inst_lst[idx].next
CSONPATH_DO_EXTRA_ARGS_NEESTED);
CSONPATH_DO_FIND_ALL;
});
CSONPATH_DO_GET_ALL_OUT;
break;
}
case CSONPATH_INST_GET_OBJ:
ctx = tmp;
CSONPATH_PRE_GET(walker);
tmp = CSONPATH_GET(tmp, walker);
if (tmp == CSONPATH_NULL) {
CSONPATH_DO_GET_NOTFOUND(walker);
}
CSONPATH_DO_POST_FIND_OBJ;
walker += cjp->inst_lst[idx].next;
break;
case CSONPATH_INST_GET_ARRAY_SMALL:
{
int this_idx;
ctx = tmp;
this_idx = (int)*walker;
CSONPATH_PRE_GET(this_idx);
tmp = CSONPATH_AT(tmp, this_idx);
if (tmp == CSONPATH_NULL) {
CSONPATH_DO_GET_NOTFOUND(this_idx);
}
walker += cjp->inst_lst[idx].next;
CSONPATH_DO_POST_FIND_ARRAY
break;
}
case CSONPATH_INST_GET_ARRAY_BIG:
ctx = tmp;
{
CSONPATH_UNUSED int this_idx;
union {int n; char c[4];} to_num =
{ .c= { walker[0], walker[1], walker[2], walker[3] } };
this_idx = to_num.n;
CSONPATH_PRE_GET(this_idx);
tmp = CSONPATH_AT(tmp, to_num.n);
if (tmp == CSONPATH_NULL) {
CSONPATH_DO_GET_NOTFOUND(this_idx);
}
walker += cjp->inst_lst[idx].next;
CSONPATH_DO_POST_FIND_ARRAY
break;
}
default:
CSONPATH_GETTER_ERR("unimplemented %d (%s) at idx %d\n", cjp->inst_lst[idx].inst,
cjp->inst_lst[idx].inst <= CSONPATH_INST_BROKEN ?
csonpath_instuction_str[cjp->inst_lst[idx].inst] : "(unknow)",
idx);
}
next_inst:
++idx;
}
CSONPATH_DO_RETURN;
}
#define csonpath_do__(name) CAT(csonpath_, name)
#define csonpath_do_ csonpath_do__(CSONPATH_DO_FUNC_NAME)
static CSONPATH_DO_RET_TYPE csonpath_do_(struct csonpath cjp[static 1],
CSONPATH_JSON value CSONPATH_DO_EXTRA_ARGS)
{
char *walker = cjp->path;
CSONPATH_JSON ctx = CSONPATH_NULL;
(void)ctx;
csonpath_compile(cjp);
if (cjp->inst_lst[0].inst == CSONPATH_INST_BROKEN) {
CSONPATH_GETTER_ERR("fail to compile: %s\n", cjp->compile_error ?
cjp->compile_error : "(unknow error)");
return CSONPATH_NONE_FOUND_RET;
}
CSONPATH_DO_PRE_OPERATION;
CSONPATH_DO_RET_TYPE ret = csonpath_do_internal(cjp, value, value,
CSONPATH_NULL, 0,
walker CSONPATH_DO_EXTRA_ARGS_IN);
CSONPATH_DO_POST_OPERATION;
return ret;
}
#ifndef CSONPATH_NO_UNDEF
#undef CSONPATH_PRE_GET_ROOT
#undef CSONPATH_PRE_GET
#undef CSONPATH_DO_PRE_OPERATION
#undef CSONPATH_DO_POST_OPERATION
#undef CSONPATH_DO_FUNC_NAME
#undef CSONPATH_DO_RET_TYPE
#undef CSONPATH_DO_RETURN
#undef CSONPATH_DO_DECLARATION
#undef CSONPATH_DO_FIND_ALL
#undef CSONPATH_DO_FIND_ALL_OUT
#undef CSONPATH_DO_POST_FIND_ARRAY
#undef CSONPATH_DO_POST_FIND_OBJ
#undef CSONPATH_DO_EXTRA_ARGS_NEESTED
#undef CSONPATH_DO_EXTRA_ARGS_IN
#undef CSONPATH_DO_EXTRA_ARGS_FIND_ALL
#undef CSONPATH_DO_EXTRA_ARGS
#undef CSONPATH_DO_EXTRA_DECLATION
#undef CSONPATH_DO_FIND_ALL_PRE_LOOP
#undef CSONPATH_DO_FOREACH_PRE_SET
#undef CSONPATH_DO_GET_ALL_OUT
#undef CSONPATH_DO_GET_NOTFOUND
#undef CSONPATH_DO_FIND_ALL_CLEAUP
#undef CSONPATH_DO_FILTER_OUT
#undef CSONPATH_DO_FILTER_PRE_LOOP
#undef CSONPATH_DO_FILTER_FIND
#undef CSONPATH_DO_FILTER_LOOP_PRE_SET
#undef CSONPATH_DO_RANGE
#undef CSONPATH_DO_RANGE_PRE_LOOP
#endif