-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.cpp
762 lines (740 loc) · 25.7 KB
/
parser.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
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/**
* @file parser.cpp
* Turns XML game files into C++ objects.
*
* @brief The source file for parser functionality.
*
* @author Michael Abrams
* @author James Boocock
* @author Toby Herbert
* @author Tatai Nikora
* @version 0.3
*/
#include "parser.h"
#include "tinyxml.h"
#define WORLD_ATTRIBUTES 3
#define AREA_ATTRIBUTES 2
#define STATE_DESCRIPTION_ATTRIBUTES 1
#define ITEM_ATTRIBUTES 3
#define COMBINE_ATTRIBUTES 3
#define PARSING_ERROR 2
#define AREA_COMMAND_ATTRIBUTES 2
#define ITEM_COMMAND_ATTRIBUTES 5
#define INVALID "invalid"
#define NONE "none"
#define MISSING_TAGS "missing tags"
#define UNDER_PARENT "under tag with id: "
#define SEPERATOR ","
#define INSIDE_INDEX -1
World *read_file(const char* pFilename, World *world) {
TiXmlDocument doc(pFilename);
bool loadOkay = doc.LoadFile();
if (loadOkay) {
return make_objects( &doc, world );
}
else {
printf("Failed to load file \"%s\"\n", pFilename);
return NULL;
}
}
void string_explode(std::string str, std::string seperator, std::vector<std::string> *&result) {
size_t found;
result = new std::vector<std::string>();
found = str.find_first_of(seperator);
while(found != std::string::npos) {
if(found > 0) {
(*result).push_back(str.substr(0, found));
}
str = str.substr(found+1);
found = str.find_first_of(seperator);
}
if(str.length() > 0) {
(*result).push_back(str);
}
}
combine *make_combine(TiXmlNode * pCommand,const char *parent_id, World *world ){
TiXmlNode *pChild;
std::string first_id, second_id, combine_id, combine_desc;
std::string error_tag = MISSING_TAGS;
combine *combine_var = NULL;
bool one = false, two = false, has_id = false;
TiXmlElement *element = pCommand->ToElement();
TiXmlAttribute *attributes = element->FirstAttribute();
int num_attributes = 0;
while(attributes){
if(!strcmp(attributes->Name(), "first_id")){
first_id = attributes->Value();
if(one){
error_tag = "More than one first_id tag";
}
num_attributes++;
one = true;
} else if(!strcmp(attributes->Name() , "second_id")){
second_id = attributes->Value();
if(two){
error_tag = "More than one second_id tag";
}
num_attributes++;
two = true;
} else if(!strcmp(attributes->Name() , "id")){
combine_id = attributes->Value();
if(has_id){
error_tag = "More than one id tag";
}
num_attributes++;
has_id = true;
}
attributes = attributes->Next();
}
if(one && two && has_id && num_attributes == COMBINE_ATTRIBUTES){
combine_var = new combine(combine_id, first_id, second_id);
for ( pChild = pCommand->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) {
if(pChild->Type() == TiXmlNode::TINYXML_ELEMENT) {
if(!strcmp(pChild->Value(), "statedescriptor")) {
combine_var->set_description(make_state_descriptor(pChild, combine_id.c_str(), world));
} else if(!strcmp(pChild->Value(), "item")){
combine_var->set_combination(make_item(pChild,"combine",world ));
} else {
error_tag = "Wrong element under combine: ";
error_tag.append(combine_id);
error_tag.append(" found ");
error_tag.append(pChild->Value());
}
} else {
std::ostringstream sin;
sin << "Under parent ";
sin << parent_id;
sin << " there is a wrong element after ";
sin << combine_id;
std::string message = sin.str();
error_parsing(message, world);
}
}
if(strcmp(error_tag.c_str(), MISSING_TAGS)){
error_parsing(error_tag, world);
}
} else {
std::ostringstream sin;
sin << "Under parent ";
sin << parent_id;
sin << " there is an attribute error in a item command tag, found: ";
sin << error_tag;
std::string message = sin.str();
error_parsing(message, world);
}
return combine_var;
}
ItemCommand *make_item_command(TiXmlNode *pCommand, const char *parent_id, World *world) {
TiXmlNode* pChild;
const char *error_tag = MISSING_TAGS,
*command_name = INVALID, *command_state = INVALID,
*command_area = INVALID, *command_status = NONE, *command_depends = NONE,
*synonyms = NONE, *unlock = NONE;
int attributesFound = 0;
bool has_name = false, has_state = false, command_chg_col = true, command_dep =true,
has_collect = false, has_collec_dep = false, has_area = false, has_status = false,
has_depends = false, has_synonyms = false, has_unlock = false;
std::vector<std::string> *synonyms_vec = NULL;
ItemCommand *item_command = NULL;
TiXmlElement *element = pCommand->ToElement();
TiXmlAttribute *attributes = element->FirstAttribute();
while(attributes) {
if(!strcmp(attributes->Name(), "changecollectable")) {
if(!strcmp(attributes->Value(), "false")) {
command_chg_col = false;
}
else {
command_chg_col = true;
}
attributesFound++;
if(has_collect) {
error_tag = "More than one change collectable tag for an item.";
}
has_collect = true;
}
else if(!strcmp(attributes->Name(), "name")) {
command_name = attributes->Value();
attributesFound++;
if(has_name) {
error_tag = "More than one name tag for an item.";
}
has_name = true;
}
else if(!strcmp(attributes->Name(), "depends")) {
command_depends = attributes->Value();
if(has_depends) {
error_tag = "More than one depends tag for an item.";
}
has_depends = true;
}
else if(!strcmp(attributes->Name(), "statedescription")) {
command_state = attributes->Value();
attributesFound++;
if(has_state) {
error_tag = "More than one state description tag for an item.";
}
has_state = true;
}
else if(!strcmp(attributes->Name(), "areachange")) {
command_area = attributes->Value();
attributesFound++;
if(has_area) {
error_tag = "More than one change collectable tag for an item.";
}
has_area = true;
}
else if(!strcmp(attributes->Name(), "collectabledependent")) {
if(!strcmp(attributes->Value(), "false")) {
command_dep = NULL;
}
else {
command_dep = "false";
}
attributesFound++;
if(has_collec_dep) {
error_tag = "More than one collectable dependent tag for an item.";
}
has_collec_dep = true;
}
else if (!strcmp(attributes->Name(), "status")) {
command_status = attributes->Value();
if(has_status) {
error_tag = "More than one status tag for an item.";
}
has_status = true;
}
else if (!strcmp(attributes->Name(), "synonyms")) {
synonyms = attributes->Value();
string_explode(synonyms, SEPERATOR, synonyms_vec);
if(has_synonyms) {
error_tag = "More than one synonyms tags for an item.";
}
has_synonyms = true;
}
else if(!strcmp(attributes->Name(), "unlock")) {
unlock = attributes->Value();
if(has_unlock) {
error_tag = "More than one unlock tag for an item.";
}
}
else {
error_tag = attributes->Name();
fprintf(stderr, "Unknown tag.\n");
attributesFound++;
}
attributes = attributes->Next();
}
if(ITEM_COMMAND_ATTRIBUTES == attributesFound && has_collec_dep && has_name && has_state && has_area && has_collect) {
item_command = new ItemCommand(command_name, command_state, command_chg_col,
command_dep, command_area, command_status,
command_depends, synonyms_vec, unlock, false);
for ( pChild = pCommand->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) {
if(pChild->Type() == TiXmlNode::TINYXML_TEXT) {
item_command->set_message((pChild->ToText()->Value()));
}
else {
std::ostringstream sin;
sin << "Under parent ";
sin << parent_id;
sin << " there is a wrong element after ";
sin << command_name;
std::string message = sin.str();
error_parsing(message, world);
}
}
}
else {
std::ostringstream sin;
sin << "Under parent ";
sin << parent_id;
sin << " there is an attribute error in a item command tag, found: ";
sin << error_tag;
std::string message = sin.str();
error_parsing(message, world);
}
return item_command;
}
AreaCommand *make_area_command(TiXmlNode *pCommand, const char *parent_id, World *world) {
TiXmlNode* pChild;
const char *error_tag = MISSING_TAGS, *command_depends = NONE,
*command_name = INVALID, *command_area = INVALID, *command_status = NONE, *synonyms= NONE;
int attributesFound = 0;
bool has_name = false, has_area = false, has_status = false, has_depends = false,
has_synonyms = false, has_locked = false, locked = false;
std::vector<std::string> *synonyms_vec = NULL;
AreaCommand *area_command = NULL;
TiXmlElement *element = pCommand->ToElement();
TiXmlAttribute *attributes = element->FirstAttribute();
while(attributes) {
if(!strcmp(attributes->Name(), "name")) {
command_name = attributes->Value();
attributesFound++;
if(has_name) {
error_tag = "More than one name tag";
}
has_name = true;
}
else if(!strcmp(attributes->Name(), "area")) {
command_area = attributes->Value();
attributesFound++;
if(has_area) {
error_tag = "More than one area tag.";
}
has_area = true;
}
else if(!strcmp(attributes->Name(), "depends")) {
command_depends = attributes->Value();
if(has_depends) {
error_tag = "More than one depends tag.";
}
has_depends = true;
}
else if (!strcmp(attributes->Name(), "status")) {
command_status = attributes->Value();
if(has_status) {
error_tag = "More than one status tag";
}
has_status = true;
}
else if (!strcmp(attributes->Name(), "synonyms")) {
synonyms = attributes->Value();
string_explode(synonyms, SEPERATOR, synonyms_vec);
if(has_synonyms) {
error_tag = "More than one synonyms tags.";
}
has_synonyms = true;
}
else if(!strcmp(attributes->Name(), "locked")) {
if(!strcmp(attributes->Value(), "true")) {
locked = true;
}
if(has_locked) {
error_tag = "More than one locked tag.";
}
has_locked = true;
}
else {
error_tag = attributes->Name();
fprintf(stderr, "found something but shouldnt have in make_area_command.\n");
attributesFound++;
}
attributes = attributes->Next();
}
if(AREA_COMMAND_ATTRIBUTES == attributesFound && has_name && has_area) {
area_command = new AreaCommand(command_name, command_area,command_status,
command_depends, synonyms_vec, locked);
for ( pChild = pCommand->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) {
if(pChild->Type() == TiXmlNode::TINYXML_TEXT) {
area_command->set_message(pChild->ToText()->Value());
}
else {
std::ostringstream sin;
sin << "Under parent ";
sin << parent_id;
sin << " there is a tag error in ";
sin << command_name;
std::string message = sin.str();
error_parsing(message, world);
}
}
}
else {
std::ostringstream sin;
sin << "Under parent ";
sin << parent_id;
sin << " there is an attribute error in a area command tag, found: ";
sin << error_tag;
std::string message = sin.str();
error_parsing(message, world);
}
return area_command;
}
StateDescriptor *make_state_descriptor(TiXmlNode *pDescription, const char *parent_id, World *world) {
TiXmlNode* pChild;
const char *state_desc_id = INVALID, *error_tag = MISSING_TAGS;
int attributesFound = 0;
bool has_id = false;
StateDescriptor *state_descriptor = NULL;
TiXmlElement *element = pDescription->ToElement();
TiXmlAttribute *attributes = element->FirstAttribute();
while(attributes) {
if(!strcmp(attributes->Name(), "id")) {
state_desc_id = attributes->Value();
attributesFound++;
if(has_id) {
error_tag = "More than one id tag";
}
has_id = true;
}
else {
error_tag = attributes->Name();
fprintf(stderr, "found something but shouldnt have.\n");
attributesFound++;
}
attributes = attributes->Next();
}
if(STATE_DESCRIPTION_ATTRIBUTES == attributesFound && has_id) {
state_descriptor = new StateDescriptor(state_desc_id);
for ( pChild = pDescription->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) {
if(pChild->Type() == TiXmlNode::TINYXML_TEXT) {
state_descriptor->set_description(pChild->ToText()->Value());
}
else {
std::ostringstream sin;
sin << "Under parent ";
sin << parent_id;
sin << " there is a wrong element after ";
sin << state_desc_id;
std::string message = sin.str();
error_parsing(message, world);
}
}
}
else {
std::ostringstream sin;
sin << "Under parent ";
sin << parent_id;
sin << " there is an attribute error in a state descriptor, found: ";
sin << error_tag;
std::string message = sin.str();
error_parsing(message, world);
}
return state_descriptor;
}
Item *make_item(TiXmlNode *pItem, const char *parent_id, World *world) {
TiXmlNode* pChild;
const char *item_id = "invalid", *error_tag = MISSING_TAGS,
*item_init_desc = INVALID, *synonyms = NONE, *depends = NONE, *name = NONE;
int attributesFound =0;
bool has_id = false, item_collectable = false, has_collec = false,
has_init_desc = false, has_synonyms = false , has_depends = false,
item_container = false, item_locked = false, has_locked = false,
has_container = false, has_name = false;
Item *item = NULL;
std::vector<std::string> *synonyms_vec = NULL;
TiXmlElement *element = pItem->ToElement();
TiXmlAttribute *attributes = element->FirstAttribute();
while(attributes) {
if(!strcmp(attributes->Name(), "id")) {
item_id = attributes->Value();
attributesFound++;
if(has_id) {
error_tag = "More than one id tag";
}
has_id = true;
}
else if(!strcmp(attributes->Name(), "collectable")) {
if(!strcmp(attributes->Value(), "false")) {
item_collectable = false;
}
else {
item_collectable = true;
}
attributesFound++;
if(has_collec) {
error_tag = "More than one collectable tag";
}
has_collec = true;
}
else if(!strcmp(attributes->Name(), "initialdescription")) {
item_init_desc = attributes->Value();
attributesFound++;
if(has_init_desc) {
error_tag = "More than one initial description tag.";
}
has_init_desc = true;
}
else if (!strcmp(attributes->Name(), "synonyms")) {
synonyms = attributes->Value();
string_explode(synonyms, SEPERATOR, synonyms_vec);
if(has_synonyms) {
error_tag = "More than one synonyms tags.";
}
has_synonyms = true;
}
else if(!strcmp(attributes->Name(), "depends")){
depends = attributes->Value();
if(has_depends){
error_tag = "More than one depends tag.";
}
has_depends = true;
}
else if(!strcmp(attributes->Name(), "locked")){
if(!strcmp(attributes->Value(), "false")) {
item_locked = false;
}
else {
item_locked = true;
}
if(has_locked){
error_tag = "More than one locked tag.";
}
has_locked = true;
}
else if(!strcmp(attributes->Name(), "container")){
if(!strcmp(attributes->Value(), "false")) {
item_container = false;
}
else {
item_container = true;
}
if(has_container){
error_tag = "More than one container tag.";
}
has_container = true;
} else if(!strcmp(attributes->Name(), "name")){
name = attributes->Value();
if(has_name){
error_tag = "More than one name tag";
}
has_name = true;
}else {
error_tag = attributes->Name();
fprintf(stderr, "found something but shouldnt have in make_item.\n");
attributesFound++;
}
attributes = attributes->Next();
}
if(ITEM_ATTRIBUTES == attributesFound && has_id && has_init_desc && has_collec) {
item = new Item(item_collectable, item_id, item_init_desc, synonyms_vec ,depends, item_container,
item_locked, name);
for ( pChild = pItem->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) {
if(pChild->Type() == TiXmlNode::TINYXML_ELEMENT) {
if(!strcmp(pChild->Value(), "statedescriptor")) {
item->add_description(make_state_descriptor(pChild, item_id, world));
}
else if(!strcmp(pChild->Value(), "itemcommand")) {
item->add_command(make_item_command(pChild, item_id, world));
}
else if(!strcmp(pChild->Value(), "combine")){
item->set_combine(make_combine(pChild, item_id, world));
}else if(!strcmp(pChild->Value(), "item")){
item->add_item(make_item(pChild, item_id, world));
}
}
else {
std::ostringstream sin;
sin << "Under parent ";
sin << parent_id;
sin << " there is a wrong element after ";
sin << item_id;
std::string message = sin.str();
error_parsing(message, world);
}
}
}
else {
std::ostringstream sin;
sin << "Under parent ";
sin << parent_id;
sin << " there is an attribute error in an item, found: ";
sin << error_tag;
std::string message = sin.str();
error_parsing(message, world);
}
return item;
}
Area *make_area(TiXmlNode *pArea, int area_index, World *world) {
TiXmlNode* pChild;
int attributesFound = 0;
const char *area_id = INVALID, *desc_id = INVALID,
*error_tag = MISSING_TAGS, *area_status = NONE, *name = NONE;
bool has_id = false, has_desc = false, has_status = false, has_name = false;
Area *area = NULL;
TiXmlElement *element = pArea->ToElement();
TiXmlAttribute *attributes = element->FirstAttribute();
while(attributes) {
if(!strcmp(attributes->Name(), "id")) {
area_id = attributes->Value();
attributesFound++;
if(has_id) {
error_tag = "More than one id tag";
}
has_id = true;
}
else if (!strcmp(attributes->Name(), "initialdescription")) {
desc_id = attributes->Value();
attributesFound++;
if(has_desc) {
error_tag = "More than one initialdescription tag";
}
has_desc = true;
}
else if (!strcmp(attributes->Name(), "status")) {
area_status = attributes->Value();
if(has_status) {
error_tag = "More than one status tag";
}
has_status = true;
}
else if(!strcmp(attributes->Name(), "name")){
name = attributes->Value();
if(has_name){
error_tag = "More than one name tag";
}
has_name = true;
} else {
error_tag = attributes->Name();
}
attributes=attributes->Next();
}
if(attributesFound == AREA_ATTRIBUTES && has_desc && has_id) {
area = new Area(area_id, desc_id, area_status,name);
for ( pChild = pArea->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) {
if(pChild->Type() == TiXmlNode::TINYXML_ELEMENT) {
if(!strcmp(pChild->Value(), "statedescriptor")) {
area->add_description(make_state_descriptor(pChild, area_id, world));
}
else if(!strcmp(pChild->Value(), "item")) {
area->add_item(make_item(pChild, area_id, world));
}
else if(!strcmp(pChild->Value(), "areacommand")) {
area->add_command(make_area_command(pChild, area_id, world));
}
}
else {
std::ostringstream sin;
sin << "Under area ";
sin << area_index;
sin << ", there is tag that doesn't go there found: ";
sin << error_tag;
std::string message = sin.str();
error_parsing(message, world);
}
}
}
else {
std::ostringstream sin;
sin << "Under area ";
sin << area_index;
sin << " there is an attribute error, found: ";
sin << error_tag;
std::string message = sin.str();
error_parsing(message, world);
}
if(area->has_current_desc()||area_index == INSIDE_INDEX) {
return area;
}
else {
error_parsing("The current area can't find inital description.", world);
}
return area;
}
World *make_world(TiXmlNode *pParent, World *world) {
const char *author = INVALID, *language = INVALID,
*initialarea = INVALID, *error_tag = MISSING_TAGS;
int attributesFound = 0, num_of_areas = 0;
bool has_auth = false, has_lang = false, has_init = false;
TiXmlNode* pChild;
TiXmlElement *element = pParent->ToElement();
TiXmlAttribute *attributes = element->FirstAttribute();
while (attributes) {
if(!strcmp(attributes->Name(),"author")) {
author = attributes->Value();
attributesFound++;
if(has_auth) {
error_tag = "More than one author tag";
}
has_auth = true;
}
else if(!strcmp(attributes->Name(),"language")) {
language = attributes->Value();
attributesFound++;
if(has_lang) {
error_tag = "More than one language tag";
}
has_lang = true;
}
else if(!strcmp(attributes->Name(),"initialarea")) {
initialarea = attributes->Value();
attributesFound++;
if(has_init) {
error_tag = "More than one initialarea tag";
}
has_init = true;
}
attributes=attributes->Next();
}
if(attributesFound == WORLD_ATTRIBUTES && has_init && has_lang && has_auth) {
world = new World(author, language, initialarea);
for ( pChild = pParent->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) {
if(pChild->Type() == TiXmlNode::TINYXML_ELEMENT) {
if(!strcmp(pChild->Value(),"area")) {
num_of_areas++;
world->add_area(make_area(pChild, num_of_areas, world));
}
}
else {
error_parsing("An element below world is not an area.", world);
}
}
}
else {
std::ostringstream sin;
sin << "In make_world there is an attribute error, found: ";
sin << error_tag;
std::string message = sin.str();
error_parsing(message, world);
}
if(!world->init_active_area()) {
std::ostringstream sin;
sin << "initial area not found in the list of areas ";
std::string message = sin.str();
error_parsing(message, world);
}
for(int area = 0; area < world->get_num_areas(); area++) {
Area *temp_area = world->get_area(area);
for(int item = 0; item < temp_area->get_num_items(); item++) {
Item *temp_item = temp_area->get_item(item);
for(int item_next = item+1; item_next < temp_area->get_num_items(); item_next++) {
if(!strcmp(temp_area->get_item(item_next)->get_id().c_str(), temp_item->get_id().c_str())) {
std::ostringstream sin;
sin << "There are two items in the game with id: ";
sin << temp_item->get_id();
sin << " both in ";
sin << temp_area->get_id();
error_parsing(sin.str(), world);
}
}
for(int next_area = area + 1; next_area < world->get_num_areas(); next_area++) {
if(world->get_area(next_area)->has_item(temp_item->get_id())) {
std::ostringstream sin;
sin << "There are two items in the game with id: ";
sin << temp_item->get_id();
sin << " one in ";
sin << temp_area->get_id();
sin << " the other in ";
sin << world->get_area(next_area)->get_id();
error_parsing(sin.str(), world);
}
}
}
}
return world;
}
void error_parsing(std::string message, World *world) {
const char *error_string = message.c_str();
fprintf(stderr,"ERROR: [%s]\n", error_string);
exit(EXIT_FAILURE);
}
World *make_objects( TiXmlNode* pParent, World *world) {
if(pParent->Type() == TiXmlNode::TINYXML_DOCUMENT) {
pParent = pParent->FirstChild();
if(pParent->Type() == TiXmlNode::TINYXML_DECLARATION) {
pParent = pParent->NextSibling();
if(pParent->Type() == TiXmlNode::TINYXML_ELEMENT) {
return make_world(pParent, world);
}
else {
error_parsing("XML document element not found", world);
}
}
else {
error_parsing("Missing XML declaration", world);
}
}
else {
error_parsing("Missing world element", world);
}
return NULL;
}