-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml.cpp
445 lines (353 loc) · 10.7 KB
/
xml.cpp
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
#include "xml.h"
XMLError ParseXMLString(String& xmlString, XMLDoc* outDoc) {
Vector<SubString> tokens;
XMLError err = LexXMLString(xmlString, &tokens);
if (err != XMLE_NONE) {
return err;
}
enum ParseState {
PS_TAGSTART,
PS_ATTRIBUTES
};
ParseState currentState = PS_TAGSTART;
Vector<IDHandle<XMLElement>> elemIdStack;
for (int i = 0; i < tokens.count; i++) {
switch (currentState) {
case PS_TAGSTART:{
if (tokens.Get(i) == "<") {
if (i < tokens.count - 1 && tokens.Get(i + 1) == "?xml") {
int j = i + 1;
while (j < tokens.count - 2 && (tokens.Get(j) != "?" || tokens.Get(j+1) != ">")) {
j++;
}
i = j + 1;
}
else {
i++;
if (tokens.Get(i) == "/") {
i++;
SubString tokenName = tokens.Get(i);
if (elemIdStack.count <= 0) {
return XMLE_PARSINGERROR;
}
XMLElement* elem = outDoc->elements.GetById(elemIdStack.data[elemIdStack.count - 1]);
if (elem == nullptr || elem->name != tokenName) {
return XMLE_PARSINGERROR;
}
elemIdStack.PopBack();
i++;
if (tokens.Get(i) != ">") {
return XMLE_PARSINGERROR;
}
}
else {
XMLElement* elem = outDoc->elements.CreateAndAdd();
elem->doc = outDoc;
elem->name = tokens.Get(i);
if (elemIdStack.count > 0) {
XMLElement* parentElem = outDoc->elements.GetById(elemIdStack.data[elemIdStack.count - 1]);
parentElem->childrenIds.PushBack(IDHandle<XMLElement>(elem->id));
}
elemIdStack.PushBack(IDHandle<XMLElement>(elem->id));
currentState = PS_ATTRIBUTES;
}
}
}
else if (tokens.Get(i).start[0] == '"' || tokens.Get(i).start[0] == '\'') {
IDHandle<XMLElement> topId = elemIdStack.data[elemIdStack.count - 1];
outDoc->elements.GetById(topId)->plainText = tokens.Get(i);
}
else {
int j = i;
while (j < tokens.count) {
if (tokens.Get(j) == "<") {
break;
}
j++;
}
if (j == tokens.count) {
return XMLE_PARSINGERROR;
}
char* sp = tokens.Get(i).start;
char* ep = tokens.Get(j - 1).start + tokens.Get(j - 1).length;
int idx = (int)(sp - xmlString.string);
int len = (int)(ep - sp);
IDHandle<XMLElement> topId = elemIdStack.data[elemIdStack.count - 1];
outDoc->elements.GetById(topId)->plainText = xmlString.GetSubString(idx, len);
i = j - 1;
}
} break;
case PS_ATTRIBUTES: {
if (tokens.Get(i) == ">") {
currentState = PS_TAGSTART;
}
else if (tokens.Get(i) == "/") {
elemIdStack.PopBack();
i++;
currentState = PS_TAGSTART;
}
else {
if (i > tokens.count - 3) {
return XMLE_PARSINGERROR;
}
SubString value = tokens.Get(i+2);
value.start++;
value.length -= 2;
IDHandle<XMLElement> topId = elemIdStack.data[elemIdStack.count - 1];
outDoc->elements.GetById(topId)->attributes.Insert(tokens.Get(i), value);
i += 2;
}
} break;
}
}
return XMLE_NONE;
}
XMLError ParseXMLStringFromFile(const char* fileName, XMLDoc* outDoc) {
String str = ReadStringFromFile(fileName);
if (str.string == nullptr) {
return XMLE_FILENOTFOUND;
}
XMLError err = ParseXMLString(str, outDoc);
return err;
}
XMLError LexXMLString(String& xmlString, Vector<SubString>* outTokens) {
enum LexerState {
LS_WHITESPACE,
LS_SINGLESTRING,
LS_DOUBLESTRING,
LS_KEYSYMBOL,
LS_IDENTIFIER
};
static const char* whitespace = " \t\n\r";
static const char* keySymbols = "<>/=";
LexerState currState = LS_WHITESPACE;
int lastTokenStart = 0;
#define EMIT_TOKEN() {outTokens->PushBack(xmlString.GetSubString(lastTokenStart, i - lastTokenStart)); lastTokenStart = i;}
int strLength = xmlString.GetLength();
for (int i = 0; i < strLength; i++) {
char currChar = xmlString.string[i];
bool isWhiteSpace = (strchr(whitespace, currChar) != nullptr);
bool isKeySymbol = (strchr(keySymbols, currChar) != nullptr);
switch (currState) {
case LS_WHITESPACE: {
if (currChar == '\'') {
currState = LS_SINGLESTRING;
}
else if (currChar == '"'){
currState = LS_DOUBLESTRING;
}
else if (isKeySymbol) {
i--;
currState = LS_KEYSYMBOL;
}
else if (!isWhiteSpace) {
currState = LS_IDENTIFIER;
}
else {
lastTokenStart = i + 1;
}
} break;
case LS_SINGLESTRING: {
if (currChar == '\'') {
i++;
EMIT_TOKEN();
i--;
currState = LS_WHITESPACE;
}
else if (currChar == '\\') {
i++;
}
} break;
case LS_DOUBLESTRING: {
if (currChar == '"') {
i++;
EMIT_TOKEN();
i--;
currState = LS_WHITESPACE;
}
else if (currChar == '\\') {
i++;
}
} break;
case LS_KEYSYMBOL: {
i++;
EMIT_TOKEN();
i--;
currState = LS_WHITESPACE;
} break;
case LS_IDENTIFIER: {
if (isWhiteSpace) {
EMIT_TOKEN();
currState = LS_WHITESPACE;
lastTokenStart++;
}
else if (isKeySymbol){
EMIT_TOKEN();
currState = LS_KEYSYMBOL;
i--;
}
} break;
}
}
return XMLE_NONE;
#undef EMIT_TOKEN
}
XMLElement* XMLElement::GetChild(const char* name, unsigned int index /*= 0*/) {
for (int i = 0; i < childrenIds.count; i++) {
XMLElement* child = doc->elements.GetById(childrenIds.Get(i));
if (child->name == name) {
if (index == 0) {
return child;
}
else {
index--;
}
}
}
return nullptr;
}
String XMLElement::GetExistingAttrValue(const char* attrName) {
String val;
bool found = attributes.LookUp(attrName, &val);
ASSERT_MSG(found, "XMLElement of type '%.*s' is missing attribute '%s'", name.length, name.start, attrName);
return val;
}
XMLElement* XMLElement::GetChildWithAttr(const char* elemName, const char* attrName, const char* attrValue, unsigned int index /*= 0*/) {
for (int i = 0; i < childrenIds.count; i++) {
XMLElement* child = doc->elements.GetById(childrenIds.Get(i));
if (child->name == elemName) {
String attrib;
bool foundAttr = child->attributes.LookUp(attrName, &attrib);
if (foundAttr && attrib == attrValue) {
if (index == 0) {
return child;
}
else {
index--;
}
}
}
}
return nullptr;
}
void WriteXMLElementToFile(XMLElement* elem, FILE* outFile, int indentation) {
#define PRINT_INDENT(n) for(int idt = 0; idt < n; idt++){fprintf(outFile, "\t");}
PRINT_INDENT(indentation);
fprintf(outFile, "<%.*s", elem->name.length, elem->name.start);
for (int i = 0; i < elem->attributes.count; i++) {
fprintf(outFile, " %s='%s'", elem->attributes.names[i].string, elem->attributes.values[i].string);
}
if (elem->childrenIds.count == 0) {
if (elem->plainText.length > 0) {
fprintf(outFile, ">");
fprintf(outFile, "%.*s", elem->plainText.length, elem->plainText.start);
fprintf(outFile, "</%.*s>\n", elem->name.length, elem->name.start);
}
else {
fprintf(outFile, "/>\n");
}
}
else {
fprintf(outFile, ">\n");
for (int i = 0; i < elem->childrenIds.count; i++) {
IDHandle<XMLElement> childId = elem->childrenIds.Get(i);
XMLElement* childElem = elem->doc->elements.GetById(childId);
WriteXMLElementToFile(childElem, outFile, indentation + 1);
}
PRINT_INDENT(indentation);
fprintf(outFile, "</%.*s>\n", elem->name.length, elem->name.start);
}
#undef PRINT_INDENT
}
XMLError SaveXMLDocToFile(XMLDoc* doc, const char* fileName) {
FILE* outFile = fopen(fileName, "wb");
if (outFile == NULL) {
return XMLE_FILENOTFOUND;
}
WriteXMLElementToFile(&doc->elements.vals[0], outFile, 0);
fclose(outFile);
return XMLE_NONE;
}
#if defined(XML_TEST_MAIN)
CREATE_TEST_CASE("XML basic") {
String xmlStr1 = "<tag attr='valval'></tag>";
String xmlStr2 = "< tag attr = 'val22'>\"This is me talkign sgnkjsngkj '''\"</tag>";
String xmlStr3 = "<tag attr1='VWG' attr2='False'><flur syn='Bah'/></tag>";
Vector<SubString> lex1;
Vector<SubString> lex2;
Vector<SubString> lex3;
LexXMLString(xmlStr1, &lex1);
LexXMLString(xmlStr2, &lex2);
LexXMLString(xmlStr3, &lex3);
XMLDoc doc1;
ParseXMLString(xmlStr1, &doc1);
XMLDoc doc3;
ParseXMLString(xmlStr3, &doc3);
ASSERT(doc3.elements.currentCount == 2);
ASSERT(doc3.elements.GetByIdNum(0)->childrenIds.count == 1);
IDHandle<XMLElement> childId = doc3.elements.GetByIdNum(0)->childrenIds.Get(0);
ASSERT(doc3.elements.GetById(childId) != nullptr);
ASSERT(doc3.elements.GetById(childId)->name == "flur");
String attrVal;
ASSERT(doc3.elements.GetById(childId)->attributes.LookUp("syn", &attrVal) && attrVal == "Bah");
String xmmlMatStr =
"<Material vs='posCol.vs' fs='texCol.fs'>\n"
"\t<uniform name='mainTex' type='tex2D' val='scribble.bmp'/>\n"
"\t<uniform name='tintCol' type='vec4' val='0.4,0.7,0.3,1.0'/>\n"
"</Material>\n";
XMLDoc doc4;
ParseXMLString(xmmlMatStr, &doc4);
XMLElement* root = &doc4.elements.vals[0];
XMLElement* tintColElem = root->GetChildWithAttr("uniform", "name", "tintCol");
ASSERT(tintColElem != nullptr);
ASSERT(tintColElem->name == "uniform");
{
//TODO: Why isn't this working?
//XMLDoc doc5;
//ParseXMLStringFromFile("test1.xml", &doc5);
}
ASSERT(doc4.elements.GetByIdNum(0)->attributes.count == 2);
ASSERT(doc4.elements.GetByIdNum(0)->childrenIds.count == 2);
String vsVal;
ASSERT(doc4.elements.GetByIdNum(0)->attributes.LookUp("vs", &vsVal) && vsVal == "posCol.vs");
String fsVal;
ASSERT(doc4.elements.GetByIdNum(0)->attributes.LookUp("fs", &fsVal) && fsVal == "texCol.fs");
IDHandle<XMLElement> uniformId = doc4.elements.GetByIdNum(0)->childrenIds.Get(0);
ASSERT(doc4.elements.GetById(uniformId)->attributes.count == 3);
String typeVal;
ASSERT(doc4.elements.GetById(uniformId)->attributes.LookUp("type", &typeVal) && typeVal == "tex2D");
IDHandle<XMLElement> uniformId2 = doc4.elements.GetByIdNum(0)->childrenIds.Get(1);
ASSERT(doc4.elements.GetById(uniformId2)->attributes.count == 3);
String typeVal2;
ASSERT(doc4.elements.GetById(uniformId2)->attributes.LookUp("type", &typeVal2) && typeVal2 == "vec4");
{
String xmlStr = "<tr> <td c='3'></td> <td c='4'></td> </tr>";
XMLDoc xmlDoc;
ParseXMLString(xmlStr, &xmlDoc);
XMLElement* root = &xmlDoc.elements.vals[0];
ASSERT(root->GetChild("td") != nullptr);
ASSERT(root->GetChild("td", 1) != nullptr);
ASSERT(root->GetChild("td", 2) == nullptr);
ASSERT(root->GetChildWithAttr("td", "c", "3") != nullptr);
ASSERT(root->GetChildWithAttr("td", "c", "4") != nullptr);
ASSERT(root->GetChildWithAttr("td", "c", "9") == nullptr);
ASSERT(root->GetChildWithAttr("td", "d", "3") == nullptr);
ASSERT(root->GetChild("td")->GetExistingAttrValue("c") == "3");
}
{
String xmlStr = "<tr fre='\\\\'></tr>";
XMLDoc xmlDoc;
ParseXMLString(xmlStr, &xmlDoc);
XMLElement* root = &xmlDoc.elements.vals[0];
ASSERT(root->GetExistingAttrValue("fre") == "\\\\")
}
{
String xmlStr = "<tr fre='\\''></tr>";
XMLDoc xmlDoc;
ParseXMLString(xmlStr, &xmlDoc);
XMLElement* root = &xmlDoc.elements.vals[0];
ASSERT(root->GetExistingAttrValue("fre") == "\\\'")
}
return 0;
}
#endif