-
Notifications
You must be signed in to change notification settings - Fork 2
/
xmlpath.c
286 lines (247 loc) · 6.48 KB
/
xmlpath.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
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
// A cheating simple "xmlpath-style" parser
// Note: it doesn't care if xml doc is compliant, neither does it validate
#include <stdio.h>
#include <stddef.h>
// xpath test-suite
// https://dev.w3.org/2007/xpath-full-text-10-test-suite/
// returned at error
#define XPERR NULL
// debug it
#define XTRACE 0
// reports content of current element
// returns pointer after result
char* gotit(char* m) {
char* s = m;
char* e = m;
if (!m || !*m) return NULL;
printf("gotit m='%s'\n", m);
// "balance" tags
int depth = 1;
while(*m && depth > 0) {
// find <
while(*m && *m!='<') {
//putchar('1');
//putchar(*m);
e = m;
m++;
}
if (!*m) break;
m++;
if (*m=='/') {
// just counting, no care what
depth--;
if (depth) {
//putchar('<');
//putchar(*m);
e = m;
}
} else {
depth++;
//putchar('<');
e = m;
// scan till end of <>
while(*m && *m!='>') {
// this handles <foo/>
if (*m=='/') depth--;
//putchar('2');
//putchar(*m);
e = m;
m++;
}
if (!*m) break;
if (depth) {
//putchar(*m);
e = m;
}
}
m++;
}
// finished
if (s != e) {
printf(" >> '");
while(s <= e && *s) {
putchar(*s);
s++;
}
printf("'\n");
return e+1;
}
return NULL;
}
// NULL if t(ext) doesn't start with the p(ath) id, full prefix until q(uoting) character found (or 0 if no care)
char* strpre(char* p, char* t, char q) {
//printf("%% p=%s\n", p);
//printf("%% t=%s\n", t);
//printf("%% q=%c\n", q);
// skip any leading q
if (q && (q=='"' || q=='\'')) {
if (*t && *t==q) t++;
if (*p && *p==q) p++;
}
// compare chars till fail
while(*p && *t && *p!=q && *t!=q) {
if (XTRACE) printf("...PRE p=%s t=%s\n", p, t);
if (*p != *t) break;
p++;
t++;
}
// more letters == not match
if (*p && *t &&
(isalpha(*p) || isalpha(*t)))
return NULL;
// otherwise we're all good!
return t;
}
// calls gotit on matches, only once if 'one' is true.
// returns pointer to next part of text to parse, NULL if finished
char* xpath(char* p, char* t, int one /* , callback? */) {
if (XTRACE) printf("\nTT '%s'\n", t);
if (XTRACE) printf("?? '%s'\n", p);
if (!t || !p) return NULL;
while (*p && *t) {
switch (*p) {
case '/': // root '/' and anyw '//'
p++;
if (!*p) return NULL;
if (*p=='/') { // any depth '//'
p++;
while(*p && t && *t) {
// not care if match
xpath(p, t, one);
// TODO: if it did find a match we need to skip it!
// NOT CORRECT:
//if (!t) return NULL;
// if not, skip to next element
if (XTRACE) printf("...skipped...\n");
if (XTRACE) printf("TT '%s'\n", t);
if (*t && *t=='<') t++;
while(*t && *t != '<') t++;
// TODO: BUG: we could go negative here on depth, so...
}
return NULL; // fail
} else {
// go next direct child
// (skip a <tag>..</tag>)
//printf("...next child: p=%s t=%s\n", p, t);
int depth = -1;
xpath(p, t, one);
do {
if (depth < 0) depth = 0;
// find start tag
while(*t && *t!='<') t++;
if (!*t) return NULL;
t++;
if (!*t) return NULL;
//printf("...found < depth=%d t=%s\n", depth, t);
if (*t == '/') {
depth--;
} else {
depth++;
}
// find end of tag
while(*t && *t!='>') {
if (*t == '/') depth--;
t++;
}
if (!*t) return NULL;
t++;
//printf("...found > depth=%d t=%s\n", depth, t);
} while (*t && depth > 0);
if (!*t) return NULL;
// skip till next start tag
while(*t && *t!='<') t++;
if (!*t) return NULL;
// go back to '/', lol
p--;
printf("...=>next child: p=%s t=%s\n", p, t);
}
break;
case '@': // attribute foo, @* any attr
// enter start tag
while(*t && *t!='<') t++;
if (!*t) return NULL;
// loop over attributes
while(*t && *t!='>') {
// find attribute
while(*t && *t!='=' && *t!='>') t++;
if (!*t || *t=='>') return NULL;
t++;
// text have '=' attribute
char* q = *t;
if (q=='\'' || q=='"') t++;
if (!strpre(p, t, q)) {
// match!
// skip to content
while(*t && *t!='>') t++;
// skip id
char* pp = p;
while(*pp && isalpha(*pp)) pp++;
// continue
t = xpath(pp, t, one);
if (!t) return NULL;
// try next attr
}
}
return NULL;
case '[': return XPERR; // "has"/with 1-n, last(), last()-1, position()<3, condition [@lang='en'], [price>35],
case '*': // any element, node()
// skip one element in text
while(*t && *t!='<') t++;
if (!*t || *t!= '<') return NULL;
while(*t && *t!='>') t++;
if (!*t || *t!= '>') return NULL;
p++;
break;
case '|': return XPERR; // or...
case ':': return XPERR; // axis ancestor:book, following::label
case '(': return XPERR; // text()=='foo', node()
default: // a-z
if (XTRACE) printf("...tag p=%s t=%s\n", p, t);
if (!isalpha(*p)) return NULL;
// tag name
// find begin tag
while(*t && *t!='<') t++;
if (!*t || *t!= '<') return NULL;
t++;
if (!*p) return NULL;
// not right tag
if (!strpre(p, t, 0)) return NULL;
if (XTRACE) printf("...tag match!");
// find end tag in text
while(*t && *t!='>') t++;
if (!*t || *t!= '>') return NULL;
t++;
// advance path
while(*p && isalpha(*p)) p++;
// if at end of pat then result
if (!*p)
return gotit(t);
// TODO: we don't require direct following matching, just one direct child... lol
if (XTRACE) printf("...more to go p=%s t=%s\n", p, t);
}
}
return NULL;
}
void main() {
//char* t = "<foo>FOO</foo>";
//char* t = "<foo>FOO<bar>BAR</bar>FIE</foo>";
//char* t = "<foo>F<fie/>OO<bar>BAR</bar>FIE</foo>";
//char* t = "<xoo><foo>FOO<bar>BAR</bar>FIE</foo></xoo>";
//char* t = "<xoo><abba>ABBA</abba>x sd <a>fds</a> sdf <foo>FOO<bar>BAR</bar>FIE</foo></xoo>";
char* t = "<xoo><abba>ABBA</abba><gurk>GURK</gurk><foo>FOO<bar>BAR</bar>FIE</foo></xoo>";
char* p = "foo"; // yes (must be top?) TODO: switch /foo and foo ???
//char* p = "//bar"; // yes
//char* p = "bar";
//char* p = "/foo"; // don't care depth? (incorrect?)
//char* p = "//foo"; // same
//char* p = "xoo"; // <foo>..
//char* p = "//fie"; // TODO: not foo
//char* p = "//fie"; // TODO: only one match, not <bar> lol
//char* p = "xoo//bar"; // TODO:
//char* p = "xoo/foo"; // TODO:
//char* p = "xoo//foo"; // yes
printf("\nTT '%s'\n", t);
printf("?? '%s'\n", p);
char* r = xpath(p, t, 1);
printf(".. '%s'\n", r);
}