· 10 years ago · Sep 20, 2016, 03:38 PM
1//Author : Sygmei
2//Key : 976938ef7d46c286a2027d73f3a99467bcfa8ff0c1e10bd0016139744ef5404f4eb4d069709f9831f6de74a094944bf0f1c5bf89109e9855290336a66420376f
3
4#include "DataParser.hpp"
5
6namespace mse
7{
8 namespace Data
9 {
10 std::vector<std::string> convertPath(std::string path)
11 {
12 std::vector<std::string> vecPath = {};
13 if (Functions::String::occurencesInString(path, "/") >= 1)
14 vecPath = Functions::String::split(path, "/");
15 else
16 {
17 if (path != "")
18 vecPath.push_back(path);
19 }
20 return vecPath;
21 }
22
23 std::string reverseConvertPath(std::vector<std::string> path)
24 {
25 std::string strPath = "";
26 for (unsigned int i = 0; i < path.size(); i++)
27 {
28 strPath += path[i];
29 if (i != path.size() - 1)
30 strPath += "/";
31 }
32 return strPath;
33 }
34
35
36 //DataParserNavigator
37 std::string DataParserNavigator::getCurrentDataObject() {
38 return currentDataObject;
39 }
40 std::string DataParserNavigator::getCurrentPath() {
41 return currentPath;
42 }
43 void DataParserNavigator::setCurrentDataObject(std::string object) {
44 currentDataObject = object;
45 goRoot();
46 }
47 void DataParserNavigator::setCurrentDataObject(std::string object, std::string path) {
48 currentDataObject = object;
49 currentPath = path;
50 }
51 void DataParserNavigator::setCurrentPath(std::string path) {
52 currentPath = path;
53 }
54 void DataParserNavigator::goTo(std::string path) {
55 currentPath += "/" + path;
56 }
57 void DataParserNavigator::goRoot() {
58 currentPath = "";
59 }
60 void DataParserNavigator::goBack() {
61 if (currentPath != "")
62 {
63 if (Functions::String::split(currentPath, "/").size() > 1)
64 {
65 std::vector<std::string> splittedPath = Functions::String::split(currentPath, "/");
66 currentPath = Functions::Vector::join(splittedPath, "/", 0, splittedPath.size() - 2);
67 }
68
69 }
70 }
71
72 //BaseAttribute
73 BaseAttribute::BaseAttribute(std::string bname, std::string btype, std::string bdata) {
74 name = bname;
75 dtype = btype;
76 data = bdata;
77 if (dtype == "str")
78 Functions::String::replaceStringInPlace(data, "\"", "");
79 }
80 std::string BaseAttribute::getName() {
81 return name;
82 }
83 std::string BaseAttribute::returnData() {
84 if (dtype != "str")
85 return data;
86 else
87 return "\"" + data + "\"";
88 }
89 /*int BaseAttribute::getData(int* var) {
90 if (dtype == "int")
91 return *var = std::stoi(data);
92 else
93 std::cout << "<Error:DataParser:BaseAttribute>[getData] : " \
94 << name << " is not a <int> BaseAttribute (" << dtype << ")" << std::endl;
95 }
96 double BaseAttribute::getData(double* var) {
97 if (dtype == "float")
98 return *var = std::stod(data);
99 else
100 std::cout << "<Error:DataParser:BaseAttribute>[getData] : " \
101 << name << " is not a <float> BaseAttribute (" << dtype << ")" << std::endl;
102 }
103 std::string BaseAttribute::getData(std::string* var) {
104 if (dtype == "str")
105 {
106 *var = data;
107 return data;
108 }
109 else
110 std::cout << "<Error:DataParser:BaseAttribute>[getData] : " \
111 << name << " is not a <str> BaseAttribute (" << dtype << ")" << std::endl;
112 }
113 bool BaseAttribute::getData(bool* var) {
114 if (dtype == "bool")
115 {
116 if (data == "True")
117 *var = true;
118 else
119 *var = false;
120 return *var;
121 }
122 else
123 std::cout << "<Error:DataParser:BaseAttribute>[getData] : " \
124 << name << " is not a <bool> BaseAttribute (" << dtype << ")" << std::endl;
125 }*/
126 void BaseAttribute::set(int var) {
127 if (dtype == "int")
128 data = std::to_string(var);
129 else
130 std::cout << "<Error:DataParser:BaseAttribute>[setData] : " \
131 << name << " is not a <int> BaseAttribute (" << dtype << ")" << std::endl;
132 }
133 void BaseAttribute::set(double var) {
134 if (dtype == "float")
135 data = std::to_string(var);
136 else
137 std::cout << "<Error:DataParser:BaseAttribute>[setData] : " \
138 << name << " is not a <float> BaseAttribute (" << dtype << ")" << std::endl;
139 }
140 void BaseAttribute::set(std::string var) {
141 if (dtype == "str")
142 data = var;
143 else
144 std::cout << "<Error:DataParser:BaseAttribute>[setData] : " \
145 << name << " is not a <str> BaseAttribute (" << dtype << ")" << std::endl;
146 }
147 void BaseAttribute::set(bool var) {
148 if (dtype == "bool")
149 {
150 if (var)
151 data = "True";
152 else
153 data = "False";
154 }
155 else
156 std::cout << "<Error:DataParser:BaseAttribute>[setData] : " \
157 << name << " is not a <bool> BaseAttribute (" << dtype << ")" << std::endl;
158 }
159 std::string BaseAttribute::getType() {
160 return dtype;
161 }
162
163 //ListAttribute
164 ListAttribute::ListAttribute(std::string id, std::string type) {
165 this->listId = id;
166 this->dataType = type;
167 }
168 ListAttribute::~ListAttribute()
169 {
170 for (int i = 0; i < dataList.size(); i++)
171 delete dataList[i];
172 }
173 unsigned int ListAttribute::getSize() {
174 return dataList.size();
175 }
176 std::string ListAttribute::getID() {
177 return listId;
178 }
179 std::string ListAttribute::getType() {
180 return dataType;
181 }
182 BaseAttribute* ListAttribute::getElement(unsigned int index) {
183 if (index < dataList.size())
184 return dataList[index];
185 else
186 std::cout << "<Error:DataParser:ListAttribute> : Can't access index " << index << " of " \
187 << this->getID() << " (Size:" << dataList.size() << ")" << std::endl;
188 }
189 void ListAttribute::createElement(std::string element) {
190 BaseAttribute* tempElem = new BaseAttribute("None", dataType, element);
191 dataList.push_back(tempElem);
192 }
193
194
195 //ComplexAttribute
196 ComplexAttribute::ComplexAttribute(std::string attrID) {
197 id = attrID;
198 }
199 ComplexAttribute::ComplexAttribute(std::string attrID, ComplexAttribute* herit) {
200 id = attrID;
201 this->heritage(herit);
202 }
203 ComplexAttribute::ComplexAttribute(std::string attrID, std::vector<ComplexAttribute*>* multipleHerit) {
204 id = attrID;
205 for (unsigned int i = 0; i < multipleHerit->size(); i++)
206 this->heritage(multipleHerit->at(i));
207 }
208 ComplexAttribute::~ComplexAttribute()
209 {
210 for (auto it = baseAttributes.begin(); it != baseAttributes.end(); it++)
211 delete it->second;
212 for (auto it = listAttributes.begin(); it != listAttributes.end(); it++)
213 delete it->second;
214 for (auto it = complexAttributes.begin(); it != complexAttributes.end(); it++)
215 delete it->second;
216 for (auto it = listGenerators.begin(); it != listGenerators.end(); it++)
217 delete it->second;
218 }
219 void ComplexAttribute::heritage(ComplexAttribute* heritTarget) {
220 for (unsigned int i = 0; i < heritTarget->getAllAttributes().size(); i++)
221 {
222 BaseAttribute* currentBaseAttr = heritTarget->getAttribute(heritTarget->getAllAttributes()[i]);
223 this->createBaseAttribute(currentBaseAttr->getName(), currentBaseAttr->getType(), currentBaseAttr->returnData());
224 }
225 for (unsigned int i = 0; i < heritTarget->getAllComplex().size(); i++)
226 {
227 ComplexAttribute* currentCmplxAttr = heritTarget->getComplexAttribute(heritTarget->getAllComplex()[i]);
228 this->createComplexAttribute(currentCmplxAttr->getID());
229 this->getComplexAttribute(heritTarget->getAllComplex()[i])->heritage(currentCmplxAttr);
230 }
231 }
232 std::string ComplexAttribute::getID() {
233 return id;
234 }
235 BaseAttribute* ComplexAttribute::getAttribute(std::string attributeName) {
236 if (baseAttributes.find(attributeName) != baseAttributes.end())
237 return baseAttributes[attributeName];
238 else
239 std::cout << "<Error:DataParser:ComplexAttribute>[getAttribute] : Can't find BaseAttribute " << attributeName << " in " << this->getID() << std::endl;
240 }
241 ListAttribute* ComplexAttribute::getListAttribute(std::string id) {
242 for (std::map<std::string, ListAttribute*>::iterator it = listAttributes.begin(); it != listAttributes.end(); ++it) {
243 if (it->second->getID() == id)
244 return it->second;
245 }
246 std::cout << "<Error:DataParser:ComplexAttribute>[getListAttribute] : Can't find ListAttribute " << id << " in " << this->getID() << std::endl;
247 }
248 ComplexAttribute* ComplexAttribute::getComplexAttribute(std::string id) {
249 for (std::map<std::string, ComplexAttribute*>::iterator it = complexAttributes.begin(); it != complexAttributes.end(); ++it) {
250 if (it->second->getID() == id)
251 return it->second;
252 }
253 std::cout << "<Error:DataParser:ComplexAttribute>[getComplexAttribute] : Can't find ComplexAttribute " << id << " in " << this->getID() << std::endl;
254 }
255 std::vector<std::string> ComplexAttribute::getAllComplex() {
256 return complexAttributeList;
257 }
258 std::vector<std::string> ComplexAttribute::getAllAttributes() {
259 return baseAttributeList;
260 }
261 std::vector<std::string> ComplexAttribute::getAllLists() {
262 return listAttributeList;
263 }
264 bool ComplexAttribute::attributeExists(std::string attributeName) {
265 if (baseAttributes.find(attributeName) == baseAttributes.end())
266 return false;
267 else
268 return true;
269 }
270 bool ComplexAttribute::complexExists(std::string attributeName) {
271 if (complexAttributes.find(attributeName) == complexAttributes.end())
272 return false;
273 else
274 return true;
275 }
276 bool ComplexAttribute::listExists(std::string attributeName) {
277 if (listAttributes.find(attributeName) == listAttributes.end())
278 return false;
279 else
280 return true;
281 }
282 void ComplexAttribute::createBaseAttribute(std::string name, std::string type, std::string data) {
283 BaseAttribute* tempAttr = new BaseAttribute(name, type, data);
284 baseAttributes[name] = tempAttr;
285 baseAttributeList.push_back(name);
286 }
287 void ComplexAttribute::pushBaseAttribute(BaseAttribute* attr) {
288 baseAttributes[attr->getName()] = attr;
289 baseAttributeList.push_back(attr->getName());
290 }
291 void ComplexAttribute::createListAttribute(std::string id, std::string type) {
292 ListAttribute* tempAttr = new ListAttribute(id, type);
293 listAttributes[id] = tempAttr;
294 listAttributeList.push_back(id);
295 }
296 void ComplexAttribute::pushListAttribute(ListAttribute* attr) {
297 listAttributes[attr->getID()] = attr;
298 listAttributeList.push_back(attr->getID());
299 }
300 void ComplexAttribute::createListItem(std::string listID, std::string value) {
301 this->getListAttribute(listID)->createElement(value);
302 }
303 void ComplexAttribute::createListGenerator(std::string gtarget, std::string gtype, std::string regex) {
304 if (listAttributes.find(gtarget) != listAttributes.end())
305 {
306 ListGenerator* lGen = new ListGenerator(gtype, regex, listAttributes[gtarget]);
307 listGenerators[gtarget] = lGen;
308 listGeneratorsList.push_back(gtarget);
309 }
310 else
311 std::cout << "<Error:DataParser:ComplexAttribute>[createListGenerator] : ListAttribute <" << gtarget << "> does not exists" << std::endl;
312 }
313 void ComplexAttribute::addBoundToListGenerator(std::string listID, int gstart, int gend) {
314 if (listGenerators.find(listID) != listGenerators.end())
315 listGenerators[listID]->addBound(gstart, gend);
316 else
317 {
318 if (listAttributes.find(listID) != listAttributes.end())
319 std::cout << "<Error:DataParser:ComplexAttribute>[addBoundToListGenerator] : ListAttribute <" << listID << "> has no ListGenerator" << std::endl;
320 else
321 std::cout << "<Error:DataParser:ComplexAttribute>[addBoundToListGenerator] : ListAttribute <" << listID << "> does not exists" << std::endl;
322 }
323 }
324 void ComplexAttribute::generateInList(std::string listID) {
325 if (listAttributes.find(listID) != listAttributes.end())
326 {
327 if (listGenerators.find(listID) != listGenerators.end())
328 {
329 listGenerators[listID]->generate();
330 free(listGenerators[listID]);
331 listGenerators.erase(listID);
332 listGeneratorsList.erase(std::remove(listGeneratorsList.begin(), listGeneratorsList.end(), listID), listGeneratorsList.end());
333 }
334 else
335 std::cout << "<Error:DataParser:ComplexAttribute>[generateInList] : ListAttribute <" << listID << "> has no ListGenerator" << std::endl;
336 }
337 else
338 std::cout << "<Error:DataParser:ComplexAttribute>[generateInList] : ListAttribute <" << listID << "> does not exists" << std::endl;
339 }
340 void ComplexAttribute::createComplexAttribute(std::string id) {
341 ComplexAttribute* tempAttr = new ComplexAttribute(id);
342 complexAttributes[id] = tempAttr;
343 complexAttributeList.push_back(id);
344 }
345 void ComplexAttribute::pushComplexAttribute(ComplexAttribute* cmplx) {
346 complexAttributes[cmplx->getID()] = cmplx;
347 complexAttributeList.push_back(cmplx->getID());
348 }
349 void ComplexAttribute::writeAttributes(std::ofstream* file, unsigned int depth) {
350 for (unsigned int i = 0; i < depth; i++)
351 (*file) << " ";
352 (*file) << "@" << this->id << std::endl;
353 for (unsigned int i = 0; i < baseAttributeList.size(); i++)
354 {
355 for (unsigned int j = 0; j < depth + 1; j++)
356 (*file) << " ";
357 (*file) << baseAttributeList[i] << ":" << this->getAttribute(baseAttributeList[i])->returnData() << std::endl;
358 }
359 for (unsigned int i = 0; i < listAttributeList.size(); i++)
360 {
361 for (unsigned int j = 0; j < depth + 1; j++)
362 (*file) << " ";
363 if (listGenerators.find(listAttributeList[i]) != listGenerators.end())
364 {
365 std::vector<std::pair<int, int>> allBounds = listGenerators[listAttributeList[i]]->getAllBounds();
366 (*file) << "?" << listAttributeList[i] << "(" << this->getListAttribute(listAttributeList[i])->getType() \
367 << ")<" << listGenerators[listAttributeList[i]]->getRegex() << ">{";
368 for (unsigned int j = 0; j < allBounds.size(); j++)
369 {
370 (*file) << allBounds[j].first << "-" << allBounds[j].second;
371 if (j != allBounds.size() - 1) (*file) << "," << std::endl;
372 }
373 (*file) << "}:" << std::endl;
374 }
375 else
376 (*file) << "?" << listAttributeList[i] << "(" << this->getListAttribute(listAttributeList[i])->getType() << "):" << std::endl;
377 for (unsigned int k = 0; k < this->getListAttribute(listAttributeList[i])->getSize(); k++)
378 {
379 for (unsigned int l = 0; l < depth + 2; l++)
380 (*file) << " ";
381 (*file) << this->getListAttribute(listAttributeList[i])->getElement(k)->returnData() << std::endl;
382 }
383 }
384 for (unsigned int i = 0; i < complexAttributeList.size(); i++)
385 this->getComplexAttribute(complexAttributeList[i])->writeAttributes(file, depth + 1);
386 if (depth == 1)
387 (*file) << std::endl;
388 }
389 void ComplexAttribute::deleteAttribute(std::string id, bool freeMemory) {
390 for (int i = 0; i < baseAttributeList.size(); i++)
391 {
392 if (baseAttributeList[i] == id)
393 {
394 baseAttributeList.erase(baseAttributeList.begin() + i);
395 break;
396 }
397 }
398 typedef std::map<std::string, BaseAttribute*>::iterator it_type;
399 it_type itDel = baseAttributes.find(id);
400 if (itDel != baseAttributes.end())
401 {
402 if (freeMemory)
403 free(baseAttributes[id]);
404 baseAttributes.erase(itDel);
405 }
406 }
407 void ComplexAttribute::deleteComplexAttribute(std::string id, bool freeMemory) {
408 for (int i = 0; i < complexAttributeList.size(); i++)
409 {
410 if (complexAttributeList[i] == id)
411 {
412 complexAttributeList.erase(complexAttributeList.begin() + i);
413 break;
414 }
415 }
416 typedef std::map<std::string, ComplexAttribute*>::iterator it_type;
417 it_type itDel = complexAttributes.find(id);
418 if (itDel != complexAttributes.end())
419 {
420 if (freeMemory)
421 free(complexAttributes[id]);
422 complexAttributes.erase(itDel);
423 }
424 }
425 void ComplexAttribute::deleteListAttribute(std::string id, bool freeMemory) {
426 for (int i = 0; i < listAttributeList.size(); i++)
427 {
428 if (listAttributeList[i] == id)
429 {
430 listAttributeList.erase(listAttributeList.begin() + i);
431 break;
432 }
433 }
434 typedef std::map<std::string, ListAttribute*>::iterator it_type;
435 it_type itDel = listAttributes.find(id);
436 if (itDel != listAttributes.end())
437 {
438 if (freeMemory)
439 free(listAttributes[id]);
440 listAttributes.erase(itDel);
441 }
442 }
443
444 //ListGenerator
445 ListGenerator::ListGenerator(std::string gtype, std::string regex, ListAttribute* gtarget) {
446 genType = gtype;
447 genRegex = regex;
448 genTarget = gtarget;
449 }
450 std::string ListGenerator::getRegex() {
451 return genRegex;
452 }
453 void ListGenerator::addBound(int gstart, int gend) {
454 std::pair<int, int> bound = { gstart, gend };
455 genBounds.push_back(bound);
456 }
457 std::vector<std::pair<int, int>> ListGenerator::getAllBounds() {
458 return genBounds;
459 }
460 void ListGenerator::generate() {
461 for (unsigned int i = 0; i < genBounds.size(); i++)
462 {
463 int start = genBounds[i].first;
464 int stop = genBounds[i].second;
465 int step = 0;
466 if (genBounds[i].first <= genBounds[i].second)
467 {
468 for (int j = start; j <= stop; j++)
469 {
470 std::string elemToCreate = std::to_string(j);
471 if (genType == "str")
472 {
473 elemToCreate = genRegex;
474 Functions::String::replaceStringInPlace(elemToCreate, "%s", std::to_string(j));
475 Functions::String::replaceStringInPlace(elemToCreate, "\"", "");
476 }
477 genTarget->createElement(elemToCreate);
478 }
479 }
480 else if (genBounds[i].first > genBounds[i].second)
481 {
482 for (int j = start; j >= stop; j--)
483 {
484 std::string elemToCreate = std::to_string(j);
485 if (genType == "str")
486 {
487 elemToCreate = genRegex;
488 Functions::String::replaceStringInPlace(elemToCreate, "%s", std::to_string(j));
489 Functions::String::replaceStringInPlace(elemToCreate, "\"", "");
490 }
491 genTarget->createElement(elemToCreate);
492 }
493 }
494 }
495 }
496
497 //DataObject
498 DataObject::DataObject(std::string objectname) {
499 this->objectName = objectname;
500 }
501 DataObject::~DataObject()
502 {
503 for (auto it = specialAttributes.begin(); it != specialAttributes.end(); it++)
504 delete it->second;
505 for (auto it = baseAttributes.begin(); it != baseAttributes.end(); it++)
506 delete it->second;
507 for (auto it = listAttributes.begin(); it != listAttributes.end(); it++)
508 delete it->second;
509 for (auto it = complexAttributes.begin(); it != complexAttributes.end(); it++)
510 delete it->second;
511 for (auto it = listGenerators.begin(); it != listGenerators.end(); it++)
512 delete it->second;
513 }
514 std::string DataObject::getName() {
515 return objectName;
516 }
517 ComplexAttribute* DataObject::getPath(std::vector<std::string> attributePath) {
518 if (attributePath.size() > 0)
519 {
520 int pathIndex = 1;
521 ComplexAttribute* getToPath;
522 getToPath = this->getComplexAttribute({}, attributePath[0]);
523 while (pathIndex != attributePath.size())
524 {
525 getToPath = getToPath->getComplexAttribute(attributePath[pathIndex]);
526 pathIndex++;
527 }
528 return getToPath;
529 }
530 else
531 {
532 ComplexAttribute* tempCmplx = new ComplexAttribute("Root");
533 return tempCmplx;
534 }
535 }
536 BaseAttribute* DataObject::getAttribute(std::vector<std::string> attributePath, std::string attributeName) {
537 if (attributePath.size() > 0)
538 return getPath(attributePath)->getAttribute(attributeName);
539 else
540 {
541 if (baseAttributes.find(attributeName) != baseAttributes.end())
542 return baseAttributes[attributeName];
543 else
544 std::cout << "<Error:DataParser:DataObject>[getAttribute] : Can't find attribute " << attributeName << \
545 " inside " << objectName << std::endl;
546 }
547 }
548 ListAttribute* DataObject::getListAttribute(std::vector<std::string> attributePath, std::string id) {
549 if (attributePath.size() == 0)
550 return listAttributes[id];
551 else
552 return getPath(attributePath)->getListAttribute(id);
553 }
554 ComplexAttribute* DataObject::getComplexAttribute(std::vector<std::string> attributePath, std::string id) {
555 if (attributePath.size() == 0)
556 {
557 if (complexAttributes.find(id) != complexAttributes.end())
558 return complexAttributes[id];
559 else if (heritComplexAttributes.find(id) != heritComplexAttributes.end())
560 return heritComplexAttributes[id];
561 else
562 std::cout << "<Error:DataParser:DataObject>[getComplexAttribute] : Can't find " << id << " in " << objectName << "::" << reverseConvertPath(attributePath) << std::endl;
563 }
564 else
565 return getPath(attributePath)->getComplexAttribute(id);
566 }
567 std::vector<std::string> DataObject::getAllComplex(std::vector<std::string> attributePath) {
568 if (attributePath.size() > 0)
569 return getPath(attributePath)->getAllComplex();
570 else
571 return complexAttributeList;
572 }
573 std::vector<std::string> DataObject::getAllAttributes(std::vector<std::string> attributePath) {
574 if (attributePath.size() > 0)
575 return getPath(attributePath)->getAllAttributes();
576 else
577 return baseAttributeList;
578 }
579 std::vector<std::string> DataObject::getAllLists(std::vector<std::string> attributePath) {
580 if (attributePath.size() > 0)
581 return getPath(attributePath)->getAllLists();
582 else
583 return listAttributeList;
584 }
585 bool DataObject::attributeExists(std::vector<std::string> attributePath, std::string attributeName) {
586 if (attributePath.size() > 0)
587 return getPath(attributePath)->attributeExists(attributeName);
588 else
589 {
590 if (baseAttributes.find(attributeName) == baseAttributes.end())
591 return false;
592 else
593 return true;
594 }
595 }
596 bool DataObject::complexExists(std::vector<std::string> attributePath, std::string attributeName) {
597 if (attributePath.size() > 0)
598 return getPath(attributePath)->complexExists(attributeName);
599 else
600 {
601 if (complexAttributes.find(attributeName) == complexAttributes.end())
602 return false;
603 else
604 return true;
605 }
606 }
607 bool DataObject::listExists(std::vector<std::string> attributePath, std::string attributeName) {
608 if (attributePath.size() > 0)
609 return getPath(attributePath)->listExists(attributeName);
610 else
611 {
612 if (listAttributes.find(attributeName) == listAttributes.end())
613 return false;
614 else
615 return true;
616 }
617 }
618 void DataObject::createSpecialAttribute(std::string name, std::string type, std::string data) {
619 BaseAttribute* tempAttr = new BaseAttribute(name, type, data);
620 specialAttributes[name] = tempAttr;
621 specialAttributeList.push_back(name);
622 }
623 void DataObject::createBaseAttribute(std::vector<std::string> attributePath, std::string name, std::string type, std::string data)
624 {
625 BaseAttribute* tempAttr = new BaseAttribute(name, type, data);
626 if (attributePath.size() == 0)
627 {
628 baseAttributes[name] = tempAttr;
629 baseAttributeList.push_back(name);
630 }
631 else
632 getPath(attributePath)->pushBaseAttribute(tempAttr);
633 }
634 void DataObject::createBaseAttribute(std::vector<std::string> attributePath, std::string name, std::string data)
635 {
636 BaseAttribute* tempAttr = new BaseAttribute(name, "str", data);
637 if (attributePath.size() == 0)
638 {
639 baseAttributes[name] = tempAttr;
640 baseAttributeList.push_back(name);
641 }
642 else
643 getPath(attributePath)->pushBaseAttribute(tempAttr);
644 }
645 void DataObject::createBaseAttribute(std::vector<std::string> attributePath, std::string name, bool data)
646 {
647 BaseAttribute* tempAttr = new BaseAttribute(name, "bool", (data == true ? "True" : "False"));
648 if (attributePath.size() == 0)
649 {
650 baseAttributes[name] = tempAttr;
651 baseAttributeList.push_back(name);
652 }
653 else
654 getPath(attributePath)->pushBaseAttribute(tempAttr);
655 }
656 void DataObject::createBaseAttribute(std::vector<std::string> attributePath, std::string name, int data)
657 {
658 BaseAttribute* tempAttr = new BaseAttribute(name, "int", std::to_string(data));
659 if (attributePath.size() == 0)
660 {
661 baseAttributes[name] = tempAttr;
662 baseAttributeList.push_back(name);
663 }
664 else
665 getPath(attributePath)->pushBaseAttribute(tempAttr);
666 }
667 void DataObject::createBaseAttribute(std::vector<std::string> attributePath, std::string name, double data)
668 {
669 BaseAttribute* tempAttr = new BaseAttribute(name, "float", std::to_string(data));
670 if (attributePath.size() == 0)
671 {
672 baseAttributes[name] = tempAttr;
673 baseAttributeList.push_back(name);
674 }
675 else
676 getPath(attributePath)->pushBaseAttribute(tempAttr);
677 }
678 void DataObject::pushBaseAttribute(std::vector<std::string> attributePath, BaseAttribute* attr) {
679 if (attributePath.size() == 0)
680 {
681 baseAttributes[attr->getName()] = attr;
682 baseAttributeList.push_back(attr->getName());
683 }
684 else
685 getPath(attributePath)->pushBaseAttribute(attr);
686 }
687 void DataObject::createListAttribute(std::vector<std::string> attributePath, std::string id, std::string type) {
688 ListAttribute* tempAttr = new ListAttribute(id, type);
689 if (attributePath.size() == 0)
690 {
691 listAttributes[id] = tempAttr;
692 listAttributeList.push_back(id);
693 }
694 else
695 getPath(attributePath)->pushListAttribute(tempAttr);
696 }
697 void DataObject::pushListAttribute(std::vector<std::string> attributePath, ListAttribute* attr) {
698 if (attributePath.size() == 0)
699 {
700 listAttributes[attr->getID()] = attr;
701 listAttributeList.push_back(attr->getID());
702 }
703 else
704 getPath(attributePath)->pushListAttribute(attr);
705 }
706 void DataObject::createListItem(std::vector<std::string> attributePath, std::string listID, std::string value) {
707 if (attributePath.size() == 0)
708 {
709 listAttributes[listID]->createElement(value);
710 }
711 else
712 getPath(attributePath)->createListItem(listID, value);
713 }
714 void DataObject::createListGenerator(std::vector<std::string> attributePath, std::string gtarget, std::string gtype, std::string regex) {
715 if (attributePath.size() == 0)
716 {
717 if (listAttributes.find(gtarget) != listAttributes.end())
718 {
719 ListGenerator* lGen = new ListGenerator(gtype, regex, listAttributes[gtarget]);
720 listGenerators[gtarget] = lGen;
721 listGeneratorsList.push_back(gtarget);
722 }
723 else
724 std::cout << "<Error:DataParser:DataObject>[createListGenerator] : ListAttribute <" << gtarget << "> does not exists" << std::endl;
725 }
726 else
727 getPath(attributePath)->createListGenerator(gtarget, gtype, regex);
728 }
729 void DataObject::addBoundToListGenerator(std::vector<std::string> attributePath, std::string listID, int gstart, int gend) {
730 if (attributePath.size() == 0)
731 {
732 if (listGenerators.find(listID) != listGenerators.end())
733 listGenerators[listID]->addBound(gstart, gend);
734 else
735 {
736 if (listAttributes.find(listID) != listAttributes.end())
737 std::cout << "<Error:DataParser:DataObject>[addBoundToListGenerator] : ListAttribute <" << listID << "> has no ListGenerator" << std::endl;
738 else
739 std::cout << "<Error:DataParser:DataObject>[addBoundToListGenerator] : ListAttribute <" << listID << "> does not exists" << std::endl;
740 }
741 }
742 else
743 getPath(attributePath)->addBoundToListGenerator(listID, gstart, gend);
744 }
745 void DataObject::generateInList(std::vector<std::string> attributePath, std::string listID) {
746 if (attributePath.size() == 0)
747 {
748 if (listAttributes.find(listID) != listAttributes.end())
749 {
750 if (listGenerators.find(listID) != listGenerators.end())
751 {
752 listGenerators[listID]->generate();
753 free(listGenerators[listID]);
754 listGenerators.erase(listID);
755 listGeneratorsList.erase(std::remove(listGeneratorsList.begin(), listGeneratorsList.end(), listID), listGeneratorsList.end());
756 }
757 else
758 std::cout << "<Error:DataParser:ComplexAttribute>[generateInList] : ListAttribute <" << listID << "> has no ListGenerator" << std::endl;
759 }
760 else
761 std::cout << "<Error:DataParser:ComplexAttribute>[generateInList] : ListAttribute <" << listID << "> does not exists" << std::endl;
762 }
763 else
764 getPath(attributePath)->generateInList(listID);
765 }
766 void DataObject::createComplexAttribute(std::vector<std::string> attributePath, std::string id) {
767 if (attributePath.size() == 0)
768 {
769 ComplexAttribute* tempAttr = new ComplexAttribute(id);
770 complexAttributes[id] = tempAttr;
771 complexAttributeList.push_back(id);
772 }
773 else if (attributePath.size() > 0)
774 {
775 ComplexAttribute* tempAttr = new ComplexAttribute(id);
776 getPath(attributePath)->pushComplexAttribute(tempAttr);
777 }
778 }
779 void DataObject::pushComplexAttribute(std::vector<std::string> attributePath, ComplexAttribute* cmplx) {
780 if (attributePath.size() == 0)
781 {
782 complexAttributes[cmplx->getID()] = cmplx;
783 complexAttributeList.push_back(cmplx->getID());
784 }
785 else if (attributePath.size() > 0)
786 getPath(attributePath)->pushComplexAttribute(cmplx);
787 }
788
789 void DataObject::createHeritComplexAttribute(std::string id) {
790 ComplexAttribute* tempAttr = new ComplexAttribute(id);
791 heritComplexAttributes[id] = tempAttr;
792 heritComplexAttributeList.push_back(id);
793 }
794
795 void DataObject::pushHeritComplexAttribute(ComplexAttribute* cmplx) {
796 complexAttributes[cmplx->getID()] = cmplx;
797 complexAttributeList.push_back(cmplx->getID());
798 }
799 void DataObject::writeAttributes(std::ofstream* file) {
800 (*file) << objectName << ":" << std::endl;
801 for (unsigned int i = 0; i < this->getAllAttributes({}).size(); i++)
802 {
803 std::string attributeName = this->getAllAttributes({})[i];
804 std::string attributeValue = this->getAttribute({}, attributeName)->returnData();
805 (*file) << " ";
806 (*file) << attributeName << ":" << attributeValue << std::endl;
807 }
808 for (unsigned int i = 0; i < this->getAllLists({}).size(); i++)
809 {
810 ListAttribute* cList = this->getListAttribute(convertPath(""), this->getAllLists({})[i]);
811 (*file) << " ";
812 (*file) << "?" << this->getAllLists({})[i] << "(" << cList->getType() << "):" << std::endl;
813 for (unsigned int k = 0; k < cList->getSize(); k++)
814 {
815 (*file) << " ";
816 (*file) << cList->getElement(k)->returnData() << std::endl;
817 }
818 (*file) << " ";
819 }
820 std::string currentPath = "";
821 for (unsigned int i = 0; i < this->getAllComplex(convertPath(currentPath)).size(); i++)
822 {
823 this->getComplexAttribute({}, this->getAllComplex({})[i])->writeAttributes(file);
824 }
825 (*file) << std::endl;
826 }
827 void DataObject::deleteAttribute(std::vector<std::string> attributePath, std::string id, bool freeMemory) {
828 if (attributePath.size() == 0)
829 {
830 for (int i = 0; i < baseAttributeList.size(); i++)
831 {
832 if (baseAttributeList[i] == id)
833 {
834 baseAttributeList.erase(baseAttributeList.begin() + i);
835 break;
836 }
837 }
838 typedef std::map<std::string, BaseAttribute*>::iterator it_type;
839 it_type itDel = baseAttributes.find(id);
840 if (itDel != baseAttributes.end())
841 {
842 if (freeMemory)
843 free(baseAttributes[id]);
844 baseAttributes.erase(itDel);
845 }
846 }
847 else if (attributePath.size() > 0)
848 {
849 getPath(attributePath)->deleteAttribute(id, freeMemory);
850 }
851 }
852 void DataObject::deleteComplexAttribute(std::vector<std::string> attributePath, std::string id, bool freeMemory) {
853 if (attributePath.size() == 0)
854 {
855 for (int i = 0; i < complexAttributeList.size(); i++)
856 {
857 if (complexAttributeList[i] == id)
858 {
859 complexAttributeList.erase(complexAttributeList.begin() + i);
860 break;
861 }
862 }
863 typedef std::map<std::string, ComplexAttribute*>::iterator it_type;
864 it_type itDel = complexAttributes.find(id);
865 if (itDel != complexAttributes.end())
866 {
867 if (freeMemory)
868 free(complexAttributes[id]);
869 complexAttributes.erase(itDel);
870 }
871 }
872 else if (attributePath.size() > 0)
873 {
874 getPath(attributePath)->deleteComplexAttribute(id, freeMemory);
875 }
876 }
877 void DataObject::deleteListAttribute(std::vector<std::string> attributePath, std::string id, bool freeMemory) {
878 if (attributePath.size() == 0)
879 {
880 for (int i = 0; i < listAttributeList.size(); i++)
881 {
882 if (listAttributeList[i] == id)
883 {
884 listAttributeList.erase(listAttributeList.begin() + i);
885 break;
886 }
887 }
888 typedef std::map<std::string, ListAttribute*>::iterator it_type;
889 it_type itDel = listAttributes.find(id);
890 if (itDel != listAttributes.end())
891 {
892 if (freeMemory)
893 free(listAttributes[id]);
894 listAttributes.erase(itDel);
895 }
896 }
897 else if (attributePath.size() > 0)
898 {
899 getPath(attributePath)->deleteListAttribute(id, freeMemory);
900 }
901 }
902
903 //DataParser
904 DataParser::DataParser()
905 {
906 this->autoMemory = false;
907 }
908 DataParser::DataParser(bool autoMemoryManagement)
909 {
910 this->autoMemory = autoMemoryManagement;
911 }
912 DataParser::~DataParser()
913 {
914 if (autoMemory)
915 {
916 if (dpNav != nullptr) delete dpNav;
917 for (auto it = objectMap.begin(); it != objectMap.end(); it++)
918 delete it->second;
919 }
920 }
921 DataParserNavigator* DataParser::hookNavigator(DataParserNavigator* dpNav) {
922 return (this->dpNav = dpNav);
923 }
924 DataParserNavigator* DataParser::accessNavigator() {
925 return this->dpNav;
926 }
927 bool DataParser::checkNavigator() {
928 return dpNav != NULL;
929 }
930 DataObject* DataParser::accessDataObject(std::string name) {
931 if (objectMap.find(name) != objectMap.end())
932 return objectMap[name];
933 else
934 std::cout << "<Error:DataParser:DataParser>[accessDataObject] : Can't find \"" << name << "\" DataObject" << std::endl;
935 }
936 void DataParser::createFlag(std::string flag) {
937 flagList.push_back(flag);
938 }
939 bool DataParser::hasFlag(std::string flagName) {
940 return std::find(flagList.begin(), flagList.end(), flagName) != flagList.end();
941 }
942 unsigned int DataParser::getAmountOfFlags() {
943 return flagList.size();
944 }
945 std::string DataParser::getFlagAtIndex(int index) {
946 return flagList[index];
947 }
948 void DataParser::createDataObject(std::string objectName) {
949 DataObject* tempObject = new DataObject(objectName);
950 objectMap[objectName] = tempObject;
951 objectList.push_back(objectName);
952 if (checkNavigator()) dpNav->setCurrentDataObject(objectName);
953 }
954 void DataParser::pushDataObject(DataObject* object) {
955 objectMap[object->getName()] = object;
956 objectList.push_back(object->getName());
957 if (checkNavigator()) dpNav->setCurrentDataObject(object->getName());
958 }
959 bool DataParser::dataObjectExists(std::string objectName)
960 {
961 if (objectMap.find(objectName) != objectMap.end())
962 return true;
963 else
964 return false;
965 }
966 void DataParser::createSpecialAttribute(std::string object, std::string name, std::string type, std::string data) {
967 this->accessDataObject(object)->createSpecialAttribute(name, type, data);
968 }
969 void DataParser::createSpecialAttribute(std::string name, std::string type, std::string data) {
970 if (checkNavigator()) this->accessDataObject(dpNav->getCurrentDataObject())->createSpecialAttribute(name, type, data);
971 }
972 void DataParser::createBaseAttribute(std::string object, std::string attributePath, std::string name, std::string data) {
973 this->accessDataObject(object)->pushBaseAttribute(convertPath(attributePath), new BaseAttribute(name, "str", data));
974 }
975 void DataParser::createBaseAttribute(std::string name, std::string data) {
976 if (checkNavigator()) this->accessDataObject(dpNav->getCurrentDataObject())->pushBaseAttribute(convertPath(dpNav->getCurrentPath()), new BaseAttribute(name, "str", data));
977 }
978 void DataParser::createBaseAttribute(std::string object, std::string attributePath, std::string name, bool data) {
979 this->accessDataObject(object)->pushBaseAttribute(convertPath(attributePath), new BaseAttribute(name, "int", data ? "True" : "False"));
980 }
981 void DataParser::createBaseAttribute(std::string name, bool data) {
982 if (checkNavigator()) this->accessDataObject(dpNav->getCurrentDataObject())->pushBaseAttribute(convertPath(dpNav->getCurrentPath()), new BaseAttribute(name, "bool", data ? "True" : "False"));
983 }
984 void DataParser::createBaseAttribute(std::string object, std::string attributePath, std::string name, int data) {
985 this->accessDataObject(object)->pushBaseAttribute(convertPath(attributePath), new BaseAttribute(name, "int", std::to_string(data)));
986 }
987 void DataParser::createBaseAttribute(std::string name, int data) {
988 if (checkNavigator()) this->accessDataObject(dpNav->getCurrentDataObject())->pushBaseAttribute(convertPath(dpNav->getCurrentPath()), new BaseAttribute(name, "int", std::to_string(data)));
989 }
990 void DataParser::createBaseAttribute(std::string object, std::string attributePath, std::string name, double data) {
991 this->accessDataObject(object)->pushBaseAttribute(convertPath(attributePath), new BaseAttribute(name, "float", std::to_string(data)));
992 }
993 void DataParser::createBaseAttribute(std::string name, double data) {
994 if (checkNavigator()) this->accessDataObject(dpNav->getCurrentDataObject())->pushBaseAttribute(convertPath(dpNav->getCurrentPath()), new BaseAttribute(name, "float", std::to_string(data)));
995 }
996 void DataParser::pushBaseAttribute(std::string object, std::string attributePath, BaseAttribute* attr) {
997 this->accessDataObject(object)->pushBaseAttribute(convertPath(attributePath), attr);
998 }
999 void DataParser::pushBaseAttribute(BaseAttribute* attr) {
1000 if (checkNavigator()) this->accessDataObject(dpNav->getCurrentDataObject())->pushBaseAttribute(convertPath(dpNav->getCurrentPath()), attr);
1001 }
1002 void DataParser::createListAttribute(std::string object, std::string attributePath, std::string id, std::string type) {
1003 this->accessDataObject(object)->createListAttribute(convertPath(attributePath), id, type);
1004 }
1005 void DataParser::createListAttribute(std::string id, std::string type) {
1006 if (checkNavigator()) this->accessDataObject(dpNav->getCurrentDataObject())->createListAttribute(convertPath(dpNav->getCurrentPath()), id, type);
1007 }
1008 void DataParser::pushListAttribute(std::string object, std::string attributePath, ListAttribute* attr) {
1009 this->accessDataObject(object)->pushListAttribute(convertPath(attributePath), attr);
1010 }
1011 void DataParser::pushListAttribute(ListAttribute* attr) {
1012 if (checkNavigator()) this->accessDataObject(dpNav->getCurrentDataObject())->pushListAttribute(convertPath(dpNav->getCurrentPath()), attr);
1013 }
1014 void DataParser::createListItem(std::string object, std::string attributePath, std::string listID, std::string value) {
1015 this->accessDataObject(object)->createListItem(convertPath(attributePath), listID, value);
1016 }
1017 void DataParser::createListItem(std::string listID, std::string value) {
1018 if (checkNavigator()) this->accessDataObject(dpNav->getCurrentDataObject())->createListItem(convertPath(dpNav->getCurrentPath()), listID, value);
1019 }
1020 void DataParser::createListGenerator(std::string object, std::string attributePath, std::string gtarget, std::string gtype, std::string regex) {
1021 this->accessDataObject(object)->createListGenerator(convertPath(attributePath), gtarget, gtype, regex);
1022 }
1023 void DataParser::createListGenerator(std::string gtarget, std::string gtype, std::string regex) {
1024 if (checkNavigator()) this->accessDataObject(dpNav->getCurrentDataObject())->createListGenerator(convertPath(dpNav->getCurrentPath()), gtarget, gtype, regex);
1025 }
1026 void DataParser::addBoundToListGenerator(std::string object, std::string attributePath, std::string listID, int gstart, int gend) {
1027 this->accessDataObject(object)->addBoundToListGenerator(convertPath(attributePath), listID, gstart, gend);
1028 }
1029 void DataParser::addBoundToListGenerator(std::string listID, int gstart, int gend) {
1030 if (checkNavigator()) this->accessDataObject(dpNav->getCurrentDataObject())->addBoundToListGenerator(convertPath(dpNav->getCurrentPath()), listID, gstart, gend);
1031 }
1032 void DataParser::generateInList(std::string object, std::string attributePath, std::string listID) {
1033 this->accessDataObject(object)->generateInList(convertPath(attributePath), listID);
1034 }
1035 void DataParser::generateInList(std::string listID) {
1036 if (checkNavigator()) this->accessDataObject(dpNav->getCurrentDataObject())->generateInList(convertPath(dpNav->getCurrentPath()), listID);
1037 }
1038 void DataParser::createComplexAttribute(std::string object, std::string attributePath, std::string id) {
1039 this->accessDataObject(object)->createComplexAttribute(convertPath(attributePath), id);
1040 }
1041 void DataParser::createComplexAttribute(std::string id) {
1042 if (checkNavigator()) this->accessDataObject(dpNav->getCurrentDataObject())->createComplexAttribute(convertPath(dpNav->getCurrentPath()), id);
1043 if (checkNavigator()) dpNav->goTo(id);
1044 }
1045 void DataParser::pushComplexAttribute(std::string object, std::string attributePath, ComplexAttribute* cmplx) {
1046 this->accessDataObject(object)->pushComplexAttribute(convertPath(attributePath), cmplx);
1047 }
1048 void DataParser::pushComplexAttribute(ComplexAttribute* cmplx) {
1049 if (checkNavigator()) this->accessDataObject(dpNav->getCurrentDataObject())->pushComplexAttribute(convertPath(dpNav->getCurrentPath()), cmplx);
1050 if (checkNavigator()) dpNav->goTo(cmplx->getID());
1051 }
1052 void DataParser::createHeritComplexAttribute(std::string object, std::string id) {
1053 this->accessDataObject(object)->createHeritComplexAttribute(id);
1054 }
1055 void DataParser::createHeritComplexAttribute(std::string id) {
1056 if (checkNavigator()) this->accessDataObject(dpNav->getCurrentDataObject())->createHeritComplexAttribute(id);
1057 if (checkNavigator()) dpNav->goTo(id);
1058 }
1059 void DataParser::pushHeritComplexAttribute(std::string object, ComplexAttribute* cmplx) {
1060 this->accessDataObject(object)->pushHeritComplexAttribute(cmplx);
1061 }
1062 void DataParser::pushHeritComplexAttribute(ComplexAttribute* cmplx) {
1063 if (checkNavigator()) this->accessDataObject(dpNav->getCurrentDataObject())->pushHeritComplexAttribute(cmplx);
1064 if (checkNavigator()) dpNav->goTo(cmplx->getID());
1065 }
1066 unsigned int DataParser::getListSize(std::string object, std::string attributePath, std::string listName) {
1067 return this->accessDataObject(object)->getListAttribute(convertPath(attributePath), listName)->getSize();
1068 }
1069 unsigned int DataParser::getListSize(std::string listName) {
1070 if (checkNavigator()) return this->accessDataObject(dpNav->getCurrentDataObject())->getListAttribute(convertPath(dpNav->getCurrentPath()), listName)->getSize();
1071 }
1072 std::vector<std::string> DataParser::getAllComplex(std::string object, std::string attributePath) {
1073 return this->accessDataObject(object)->getAllComplex(convertPath(attributePath));
1074 }
1075 std::vector<std::string> DataParser::getAllComplex() {
1076 if (checkNavigator()) return this->accessDataObject(dpNav->getCurrentDataObject())->getAllComplex(convertPath(dpNav->getCurrentPath()));
1077 }
1078 std::vector<std::string> DataParser::getAllAttributes(std::string object, std::string attributePath) {
1079 return this->accessDataObject(object)->getAllAttributes(convertPath(attributePath));
1080 }
1081 std::vector<std::string> DataParser::getAllAttributes() {
1082 if (checkNavigator()) return this->accessDataObject(dpNav->getCurrentDataObject())->getAllAttributes(convertPath(dpNav->getCurrentPath()));
1083 }
1084 std::vector<std::string> DataParser::getAllLists(std::string object, std::string attributePath) {
1085 return this->accessDataObject(object)->getAllLists(convertPath(attributePath));
1086 }
1087 std::vector<std::string> DataParser::getAllLists() {
1088 if (checkNavigator()) return this->accessDataObject(dpNav->getCurrentDataObject())->getAllLists(convertPath(dpNav->getCurrentPath()));
1089 }
1090 bool DataParser::attributeExists(std::string object, std::string attributePath, std::string attributeName) {
1091 return this->accessDataObject(object)->attributeExists(convertPath(attributePath), attributeName);
1092 }
1093 bool DataParser::attributeExists(std::string attributeName) {
1094 if (checkNavigator()) return this->accessDataObject(dpNav->getCurrentDataObject())->attributeExists(convertPath(dpNav->getCurrentPath()), attributeName);
1095 }
1096 bool DataParser::complexExists(std::string object, std::string attributePath, std::string attributeName) {
1097 return this->accessDataObject(object)->complexExists(convertPath(attributePath), attributeName);
1098 }
1099 bool DataParser::complexExists(std::string attributeName) {
1100 if (checkNavigator()) return this->accessDataObject(dpNav->getCurrentDataObject())->complexExists(convertPath(dpNav->getCurrentPath()), attributeName);
1101 }
1102 bool DataParser::listExists(std::string object, std::string attributePath, std::string attributeName) {
1103 return this->accessDataObject(object)->listExists(convertPath(attributePath), attributeName);
1104 }
1105 bool DataParser::listExists(std::string attributeName) {
1106 if (checkNavigator()) return this->accessDataObject(dpNav->getCurrentDataObject())->listExists(convertPath(dpNav->getCurrentPath()), attributeName);
1107 }
1108 BaseAttribute* DataParser::getAttribute(std::string object, std::string attributePath, std::string attributeName) {
1109 return this->accessDataObject(object)->getAttribute(convertPath(attributePath), attributeName);
1110 }
1111 BaseAttribute* DataParser::getAttribute(std::string attributeName) {
1112 if (checkNavigator()) return this->accessDataObject(dpNav->getCurrentDataObject())->getAttribute(convertPath(dpNav->getCurrentPath()), attributeName);
1113 }
1114 BaseAttribute* DataParser::getListItem(std::string object, std::string attributePath, std::string listName, int listItem) {
1115 return this->accessDataObject(object)->getListAttribute(convertPath(attributePath), listName)->getElement(listItem);
1116 }
1117 BaseAttribute* DataParser::getListItem(std::string listName, int listItem) {
1118 if (checkNavigator()) return this->accessDataObject(dpNav->getCurrentDataObject())->getListAttribute(convertPath(dpNav->getCurrentPath()), listName)->getElement(listItem);
1119 }
1120 ComplexAttribute* DataParser::getComplexAttribute(std::string object, std::string attributePath, std::string id) {
1121 return this->accessDataObject(object)->getComplexAttribute(convertPath(attributePath), id);
1122 }
1123 ComplexAttribute* DataParser::getComplexAttribute(std::string id) {
1124 if (checkNavigator()) return this->accessDataObject(dpNav->getCurrentDataObject())->getComplexAttribute(convertPath(dpNav->getCurrentPath()), id);
1125 }
1126 ListAttribute* DataParser::getListAttribute(std::string object, std::string attributePath, std::string listName) {
1127 return this->accessDataObject(object)->getListAttribute(convertPath(attributePath), listName);
1128 }
1129 ListAttribute* DataParser::getListAttribute(std::string listName) {
1130 if (checkNavigator()) return this->accessDataObject(dpNav->getCurrentDataObject())->getListAttribute(convertPath(dpNav->getCurrentPath()), listName);
1131 }
1132 std::string DataParser::getVarType(std::string line) {
1133 std::string attributeType;
1134 std::vector<std::string> boolType = { "True", "False" };
1135 if (Functions::Vector::isInList(line, boolType))
1136 attributeType = "bool";
1137 else if (line.substr(0, 1) == "\"" && line.substr(line.size() - 1, 1) == "\"")
1138 attributeType = "str";
1139 else if (Functions::String::isStringFloat(line))
1140 attributeType = "float";
1141 else if (Functions::String::isStringInt(line))
1142 attributeType = "int";
1143 else
1144 attributeType = "unknown";
1145 return attributeType;
1146 }
1147 bool DataParser::parseFile(std::string filename, bool verbose) {
1148 useFile.open(filename);
1149 std::string currentLine;
1150 std::string parsedLine;
1151 std::vector<std::string> addPath = {};
1152 unsigned int currentIndent;
1153 int currentIndex = 1;
1154 std::string curCat = "None";
1155 std::string curList = "None";
1156 int curListIndent = 0;
1157 bool errorInFile = false;
1158 if (useFile.is_open())
1159 {
1160 if (verbose) std::cout << "Start Parsing File : " << filename << std::endl;
1161 while (getline(useFile, currentLine))
1162 {
1163 parsedLine = Functions::String::replaceString(currentLine, " ", " ");
1164 currentIndent = Functions::String::occurencesInString(parsedLine, " ");
1165 while (currentIndent < addPath.size() + 1 && addPath.size() > 0)
1166 {
1167 addPath.pop_back();
1168 }
1169 if (curList != "None")
1170 {
1171 if (currentIndent != curListIndent + 1)
1172 {
1173 curList = "None";
1174 }
1175 }
1176 Functions::String::replaceStringInPlace(parsedLine, " ", "");
1177 if (parsedLine != "" && currentIndent == 0)
1178 {
1179 if (parsedLine.substr(parsedLine.size() - 1, 1) == ":")
1180 {
1181 if (verbose) std::cout << " Create new DataObject : " << parsedLine.substr(0, parsedLine.size() - 1) << std::endl;
1182 curCat = parsedLine.substr(0, parsedLine.size() - 1);
1183 this->createDataObject(curCat);
1184 }
1185 else if (parsedLine.substr(0, 6) == "Define" && parsedLine.substr(parsedLine.size() - 1, 1) == ";")
1186 {
1187 std::vector<std::string> defineInstructions = Functions::String::split(parsedLine, " ");
1188 if (defineInstructions[1] == "Flag")
1189 {
1190 std::string newFlag = defineInstructions[2];
1191 Functions::String::replaceStringInPlace(newFlag, "(", "");
1192 Functions::String::replaceStringInPlace(newFlag, ");", "");
1193 flagList.push_back(newFlag);
1194 if (verbose) std::cout << " Define New Flag : " << newFlag << std::endl;
1195 }
1196 }
1197 }
1198 else if (curCat != "None" && parsedLine != "" && currentIndent > 0)
1199 {
1200 std::vector<std::string> symbolExclusion = { "@", "?" ,"\"", "&", "{" };
1201 for (unsigned int p = 0; p <= currentIndent; p++)
1202 {
1203 if (verbose) std::cout << " ";
1204 }
1205 if (!Functions::Vector::isInList(parsedLine.substr(0, 1), symbolExclusion) && Functions::String::occurencesInString(parsedLine, ":"))
1206 {
1207 std::string attributeName = Functions::String::split(parsedLine, ":")[0];
1208 std::vector<std::string> splittedLine = Functions::String::split(parsedLine, ":");
1209 std::string attributeValue = Functions::Vector::join(splittedLine, ":", 1, 0);
1210 std::string attributeType = getVarType(attributeValue);
1211 if (attributeType == "unknown") std::cout << "<Warning:DataParser:DataParser>[parseFile] : " << attributeName << " is unknown type" << std::endl;
1212 objectMap[curCat]->createBaseAttribute(addPath, attributeName, attributeType, attributeValue);
1213 std::string pushIndicator;
1214 if (addPath.size() == 0) { pushIndicator = curCat + ":Root"; }
1215 else { pushIndicator = addPath[addPath.size() - 1]; }
1216 if (verbose) std::cout << "Create base attribute " << attributeName << "(" << attributeValue << ") inside " << pushIndicator << " (Type:" << attributeType << ")" << std::endl;
1217 }
1218 else if (parsedLine.substr(0, 1) == "&" && currentIndent == 1)
1219 {
1220 std::string specialElement = parsedLine.substr(1);
1221 std::string attributeName = Functions::String::split(specialElement, ":")[0];
1222 std::string attributeValue = Functions::String::split(specialElement, ":")[1];
1223 std::string attributeType = getVarType(attributeValue);
1224 objectMap[curCat]->createSpecialAttribute(attributeName, attributeType, attributeValue);
1225 if (verbose) std::cout << "Create special attribute &" << attributeName << " inside " << curCat << std::endl;
1226 }
1227 else if (parsedLine.substr(0, 1) == "@")
1228 {
1229 std::string complexElementID = parsedLine.substr(1);
1230 std::string complexToHeritID;
1231 std::vector<std::string> complexToHeritIDList;
1232 std::vector<ComplexAttribute*> complexToHerit;
1233 if (Functions::String::occurencesInString(complexElementID, ":") == 1 && Functions::String::split(complexElementID, ":").size() == 2)
1234 {
1235 complexToHeritID = Functions::String::replaceString(Functions::String::split(complexElementID, ":")[1], " ", "");
1236 complexElementID = Functions::String::split(complexElementID, ":")[0];
1237 complexToHeritIDList = Functions::String::split(complexToHeritID, ",");
1238
1239 for (unsigned int i = 0; i < complexToHeritIDList.size(); i++)
1240 {
1241 complexToHerit.push_back(objectMap[curCat]->getComplexAttribute(convertPath(""), complexToHeritIDList[i]));
1242 }
1243 }
1244 objectMap[curCat]->createComplexAttribute(addPath, complexElementID);
1245 if (verbose) std::cout << "Create cmplx attribute @" << complexElementID << " inside " << curCat + ":" \
1246 + objectMap[curCat]->getPath(addPath)->getID() << " (&" << \
1247 objectMap[curCat]->getPath(addPath) << ")" << std::endl;
1248 for (unsigned int i = 0; i < complexToHerit.size(); i++)
1249 {
1250 for (unsigned int p = 0; p <= currentIndent + 1; p++)
1251 {
1252 if (verbose) std::cout << " ";
1253 }
1254 if (verbose) std::cout << "{Herit from : " << complexToHeritIDList[i] << "}" << std::endl;
1255 objectMap[curCat]->getComplexAttribute(addPath, complexElementID)->heritage(complexToHerit[i]);
1256 }
1257
1258 addPath.push_back(complexElementID);
1259 }
1260 else if (parsedLine.substr(0, 3) == "{@}")
1261 {
1262 std::string complexElementID = parsedLine.substr(3);
1263 objectMap[curCat]->createHeritComplexAttribute(complexElementID);
1264 if (verbose) std::cout << "Create h_cmplx attribute {@}" << complexElementID << " inside " << curCat + ":" \
1265 + objectMap[curCat]->getPath(addPath)->getID() << " (&" << \
1266 objectMap[curCat]->getPath(addPath) << ")" << std::endl;
1267 addPath.push_back(complexElementID);
1268 }
1269 else if (parsedLine.substr(0, 1) == "?")
1270 {
1271 std::string listElementID = parsedLine.substr(1);
1272 listElementID = Functions::String::split(listElementID, "(")[0];
1273 std::string listElementType = parsedLine.substr(1);
1274 listElementType = Functions::String::split(Functions::String::split(listElementType, ")")[0], "(")[1];
1275 objectMap[curCat]->createListAttribute(addPath, listElementID, listElementType);
1276 curList = listElementID;
1277 curListIndent = currentIndent;
1278 if (verbose) std::cout << "Create list attribute ?" << curList << " inside " << curCat + ":" + objectMap[curCat]->getPath(addPath)->getID() << std::endl;
1279 if (Functions::String::occurencesInString(Functions::String::split(parsedLine.substr(1), ")")[1], "<") == 1)
1280 {
1281 std::string generatorRegex = Functions::String::split(parsedLine.substr(1), "<")[1];
1282 std::string genIt = Functions::String::split(generatorRegex, "{")[1];
1283 genIt = Functions::String::split(genIt, "}")[0];
1284 generatorRegex = Functions::String::split(generatorRegex, ">")[0];
1285 for (unsigned int p = 0; p <= currentIndent + 1; p++)
1286 {
1287 if (verbose) std::cout << " ";
1288 }
1289 if (verbose) std::cout << "Generator of type " << listElementType << " hooked to " << listElementID << " with pattern " << generatorRegex << " and iterator " << genIt << std::endl;
1290 ListGenerator gen(listElementType, generatorRegex, objectMap[curCat]->getListAttribute(addPath, listElementID));
1291 std::vector<std::string> listOfBounds;
1292 listOfBounds = Functions::String::split(genIt, ",");
1293 for (unsigned int i = 0; i < listOfBounds.size(); i++)
1294 {
1295 if (Functions::String::occurencesInString(listOfBounds[i], "-") == 1)
1296 {
1297 int startBound = std::stoi(Functions::String::split(listOfBounds[i], "-")[0]);
1298 int endBound = std::stoi(Functions::String::split(listOfBounds[i], "-")[1]);
1299 gen.addBound(startBound, endBound);
1300 }
1301 else
1302 gen.addBound(stoi(listOfBounds[i]), stoi(listOfBounds[i]));
1303 }
1304 gen.generate();
1305 }
1306 }
1307 else if (curList != "None")
1308 {
1309 std::string attributeValue = parsedLine;
1310 std::string attributeType = getVarType(attributeValue);
1311 if (attributeType == objectMap[curCat]->getListAttribute(addPath, curList)->getType())
1312 {
1313 objectMap[curCat]->getListAttribute(addPath, curList)->createElement(attributeValue);
1314 if (verbose) std::cout << "Push element " << attributeValue << " inside " << curList << std::endl;
1315 }
1316 else
1317 {
1318 if (verbose) std::cout << "Error ! Can't push " << attributeType << " inside " << curList << " which is ";
1319 if (verbose) std::cout << objectMap[curCat]->getListAttribute(addPath, curList)->getType() << " type " << std::endl;
1320 }
1321 }
1322 }
1323 currentIndex++;
1324 }
1325 useFile.close();
1326 return true;
1327 if (verbose) std::cout << "Parsed over.." << std::endl;
1328 }
1329 else
1330 {
1331 std::cout << "<Error:DataParser:DataParser>[parseFile] : Can't open " << filename << ". File does not exists" << std::endl;
1332 return false;
1333 }
1334 }
1335 void DataParser::writeFile(std::string filename, bool verbose) {
1336 outFile.open(filename);
1337 std::vector<DataObject*> orderedDataObjects;
1338 if (verbose) std::cout << "Displaying data table" << std::endl;
1339 unsigned int currentDeepness = 1;
1340 for (unsigned int i = 0; i < this->getAmountOfFlags(); i++)
1341 outFile << "Define Flag (" << this->getFlagAtIndex(i) << ");" << std::endl;
1342 if (this->getAmountOfFlags() > 0) outFile << std::endl;
1343 for (unsigned int objIt = 0; objIt < objectList.size(); objIt++) {
1344 objectMap[objectList[objIt]]->writeAttributes(&outFile);
1345 }
1346 outFile.close();
1347 }
1348 std::vector<std::string> DataParser::getAllDataObjects() {
1349 return objectList;
1350 }
1351 }
1352}