-
Notifications
You must be signed in to change notification settings - Fork 0
/
RapidXMLMasksParser.cpp
79 lines (73 loc) · 2.3 KB
/
RapidXMLMasksParser.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
#include "RapidXMLMasksParser.h"
#include "StringsOperator.h"
using namespace std;
using namespace rapidxml;
const char RapidXMLMasksParser::DELIMITER = ';';
RapidXMLMasksParser::RapidXMLMasksParser(const std::string& masksPath) : MasksParser()
{
ifstream file(masksPath);
doc = shared_ptr<xml_document<>>(new xml_document<>);
buffer = vector<char>((istreambuf_iterator<char>(file)), istreambuf_iterator<char>());
buffer.push_back('\0');
(*doc).parse<0>(&buffer[0]);
root_node = (*doc).first_node("Transformators");
}
std::vector<Mask> RapidXMLMasksParser::loadMasks(const string& transformatorName)
{
vector<Mask> masks;
xml_node<>* masksNode = findMasksNode(transformatorName);
if (masksNode != nullptr)
{
masks = readMasksFromNode(masksNode);
}
return masks;
}
rapidxml::xml_node<>* RapidXMLMasksParser::findMasksNode(const string& transformatorName)
{
for (xml_node<>* transformatorNode = root_node->first_node("Transformator");
transformatorNode; transformatorNode = transformatorNode->next_sibling())
{
string name = transformatorNode->first_attribute("name")->value();
if (name == transformatorName)
return transformatorNode->first_node("Masks");
}
return nullptr;
}
std::vector<Mask> RapidXMLMasksParser::readMasksFromNode(xml_node<>* masksNode)
{
vector<Mask> masks;
for (xml_node<>* maskNode = masksNode->first_node("Mask");
maskNode; maskNode = maskNode->next_sibling())
{
Mask mask = readMaskFromNode(maskNode);
masks.push_back(mask);
}
return masks;
}
Mask RapidXMLMasksParser::readMaskFromNode(xml_node<>* maskNode)
{
vector<int> maskValues;
int numberOfRows = 0;
for (xml_node<>* rowNode = maskNode->first_node("Row");
rowNode; rowNode = rowNode->next_sibling())
{
numberOfRows++;
string rowValue = rowNode->value();
vector<int> parsedValues = parseValuesFrom(rowValue);
maskValues.insert(maskValues.end(), parsedValues.begin(), parsedValues.end());
}
int numberOfCols = maskValues.size() / numberOfRows;
pair<int, int> maskSize {numberOfCols, numberOfRows};
return Mask(maskSize, maskValues);
}
vector<int> RapidXMLMasksParser::parseValuesFrom(const string& rowValue)
{
vector<int> finalValues;
StringsOperator splitter;
vector<string> values = splitter.split(rowValue, DELIMITER);
for (string value : values)
{
finalValues.push_back(stoi(value));
}
return finalValues;
}