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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
// FIXME: Avoid using fprintf
#define FORBIDDEN_SYMBOL_EXCEPTION_fprintf
#define FORBIDDEN_SYMBOL_EXCEPTION_stderr
#include "common/xmlparser.h"
#include "common/archive.h"
#include "common/fs.h"
#include "common/memstream.h"
namespace Common {
XMLParser::~XMLParser() {
while (!_activeKey.empty())
freeNode(_activeKey.pop());
delete _XMLkeys;
delete _stream;
for (List<XMLKeyLayout *>::iterator i = _layoutList.begin();
i != _layoutList.end(); ++i)
delete *i;
_layoutList.clear();
}
bool XMLParser::loadFile(const String &filename) {
_stream = SearchMan.createReadStreamForMember(filename);
if (!_stream)
return false;
_fileName = filename;
return true;
}
bool XMLParser::loadFile(const FSNode &node) {
_stream = node.createReadStream();
if (!_stream)
return false;
_fileName = node.getName();
return true;
}
bool XMLParser::loadBuffer(const byte *buffer, uint32 size, DisposeAfterUse::Flag disposable) {
_stream = new MemoryReadStream(buffer, size, disposable);
_fileName = "Memory Stream";
return true;
}
bool XMLParser::loadStream(SeekableReadStream *stream) {
_stream = stream;
_fileName = "File Stream";
return true;
}
void XMLParser::close() {
delete _stream;
_stream = 0;
}
bool XMLParser::parserError(const String &errStr) {
_state = kParserError;
const int startPosition = _stream->pos();
int currentPosition = startPosition;
int lineCount = 1;
char c = 0;
_stream->seek(0, SEEK_SET);
while (currentPosition--) {
c = _stream->readByte();
if (c == '\n' || c == '\r')
lineCount++;
}
assert(_stream->pos() == startPosition);
currentPosition = startPosition;
int keyOpening = 0;
int keyClosing = 0;
while (currentPosition-- && keyOpening == 0) {
_stream->seek(-2, SEEK_CUR);
c = _stream->readByte();
if (c == '<')
keyOpening = currentPosition - 1;
else if (c == '>')
keyClosing = currentPosition;
}
_stream->seek(startPosition, SEEK_SET);
currentPosition = startPosition;
while (keyClosing == 0 && c && currentPosition++) {
c = _stream->readByte();
if (c == '>')
keyClosing = currentPosition;
}
fprintf(stderr, "\n File <%s>, line %d:\n", _fileName.c_str(), lineCount);
currentPosition = (keyClosing - keyOpening);
_stream->seek(keyOpening, SEEK_SET);
while (currentPosition--)
fprintf(stderr, "%c", _stream->readByte());
fprintf(stderr, "\n\nParser error: ");
fprintf(stderr, "%s", errStr.c_str());
fprintf(stderr, "\n\n");
return false;
}
bool XMLParser::parseXMLHeader(ParserNode *node) {
assert(node->header);
if (_activeKey.size() != 1)
return parserError("XML Header is expected in the global scope.");
if (!node->values.contains("version"))
return parserError("Missing XML version in XML header.");
if (node->values["version"] != "1.0")
return parserError("Unsupported XML version.");
return true;
}
bool XMLParser::parseActiveKey(bool closed) {
bool ignore = false;
assert(_activeKey.empty() == false);
ParserNode *key = _activeKey.top();
if (key->name == "xml" && key->header == true) {
assert(closed);
return parseXMLHeader(key) && closeKey();
}
XMLKeyLayout *layout = (_activeKey.size() == 1) ? _XMLkeys : getParentNode(key)->layout;
if (layout->children.contains(key->name)) {
key->layout = layout->children[key->name];
StringMap localMap = key->values;
int keyCount = localMap.size();
for (List<XMLKeyLayout::XMLKeyProperty>::const_iterator i = key->layout->properties.begin(); i != key->layout->properties.end(); ++i) {
if (i->required && !localMap.contains(i->name))
return parserError("Missing required property '" + i->name + "' inside key '" + key->name + "'");
else if (localMap.contains(i->name))
keyCount--;
}
if (keyCount > 0)
return parserError("Unhandled property inside key '" + key->name + "'.");
} else {
return parserError("Unexpected key in the active scope ('" + key->name + "').");
}
// check if any of the parents must be ignored.
// if a parent is ignored, all children are too.
for (int i = _activeKey.size() - 1; i >= 0; --i) {
if (_activeKey[i]->ignore)
ignore = true;
}
if (ignore == false && keyCallback(key) == false) {
// HACK: People may be stupid and overlook the fact that
// when keyCallback() fails, a parserError() must be set.
// We set it manually in that case.
if (_state != kParserError)
parserError("Unhandled exception when parsing '" + key->name + "' key.");
return false;
}
if (closed)
return closeKey();
return true;
}
bool XMLParser::parseKeyValue(String keyName) {
assert(_activeKey.empty() == false);
if (_activeKey.top()->values.contains(keyName))
return false;
_token.clear();
char stringStart;
if (_char == '"' || _char == '\'') {
stringStart = _char;
_char = _stream->readByte();
while (_char && _char != stringStart) {
_token += _char;
_char = _stream->readByte();
}
if (_char == 0)
return false;
_char = _stream->readByte();
} else if (!parseToken()) {
return false;
}
_activeKey.top()->values[keyName] = _token;
return true;
}
bool XMLParser::parseIntegerKey(const char *key, int count, ...) {
bool result;
va_list args;
va_start(args, count);
result = vparseIntegerKey(key, count, args);
va_end(args);
return result;
}
bool XMLParser::parseIntegerKey(const String &key, int count, ...) {
bool result;
va_list args;
va_start(args, count);
result = vparseIntegerKey(key.c_str(), count, args);
va_end(args);
return result;
}
bool XMLParser::vparseIntegerKey(const char *key, int count, va_list args) {
char *parseEnd;
int *num_ptr;
while (count--) {
while (isspace(static_cast<unsigned char>(*key)))
key++;
num_ptr = va_arg(args, int*);
*num_ptr = strtol(key, &parseEnd, 10);
key = parseEnd;
while (isspace(static_cast<unsigned char>(*key)))
key++;
if (count && *key++ != ',')
return false;
}
return (*key == 0);
}
bool XMLParser::closeKey() {
bool ignore = false;
bool result = true;
for (int i = _activeKey.size() - 1; i >= 0; --i) {
if (_activeKey[i]->ignore)
ignore = true;
}
if (ignore == false)
result = closedKeyCallback(_activeKey.top());
freeNode(_activeKey.pop());
return result;
}
bool XMLParser::parse() {
if (_stream == 0)
return parserError("XML stream not ready for reading.");
// Make sure we are at the start of the stream.
_stream->seek(0, SEEK_SET);
if (_XMLkeys == 0)
buildLayout();
while (!_activeKey.empty())
freeNode(_activeKey.pop());
cleanup();
bool activeClosure = false;
bool activeHeader = false;
bool selfClosure;
_state = kParserNeedHeader;
_activeKey.clear();
_char = _stream->readByte();
while (_char && _state != kParserError) {
if (skipSpaces())
continue;
if (skipComments())
continue;
switch (_state) {
case kParserNeedHeader:
case kParserNeedKey:
if (_char != '<') {
parserError("Parser expecting key start.");
break;
}
if ((_char = _stream->readByte()) == 0) {
parserError("Unexpected end of file.");
break;
}
if (_state == kParserNeedHeader) {
if (_char != '?') {
parserError("Expecting XML header.");
break;
}
_char = _stream->readByte();
activeHeader = true;
} else if (_char == '/') {
_char = _stream->readByte();
activeClosure = true;
} else if (_char == '?') {
parserError("Unexpected header. There may only be one XML header per file.");
break;
}
_state = kParserNeedKeyName;
break;
case kParserNeedKeyName:
if (!parseToken()) {
parserError("Invalid key name.");
break;
}
if (activeClosure) {
if (_activeKey.empty() || _token != _activeKey.top()->name) {
parserError("Unexpected closure.");
break;
}
} else {
ParserNode *node = allocNode(); //new ParserNode;
node->name = _token;
node->ignore = false;
node->header = activeHeader;
node->depth = _activeKey.size();
node->layout = 0;
_activeKey.push(node);
}
_state = kParserNeedPropertyName;
break;
case kParserNeedPropertyName:
if (activeClosure) {
if (!closeKey()) {
parserError("Missing data when closing key '" + _activeKey.top()->name + "'.");
break;
}
activeClosure = false;
if (_char != '>')
parserError("Invalid syntax in key closure.");
else
_state = kParserNeedKey;
_char = _stream->readByte();
break;
}
selfClosure = false;
if (_char == '/' || (_char == '?' && activeHeader)) {
selfClosure = true;
_char = _stream->readByte();
}
if (_char == '>') {
if (activeHeader && !selfClosure) {
parserError("XML Header must be self-closed.");
} else if (parseActiveKey(selfClosure)) {
_char = _stream->readByte();
_state = kParserNeedKey;
}
activeHeader = false;
break;
}
if (selfClosure)
parserError("Expecting key closure after '/' symbol.");
else if (!parseToken())
parserError("Error when parsing key value.");
else
_state = kParserNeedPropertyOperator;
break;
case kParserNeedPropertyOperator:
if (_char != '=')
parserError("Syntax error after key name.");
else
_state = kParserNeedPropertyValue;
_char = _stream->readByte();
break;
case kParserNeedPropertyValue:
if (!parseKeyValue(_token))
parserError("Invalid key value.");
else
_state = kParserNeedPropertyName;
break;
default:
break;
}
}
if (_state == kParserError)
return false;
if (_state != kParserNeedKey || !_activeKey.empty())
return parserError("Unexpected end of file.");
return true;
}
bool XMLParser::skipSpaces() {
if (!isspace(static_cast<unsigned char>(_char)))
return false;
while (_char && isspace(static_cast<unsigned char>(_char)))
_char = _stream->readByte();
return true;
}
bool XMLParser::skipComments() {
if (_char == '<') {
_char = _stream->readByte();
if (_char != '!') {
_stream->seek(-1, SEEK_CUR);
_char = '<';
return false;
}
if (_stream->readByte() != '-' || _stream->readByte() != '-')
return parserError("Malformed comment syntax.");
_char = _stream->readByte();
while (_char) {
if (_char == '-') {
if (_stream->readByte() == '-') {
if (_stream->readByte() != '>')
return parserError("Malformed comment (double-hyphen inside comment body).");
_char = _stream->readByte();
return true;
}
}
_char = _stream->readByte();
}
return parserError("Comment has no closure.");
}
return false;
}
bool XMLParser::parseToken() {
_token.clear();
while (isValidNameChar(_char)) {
_token += _char;
_char = _stream->readByte();
}
return isspace(static_cast<unsigned char>(_char)) != 0 || _char == '>' || _char == '=' || _char == '/';
}
} // End of namespace Common
|