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
////////////////////////////////////////////////////////////////////////
// Copyright (c) Nehmulos 2011-2014
// This file is part of N0 Strain Serialization Library.
//
// N0Strain-Serialization-Library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// N0Strain-Serialization-Library 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with N0Strain-Serialization-Library. If not, see https://gnu.org/licenses/lgpl-3.0
////////////////////////////////////////////////////////////////////////
#include "MarkupReader.h"
namespace nw {
MarkupReader::MarkupReader()
{
// Use init() as constructor
// Abstract calls are not allowed in c++ constructor
init();
}
MarkupReader::~MarkupReader()
{
if(motherTag)
{
delete motherTag; // delete recursively
motherTag = NULL;
}
}
void MarkupReader::init()
{
motherTag = new Tag("");
parentTag = motherTag;
selectedTag = parentTag;
}
void MarkupReader::init(const String filepath)
{
init();
std::ifstream file;
file.open(filepath.c_str());
if(!file.fail()) {
int error = readWholeDocument(file);
file.close();
if(error != 0) {
std::cout << "Failed to parse the document: " << filepath << std::endl;
}
describePointers();
} else {
std::cout << "failed to open file " << filepath << std::endl;
}
}
bool MarkupReader::push(const String tagName)
{
if (!parentTag) {
return false;
}
Tag* child = this->parentTag->getChildWithName(tagName);
if(child != NULL)
{
this->parentTag = child;
return true;
}
return false;
}
void MarkupReader::describeArray(const String arrayName, const String elementsName, unsigned int size)
{
MarkupReaderArray array;
if(!arrayName.empty())
{
//TODO push already inited in the mothertag?
if (!push(arrayName)) {
array.hasWrapperTag = false;
//this->tagArrays.push(array);
//return; // add an empty array, so enterNextElement returns false on first call
} else {
array.hasWrapperTag = true;
}
}
else
{
array.hasWrapperTag = false;
}
if(parentTag != NULL)
{
std::vector<Tag*> children = this->parentTag->getChildren();
int numberOfChildren = children.size();
std::stack<Tag*> tags;
for (int i = numberOfChildren-1; i > -1; --i)
{
if(children.at(i)->getName() == elementsName)
{
array.tags.push(children.at(i));
}
}
}
array.isValueArray = false;
this->tagArrays.push(array);
}
/// by default assume that anonymous values are not available,
/// otherwise the childclass can overwrite this method
void MarkupReader::describeValueArray(const String arrayName, unsigned int size)
{
// MarkupReaderArray array;
//
// if(!arrayName.empty())
// {
// push(arrayName); //TODO already inited in the mothertag
// array.hasWrapperTag = true;
// }
//
//// if(currentTag != NULL)
//// {
//// array.tags = currentTag->getCsvTags();
//// }
//
// if(currentTag != NULL)
// {
// std::vector<Tag*> children = this->currentTag->getChildren();
// int numberOfChildren = children.size();
// std::stack<Tag*> tags;
// for (int i = numberOfChildren-1; i > -1; --i)
// {
// if(children.at(i)->getName() == "value")
// {
// array.tags.push(children.at(i));
// }
// }
// }
// array.isValueArray = false;
// this->tagArrays.push(array);
describeArray(arrayName, "", size);
tagArrays.top().isValueArray = true;
}
bool MarkupReader::enterNextElement(unsigned int iterator)
{
if(!tagArrays.empty()) //There are any Arrays
{
if(tagArrays.top().isValueArray)
{
if(!tagArrays.top().tags.empty()) //The Array still has Elements
{
//Directly select the next tag. Works, if anonymous tags are allowed
selectedTag = tagArrays.top().tags.top();
tagArrays.top().tags.pop(); //Drop Pointer from list
return true;
}
}
else
{
if(!tagArrays.top().tags.empty()) //The Array still has Elements
{
parentTag = tagArrays.top().tags.top(); //Direct Jump into Element
tagArrays.top().tags.pop(); //Drop Pointer from list
return true;
} else if (iterator > 0) {
pop(); // Leave last element
}
}
if(tagArrays.top().hasWrapperTag)
pop(); // Leave array
tagArrays.pop(); //Array finished!
}
return false;
}
void MarkupReader::pop()
{
this->parentTag = this->parentTag->getParent();
if(this->parentTag == NULL)
{
this->parentTag = motherTag;
}
}
Tag* MarkupReader::extractMotherTag()
{
Tag* ret = motherTag;
motherTag = NULL; // prevent it from being deleted in the destructor
return ret;
}
Tag* MarkupReader::extractTags()
{
Tag* ret = new Tag();
std::vector<Tag*>& children = parentTag->getChildren();
for (int i = 0; i < children.size(); ++i)
{
ret->addChild(children.at(i));
}
children.clear();
return ret;
}
void MarkupReader::close()
{
//we read all the stuff earlier, so there is nothing to close anymore;
// file should be closed after reading
}
void MarkupReader::describeName(const String name)
{
if (parentTag) {
selectedTag = parentTag->getChildWithName(name);
} else {
selectedTag = NULL;
}
}
void MarkupReader::describeValue(String& ref)
{
getTagValue(ref);
}
void MarkupReader::describeValue(double& ref)
{
getTagValue(ref);
}
void MarkupReader::describeValue(char& ref)
{
getTagValue(ref);
}
void MarkupReader::describeValue(unsigned char& ref)
{
getTagValue(ref);
}
void MarkupReader::describeValue(int& ref)
{
getTagValue(ref);
}
void MarkupReader::describeValue(unsigned int& ref)
{
getTagValue(ref);
}
void MarkupReader::describeValue(float& ref)
{
getTagValue(ref);
}
void MarkupReader::describeValue(bool& ref)
{
getTagValue(ref);
}
void MarkupReader::describeValue( Pointer ref)
{
unsigned int id;
getTagValue(id);
ref = Describer::getSerializedObjectForId(id);
}
void MarkupReader::describeBlob(const String blobName, nw::Pointer blob, unsigned int blobSize)
{
Tag* child = this->parentTag->getChildWithName(blobName);
if(child != NULL)
{
if(child->hasChildren())
{
Tag* encodingTag = child->getChildWithName(N0Slib_BinaryToTextEncoding_EncodingTagName);
Tag* valueTag = child->getChildWithName(N0Slib_BinaryToTextEncoding_ValueTagName);
if(encodingTag != NULL && valueTag != NULL)
{
String encoding = encodingTag->getValue();
if(encoding == N0Slib_BinaryToTextEncoding_Base64)
blob = valueTag->getBase64Value();
else if(encoding == N0Slib_BinaryToTextEncoding_Base95)
std::cout << "Base95 not implemented, yet" << std::endl;
else if(encoding == N0Slib_BinaryToTextEncoding_Hexadecimal)
blob = valueTag->getHexValue();
else
std::cout << "Unkown encoding: " << encoding << std::endl;
}
else
{
std::cout << "Warning: Broken Blob: EncodingTag or ValueTag not specified, trying base64" << std::endl;
}
}
else
{
blob = child->getBase64Value();
}
}
}
void MarkupReader::describeValue( signed char& ref)
{
getTagValue(ref);
}
void MarkupReader::describeValue( short& ref)
{
getTagValue(ref);
}
void MarkupReader::describeValue( unsigned short& ref)
{
getTagValue(ref);
}
void MarkupReader::describeValue( long& ref)
{
getTagValue(ref);
}
void MarkupReader::describeValue( unsigned long& ref)
{
getTagValue(ref);
}
void MarkupReader::describeValue( long long& ref)
{
getTagValue(ref);
}
void MarkupReader::describeValue( long double& ref)
{
getTagValue(ref);
}
void MarkupReader::comment(const String text) {} //Don't read comments
bool MarkupReader::isInWriteMode()
{
return false;
}
template<class T> inline void MarkupReader::getTagValue(T& ref)
{
if(selectedTag)
{
ref = selectedTag->getValueAs<T>();
}
}
} // namespace nw