/
XDataLoader.cpp
782 lines (715 loc) · 24.3 KB
/
XDataLoader.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
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
#include "XDataLoader.h"
#include "string/convert.h"
#include "iarchive.h"
namespace XData
{
const bool XDataLoader::importDef( const std::string& definitionName, XDataMap& target, const std::string& filename )
{
// Initialization:
_errorList.clear();
_newXData.reset();
target.clear();
StringList files;
if (filename != "")
{
//Check fileextension:
if (filename.substr( filename.rfind(".")+1 ) != "xd")
return reportError("[XDataLoader::import] Error: File-extension is not .xd: " + filename + "\n");
files.push_back(filename);
}
else
{
//Find the files in which the definition can be found.
retrieveXdInfo();
StringVectorMap::iterator it = _defMap.find(definitionName);
if (it == _defMap.end())
return reportError("[XDataLoader::importDef] Error: Specified definition " + definitionName + " not found.\n");
files = it->second;
if (files.size() > 1) //Definition contained in multiple files.
reportError("[XData::import] Warning: The requested definition " + definitionName + " exists in multiple files.\n");
}
// Parse the requested definition from all files:
for (std::size_t n = 0; n < files.size(); n++)
{
// Attempt to open the file in text mode and retrieve DefTokeniser
ArchiveTextFilePtr file =
GlobalFileSystem().openTextFile(files[n]);
if (file == NULL)
return reportError("[XDataLoader::importDef] Error: Failed to open file " + files[n] + "\n");
std::istream is(&(file->getInputStream()));
parser::BasicDefTokeniser<std::istream> tok(is);
// Parse the desired definition:
while (tok.hasMoreTokens() && !parseXDataDef(tok,definitionName)) {}
if (_newXData)
target.insert(XDataMap::value_type(file->getModName() + "/" + file->getName(),_newXData));
else
reportError("[XDataLoader::importDef] Error: Failed to load " + definitionName + " from file " + files[n] + ".\n");
}
//Summarizing report:
if (target.size() == 0)
{
if (filename == "")
reportError("[XDataLoader::importDef] Error: Failed to load " + definitionName + ".\n");
return false;
}
else
{
_errorList.push_back("[XDataLoader::importDef] " + definitionName + " loaded successfully with "
+ string::to_string(_errorList.size()) + " error(s)/warning(s).\n");
//Summary output:
if (_errorList.size() > 1)
rConsoleError() << _errorList[_errorList.size() - 1];
else
rMessage() << _errorList[0];
}
return true;
}
const bool XDataLoader::import( const std::string& filename, XDataMap& target )
{
//initialization:
_errorList.clear();
target.clear();
unsigned int ErrorCount = 0;
//Check fileextension:
if (filename.substr( filename.rfind(".")+1 ) != "xd")
return reportError("[XDataLoader::import] Fileextension is not .xd: " + filename + "\n");
// Attempt to open the file in text mode and retrieve DefTokeniser
ArchiveTextFilePtr file =
GlobalFileSystem().openTextFile(filename);
if (file == NULL)
return reportError("[XDataLoader::import] Failed to open file: " + filename + "\n");
std::istream is(&(file->getInputStream()));
parser::BasicDefTokeniser<std::istream> tok(is);
//Parse Loop:
while (tok.hasMoreTokens())
{
if (parseXDataDef(tok))
target.insert(XDataMap::value_type(_newXData->getName(),_newXData));
else
ErrorCount += 1;
}
//Write summary
_errorList.push_back(
"[XDataLoader::import] Import finished with " + string::to_string(_errorList.size())
+ " error(s)/warning(s). " + string::to_string(target.size())
+ " XData-definition(s) successfully imported, but failed to import at least "
+ string::to_string(ErrorCount) + " definitions.\n"
);
//Summary output:
if (_errorList.size() == 1) //No errors.
rMessage() << _errorList[0];
else
rConsoleError() << _errorList[_errorList.size() - 1];
if (target.size() == 0)
return false;
return true;
} // XDataLoader::import
const bool XDataLoader::parseXDataDef(parser::DefTokeniser& tok, const std::string& definitionName)
{
_name = tok.nextToken();
_newXData.reset();
//Check Syntax:
try { tok.assertNextToken("{"); }
catch (...)
{
while (tok.hasMoreTokens() && (tok.nextToken() != "{")) {}
jumpOutOfBrackets(tok);
return reportError(tok, "[XDataLoader::import] Syntaxerror in definition: " + _name + ". '{' expected. Undefined behavior!\n\tTrying to Jump to next XData definition. Might lead to furthers errors.\n");
}
//Check if every definition shall be parsed or only a specific one:
if (!definitionName.empty() && _name != definitionName)
{
jumpOutOfBrackets(tok);
return false;
}
//Initialization:
_guiPageError.clear();
_maxPageCount = 0;
_maxGuiNumber = 0;
_guiPageDef = "";
_numPages = 0;
_sndPageTurn = "";
_guiPage.clear();
_guiPage.resize(MAX_PAGE_COUNT,""); //see MAX_PAGE_COUNT declaration in XData.h for explanation.
//Parse-Loop:
while (tok.hasMoreTokens())
{
std::string token = tok.nextToken();
if (token == "}")
break;
if (!storeContent(token, &tok, _name))
return false;
}
//cleaning up:
if ( _maxGuiNumber+1 > _numPages) //Append missing GUI-errormessages... Until now, it wasn't clear how many guipages are actually discarded.
{
int diff = _maxGuiNumber + 1 - _maxPageCount;
for (unsigned int n = _guiPageError.size()-diff; n<_guiPageError.size(); n++)
{
reportError(_guiPageError[n]);
}
}
// Check if _guiPage-statements for all pages are available.
if (_guiPageDef.empty())
{
reportError("[XDataLoader::import] Warning for definition: " + _name
+ ". _guiPage-statement(s) missing. Setting default value...\n");
if (_newXData->getPageLayout() == TwoSided)
_guiPageDef = DEFAULT_TWOSIDED_GUI;
else
_guiPageDef = DEFAULT_ONESIDED_GUI;
}
for (std::size_t n=0; n<_numPages; n++ )
{
if (_guiPage[n].empty())
_guiPage[n] = _guiPageDef;
}
//Store values:
_newXData->setGuiPage(_guiPage);
_newXData->setNumPages(_numPages);
//Use default sndPageTurn if the statement was missing:
if (_sndPageTurn.empty())
{
_newXData->setSndPageTurn(DEFAULT_SNDPAGETURN);
reportError("[XDataLoader::import] Warning for definition: " + _name
+ ". _sndPageTurn-statement missing. Setting default value...\n");
}
else
_newXData->setSndPageTurn(_sndPageTurn);
return true;
}
const bool XDataLoader::storeContent(const std::string& statement, parser::DefTokeniser* tok, const std::string& defName, const std::string& content)
{
//page-statement:
if (statement.substr(0,4) == "page")
{
//Now that it is known, whether we are dealing with a Two- or OneSidedXDataObject, create the XData object.
if (!_newXData)
{
if (statement.find("left",6) != std::string::npos || statement.find("right",6) != std::string::npos) //TwoSided
_newXData = XDataPtr(new TwoSidedXData(_name));
else
_newXData = XDataPtr(new OneSidedXData(_name));
}
//Acquire PageIndex
std::size_t PageIndex;
std::size_t numEnd = 4;
while (statement[numEnd++] != '_') {}
std::string number = statement.substr(4,numEnd-5);
try { PageIndex = std::stoi(number)-1; }
catch (...)
{
if (tok != NULL)
{
_newXData.reset();
return reportError(*tok, "[XDataLoader::import] Error in definition: " + defName + ", "
+ statement + " statement. '" + number
+ "' is not a number.\n\tTrying to Jump to next XData definition. Might lead to furthers errors.\n"
);
}
_newXData.reset();
return reportError("[XDataLoader::import] Error in definition: " + defName + ", " + statement + " statement. '"
+ number + "' is not a number.\n"
);
}
//Read content
std::string readContent;
if (tok != NULL)
{
if (!readLines(*tok, readContent))
{
_newXData.reset();
return reportError(*tok, "[XDataLoader::import] Error in definition: " + defName + ". Failed to read content of "
+ statement + " statement.\n\tTrying to Jump to next XData definition. Might lead to furthers errors.\n"
);
}
}
else
readContent = content;
// Check whether readContent actually has content. (There might be space, linebreaks and tabs that can be discarded)
bool hasContent = false;
for (std::size_t n = 0; n < readContent.size(); n++)
{
if ( (readContent[n] != ' ') && (readContent[n] != '\t') && (readContent[n] != '\n') )
{
hasContent = true;
break;
}
}
//Check PageIndex-Range
if (PageIndex >= _numPages )
{
if (hasContent) //_numPages is raised automatically, if a page with content is detected...
{
_numPages = PageIndex+1;
reportError("[XDataLoader::import] Warning for definition: " + defName + ", " + statement
+ " statement.\n\tnumPages not (yet) specified or too low. Raising _numPages to " + number +"...\n"
);
}
else
{
reportError("[XDataLoader::import] Warning for definition: " + defName + ", " + statement
+ " statement.\n\tnumPages not (yet) specified or too low. However, this page does not contain any text and is discarded...\n"
);
return true;
}
}
//Refresh the _maxPageCount variable if this page has content.
if (hasContent)
{
if (_maxPageCount < PageIndex+1)
_maxPageCount = PageIndex+1;
//Write the content into the XData object
Side side;
if (statement.find("left",6) != std::string::npos) //Side is discarded on OneSidedXData...
side = Left;
else
side = Right;
if (statement.find("body",6) != std::string::npos)
_newXData->setPageContent(Body, PageIndex, side, readContent); //could throw if PageIndex >= MAX_PAGE_COUNT. Unlikely!
else
_newXData->setPageContent(Title, PageIndex, side, readContent);
}
return true;
}
//gui_page statement
else if (statement.substr(0,8) == "gui_page")
{
//Acquire gui_page number:
std::string number = statement.substr(8);
std::size_t guiNumber;
try { guiNumber = std::stoi(number)-1; }
catch (...)
{
if (tok != NULL)
{
_newXData.reset();
return reportError(*tok, "[XDataLoader::import] Error in definition: " + defName + ", gui_page statement. '" + number
+ "' is not a number.\n\tTrying to Jump to next XData definition. Might lead to furthers errors.\n"
);
}
_newXData.reset();
return reportError("[XDataLoader::import] Error in definition: " + defName + ", gui_page statement. '" + number + "' is not a number.\n");
}
if (_maxGuiNumber < guiNumber)
_maxGuiNumber = guiNumber;
//Get the GuiPageDef:
if (tok != NULL)
{
if (!readLines(*tok, _guiPageDef))
{
_newXData.reset();
return reportError(*tok, "[XDataLoader::import] Error in definition: " + defName + ". Failed to read content of " + statement
+ " statement.\n\tTrying to Jump to next XData definition. Might lead to furthers errors.\n"
);
}
}
else
_guiPageDef = content;
_guiPage[ guiNumber ] = _guiPageDef; //could throw if guiNumber >= MAX_PAGE_COUNT
//Append error-message if _numPages is exceeded.
if (guiNumber >= _numPages)
_guiPageError.push_back("[XDataLoader::import] Warning for definition: " + defName
+ ". More _guiPage statements, than pages. Discarding statement for Page " + number + ".\n"
);
return true;
}
//num_pages statement
else if (statement == "num_pages")
{
//Get num_pages:
if (tok != NULL)
{
std::string number;
if (!readLines(*tok, number))
{
_newXData.reset();
return reportError(*tok, "[XDataLoader::import] Error in definition: " + defName + ". Failed to read content of "
+ statement + " statement.\n\tTrying to Jump to next XData definition. Might lead to furthers errors.\n"
);
}
try { _numPages = std::stoi(number); }
catch(...)
{
_newXData.reset();
return reportError(*tok, "[XDataLoader::import] Error in definition: " + defName + ", num_pages statement. '" + number
+ "' is not a number.\n\tTrying to Jump to next XData definition. Might lead to furthers errors.\n"
);
}
}
else
{
try { _numPages = std::stoi(content); }
catch (...)
{
return reportError("[XDataLoader::import] Error in definition: " + defName + ", num_pages statement. '" + content
+ "' is not a number.\n"
);
}
}
//Correct a faulty _numPages value
if (_maxPageCount > _numPages)
{
_numPages = _maxPageCount;
reportError("[XDataLoader::import] Warning for definition: " + defName
+ ". The specified _numPages statement did not match the amount of pages with content.\n\tnumPages is set to "
+ string::to_string(_numPages) + ".\n");
}
return true;
}
//snd_page_turn statement
else if (statement == "snd_page_turn")
{
if (tok != NULL)
{
if (!readLines(*tok, _sndPageTurn))
{
_newXData.reset();
return reportError(*tok, "[XDataLoader::import] Error in definition: " + defName + ". Failed to read content of " + statement
+ " statement.\n\tTrying to Jump to next XData definition. Might lead to furthers errors.\n"
);
}
}
else
_sndPageTurn = content;
return true;
}
//import statement
else if (statement == "import")
{
StringPairList importedData;
std::string SourceDef;
StringMap statements;
if (tok == NULL) //Only works with def tokeniser.
{
_newXData.reset();
return false;
}
if (!getImportParameters(*tok, statements, SourceDef, defName))
{
_newXData.reset();
return false;
}
retrieveXdInfo(); //refresh defmap. Prevents faulty imports.
if (!recursiveImport(SourceDef, statements, defName, importedData))
{
_errorList[_errorList.size()-1] += "\tTrying to Jump to next XData definition. Might lead to furthers errors.\n";
jumpOutOfBrackets(*tok);
_newXData.reset();
return false;
}
for (std::size_t n=0; n < importedData.size(); n++)
{
if (!storeContent(importedData[n].first, NULL, defName, importedData[n].second))
{
_newXData.reset();
return reportError(*tok, "[XDataLoader::import] Error in definition: " + defName
+ ". Import-statement failed.\n\tTrying to Jump to next XData definition. Might lead to furthers errors.\n"
);
}
}
return true;
}
else if (statement == "precache")
{
return true;
}
_newXData.reset();
jumpOutOfBrackets(*tok);
return false;
}
const bool XDataLoader::recursiveImport( const std::string& sourceDef, const StringMap& statements, const std::string& defName, StringPairList& importContent)
{
//Check where the Definition is stored in the _defMap
StringVectorMap::iterator it = _defMap.find(sourceDef);
if (it == _defMap.end()) //Definition not found!!
return reportError("[XData::import] Error in definition: " + defName + ". Found an import-statement, but not the corresponding definition.\n");
std::size_t FileCount = it->second.size();
if (FileCount > 1) //Definition contained in multiple files.
reportError(
"[XData::import] Warning for definition: " + defName
+ ". Found an import-statement and the referenced definition exists multiple times.\n"
);
//Try every file the definition is stored in. If import of none succeeded, the import failed.
for (std::size_t k = 0; k < FileCount; k++)
{
//Init
StringMap StmtsCpy = statements;
StringMap ImpStmts;
std::string ImpSrcDef = "";
int BracketDepth = 1;
//Open the file
ArchiveTextFilePtr file =
GlobalFileSystem().openTextFile(it->second[k]);
if (file == NULL)
return reportError(
"[XData::import] Error in definition: " + defName
+ ". Found an import-statement, but failed to open the corresponding file: " + it->second[k] + ".\n"
);
//Find the Source-Definition in the File:
std::istream is(&(file->getInputStream()));
parser::BasicDefTokeniser<std::istream> ImpTok(is);
while (true)
{
while ( ImpTok.nextToken() != sourceDef) {}
if ( ImpTok.nextToken() == "{")
break;
}
//Import-Loop: Find the SourceStatements in the definition and parse the contents.
//Further Import-directives possibly have to be handled recursively...
while (ImpTok.hasMoreTokens())
{
//Find SourceStatements or further import-statements. Count entering and leaving brackets to identify definition end.
std::string token = ImpTok.nextToken();
StringMap::iterator StIt = StmtsCpy.find(token);
if (StIt != StmtsCpy.end())
{
//Token found that matches the Source statement. Parse it and delete statement from StmtsCpy afterwards.
std::string StmtCtnt;
if (readLines(ImpTok, StmtCtnt))
{
importContent.push_back( StringPairList::value_type(
StIt->second,
StmtCtnt
));
StmtsCpy.erase(StIt);
}
else if (!ImpTok.hasMoreTokens())
{
//Syntaxerror!
std::string errMSG = "[XData::import] Error in definition: " + defName
+ ". Found an import-statement, but an sytax-error occurred in the definition "
+ sourceDef + ", in sourcefile " + it->second[k] + ".\n";
if ( (FileCount > 1) && (k+1 < FileCount) )
errMSG += "\tThe referenced definition has been found in multiple files. Trying next file...\n";
reportError(errMSG);
break;
}
else
//The found token was not an actual statement.
continue;
}
else if (token == "{")
BracketDepth += 1;
else if (token == "}")
BracketDepth -= 1;
else if (token == "import")
{
//getImportParameters fails on syntax errors or EOF (which is an syntax-error as well), so go on with next file.
if (!getImportParameters(ImpTok, ImpStmts, ImpSrcDef, defName))
{
std::string errMSG = "[XData::import] Error in definition: " + defName
+ ". Found an import-statement, but an sytax-error occurred in the definition "
+ sourceDef + ", in sourcefile " + it->second[k] + ".\n";
if ( (FileCount > 1) && (k < FileCount-1) )
errMSG += "\tThe referenced definition has been found in multiple files. Trying next file...\n";
reportError(errMSG);
break;
}
}
if (StmtsCpy.size() == 0)
//Success: Return!
return true;
if (BracketDepth == 0)
{
//Not all Source-statements have been found in the definition. If an import statement has been found
//check if all requested statements would be imported by it.
if (ImpSrcDef != "")
{
StringMap ChkStmts = StmtsCpy;
StringMap RecoverStatements;
for (StringMap::iterator impCheck = ImpStmts.begin(); impCheck != ImpStmts.end(); impCheck++)
{
StringMap::iterator temp = ChkStmts.find(impCheck->second);
if (temp != ChkStmts.end())
{
ChkStmts.erase(temp);
RecoverStatements.insert(*impCheck);
}
}
if (ChkStmts.size() == 0)
{
//All necessary statements found in import-directive.
StringPairList imported;
if (recursiveImport(ImpSrcDef,RecoverStatements,sourceDef,imported))
{
//Success! Store imported contents and return!
for (std::size_t u = 0; u < imported.size(); u++)
{
importContent.push_back( StringPairList::value_type(
StmtsCpy.find(imported[u].first)->second,
imported[u].second
));
}
return true;
}
else
{
std::string errMSG = "[XData::import] Error in definition: " + defName
+ ". Found an import-statement, but failed to import the requested statements of definition "
+ sourceDef + ", in sourcefile " + it->second[k] + ".\n";
if ( (FileCount > 1) && (k < FileCount-1) )
errMSG += "\tThe referenced definition has been found in multiple files. Trying next file...\n";
reportError(errMSG);
break;
}
}
}
//No import-statement found or not all requested definitions in import statement. Move on with next file.
std::string errMSG = "[XData::import] Error in definition: " + defName
+ ". Found an import-statement, but couldn't find all referenced statements in the definition "
+ sourceDef + ", in sourcefile " + it->second[k] + ".\n";
if ( (FileCount > 1) && (k < FileCount-1) )
errMSG += "\tThe referenced definition has been found in multiple files. Trying next file...\n";
reportError(errMSG);
break;
} //End of definition was reached!
} //End of Import-Loop
} //End of File-duplicate-Loop
return reportError("[XData::import] Error in definition: " + defName + ". Import-statement failed.\n");
}
const bool XDataLoader::getImportParameters(parser::DefTokeniser& tok, StringMap& statements, std::string& sourceDef, const std::string& defName)
{
std::string token;
//Enter content brackets
try { tok.assertNextToken("{"); }
catch(...)
{
return reportError(tok, "[XDataLoader::import] Syntax error in definition: " + defName
+ ", import-statement. '{' expected. Undefined behavior!\n"
);
}
//Get Source- and DestStatements
try
{
token = tok.nextToken();
while (token != "}")
{
tok.skipTokens(1); //Skip "->"
statements.insert( StringMap::value_type(token,tok.nextToken()) );
token = tok.nextToken();
}
}
catch (...)
{
return reportError(tok, "[XDataLoader::import] Error in definition: " + defName
+ ". Failed to read content of import-statement. Probably Syntax-error.\n"
+ "\tTrying to Jump to next XData definition. Might lead to furthers errors.\n",
2 );
}
//Get Name of Source-Definition
try { tok.assertNextToken("from"); }
catch (...)
{
return reportError(tok, "[XDataLoader::import] Syntax error in definition: " + defName
+ ", import-statement. 'from' expected. Undefined behavior!\n"
+ "\tTrying to Jump to next XData definition. Might lead to furthers errors.\n"
);
}
try { sourceDef = tok.nextToken(); }
catch (...)
{
return reportError(tok, "[XDataLoader::import] Error in definition: " + defName
+ ". Failed to read content of import-statement.\n"
+ "\tTrying to Jump to next XData definition. Might lead to furthers errors.\n"
);
}
return true;
}
inline const bool XDataLoader::readLines(parser::DefTokeniser& tok, std::string& what) const
{
std::stringstream out;
std::string token;
//Check Syntax
try
{
tok.assertNextToken(":");
what = tok.nextToken();
if (what != "{" ) //Parsing SingleLine Content
return true;
//Parse MultiLine Content.
token = tok.nextToken();
while (token != "}")
{
if (token == "/") //newlines can also be signalized by slashed. Ignore them
{
token = tok.nextToken();
continue;
}
else if (token.c_str()[token.length()-1] == '\\') //support for escaped quotes in texts...
token = token.substr(0,token.length()-1) + "\"";
else
token = token + "\n";
out << token;
token = tok.nextToken();
}
}
catch (...) { return false; }
what = out.str();
return true;
}
void XDataLoader::jumpOutOfBrackets(parser::DefTokeniser& tok, std::size_t currentDepth) const
{
while ( tok.hasMoreTokens() && currentDepth > 0)
{
std::string token = tok.nextToken();
if (token == "{")
currentDepth += 1;
else if (token == "}")
currentDepth -= 1;
}
}
void XDataLoader::retrieveXdInfo()
{
_defMap.clear();
_fileSet.clear();
_duplicatedDefs.clear();
//ScopedDebugTimer timer("XData definitions parsed: ");
GlobalFileSystem().forEachFile(
XDATA_DIR, XDATA_EXT,
[&](const vfs::FileInfo& fileInfo) { loadFromFile(fileInfo.name); },
99
);
}
void XDataLoader::loadFromFile(const std::string& filename)
{
// Attempt to open the file in text mode
ArchiveTextFilePtr file =
GlobalFileSystem().openTextFile(XDATA_DIR + filename);
if (file != NULL) {
// File is open, so add the file to the _fileSet-set and parse the tokens
_fileSet.insert(file->getModName() + "/" + file->getName());
try
{
std::istream is(&(file->getInputStream()));
parser::BasicDefTokeniser<std::istream> tok(is);
//grab all names from stream:
while (tok.hasMoreTokens())
{
std::string tempstring = tok.nextToken();
tok.assertNextToken("{");
std::pair<StringVectorMap::iterator,bool> ret = _defMap.insert( StringVectorMap::value_type(tempstring, StringList(1, XDATA_DIR + filename) ) );
if (!ret.second) //Definition already exists.
{
ret.first->second.push_back(XDATA_DIR + filename);
rError() << "[XDataLoader] The definition " << tempstring << " of the file " << filename << " already exists. It was defined at least once. First in " << ret.first->second[0] << ".\n";
//Create an entry in the _duplicatedDefs map with the original file. If entry already exists, insert will fail.
std::pair<StringVectorMap::iterator,bool> duplRet = _duplicatedDefs.insert( StringVectorMap::value_type(tempstring, StringList(1,ret.first->second[0]) ) );
//The new file is appended to the vector.
duplRet.first->second.push_back(XDATA_DIR + filename);
}
jumpOutOfBrackets(tok);
}
}
catch (parser::ParseException& e)
{
rError() << "[XDataLoader] Failed to parse " << filename
<< ": " << e.what() << std::endl;
}
}
else
{
rError() << "[XDataLoader] Unable to open " << filename << std::endl;
}
}
} // namespace XData