-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMLRead.cpp
executable file
·154 lines (131 loc) · 4.13 KB
/
XMLRead.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
/*
* Copyright Notice : Copyright 2015, Josiah Bruner, All Rights Reserved.
*/
#include "XMLRead.h"
XMLRead::XMLRead() {
_xmlData = "";
}
XMLRead::~XMLRead() {
_xmlData = "";
}
inline std::string trim(const std::string &s)
{
auto wsfront = std::find_if_not(s.begin(), s.end(), [](int c) {return std::isspace(c); });
auto wsback = std::find_if_not(s.rbegin(), s.rend(), [](int c) {return std::isspace(c); }).base();
return (wsback <= wsfront ? std::string() : std::string(wsfront, wsback));
}
void XMLRead::Load(std::string xmlData) {
if (xmlData != "") {
_xmlData = xmlData;
}
}
std::string XMLRead::GetFieldContents(std::string fieldName) {
// _xmlData should be valid XML data.
// To verify, we should find a <qdbapi> tag.
if (_VerifyXML(_xmlData)) {
std::string result = _GetStringBetween(_xmlData, _MakeTag(fieldName, true), _MakeTag(fieldName, false));
if (result == "") {
return "ERROR";
}
return result;
}
else {
return "ERROR";
}
}
std::string XMLRead::GetRawXML() {
return _xmlData;
}
void XMLRead::MoveAttributesIntoChildren(std::string fieldName) {
if (_VerifyXML(_xmlData)) {
if (_HasAttributes(fieldName)) {
std::vector<attribute> attributes = _GetAttributes(fieldName);
_DeleteAttributes(fieldName);
for (unsigned int i = 0; i < attributes.size(); i++) {
_CreateChild(attributes[i].name, attributes[i].contents, fieldName);
}
}
}
}
std::string XMLRead::_GetStringBetween(std::string data, std::string startDelim, std::string endDelim) {
size_t first = data.find(startDelim);
size_t last = data.find(endDelim, first);
if (first != std::string::npos && last != std::string::npos) {
std::string strNew = data.substr(first + startDelim.length(), last - startDelim.length() - first);
return strNew;
}
else {
return "ERROR";
}
}
std::string XMLRead::_MakeTag(std::string name, bool open) {
if (open) {
return '<' + name + '>';
}
else {
return "</" + name + ">";
}
}
bool XMLRead::_VerifyXML(std::string data) {
if (data.find_last_of("<qdbapi>") != std::string::npos) {
return true;
}
else {
return false;
}
}
bool XMLRead::_HasAttributes(std::string fieldName) {
std::string attributeContents = _GetStringBetween(_xmlData, "<" + fieldName + " ", ">");
if (attributeContents != "") {
return true;
}
return false;
}
std::vector<attribute> XMLRead::_GetAttributes(std::string fieldName) {
std::string attributeContents = _GetStringBetween(_xmlData, "<" + fieldName + " ", ">");
attribute anAttribute;
std::vector<attribute> attributeArray;
bool collectingContents = false;
int marksFound = 0;
if (attributeContents != "") {
for (unsigned int i = 0; i < attributeContents.length(); i++) {
char theChar = attributeContents[i];
if (collectingContents) {
if (theChar == '"') {
if (marksFound != 1) {
marksFound++;
}
else {
collectingContents = false;
marksFound = 0;
attributeArray.push_back(anAttribute);
anAttribute.contents = "";
anAttribute.name = "";
}
}
else {
anAttribute.contents += theChar;
}
}
else if (theChar == '=') {
collectingContents = true;
}
else if (theChar != ' ') {
anAttribute.name += theChar;
}
}
}
return attributeArray;
}
bool XMLRead::_CreateChild(std::string name, std::string content, std::string parentTagName) {
size_t loc = _xmlData.find(_MakeTag(parentTagName, true)) + parentTagName.length() + 2;
_xmlData.insert(loc, _MakeTag(name, true) + content + _MakeTag(name, false));
return true;
}
void XMLRead::_DeleteAttributes(std::string fieldName) {
size_t loc = _xmlData.find("<" + fieldName) + fieldName.length();
std::string attributeContents = _GetStringBetween(_xmlData, "<" + fieldName, ">");
if (attributeContents.length() > 0) {
_xmlData.erase(loc + 1, attributeContents.length());
}
}