UFO: Alien Invasion
xml.cpp
Go to the documentation of this file.
1
6/*
7Copyright (C) 2002-2022 UFO: Alien Invasion.
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
18See the GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, 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
36void XML_AddString (xmlNode_t* parent, const char* name, const char* value)
37{
38 if (value)
39 mxmlElementSetAttr(parent, name, value);
40}
41
49void 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
62void XML_AddBool (xmlNode_t* parent, const char* name, bool value)
63{
64 mxmlElementSetAttr(parent, name, value ? "true" : "false");
65}
66
74void XML_AddBoolValue (xmlNode_t* parent, const char* name, bool value)
75{
76 if (!value)
77 return;
78 XML_AddBool(parent, name, value);
79}
80
87void XML_AddFloat (xmlNode_t* parent, const char* name, float value)
88{
89 XML_AddDouble(parent, name, value);
90}
91
99void XML_AddFloatValue (xmlNode_t* parent, const char* name, float value)
100{
101 XML_AddDoubleValue(parent, name, value);
102}
103
110void 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
124void XML_AddDoubleValue (xmlNode_t* parent, const char* name, double value)
125{
126 if (value) {
127 XML_AddDouble(parent, name, value);
128 }
129}
130
137void XML_AddByte (xmlNode_t* parent, const char* name, byte value)
138{
139 XML_AddLong(parent, name, value);
140}
141
149void XML_AddByteValue (xmlNode_t* parent, const char* name, byte value)
150{
151 XML_AddLongValue(parent, name, value);
152}
153
160void XML_AddShort (xmlNode_t* parent, const char* name, short value)
161{
162 XML_AddLong(parent, name, value);
163}
164
172void XML_AddShortValue (xmlNode_t* parent, const char* name, short value)
173{
174 XML_AddLongValue(parent, name, value);
175}
176
183void XML_AddInt (xmlNode_t* parent, const char* name, int value)
184{
185 XML_AddLong(parent, name, value);
186}
187
195void XML_AddIntValue (xmlNode_t* parent, const char* name, int value)
196{
197 XML_AddLongValue(parent, name, value);
198}
199
206void 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
220void XML_AddLongValue (xmlNode_t* parent, const char* name, long value)
221{
222 if (!value)
223 return;
224 XML_AddLong(parent, name, value);
225}
226
234void 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
249void 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
264void 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
277xmlNode_t* XML_AddNode (xmlNode_t* parent, const char* name)
278{
279 return mxmlNewElement(parent,name);
280}
281
288bool 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
308int 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
322short 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
336long 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
350const 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
364float 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
378double 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
394xmlNode_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
413xmlNode_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
431xmlNode_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
451xmlNode_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
471xmlNode_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
487xmlNode_t* XML_GetNode (xmlNode_t* parent, const char* name)
488{
489 return mxmlFindElement(parent, parent, name, nullptr, nullptr, MXML_DESCEND_FIRST);
490}
491
499xmlNode_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
507static 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
531xmlNode_t* XML_Parse (const char* buffer)
532{
533 return mxmlLoadString(nullptr, buffer, mxml_ufo_type_cb);
534}
QGL_EXTERN GLint GLenum type
Definition: r_gl.h:94
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition: r_gl.h:110
#define Q_streq(a, b)
Definition: shared.h:136
bool Q_strnull(const char *string)
Definition: shared.h:138
vec_t vec3_t[3]
Definition: ufotypes.h:39
vec_t vec2_t[2]
Definition: ufotypes.h:38
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
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_AddFloatValue(xmlNode_t *parent, const char *name, float value)
add a non-zero Float attribute to the XML Node
Definition: xml.cpp:99
double XML_GetDouble(xmlNode_t *parent, const char *name, const double defaultval)
retrieve a Double attribute from an XML Node
Definition: xml.cpp:378
float XML_GetFloat(xmlNode_t *parent, const char *name, const float defaultval)
retrieve a Float attribute from an XML Node
Definition: xml.cpp:364
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
void XML_AddByte(xmlNode_t *parent, const char *name, byte value)
add a Byte attribute to the XML Node
Definition: xml.cpp:137
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_AddString(xmlNode_t *parent, const char *name, const char *value)
add a String attribute to the XML Node
Definition: xml.cpp:36
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
long XML_GetLong(xmlNode_t *parent, const char *name, const long defaultval)
retrieve a Long attribute from an XML Node
Definition: xml.cpp:336
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
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
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
int XML_GetInt(xmlNode_t *parent, const char *name, const int defaultval)
retrieve an Int attribute from an XML Node
Definition: xml.cpp:308
void XML_AddFloat(xmlNode_t *parent, const char *name, float value)
add a Float attribute to the XML Node
Definition: xml.cpp:87
static mxml_type_t mxml_ufo_type_cb(xmlNode_t *node)
callback function for parsing the node tree
Definition: xml.cpp:507
short XML_GetShort(xmlNode_t *parent, const char *name, const short defaultval)
retrieve a Short attribute from an XML Node
Definition: xml.cpp:322
void XML_AddDouble(xmlNode_t *parent, const char *name, double value)
add a Double attribute to the XML Node
Definition: xml.cpp:110
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
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
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
const char * XML_GetString(xmlNode_t *parent, const char *name)
retrieve a String attribute from an XML Node
Definition: xml.cpp:350
void XML_AddLong(xmlNode_t *parent, const char *name, long value)
add a Long attribute to the XML Node
Definition: xml.cpp:206
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
void XML_AddBool(xmlNode_t *parent, const char *name, bool value)
add a Boolean attribute to the XML Node
Definition: xml.cpp:62
xmlNode_t * XML_Parse(const char *buffer)
Definition: xml.cpp:531
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
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
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
void XML_AddShort(xmlNode_t *parent, const char *name, short value)
add a Short attribute to the XML Node
Definition: xml.cpp:160
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
xmlNode_t * XML_GetNode(xmlNode_t *parent, const char *name)
Get first Node of the XML tree by name.
Definition: xml.cpp:487
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_AddBoolValue(xmlNode_t *parent, const char *name, bool value)
add a non-false Boolean attribute to the XML Node
Definition: xml.cpp:74
#define xmlNode_t
Definition: xml.h:24