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
|
#include "xmlreader.h"
#include <string.h>
XmlReader::XmlReader( bool bStrip )
{
nError = 0;
this->bStrip = bStrip;
}
XmlReader::~XmlReader()
{
}
#define gcall( x ) if( x == false ) return false;
bool XmlReader::isws( char chr )
{
return ( chr == ' ' || chr == '\t' || chr == '\n' || chr == '\r' );
}
bool XmlReader::ws()
{
while( true )
{
char chr = getChar();
if( isws( chr ) )
{
usedChar();
}
else
{
return true;
}
}
return true;
}
bool XmlReader::buildDoc()
{
// take care of initial whitespace
gcall( ws() );
gcall( node() );
return true;
}
bool XmlReader::node()
{
gcall( startNode() )
// At this point, we are closing the startNode
char chr = getChar();
if( chr == '>' )
{
usedChar();
// Now we process the guts of the node.
gcall( content() );
}
else if( chr == '/' )
{
// This is the tricky one, one more validation, then we close the node.
usedChar();
if( getChar() == '>' )
{
closeNode();
usedChar();
}
else
{
reportError("Close node in singleNode malformed!");
return false;
}
}
else
{
reportError("Close node expected, but not found.");
return false;
}
return true;
}
bool XmlReader::startNode()
{
if( getChar() == '<' )
{
usedChar();
if( getChar() == '/' )
{
// Heh, it's actually a close node, go figure
FlexBuf fbName;
usedChar();
gcall( ws() );
while( true )
{
char chr = getChar();
if( isws( chr ) || chr == '>' )
{
// Here we actually compare the name we got to the name
// we already set, they have to match exactly.
if( !strcasecmp( getCurrent()->getName(), fbName.getData() ) )
{
closeNode();
break;
}
else
{
reportError("Got a mismatched node close tag.");
return false;
}
}
else
{
fbName.appendData( chr );
usedChar();
}
}
gcall( ws() );
if( getChar() == '>' )
{
// Everything is cool.
usedChar();
}
else
{
reportError("Got extra junk data instead of node close tag.");
return false;
}
}
else
{
// We're good, format is consistant
addNode();
// Skip extra whitespace
gcall( ws() );
gcall( name() );
gcall( ws() );
gcall( paramlist() );
gcall( ws() );
}
}
else
{
reportError("Expected to find node opening char, '<'.\n");
return false;
}
return true;
}
bool XmlReader::name()
{
FlexBuf fbName;
while( true )
{
char chr = getChar();
if( isws( chr ) || chr == '>' || chr == '/' )
{
setName( fbName.getData() );
return true;
}
else
{
fbName.appendData( chr );
usedChar();
}
}
return true;
}
bool XmlReader::paramlist()
{
while( true )
{
char chr = getChar();
if( chr == '/' || chr == '>' )
{
return true;
}
else
{
gcall( param() );
gcall( ws() );
}
}
return true;
}
char XmlReader::getEscape()
{
// Right now, we just do # escapes...
if( getChar( 1 ) == '#' )
{
usedChar();
usedChar();
char buf[4];
int j = 0;
for( j = 0; getChar() != ';'; j++ )
{
buf[j] = getChar();
usedChar();
}
usedChar();
buf[j] = '\0';
return (char)atoi( buf );
}
else
{
return '\0';
}
}
bool XmlReader::param()
{
FlexBuf fbName;
FlexBuf fbValue;
while( true )
{
char chr = getChar();
if( isws( chr ) || chr == '=' )
{
break;
}
else
{
fbName.appendData( chr );
usedChar();
}
}
gcall( ws() );
if( getChar() == '=' )
{
usedChar();
gcall( ws() );
char chr = getChar();
if( chr == '"' )
{
// Better quoted rhs
usedChar();
while( true )
{
chr = getChar();
if( chr == '"' )
{
usedChar();
addProperty( fbName.getData(), fbValue.getData() );
return true;
}
else
{
if( chr == '&' )
{
chr = getEscape();
if( chr == '\0' ) return false;
fbValue.appendData( chr );
}
else
{
fbValue.appendData( chr );
usedChar();
}
}
}
}
else
{
// Simple one-word rhs
while( true )
{
chr = getChar();
if( isws( chr ) || chr == '/' || chr == '>' )
{
addProperty( fbName.getData(), fbValue.getData() );
return true;
}
else
{
if( chr == '&' )
{
chr = getEscape();
if( chr == '\0' ) return false;
fbValue.appendData( chr );
}
else
{
fbValue.appendData( chr );
usedChar();
}
}
}
}
}
else
{
reportError("Expected an equals to seperate the params.");
return false;
}
return true;
}
bool XmlReader::content()
{
FlexBuf fbContent;
if( bStrip ) gcall( ws() );
while( true )
{
char chr = getChar();
if( chr == '<' )
{
if( getChar(1) == '/' )
{
if( fbContent.getLength() > 0 )
{
if( bStrip )
{
int j;
for( j = fbContent.getLength()-1; isws(fbContent.getData()[j]); j-- );
((char *)fbContent.getData())[j+1] = '\0';
}
setContent( fbContent.getData() );
}
usedChar();
usedChar();
gcall( ws() );
FlexBuf fbName;
while( true )
{
chr = getChar();
if( isws( chr ) || chr == '>' )
{
if( !strcasecmp( getCurrent()->getName(), fbName.getData() ) )
{
closeNode();
break;
}
else
{
reportError("Mismatched close tag found.");
return false;
}
}
else
{
fbName.appendData( chr );
usedChar();
}
}
gcall( ws() );
if( getChar() == '>' )
{
usedChar();
return true;
}
else
{
reportError("Malformed close tag.");
return false;
}
}
else
{
if( fbContent.getLength() > 0 )
{
if( bStrip )
{
int j;
for( j = fbContent.getLength()-1; isws(fbContent.getData()[j]); j-- );
((char *)fbContent.getData())[j+1] = '\0';
}
setContent( fbContent.getData() );
fbContent.clearData();
}
gcall( node() );
}
if( bStrip ) gcall( ws() );
}
else
{
fbContent.appendData( chr );
usedChar();
}
}
}
void XmlReader::reportError( const char *sError )
{
printf("XmlReader error: %s\n", sError );
}
int XmlReader::getError()
{
return nError;
}
|