forked from leethomason/tinyxml2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinyxml2++.cpp
More file actions
279 lines (226 loc) · 4.97 KB
/
tinyxml2++.cpp
File metadata and controls
279 lines (226 loc) · 4.97 KB
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
#include "tinyxml2++.h"
namespace tinyxml2
{
XMLPPDocument::XMLPPDocument()
{
m_bHaveDeclaration = false;
m_xml.Clear();
m_mapChildElement.clear();
}
XMLPPDocument::XMLPPDocument(const char* version, const char* encode, const char* standalone)
{
m_xml.Clear();
m_mapChildElement.clear();
m_bHaveDeclaration = false;
SetDeclaration(version, encode, standalone);
}
XMLPPDocument::~XMLPPDocument()
{
m_xml.Clear();
for (auto& pair : m_mapChildElement)
{
for (auto& node : pair.second)
{
delete node;
}
}
m_mapChildElement.clear();
m_bHaveDeclaration = false;
}
XMLPPDocument::operator const char*()
{
m_printer.ClearBuffer();
m_xml.Print(&m_printer);
return m_printer.CStr();
}
XMLPPNode* XMLPPDocument::AddElement(const char* key)
{
tinyxml2::XMLElement* pRootElement = m_xml.NewElement(key);
m_xml.InsertEndChild(pRootElement);
XMLPPNode* pNode = new XMLPPNode(pRootElement, m_xml);
m_mapChildElement[key].insert(pNode);
return pNode;
}
XMLPPNode* XMLPPDocument::GetElement(const char* key)
{
auto it = m_mapChildElement.find(key);
if (it != m_mapChildElement.end() && !it->second.empty())
{
return (*(it->second.begin()));
}
return NULL;
}
void XMLPPDocument::SetDeclaration(const char* version, const char* encode, const char* standalone)
{
char buffer[256] = { 0 };
if (standalone == NULL)
{
_snprintf_s(buffer, 255, "xml version=\"%s\" encoding=\"%s\"", version, encode);
}
else
{
_snprintf_s(buffer, 255, "xml version=\"%s\" encoding=\"%s\" standalone=\"%s\"", version, encode, standalone);
}
if (m_bHaveDeclaration)
{
tinyxml2::XMLDeclaration* pDec = (tinyxml2::XMLDeclaration*)m_xml.FirstChild();
pDec->SetValue(buffer);
}
else
{
m_xml.InsertFirstChild(m_xml.NewDeclaration(buffer));
m_bHaveDeclaration = true;
}
}
void XMLPPDocument::SetComment(const char* comment)
{
if (m_bHaveDeclaration)
{
m_xml.InsertAfterChild(m_xml.FirstChildElement(), m_xml.NewComment(comment));
}
else
{
m_xml.InsertFirstChild(m_xml.NewComment(comment));
}
}
XMLPPNode& XMLPPDocument::operator[](const char* key)
{
XMLPPNode* pNode = GetElement(key);
if (pNode == NULL)
{
pNode = AddElement(key);
}
return (*pNode);
}
XMLPPNode::~XMLPPNode()
{
m_pCurElement = NULL;
for (auto& pair : m_mapChildElement)
{
for (auto& node : pair.second)
{
delete node;
}
}
m_mapChildElement.clear();
}
XMLPPNode& XMLPPNode::operator=(const XMLPPNode& base)
{
m_pCurElement = base.m_pCurElement;
return (*this);
}
bool XMLPPNode::operator==(const XMLPPNode& o)
{
return (m_pCurElement == o.m_pCurElement);
}
XMLPPNode::operator const char*() const
{
const char* value = m_pCurElement->GetText();
if (value == NULL)
{
return "";
}
return value;
}
XMLPPNode::operator int() const
{
const char* value = m_pCurElement->GetText();
if (value == NULL)
{
return 0;
}
return atoi(value);
}
XMLPPNode::operator double() const
{
const char* value = m_pCurElement->GetText();
if (value == NULL)
{
return 0.000000;
}
return atof(value);
}
XMLPPNode::operator long() const
{
const char* value = m_pCurElement->GetText();
if (value == NULL)
{
return 0;
}
return atol(value);
}
XMLPPNode::operator int64_t() const
{
const char* value = m_pCurElement->GetText();
if (value == NULL)
{
return 0;
}
return (int64_t) atoll(value);
}
XMLPPNode* XMLPPNode::AddElement(const char* key)
{
tinyxml2::XMLElement* pElementChild = m_doc.NewElement(key);
m_pCurElement->LinkEndChild(pElementChild);
XMLPPNode* pNode = new XMLPPNode(pElementChild,m_doc);
m_mapChildElement[key].insert(pNode);
return pNode;
}
XMLPPNode* XMLPPNode::GetElement(const char* key)
{
auto it = m_mapChildElement.find(key);
if (it != m_mapChildElement.end() && !it->second.empty())
{
return (*(it->second.begin()));
}
return NULL;
}
void XMLPPNode::SetValue(const char* value)
{
m_pCurElement->SetText(value);
}
void XMLPPNode::SetComment(const char* comment)
{
m_pCurElement->LinkEndChild(m_doc.NewComment(comment));
}
const char* XMLPPNode::GetText()
{
return m_pCurElement->GetText();
}
const char* XMLPPNode::GetName()
{
return m_pCurElement->Value();
}
const char* XMLPPNode::GetAttribute(const char* name)
{
const tinyxml2::XMLAttribute* pAttribute = (const_cast<const tinyxml2::XMLElement*>(m_pCurElement))->FindAttribute(name);
if (pAttribute)
{
return pAttribute->Value();
}
return NULL;
}
XMLPPNode& XMLPPNode::Append(const char* key)
{
XMLPPNode* pNewNode = AddElement(key);
return (*pNewNode);
}
XMLPPNode& XMLPPNode::operator[](const char* key)
{
XMLPPNode* pNode = GetElement(key);
if (pNode == NULL)
{
pNode = AddElement(key);
}
return (*pNode);
}
XMLPPNode& XMLPPNode::operator[](std::string key)
{
XMLPPNode* pNode = GetElement(key.c_str());
if (pNode == NULL)
{
pNode = AddElement(key.c_str());
}
return (*pNode);
}
}