UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
xml.cpp
Go to the documentation of this file.
1 
6 /*
7 Copyright (C) 2002-2020 UFO: Alien Invasion.
8 
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 
18 See the GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 
24 */
25 
26 #include "xml.h"
27 #include "../shared/cxx.h"
28 #include "../shared/shared.h"
29 
36 void XML_AddString (xmlNode_t* parent, const char* name, const char* value)
37 {
38  if (value)
39  mxmlElementSetAttr(parent, name, value);
40 }
41 
49 void XML_AddStringValue (xmlNode_t* parent, const char* name, const char* value)
50 {
51  if (Q_strnull(value))
52  return;
53  XML_AddString(parent, name, value);
54 }
55 
62 void XML_AddBool (xmlNode_t* parent, const char* name, bool value)
63 {
64  mxmlElementSetAttr(parent, name, value ? "true" : "false");
65 }
66 
74 void XML_AddBoolValue (xmlNode_t* parent, const char* name, bool value)
75 {
76  if (!value)
77  return;
78  XML_AddBool(parent, name, value);
79 }
80 
87 void XML_AddFloat (xmlNode_t* parent, const char* name, float value)
88 {
89  XML_AddDouble(parent, name, value);
90 }
91 
99 void XML_AddFloatValue (xmlNode_t* parent, const char* name, float value)
100 {
101  XML_AddDoubleValue(parent, name, value);
102 }
103 
110 void XML_AddDouble (xmlNode_t* parent, const char* name, double value)
111 {
112  char txt[50];
113  snprintf(txt, sizeof(txt), "%f", value);
114  mxmlElementSetAttr(parent, name, txt);
115 }
116 
124 void XML_AddDoubleValue (xmlNode_t* parent, const char* name, double value)
125 {
126  if (value) {
127  XML_AddDouble(parent, name, value);
128  }
129 }
130 
137 void XML_AddByte (xmlNode_t* parent, const char* name, byte value)
138 {
139  XML_AddLong(parent, name, value);
140 }
141 
149 void XML_AddByteValue (xmlNode_t* parent, const char* name, byte value)
150 {
151  XML_AddLongValue(parent, name, value);
152 }
153 
160 void XML_AddShort (xmlNode_t* parent, const char* name, short value)
161 {
162  XML_AddLong(parent, name, value);
163 }
164 
172 void XML_AddShortValue (xmlNode_t* parent, const char* name, short value)
173 {
174  XML_AddLongValue(parent, name, value);
175 }
176 
183 void XML_AddInt (xmlNode_t* parent, const char* name, int value)
184 {
185  XML_AddLong(parent, name, value);
186 }
187 
195 void XML_AddIntValue (xmlNode_t* parent, const char* name, int value)
196 {
197  XML_AddLongValue(parent, name, value);
198 }
199 
206 void XML_AddLong (xmlNode_t* parent, const char* name, long value)
207 {
208  char txt[50];
209  snprintf(txt, sizeof(txt), "%ld", value);
210  mxmlElementSetAttr(parent, name, txt);
211 }
212 
220 void XML_AddLongValue (xmlNode_t* parent, const char* name, long value)
221 {
222  if (!value)
223  return;
224  XML_AddLong(parent, name, value);
225 }
226 
234 void XML_AddPos3 (xmlNode_t* parent, const char* name, const vec3_t pos)
235 {
236  xmlNode_t* t = mxmlNewElement(parent, name);
237  XML_AddFloat(t, "x", pos[0]);
238  XML_AddFloat(t, "y", pos[1]);
239  XML_AddFloat(t, "z", pos[2]);
240 }
241 
249 void XML_AddPos2 (xmlNode_t* parent, const char* name, const vec2_t pos)
250 {
251  xmlNode_t* t = mxmlNewElement(parent, name);
252  XML_AddFloat(t, "x", pos[0]);
253  XML_AddFloat(t, "y", pos[1]);
254 }
255 
264 void XML_AddDate (xmlNode_t* parent, const char* name, const int day, const int sec)
265 {
266  xmlNode_t* t = mxmlNewElement(parent, name);
267  XML_AddInt(t, "day", day);
268  XML_AddInt(t, "sec", sec);
269 }
270 
277 xmlNode_t* XML_AddNode (xmlNode_t* parent, const char* name)
278 {
279  return mxmlNewElement(parent,name);
280 }
281 
288 bool XML_GetBool (xmlNode_t* parent, const char* name, const bool defaultval)
289 {
290  const char* txt = mxmlElementGetAttr(parent, name);
291  if (!txt)
292  return defaultval;
293 
294  if (Q_streq(txt, "true") || Q_streq(txt, "1"))
295  return true;
296  if (Q_streq(txt, "false") || Q_streq(txt, "0"))
297  return false;
298 
299  return defaultval;
300 }
301 
308 int XML_GetInt (xmlNode_t* parent, const char* name, const int defaultval)
309 {
310  const char* txt = mxmlElementGetAttr(parent, name);
311  if (!txt)
312  return defaultval;
313  return atoi(txt);
314 }
315 
322 short XML_GetShort (xmlNode_t* parent, const char* name, const short defaultval)
323 {
324  const char* txt = mxmlElementGetAttr(parent, name);
325  if (!txt)
326  return defaultval;
327  return atoi(txt);
328 }
329 
336 long XML_GetLong (xmlNode_t* parent, const char* name, const long defaultval)
337 {
338  const char* txt = mxmlElementGetAttr(parent, name);
339  if (!txt)
340  return defaultval;
341  return atol(txt);
342 }
343 
350 const char* XML_GetString (xmlNode_t* parent, const char* name)
351 {
352  const char* str = mxmlElementGetAttr(parent, name);
353  if (!str)
354  return "";
355  return str;
356 }
357 
364 float XML_GetFloat (xmlNode_t* parent, const char* name, const float defaultval)
365 {
366  const char* txt = mxmlElementGetAttr(parent, name);
367  if (!txt)
368  return defaultval;
369  return atof(txt);
370 }
371 
378 double XML_GetDouble (xmlNode_t* parent, const char* name, const double defaultval)
379 {
380  const char* txt = mxmlElementGetAttr(parent, name);
381  if (!txt)
382  return defaultval;
383  return atof(txt);
384 }
385 
394 xmlNode_t* XML_GetPos2 (xmlNode_t* parent, const char* name, vec2_t pos)
395 {
396  xmlNode_t* p = XML_GetNode(parent, name);
397  if (!p)
398  return nullptr;
399  pos[0] = XML_GetFloat(p, "x", 0);
400  pos[1] = XML_GetFloat(p, "y", 0);
401  return p;
402 }
403 
413 xmlNode_t* XML_GetNextPos2 (xmlNode_t* actual, xmlNode_t* parent, const char* name, vec2_t pos)
414 {
415  xmlNode_t* p = XML_GetNextNode(actual, parent, name);
416  if (!p)
417  return nullptr;
418  pos[0] = XML_GetFloat(p, "x", 0);
419  pos[1] = XML_GetFloat(p, "y", 0);
420  return p;
421 }
422 
431 xmlNode_t* XML_GetPos3 (xmlNode_t* parent, const char* name, vec3_t pos)
432 {
433  xmlNode_t* p = XML_GetNode(parent, name);
434  if (!p)
435  return nullptr;
436  pos[0] = XML_GetFloat(p, "x", 0);
437  pos[1] = XML_GetFloat(p, "y", 0);
438  pos[2] = XML_GetFloat(p, "z", 0);
439  return p;
440 }
441 
451 xmlNode_t* XML_GetNextPos3 (xmlNode_t* actual, xmlNode_t* parent, const char* name, vec3_t pos)
452 {
453  xmlNode_t* p = XML_GetNextNode(actual, parent, name);
454  if (!p)
455  return nullptr;
456  pos[0] = XML_GetFloat(p, "x", 0);
457  pos[1] = XML_GetFloat(p, "y", 0);
458  pos[2] = XML_GetFloat(p, "z", 0);
459  return p;
460 }
461 
471 xmlNode_t* XML_GetDate (xmlNode_t* parent, const char* name, int* day, int* sec)
472 {
473  xmlNode_t* p = XML_GetNode(parent, name);
474  if (!p)
475  return nullptr;
476  *day = XML_GetInt(p, "day", 0);
477  *sec = XML_GetInt(p, "sec", 0);
478  return p;
479 }
480 
487 xmlNode_t* XML_GetNode (xmlNode_t* parent, const char* name)
488 {
489  return mxmlFindElement(parent, parent, name, nullptr, nullptr, MXML_DESCEND_FIRST);
490 }
491 
499 xmlNode_t* XML_GetNextNode (xmlNode_t* current, xmlNode_t* parent, const char* name)
500 {
501  return mxmlFindElement(current, parent, name, nullptr, nullptr, MXML_NO_DESCEND);
502 }
503 
507 static mxml_type_t mxml_ufo_type_cb (xmlNode_t* node)
508 {
509  /* You can lookup attributes and/or use the
510  * element name, hierarchy, etc... */
511  const char* type = mxmlElementGetAttr(node, "type");
512  if (type == nullptr) {
513 #ifdef MXML_MAJOR_VERSION
514  type = mxmlGetElement(node);
515 #else
516  type = node->value.element.name;
517 #endif
518  }
519 
520  if (Q_streq(type, "int"))
521  return MXML_INTEGER;
522  else if (Q_streq(type, "opaque"))
523  return MXML_OPAQUE;
524  else if (Q_streq(type, "string"))
525  return MXML_OPAQUE;
526  else if (Q_streq(type, "double"))
527  return MXML_REAL;
528  return MXML_TEXT;
529 }
530 
531 xmlNode_t* XML_Parse (const char* buffer)
532 {
533  return mxmlLoadString(nullptr, buffer, mxml_ufo_type_cb);
534 }
bool Q_strnull(const char *string)
Definition: shared.h:138
xmlNode_t * XML_GetPos2(xmlNode_t *parent, const char *name, vec2_t pos)
retrieve the first Pos2 data from an XML Node
Definition: xml.cpp:394
short XML_GetShort(xmlNode_t *parent, const char *name, const short defaultval)
retrieve a Short attribute from an XML Node
Definition: xml.cpp:322
QGL_EXTERN GLint GLenum type
Definition: r_gl.h:94
void XML_AddStringValue(xmlNode_t *parent, const char *name, const char *value)
add a non-empty String attribute to the XML Node
Definition: xml.cpp:49
float XML_GetFloat(xmlNode_t *parent, const char *name, const float defaultval)
retrieve a Float attribute from an XML Node
Definition: xml.cpp:364
void XML_AddDoubleValue(xmlNode_t *parent, const char *name, double value)
add a non-zero Double attribute to the XML Node
Definition: xml.cpp:124
int XML_GetInt(xmlNode_t *parent, const char *name, const int defaultval)
retrieve an Int attribute from an XML Node
Definition: xml.cpp:308
double XML_GetDouble(xmlNode_t *parent, const char *name, const double defaultval)
retrieve a Double attribute from an XML Node
Definition: xml.cpp:378
void XML_AddDate(xmlNode_t *parent, const char *name, const int day, const int sec)
add a date data to the XML Tree
Definition: xml.cpp:264
void XML_AddPos3(xmlNode_t *parent, const char *name, const vec3_t pos)
add a Pos3 data to the XML Tree
Definition: xml.cpp:234
void XML_AddFloatValue(xmlNode_t *parent, const char *name, float value)
add a non-zero Float attribute to the XML Node
Definition: xml.cpp:99
xmlNode_t * XML_GetPos3(xmlNode_t *parent, const char *name, vec3_t pos)
retrieve the first Pos3 data from an XML Node
Definition: xml.cpp:431
xmlNode_t * XML_GetNextPos3(xmlNode_t *actual, xmlNode_t *parent, const char *name, vec3_t pos)
retrieve the next Pos3 data from an XML Node
Definition: xml.cpp:451
void XML_AddPos2(xmlNode_t *parent, const char *name, const vec2_t pos)
add a Pos2 data to the XML Tree
Definition: xml.cpp:249
const char * XML_GetString(xmlNode_t *parent, const char *name)
retrieve a String attribute from an XML Node
Definition: xml.cpp:350
#define xmlNode_t
Definition: xml.h:24
xmlNode_t * XML_AddNode(xmlNode_t *parent, const char *name)
add a new node to the XML tree
Definition: xml.cpp:277
void XML_AddInt(xmlNode_t *parent, const char *name, int value)
add an Int attribute to the XML Node
Definition: xml.cpp:183
void XML_AddString(xmlNode_t *parent, const char *name, const char *value)
add a String attribute to the XML Node
Definition: xml.cpp:36
void XML_AddFloat(xmlNode_t *parent, const char *name, float value)
add a Float attribute to the XML Node
Definition: xml.cpp:87
void XML_AddBool(xmlNode_t *parent, const char *name, bool value)
add a Boolean attribute to the XML Node
Definition: xml.cpp:62
void XML_AddLong(xmlNode_t *parent, const char *name, long value)
add a Long attribute to the XML Node
Definition: xml.cpp:206
void XML_AddShortValue(xmlNode_t *parent, const char *name, short value)
add a non-zero Short attribute to the XML Node
Definition: xml.cpp:172
void XML_AddShort(xmlNode_t *parent, const char *name, short value)
add a Short attribute to the XML Node
Definition: xml.cpp:160
long XML_GetLong(xmlNode_t *parent, const char *name, const long defaultval)
retrieve a Long attribute from an XML Node
Definition: xml.cpp:336
bool XML_GetBool(xmlNode_t *parent, const char *name, const bool defaultval)
retrieve a Boolean attribute from an XML Node
Definition: xml.cpp:288
void XML_AddBoolValue(xmlNode_t *parent, const char *name, bool value)
add a non-false Boolean attribute to the XML Node
Definition: xml.cpp:74
void XML_AddByte(xmlNode_t *parent, const char *name, byte value)
add a Byte attribute to the XML Node
Definition: xml.cpp:137
static mxml_type_t mxml_ufo_type_cb(xmlNode_t *node)
callback function for parsing the node tree
Definition: xml.cpp:507
xmlNode_t * XML_GetDate(xmlNode_t *parent, const char *name, int *day, int *sec)
retrieve the date data from an XML Node
Definition: xml.cpp:471
xmlNode_t * XML_GetNextPos2(xmlNode_t *actual, xmlNode_t *parent, const char *name, vec2_t pos)
retrieve the next Pos2 data from an XML Node
Definition: xml.cpp:413
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition: r_gl.h:110
void XML_AddDouble(xmlNode_t *parent, const char *name, double value)
add a Double attribute to the XML Node
Definition: xml.cpp:110
vec_t vec3_t[3]
Definition: ufotypes.h:39
vec_t vec2_t[2]
Definition: ufotypes.h:38
xmlNode_t * XML_GetNode(xmlNode_t *parent, const char *name)
Get first Node of the XML tree by name.
Definition: xml.cpp:487
#define Q_streq(a, b)
Definition: shared.h:136
void XML_AddIntValue(xmlNode_t *parent, const char *name, int value)
add a non-zero Int attribute to the XML Node
Definition: xml.cpp:195
uint8_t byte
Definition: ufotypes.h:34
void XML_AddByteValue(xmlNode_t *parent, const char *name, byte value)
add a non-zero Byte attribute to the XML Node
Definition: xml.cpp:149
xmlNode_t * XML_GetNextNode(xmlNode_t *current, xmlNode_t *parent, const char *name)
Get next Node of the XML tree by name.
Definition: xml.cpp:499
void XML_AddLongValue(xmlNode_t *parent, const char *name, long value)
add a non-zero Long attribute to the XML Node
Definition: xml.cpp:220
xmlNode_t * XML_Parse(const char *buffer)
Definition: xml.cpp:531