-
Notifications
You must be signed in to change notification settings - Fork 61
/
path.c
220 lines (205 loc) · 4.08 KB
/
path.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
/* sdb - MIT - Copyright 2012-2017 - pancake */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rangstr.c"
#include "json.h"
#include "../types.h"
SDB_IPI void json_path_first(Rangstr *s) {
char *p;
if (!s->p) {
return;
}
p = strchr (s->p, '.');
s->f = 0;
s->t = p? (size_t)(p - s->p): strlen (s->p);
}
SDB_IPI int json_path_next(Rangstr *s) {
int stop = '.';
if (!s||!s->p||!s->p[s->t]) {
return 0;
}
if (!s->next) {
return 0;
}
if (s->p[s->t] == '"') {
s->t++;
}
rep:
if (s->p[s->t] == '[') {
s->type = '[';
stop = ']';
} else {
s->type = 0;
}
s->f = ++s->t;
if (s->p[s->t] == stop) {
s->f = ++s->t;
}
if (!s->p[s->t]) {
return 0;
}
while (s->p[s->t] != stop) {
if (!s->p[s->t]) {
s->next = 0;
return 1;
}
if (s->p[s->t] == '[') {
break;
}
s->t++;
}
if (s->f == s->t) {
goto rep;
}
if (s->p[s->f] == '"') {
s->f++;
s->t--;
}
return 1;
}
#if 0
typedef int (*JSONCallback)();
int json_foreach(const char *s, JSONCallback cb UNUSED) {
int i, len, ret;
unsigned short *res = NULL;
len = strlen (s);
res = malloc (len);
ret = js0n ((const unsigned char *)s, len, res);
if (!ret) return 0;
if (*s=='[') {
for (i=0; res[i]; i+=2) {
printf ("%d %.*s\n", i, res[i+1], s+res[i]);
}
} else {
for (i=0; res[i]; i+=4) {
printf ("%.*s = ", res[i+1], s+res[i]);
printf ("%.*s\n", res[i+3], s+res[i+2]);
}
}
return 1;
}
#endif
#if 0 // UNUSED
SDB_IPI int json_walk (const char *s) {
RangstrType *res;
int i, ret, len = strlen (s);
res = malloc (len+1);
ret = js0n ((const unsigned char *)s, len, res);
if (!ret) {
free (res);
return 0;
}
if (*s=='[' || *s=='{') {
for (i=0; res[i]; i+=2) {
printf ("%d %.*s\n", i, res[i+1], s+res[i]);
}
} else {
for (i=0; res[i]; i+=4) {
printf ("%.*s = ", res[i+1], s+res[i]);
printf ("%.*s\n", res[i+3], s+res[i+2]);
}
}
free (res);
return 1;
}
#endif
SDB_IPI Rangstr json_find (const char *s, Rangstr *rs) {
#define RESFIXSZ 1024
RangstrType resfix[RESFIXSZ] = {0};
RangstrType *res = resfix;
int i, j, n, len, ret;
Rangstr rsn;
if (!s) {
return rangstr_null ();
}
len = strlen (s);
if (len > RESFIXSZ) {
res = calloc (len + 1, sizeof (RangstrType));
if (!res) {
eprintf ("Cannot allocate %d byte%s\n",
len + 1, (len > 1)? "s": "");
return rangstr_null ();
}
}
ret = js0n ((const unsigned char *)s, len, res);
#define PFREE(x) if (x && x != resfix) free (x)
if (ret > 0) {
PFREE (res);
return rangstr_null ();
}
if (*s == '[') {
n = rangstr_int (rs);
if (n < 0) {
goto beach;
}
for (i = j = 0; res[i] && j < n; i += 2, j++);
if (!res[i]) {
goto beach;
}
rsn = rangstr_news (s, res, i);
PFREE (res);
return rsn;
} else {
for (i=0; res[i]; i+=4) {
Rangstr rsn = rangstr_news (s, res, i);
if (!rangstr_cmp (rs, &rsn)) {
rsn = rangstr_news (s, res, i+2);
PFREE (res);
return rsn;
}
}
}
beach:
PFREE (res);
return rangstr_null ();
}
SDB_IPI Rangstr json_get (const char *js, const char *p) {
int x, n = 0;
size_t rst;
Rangstr rj2, rj = rangstr_new (js);
Rangstr rs = rangstr_new (p);
json_path_first (&rs);
do {
rst = rs.t;
rs.f++;
x = rangstr_find (&rs, '[');
rs.f--;
if (x != -1)
rs.t = x;
#if 0
printf ("x = %d f = %d t = %d\n", x, rs.f, rs.t);
fprintf (stderr, "source (%s)\n", rangstr_dup (&rs));
fprintf (stderr, "onjson (%s)\n", rangstr_dup (&rj));
#endif
if (rst == rs.t && n && rj.p) // last key
break;
if (!rj.p) break;
do {
rj2 = json_find (rangstr_str (&rj), &rs);
//fprintf (stderr, "++ (%s)(%d vs %d)\n", rangstr_dup (&rs), x, rs.t);
//if (rj.p[rj.f]=='[') { break; }
//fprintf (stderr, "ee %c\n", rj.p[rj.f]);
if (!rj2.p) {
if (!rj.p[rj.t]) return rj2;
break;
}
rj = rj2;
#if 0
fprintf (stderr, "-- (%s)\n", rangstr_dup (&rj));
#endif
} while (json_path_next (&rs));
//if (!rj.p) return rj;
#if 0
printf ("x = %d\n", x); printf ("rsf = %d\n", rs.f);
fprintf (stderr, "xxx (%s)\n", rangstr_dup (&rj));
return rj;
#endif
if ((rst == rs.t && n && rj.p)) // last key
break;
rs.t = rst;
rs.f = x;
n++;
} while (x != -1);
return rj;
}