zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
XML.cpp
Go to the documentation of this file.
1 /* This file is part of the Zenipex Library (zenilib).
2  * Copyright (C) 2011 Mitchell Keith Bloch (bazald).
3  *
4  * zenilib is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * zenilib is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with zenilib. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <zeni.h>
19 
20 #include <iostream>
21 
22 #if defined(_DEBUG) && defined(_WINDOWS)
23 #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
24 #define new DEBUG_NEW
25 #endif
26 
27 namespace Zeni {
28 
30  : m_handle(handle_)
31  {
32  }
33 
35  if(!m_handle.ToNode()) {
36  std::cerr << "Bad XML_Element_c attempted to access field '" << field << "'\n";
37  throw XML_Element_Ungood();
38  }
39 
40  TiXmlNode * node = child_element(field);
41  return XML_Element_c(node ? node->ToElement() : 0);
42  }
43 
45  if(!m_handle.ToNode()) {
46  std::cerr << "Bad XML_Element_c attempted to access first field\n";
47  throw XML_Element_Ungood();
48  }
49 
50  TiXmlNode * node = first_child_element();
51  return XML_Element_c(node ? node->ToElement() : 0);
52  }
53 
55  if(!m_handle.ToNode()) {
56  std::cerr << "Bad XML_Element_c attempted to access next field\n";
57  throw XML_Element_Ungood();
58  }
59 
60  TiXmlNode * node = m_handle.ToNode();
61  return XML_Element_c(node ? node->NextSiblingElement() : 0);
62  }
63 
64  bool XML_Element_c::good() const {
65  return m_handle.ToElement() != 0;
66  }
67 
69  if(!good()) {
70  std::cerr << "Bad XML_Element_c attempted to access value\n";
71  throw XML_Element_Ungood();
72  }
73 
74  TiXmlElement * element = m_handle.ToElement();
75  if(!element) {
76  std::cerr << "XML_Element_c invalid\n";
77  throw XML_Element_Ungood();
78  }
79 
80  return element->Value();
81  }
82 
83  bool XML_Element_c::to_bool() const {
84  return to_int() != 0;
85  }
86 
87  int XML_Element_c::to_int() const {
88  return atoi(to_string().c_str());
89  }
90 
91  float XML_Element_c::to_float() const {
92  return float(atof(to_string().c_str()));
93  }
94 
95  double XML_Element_c::to_double() const {
96  return atof(to_string().c_str());
97  }
98 
100  if(!good()) {
101  std::cerr << "Bad XML_Element_c attempted to_string()\n";
102  throw XML_Element_Ungood();
103  }
104 
105  TiXmlElement *element = m_handle.ToElement();
106  if(!element) {
107  std::cerr << "XML_Element_c invalid\n";
108  throw XML_Element_Ungood();
109  }
110 
111  const char * text = element->GetText();
112  if(!text) {
113  std::cerr << "XML_Element_c attempted to_string() on non-leaf node\n";
114  throw XML_Element_Nonleaf();
115  }
116 
117  return text;
118  }
119 
120  TiXmlNode * XML_Element_c::child(const String &field) const {
121  TiXmlNode * node = m_handle.ToNode();
122  return node ? node->FirstChild(field.c_str()) : 0;
123  }
124 
126  TiXmlNode * node = m_handle.ToNode();
127  return node ? node->FirstChild() : 0;
128  }
129 
131  TiXmlNode * node = m_handle.ToNode();
132  return node ? node->FirstChildElement(field.c_str()) : 0;
133  }
134 
136  TiXmlNode * node = m_handle.ToNode();
137  return node ? node->FirstChildElement() : 0;
138  }
139 
141  : XML_Element_c(handle_)
142  {
143  }
144 
146  if(!m_handle.ToNode()) {
147  std::cerr << "Bad XML_Element_c attempted to access field '" << field << "'\n";
148  throw XML_Element_Ungood();
149  }
150 
151  TiXmlNode * node = child_element(field);
152  return XML_Element(node ? node->ToElement() : 0);
153  }
154 
156  if(!m_handle.ToNode()) {
157  std::cerr << "Bad XML_Element_c attempted to access first field\n";
158  throw XML_Element_Ungood();
159  }
160 
161  TiXmlNode * node = first_child_element();
162  return XML_Element(node ? node->ToElement() : 0);
163  }
164 
166  if(!m_handle.ToNode()) {
167  std::cerr << "Bad XML_Element_c attempted to access next field\n";
168  throw XML_Element_Ungood();
169  }
170 
171  TiXmlNode * node = m_handle.ToNode();
172  return XML_Element(node ? node->NextSiblingElement() : 0);
173  }
174 
175  void XML_Element::create_child(const String &field) {
176  if(!m_handle.ToNode()) {
177  std::cerr << "Bad XML_Element_c attempted to create_child(...)\n";
178  throw XML_Element_Ungood();
179  }
180 
181  TiXmlElement * element = new TiXmlElement(field.c_str());
182  if(!m_handle.ToNode()->LinkEndChild(element)) {
183  std::cerr << "XML_Element_c failed to create a child\n";
184  throw XML_Create_Child_Failure();
185  }
186  }
187 
189  if(!good() || !child.good()) {
190  std::cerr << "Bad XML_Element_c attempted to remove_child(...)\n";
191  throw XML_Element_Ungood();
192  }
193 
194  if(!m_handle.ToNode()->RemoveChild(child.m_handle.ToNode())) {
195  std::cerr << "XML_Element_c failed to remove a child\n";
196  throw XML_Remove_Child_Failure();
197  }
198  }
199 
200  void XML_Element::set_bool(const bool &b) {
201  set_string(itoa(b));
202  }
203 
204  void XML_Element::set_int(const int &i) {
205  set_string(itoa(i));
206  }
207 
208  void XML_Element::set_float(const float &f) {
209  set_string(ftoa(f));
210  }
211 
212  void XML_Element::set_double(const double &d) {
213  set_string(dtoa(d));
214  }
215 
217  if(!good()) {
218  std::cerr << "Bad XML_Element_c attempted set_string(...)\n";
219  throw XML_Element_Ungood();
220  }
221 
222  TiXmlNode * const node = first_child();
223 
224  if(!node)
226  else if(node->ToText()) {
227  TiXmlText replacement(s.c_str());
228  m_handle.ToNode()->ReplaceChild(node, replacement);
229  }
230  else {
231  std::cerr << "XML_Element_c attempted set_string(...) on non-leaf node\n";
232  throw XML_Element_Nonleaf();
233  }
234  }
235 
237  : m_root(0)
238  {
239  }
240 
242  : m_root(0)
243  {
244  load(filename);
245  }
246 
248  : m_root(0)
249  {
250  if(!try_load(filename))
251  load(backup);
252  }
253 
255  delete m_root;
256  }
257 
258  bool XML_Document::good() const {
259  return m_root != 0;
260  }
261 
263  if(!try_load_mem(data)) {
264  std::cerr << "Failed to load XML_Document from memory\n"
265  << m_xml_file.ErrorDesc() << std::endl;
266  throw XML_Load_Failure();
267  }
268  }
269 
271  if(!try_load(filename)) {
272  std::cerr << "Failed to load XML_Document '" << filename << "'\n"
273  << m_xml_file.ErrorDesc() << std::endl;
274  throw XML_Load_Failure();
275  }
276  }
277 
279  if(!try_save()) {
280  std::cerr << "Failed to save XML_Document" << std::endl
281  << m_xml_file.ErrorDesc() << std::endl;
282  throw XML_Save_Failure();
283  }
284  }
285 
287  if(!try_save(filename)) {
288  std::cerr << "Failed to save XML_Document '" << filename << "'\n"
289  << m_xml_file.ErrorDesc() << std::endl;
290  throw XML_Save_Failure();
291  }
292  }
293 
295  TiXmlDocument next;
296 
297  if(!next.Parse(data.c_str())) {
298  ZENI_LOGE("Loading XML data failed.");
299  return false;
300  }
301  else {
302  delete m_root;
303  m_root = 0;
304 
305  m_xml_file = next;
306 
307  TiXmlHandle root = &m_xml_file;
308  m_root = new XML_Element(root);
309 
310  return true;
311  }
312  }
313 
315  TiXmlDocument next;
316 
317  if(!next.LoadFile(filename.c_str()))
318  return false;
319  else {
320  delete m_root;
321  m_root = 0;
322 
323  m_xml_file = next;
324 
325  TiXmlHandle root = &m_xml_file;
326  m_root = new XML_Element(root);
327 
328  return true;
329  }
330  }
331 
333  if(!good()) {
334  std::cerr << "Bad XML_Document attempted to save\n";
335  throw XML_Document_Ungood();
336  }
337 
338  return m_xml_file.SaveFile();
339  }
340 
342  if(!good()) {
343  std::cerr << "Bad XML_Document attempted to save to '" << filename << "'\n";
344  throw XML_Document_Ungood();
345  }
346 
347  return m_xml_file.SaveFile(filename.c_str());
348  }
349 
351  if(!good()) {
352  std::cerr << "Bad XML_Document attempted to access field '" << field << "'\n";
353  throw XML_Document_Ungood();
354  }
355 
356  return (*m_root)[field];
357  }
358 
360  if(!good()) {
361  std::cerr << "Bad XML_Document attempted to access field '" << field << "'\n";
362  throw XML_Document_Ungood();
363  }
364 
365  return (*m_root)[field];
366  }
367 
369  if(!good()) {
370  std::cerr << "Bad XML_Document attempted to access first field\n";
371  throw XML_Document_Ungood();
372  }
373 
374  return m_root->first();
375  }
376 
378  if(!good()) {
379  std::cerr << "Bad XML_Document attempted to access first field\n";
380  throw XML_Document_Ungood();
381  }
382 
383  return m_root->first();
384  }
385 
386  void XML_Document::create_root(const String &field) {
387  delete m_root;
388  m_root = 0;
389 
390  m_xml_file = TiXmlDocument();
391 
392  m_xml_file.LinkEndChild(new TiXmlDeclaration("1.0", "", ""));
393  m_xml_file.LinkEndChild(new TiXmlElement(field.c_str()));
394 
395  TiXmlHandle root = &m_xml_file;
396  m_root = new XML_Element(root);
397  }
398 
399 }
XML_Element operator[](const String &field)
Get a named subelement.
Definition: XML.cpp:145
TiXmlNode * child(const String &field) const
Definition: XML.cpp:120
String to_string() const
Get the contained string.
Definition: XML.cpp:99
GLdouble s
Definition: glew.h:1376
TiXmlNode * LinkEndChild(TiXmlNode *addThis)
Definition: tinyxml.cpp:186
TiXmlHandle m_handle
Definition: XML.h:83
void remove_child(const XML_Element &child)
Remove a child node, which will be rendered invalid.
Definition: XML.cpp:188
void set_float(const float &f)
Set the contained string as a floating point number.
Definition: XML.cpp:208
GLclampf f
Definition: glew.h:3390
XML_Element_c(const TiXmlHandle &handle)
Definition: XML.cpp:29
bool try_load(const String &filename)
Definition: XML.cpp:314
bool SaveFile() const
Save a file using the current document value. Returns true if successful.
Definition: tinyxml.cpp:915
void create_child(const String &field)
Create a child node.
Definition: XML.cpp:175
bool RemoveChild(TiXmlNode *removeThis)
Delete a child of this node.
Definition: tinyxml.cpp:331
XML_Element first()
Get the first subelement.
Definition: XML.cpp:155
const char * c_str() const
Definition: String.cpp:467
String dtoa(const double &number)
void load_mem(const String &data)
Definition: XML.cpp:262
return Display return Display Bool Bool int d
Definition: SDL_x11sym.h:30
void create_root(const String &field)
(Re)Initializes the document with a given root field
Definition: XML.cpp:386
float to_float() const
Get the contained string as a floating point number.
Definition: XML.cpp:91
const char * Value() const
Definition: tinyxml.h:487
TiXmlElement * ToElement() const
Definition: tinyxml.h:1681
void set_string(const String &s)
Set the contained string.
Definition: XML.cpp:216
bool to_bool() const
Get the contained string as a boolean.
Definition: XML.cpp:83
bool try_load_mem(const String &data)
Definition: XML.cpp:294
TiXmlNode * ReplaceChild(TiXmlNode *replaceThis, const TiXmlNode &withThis)
Definition: tinyxml.cpp:292
String ftoa(const float &number)
virtual const char * Parse(const char *p, TiXmlParsingData *data=0, TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
TiXmlNode * first_child_element() const
Definition: XML.cpp:135
double to_double() const
Get the contained string as a double precision floating point number.
Definition: XML.cpp:95
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl2ext.h:848
XML_Element_c operator[](const String &field) const
Get a named subelement.
Definition: XML.cpp:34
TiXmlNode * first_child() const
Definition: XML.cpp:125
const TiXmlElement * NextSiblingElement() const
Definition: tinyxml.cpp:478
String itoa(const int &number)
void set_bool(const bool &b)
Set the contained string as a boolean.
Definition: XML.cpp:200
XML_Element_c operator[](const String &field) const
Get a named subelement.
Definition: XML.cpp:350
void load(const String &filename)
Definition: XML.cpp:270
bool good() const
Definition: XML.cpp:258
void ZENI_LOGE(const Zeni::String &str)
Definition: Android.h:59
virtual const TiXmlText * ToText() const
Cast to a more defined type. Will return null if not of the requested type.
Definition: tinyxml.h:701
A node in an XML tree, possibly containing useful data.
Definition: XML.h:86
virtual const TiXmlElement * ToElement() const
Cast to a more defined type. Will return null if not of the requested type.
Definition: tinyxml.h:698
XML_Element next()
Get the next subelement.
Definition: XML.cpp:165
XML_Element_c first() const
Get the first subelement.
Definition: XML.cpp:368
const char * GetText() const
Definition: tinyxml.cpp:856
void set_double(const double &d)
Set the contained string as a double precision floating point number.
Definition: XML.cpp:212
String value() const
Get the value of the element (For nodes, this is the name)
Definition: XML.cpp:68
bool LoadFile(TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
Definition: tinyxml.cpp:909
const char * ErrorDesc() const
Contains a textual (english) description of the error if one occurs.
Definition: tinyxml.h:1456
TiXmlNode * child_element(const String &field) const
Definition: XML.cpp:130
GLdouble GLdouble GLdouble b
Definition: glew.h:8383
int to_int() const
Get the contained string as an integer.
Definition: XML.cpp:87
XML_Element_c next() const
Get the next subelement.
Definition: XML.cpp:54
void set_int(const int &i)
Set the contained string as an integer.
Definition: XML.cpp:204
XML_Element(const TiXmlHandle &handle)
Definition: XML.cpp:140
int i
Definition: pngrutil.c:1377
bool try_save()
Definition: XML.cpp:332
const TiXmlNode * FirstChild() const
The first child of this node. Will be null if there are no children.
Definition: tinyxml.h:522
XML_Element_c first() const
Get the first subelement.
Definition: XML.cpp:44
TiXmlNode * ToNode() const
Definition: tinyxml.h:1678
const TiXmlElement * FirstChildElement() const
Convenience function to get through elements.
Definition: tinyxml.cpp:448
bool good() const
Test to see if the Element is valid.
Definition: XML.cpp:64