/
XData.cpp
438 lines (394 loc) · 12.2 KB
/
XData.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
#include "XData.h"
#include "i18n.h"
#include "itextstream.h"
#include <boost/lexical_cast.hpp>
#include <boost/filesystem/convenience.hpp>
namespace XData
{
//XData implementations:
//->export:
FileStatus XData::xport( const std::string& filename, ExporterCommand cmd )
{
boost::filesystem::path Path(filename);
boost::filesystem::path parent = Path.parent_path();
// Ensure the parent path exists
boost::filesystem::create_directories(parent);
if (boost::filesystem::exists(Path))
{
switch (cmd)
{
case Merge:
{
//Check if definition already exists and return DefinitionExists. If it does not, append the definition to the file.
boost::filesystem::fstream file(Path, std::ios_base::in | std::ios_base::out | std::ios_base::app);
if (!file.is_open())
return OpenFailed;
std::stringstream ss;
ss << file.rdbuf();
std::string String = ss.str();
std::size_t DefPos = String.find(_name);
while (DefPos != std::string::npos) //A name of a XData could be contained in another XData's name. Check that...
{
char before = String[DefPos > 0 ? DefPos-1 : 0];
char after = String[DefPos+_name.length() < String.size() ? DefPos+_name.length() : 0];
if ((DefPos == 0 || before == ' ' || before == '\t' || before == '\n') && (DefPos+_name.length() == String.size() || after == ' ' || after == '\t' || after == '\n')) //other delimiters necessary?
{
_definitionStart = 0;
//Definition found. But we want to have it clean, so let's see how many spaces are before the definition begin.
while (DefPos > 0)
{
if (String[--DefPos] != ' ' && String[DefPos] != '\t' && String[DefPos] != '\n')
{
_definitionStart = DefPos+1;
break;
}
}
file.close();
return DefinitionExists;
}
DefPos = String.find(_name,DefPos+_name.length());
}
file << "\n\n\n" << generateXDataDef();
file.close();
return AllOk;
}
case MergeOverwriteExisting:
{
//Find the old definition in the target file and delete it. Append the new definition.
//_definitionStart has been set in the first iteration of this method.
boost::filesystem::fstream file(Path, std::ios_base::in);
if (!file.is_open())
return OpenFailed;
std::stringstream ss;
ss << file.rdbuf();
std::string OutString = ss.str();
file.close();
std::size_t DefLength = getDefLength(OutString.substr(_definitionStart));
if (DefLength == 0) //If the definitionlength can't be obtained, the merge fails.
return MergeFailed;
OutString.erase(_definitionStart, DefLength);
std::string insString = std::string( (_definitionStart != 0) ? "\n\n\n" : "" ) //No new lines in the first line
+ generateXDataDef()
+ std::string( (_definitionStart == OutString.size()) ? "" : "\n\n\n" ); //No new lines at the end of file
OutString.insert(_definitionStart, insString);
file.open(Path, std::ios_base::out | std::ios_base::trunc);
if (!file.is_open())
return OpenFailed;
file << OutString;
file.close();
return AllOk;
}
case Overwrite:
{
//Warn if the target file contains multiple definitions: return MultipleDefinitions.
//Warn if the definition in the target file does not match the current definition: return DefinitionMisMatch
//else overwrite existing file.
std::string DefName;
boost::filesystem::ifstream file(Path, std::ios_base::in);
if (!file.is_open())
return OpenFailed;
try { DefName = getDefinitionNameFromXD(file); }
catch (...)
{
rConsoleError() << "[XData::xport] Syntax error in file " << filename
<< ". Overwriting the file..." << std::endl;
break;
}
if (DefName == _name) //Definition will be overwritten...
break;
else if (DefName == "") //MultipleDefinitions exist
return MultipleDefinitions;
return DefinitionMismatch;
}
case OverwriteMultDef:
//Replace the file no matter what.
break;
default: return FileExists;
}
}
//Write the definition into the file.
boost::filesystem::ofstream file(Path, std::ios_base::out | std::ios_base::trunc);
if (!file.is_open())
return OpenFailed;
file << generateXDataDef();
file.close();
return AllOk;
}
const std::string XData::generateXDataDef() const
{
std::stringstream xDataDef;
xDataDef << _name << "\n" << "{" << "\n" << "\tprecache" << "\n" << "\t\"num_pages\"\t: \"" << _numPages << "\"\n";
std::stringstream ss;
std::string TempString;
xDataDef << getContentDef();
for (std::size_t n=0; n<_numPages; n++)
{
xDataDef << "\t\"gui_page" << n+1 << "\"\t: \"" << _guiPage[n] << "\"\n";
}
xDataDef << "\t\"snd_page_turn\"\t: \"" << _sndPageTurn << "\"\n}";//*/
return xDataDef.str();
}
const std::size_t XData::getDefLength(const std::string& def) const
{
std::size_t charIndex = 0;
while (def[charIndex] != '\0')
{
if (def[++charIndex] == '{')
{
int BracketCount = 1;
while (def[++charIndex] != '\0' && BracketCount > 0)
{
// Move out of brackets
if (def[charIndex] == '{')
++BracketCount;
else if (def[charIndex] == '}')
--BracketCount;
}
if (BracketCount > 0)
break;
while (def[charIndex] != '\0')
{
// The definitionlength also expands over trailing spaces
if ( (def[charIndex] != ' ') && (def[charIndex] != '\t') && (def[charIndex] != '\n') )
break;
++charIndex;
}
return charIndex;
}
}
return 0; //no appropriate bracketstructure was found.
}
const std::string XData::getDefinitionNameFromXD(boost::filesystem::ifstream& file) const
{
std::string ReturnString;
parser::BasicDefTokeniser<std::istream> tok(file);
bool FirstDef = true;
while (tok.hasMoreTokens())
{
if (FirstDef)
ReturnString = tok.nextToken();
else
return "";
FirstDef = false;
tok.assertNextToken("{");
jumpOutOfBrackets(tok, 1);
}
return ReturnString;
}
const std::string XData::generateTextDef(const std::string& rawString) const
{
std::stringstream ss;
std::stringstream xDataDef;
std::string TempString;
xDataDef << "\t{\n";
if (rawString != "")
{
ss << rawString;
while ( std::getline(ss, TempString) ) //replace "\n" and add an escape-character before quotes.
{
std::size_t QuotePos = TempString.find("\"",0);
while ( QuotePos != std::string::npos )
{
TempString.insert(QuotePos, "\\");
QuotePos = TempString.find("\"",QuotePos+2);
}
xDataDef << "\t\t\"" << TempString << "\"\n";
}
xDataDef << "\t}\n";
}
else
xDataDef << "\t\t\"\"\n\t}\n";
return xDataDef.str();
}
//->general:
void XData::resizeVectors(std::size_t targetSize)
{
std::string fill = "";
if (_guiPage.size() > 0)
fill = _guiPage[_guiPage.size()-1];
_guiPage.resize(targetSize, fill);
}
void XData::jumpOutOfBrackets(parser::DefTokeniser& tok, int currentDepth) const
{
while ( tok.hasMoreTokens() && currentDepth > 0)
{
std::string token = tok.nextToken();
if (token == "{")
currentDepth += 1;
else if (token == "}")
currentDepth -= 1;
}
}
//TwoSidedXData implementations:
void TwoSidedXData::resizeVectors(std::size_t targetSize)
{
XData::resizeVectors(targetSize);
_pageLeftBody.resize(targetSize, "");
_pageLeftTitle.resize(targetSize, "");
_pageRightBody.resize(targetSize, "");
_pageRightTitle.resize(targetSize, "");
}
void TwoSidedXData::setPageContent(ContentType cc, std::size_t pageIndex, Side side, const std::string& content)
{
if (pageIndex >= _numPages)
throw std::runtime_error(_("Page Index out of bounds."));
switch (cc)
{
case Title:
switch (side)
{
case Left:
_pageLeftTitle[pageIndex] = content;
break;
default:
_pageRightTitle[pageIndex] = content;
}
break;
default:
switch (side)
{
case Left:
_pageLeftBody[pageIndex] = content;
break;
default:
_pageRightBody[pageIndex] = content;
}
}
}
const std::string& TwoSidedXData::getPageContent(ContentType cc, std::size_t pageIndex, Side side) const
{
if (pageIndex >= _numPages)
throw std::runtime_error(_("Page Index out of bounds."));
switch (cc)
{
case Title:
switch (side)
{
case Left:
return _pageLeftTitle[pageIndex];
default:
return _pageRightTitle[pageIndex];
}
default:
switch (side)
{
case Left:
return _pageLeftBody[pageIndex];
default:
return _pageRightBody[pageIndex];
}
}
}
const std::string TwoSidedXData::getContentDef() const
{
std::stringstream xDataDef;
for (std::size_t n = 0; n < _numPages; n++)
{
//Page Left Title
xDataDef << "\t\"page" << n+1 << "_left_title\"\t:\n";
xDataDef << generateTextDef(_pageLeftTitle[n]);
//Page Left Body
xDataDef << "\t\"page" << n+1 << "_left_body\"\t:\n";
xDataDef << generateTextDef(_pageLeftBody[n]);
//Page Right Title
xDataDef << "\t\"page" << n+1 << "_right_title\"\t:\n";
xDataDef << generateTextDef(_pageRightTitle[n]);
//Page Right Body
xDataDef << "\t\"page" << n+1 << "_right_body\"\t:\n";
xDataDef << generateTextDef(_pageRightBody[n]);
}
return xDataDef.str();
}
void TwoSidedXData::togglePageLayout(XDataPtr& target) const
{
XDataPtr newXData(new OneSidedXData(_name));
newXData->setNumPages(_numPages*2); //delete last page if no content later.
newXData->setSndPageTurn(_sndPageTurn);
// Add default guiPage to all guiPage-entries.
newXData->setGuiPage( StringList( newXData->getNumPages(), DEFAULT_ONESIDED_GUI) );
// Reshuffle the TwoSided pages contents into the OneSided page contents.
for (std::size_t n = 0; n < _numPages; n++)
{
newXData->setPageContent(Title, 2*n, Left, _pageLeftTitle[n] );
newXData->setPageContent(Body, 2*n, Left, _pageLeftBody[n] );
newXData->setPageContent(Title, 2*n+1, Left, _pageRightTitle[n] );
newXData->setPageContent(Body, 2*n+1, Left, _pageRightBody[n] );
}
if ( (_pageRightTitle[_numPages-1] == "") && (_pageRightBody[_numPages-1] == "") )
// Last page is empty. Set new numpages accordingly.
newXData->setNumPages(newXData->getNumPages()-1);
target = newXData;
}
//OneSidedXData implementations:
void OneSidedXData::resizeVectors(std::size_t targetSize)
{
XData::resizeVectors(targetSize);
_pageBody.resize(targetSize, "");
_pageTitle.resize(targetSize, "");
}
void OneSidedXData::setPageContent(ContentType cc, std::size_t pageIndex, Side side, const std::string& content)
{
if (pageIndex >= _numPages)
throw std::runtime_error(_("Page Index out of bounds."));
switch (cc)
{
case Title:
_pageTitle[pageIndex] = content;
break;
case Body:
default:
_pageBody[pageIndex] = content;
}
}
const std::string& OneSidedXData::getPageContent(ContentType cc, std::size_t pageIndex, Side side) const
{
if (pageIndex >= _numPages)
throw std::runtime_error(_("Page Index out of bounds."));
switch (cc)
{
case Title:
return _pageTitle[pageIndex];
default:
return _pageBody[pageIndex];
}
}
const std::string OneSidedXData::getContentDef() const
{
std::stringstream xDataDef;
for (std::size_t n = 0; n < _numPages; n++)
{
//Title
xDataDef << "\t\"page" << n+1 << "_title\"\t:\n";
xDataDef << generateTextDef(_pageTitle[n]);
//Body
xDataDef << "\t\"page" << n+1 << "_body\"\t:\n";
xDataDef << generateTextDef(_pageBody[n]);
}
return xDataDef.str();
}
void OneSidedXData::togglePageLayout(XDataPtr& target) const
{
XDataPtr newXData(new TwoSidedXData(_name));
newXData->setNumPages( (_numPages+1)/2);
newXData->setSndPageTurn(_sndPageTurn);
// Add default guiPage to all guiPage-entries.
newXData->setGuiPage( StringList( newXData->getNumPages(), DEFAULT_TWOSIDED_GUI) );
// Reshuffle the TwoSided pages contents into the OneSided page contents.
for (std::size_t n = 0; n < newXData->getNumPages()-1; n++)
{
newXData->setPageContent(Title, n, Left, _pageTitle[2*n] );
newXData->setPageContent(Body, n, Left, _pageBody[2*n] );
newXData->setPageContent(Title, n, Right, _pageTitle[2*n+1] );
newXData->setPageContent(Body, n, Right, _pageBody[2*n+1] );
}
newXData->setPageContent(Title, newXData->getNumPages()-1, Left, _pageTitle[2*(newXData->getNumPages()-1)] );
newXData->setPageContent(Body, newXData->getNumPages()-1, Left, _pageBody[2*(newXData->getNumPages()-1)] );
if ( (_numPages % 2) == 0)
{
// Prevent vector subscript exceeded.
newXData->setPageContent(Title, newXData->getNumPages()-1, Right, _pageTitle[_numPages-1] );
newXData->setPageContent(Body, newXData->getNumPages()-1, Right, _pageBody[_numPages-1] );
}
target = newXData;
}
} // namespace XData