UFO: Alien Invasion
Loading...
Searching...
No Matches
xml.cpp
Go to the documentation of this file.
1
5
6/*
7Copyright (C) 2002-2025 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
30#if MXML_MAJOR_VERSION < 4
31#define MXML_DESCEND_NONE MXML_NO_DESCEND
32#define MXML_TYPE_INTEGER MXML_INTEGER
33#define MXML_TYPE_OPAQUE MXML_OPAQUE
34#define MXML_TYPE_REAL MXML_REAL
35#define MXML_TYPE_TEXT MXML_TEXT
36#endif
37
38
45void XML_AddString (xmlNode_t* parent, const char* name, const char* value)
46{
47 if (value)
48 mxmlElementSetAttr(parent, name, value);
49}
50
58void XML_AddStringValue (xmlNode_t* parent, const char* name, const char* value)
59{
60 if (Q_strnull(value))
61 return;
62 XML_AddString(parent, name, value);
63}
64
71void XML_AddBool (xmlNode_t* parent, const char* name, bool value)
72{
73 mxmlElementSetAttr(parent, name, value ? "true" : "false");
74}
75
83void XML_AddBoolValue (xmlNode_t* parent, const char* name, bool value)
84{
85 if (!value)
86 return;
87 XML_AddBool(parent, name, value);
88}
89
96void XML_AddFloat (xmlNode_t* parent, const char* name, float value)
97{
98 XML_AddDouble(parent, name, value);
99}
100
108void XML_AddFloatValue (xmlNode_t* parent, const char* name, float value)
109{
110 XML_AddDoubleValue(parent, name, value);
111}
112
119void XML_AddDouble (xmlNode_t* parent, const char* name, double value)
120{
121 char txt[50];
122 snprintf(txt, sizeof(txt), "%f", value);
123 mxmlElementSetAttr(parent, name, txt);
124}
125
133void XML_AddDoubleValue (xmlNode_t* parent, const char* name, double value)
134{
135 if (value) {
136 XML_AddDouble(parent, name, value);
137 }
138}
139
146void XML_AddByte (xmlNode_t* parent, const char* name, byte value)
147{
148 XML_AddLong(parent, name, value);
149}
150
158void XML_AddByteValue (xmlNode_t* parent, const char* name, byte value)
159{
160 XML_AddLongValue(parent, name, value);
161}
162
169void XML_AddShort (xmlNode_t* parent, const char* name, short value)
170{
171 XML_AddLong(parent, name, value);
172}
173
181void XML_AddShortValue (xmlNode_t* parent, const char* name, short value)
182{
183 XML_AddLongValue(parent, name, value);
184}
185
192void XML_AddInt (xmlNode_t* parent, const char* name, int value)
193{
194 XML_AddLong(parent, name, value);
195}
196
204void XML_AddIntValue (xmlNode_t* parent, const char* name, int value)
205{
206 XML_AddLongValue(parent, name, value);
207}
208
215void XML_AddLong (xmlNode_t* parent, const char* name, long value)
216{
217 char txt[50];
218 snprintf(txt, sizeof(txt), "%ld", value);
219 mxmlElementSetAttr(parent, name, txt);
220}
221
229void XML_AddLongValue (xmlNode_t* parent, const char* name, long value)
230{
231 if (!value)
232 return;
233 XML_AddLong(parent, name, value);
234}
235
243void XML_AddPos3 (xmlNode_t* parent, const char* name, const vec3_t pos)
244{
245 xmlNode_t* t = mxmlNewElement(parent, name);
246 XML_AddFloat(t, "x", pos[0]);
247 XML_AddFloat(t, "y", pos[1]);
248 XML_AddFloat(t, "z", pos[2]);
249}
250
258void XML_AddPos2 (xmlNode_t* parent, const char* name, const vec2_t pos)
259{
260 xmlNode_t* t = mxmlNewElement(parent, name);
261 XML_AddFloat(t, "x", pos[0]);
262 XML_AddFloat(t, "y", pos[1]);
263}
264
273void XML_AddDate (xmlNode_t* parent, const char* name, const int day, const int sec)
274{
275 xmlNode_t* t = mxmlNewElement(parent, name);
276 XML_AddInt(t, "day", day);
277 XML_AddInt(t, "sec", sec);
278}
279
286xmlNode_t* XML_AddNode (xmlNode_t* parent, const char* name)
287{
288 return mxmlNewElement(parent,name);
289}
290
297bool XML_GetBool (xmlNode_t* parent, const char* name, const bool defaultval)
298{
299 const char* txt = mxmlElementGetAttr(parent, name);
300 if (!txt)
301 return defaultval;
302
303 if (Q_streq(txt, "true") || Q_streq(txt, "1"))
304 return true;
305 if (Q_streq(txt, "false") || Q_streq(txt, "0"))
306 return false;
307
308 return defaultval;
309}
310
317int XML_GetInt (xmlNode_t* parent, const char* name, const int defaultval)
318{
319 const char* txt = mxmlElementGetAttr(parent, name);
320 if (!txt)
321 return defaultval;
322 return atoi(txt);
323}
324
331short XML_GetShort (xmlNode_t* parent, const char* name, const short defaultval)
332{
333 const char* txt = mxmlElementGetAttr(parent, name);
334 if (!txt)
335 return defaultval;
336 return atoi(txt);
337}
338
345long XML_GetLong (xmlNode_t* parent, const char* name, const long defaultval)
346{
347 const char* txt = mxmlElementGetAttr(parent, name);
348 if (!txt)
349 return defaultval;
350 return atol(txt);
351}
352
359const char* XML_GetString (xmlNode_t* parent, const char* name)
360{
361 const char* str = mxmlElementGetAttr(parent, name);
362 if (!str)
363 return "";
364 return str;
365}
366
373float XML_GetFloat (xmlNode_t* parent, const char* name, const float defaultval)
374{
375 const char* txt = mxmlElementGetAttr(parent, name);
376 if (!txt)
377 return defaultval;
378 return atof(txt);
379}
380
387double XML_GetDouble (xmlNode_t* parent, const char* name, const double defaultval)
388{
389 const char* txt = mxmlElementGetAttr(parent, name);
390 if (!txt)
391 return defaultval;
392 return atof(txt);
393}
394
403xmlNode_t* XML_GetPos2 (xmlNode_t* parent, const char* name, vec2_t pos)
404{
405 xmlNode_t* p = XML_GetNode(parent, name);
406 if (!p)
407 return nullptr;
408 pos[0] = XML_GetFloat(p, "x", 0);
409 pos[1] = XML_GetFloat(p, "y", 0);
410 return p;
411}
412
422xmlNode_t* XML_GetNextPos2 (xmlNode_t* actual, xmlNode_t* parent, const char* name, vec2_t pos)
423{
424 xmlNode_t* p = XML_GetNextNode(actual, parent, name);
425 if (!p)
426 return nullptr;
427 pos[0] = XML_GetFloat(p, "x", 0);
428 pos[1] = XML_GetFloat(p, "y", 0);
429 return p;
430}
431
440xmlNode_t* XML_GetPos3 (xmlNode_t* parent, const char* name, vec3_t pos)
441{
442 xmlNode_t* p = XML_GetNode(parent, name);
443 if (!p)
444 return nullptr;
445 pos[0] = XML_GetFloat(p, "x", 0);
446 pos[1] = XML_GetFloat(p, "y", 0);
447 pos[2] = XML_GetFloat(p, "z", 0);
448 return p;
449}
450
460xmlNode_t* XML_GetNextPos3 (xmlNode_t* actual, xmlNode_t* parent, const char* name, vec3_t pos)
461{
462 xmlNode_t* p = XML_GetNextNode(actual, parent, name);
463 if (!p)
464 return nullptr;
465 pos[0] = XML_GetFloat(p, "x", 0);
466 pos[1] = XML_GetFloat(p, "y", 0);
467 pos[2] = XML_GetFloat(p, "z", 0);
468 return p;
469}
470
480xmlNode_t* XML_GetDate (xmlNode_t* parent, const char* name, int* day, int* sec)
481{
482 xmlNode_t* p = XML_GetNode(parent, name);
483 if (!p)
484 return nullptr;
485 *day = XML_GetInt(p, "day", 0);
486 *sec = XML_GetInt(p, "sec", 0);
487 return p;
488}
489
496xmlNode_t* XML_GetNode (xmlNode_t* parent, const char* name)
497{
498 return mxmlFindElement(parent, parent, name, nullptr, nullptr, MXML_DESCEND_FIRST);
499}
500
508xmlNode_t* XML_GetNextNode (xmlNode_t* current, xmlNode_t* parent, const char* name)
509{
510 return mxmlFindElement(current, parent, name, nullptr, nullptr, MXML_DESCEND_NONE);
511}
512
516#if MXML_MAJOR_VERSION < 4
517static mxml_type_t mxml_ufo_type_cb (xmlNode_t* node)
518#else
519static mxml_type_t mxml_ufo_type_cb (void *cbdata, xmlNode_t* node)
520#endif
521{
522 /* You can lookup attributes and/or use the
523 * element name, hierarchy, etc... */
524 const char* type = mxmlElementGetAttr(node, "type");
525 if (type == nullptr) {
526#ifdef MXML_MAJOR_VERSION
527 type = mxmlGetElement(node);
528#else
529 type = node->value.element.name;
530#endif
531 }
532
533 if (Q_streq(type, "int"))
534 return MXML_TYPE_INTEGER;
535 else if (Q_streq(type, "opaque"))
536 return MXML_TYPE_OPAQUE;
537 else if (Q_streq(type, "string"))
538 return MXML_TYPE_OPAQUE;
539 else if (Q_streq(type, "double"))
540 return MXML_TYPE_REAL;
541 return MXML_TYPE_TEXT;
542}
543
544xmlNode_t* XML_Parse (const char* buffer)
545{
546#if MXML_MAJOR_VERSION < 4
547 return mxmlLoadString(nullptr, buffer, mxml_ufo_type_cb);
548#else
549 mxml_options_t *options = mxmlOptionsNew();
550 mxmlOptionsSetTypeCallback(options, &mxml_ufo_type_cb, nullptr);
551 xmlNode_t *ret = mxmlLoadString(nullptr, nullptr, buffer);
552 mxmlOptionsDelete(options);
553 return ret;
554#endif
555}
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:258
#define MXML_TYPE_OPAQUE
Definition xml.cpp:33
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:508
void XML_AddFloatValue(xmlNode_t *parent, const char *name, float value)
add a non-zero Float attribute to the XML Node
Definition xml.cpp:108
double XML_GetDouble(xmlNode_t *parent, const char *name, const double defaultval)
retrieve a Double attribute from an XML Node
Definition xml.cpp:387
float XML_GetFloat(xmlNode_t *parent, const char *name, const float defaultval)
retrieve a Float attribute from an XML Node
Definition xml.cpp:373
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:403
void XML_AddByte(xmlNode_t *parent, const char *name, byte value)
add a Byte attribute to the XML Node
Definition xml.cpp:146
#define MXML_DESCEND_NONE
Definition xml.cpp:31
#define MXML_TYPE_INTEGER
Definition xml.cpp:32
bool XML_GetBool(xmlNode_t *parent, const char *name, const bool defaultval)
retrieve a Boolean attribute from an XML Node
Definition xml.cpp:297
void XML_AddString(xmlNode_t *parent, const char *name, const char *value)
add a String attribute to the XML Node
Definition xml.cpp:45
void XML_AddIntValue(xmlNode_t *parent, const char *name, int value)
add a non-zero Int attribute to the XML Node
Definition xml.cpp:204
long XML_GetLong(xmlNode_t *parent, const char *name, const long defaultval)
retrieve a Long attribute from an XML Node
Definition xml.cpp:345
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:58
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:422
void XML_AddShortValue(xmlNode_t *parent, const char *name, short value)
add a non-zero Short attribute to the XML Node
Definition xml.cpp:181
int XML_GetInt(xmlNode_t *parent, const char *name, const int defaultval)
retrieve an Int attribute from an XML Node
Definition xml.cpp:317
void XML_AddFloat(xmlNode_t *parent, const char *name, float value)
add a Float attribute to the XML Node
Definition xml.cpp:96
static mxml_type_t mxml_ufo_type_cb(xmlNode_t *node)
callback function for parsing the node tree
Definition xml.cpp:517
short XML_GetShort(xmlNode_t *parent, const char *name, const short defaultval)
retrieve a Short attribute from an XML Node
Definition xml.cpp:331
void XML_AddDouble(xmlNode_t *parent, const char *name, double value)
add a Double attribute to the XML Node
Definition xml.cpp:119
void XML_AddByteValue(xmlNode_t *parent, const char *name, byte value)
add a non-zero Byte attribute to the XML Node
Definition xml.cpp:158
void XML_AddLongValue(xmlNode_t *parent, const char *name, long value)
add a non-zero Long attribute to the XML Node
Definition xml.cpp:229
void XML_AddDoubleValue(xmlNode_t *parent, const char *name, double value)
add a non-zero Double attribute to the XML Node
Definition xml.cpp:133
const char * XML_GetString(xmlNode_t *parent, const char *name)
retrieve a String attribute from an XML Node
Definition xml.cpp:359
void XML_AddLong(xmlNode_t *parent, const char *name, long value)
add a Long attribute to the XML Node
Definition xml.cpp:215
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:440
void XML_AddBool(xmlNode_t *parent, const char *name, bool value)
add a Boolean attribute to the XML Node
Definition xml.cpp:71
xmlNode_t * XML_Parse(const char *buffer)
Definition xml.cpp:544
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:460
xmlNode_t * XML_AddNode(xmlNode_t *parent, const char *name)
add a new node to the XML tree
Definition xml.cpp:286
#define MXML_TYPE_TEXT
Definition xml.cpp:35
void XML_AddInt(xmlNode_t *parent, const char *name, int value)
add an Int attribute to the XML Node
Definition xml.cpp:192
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:480
void XML_AddShort(xmlNode_t *parent, const char *name, short value)
add a Short attribute to the XML Node
Definition xml.cpp:169
void XML_AddPos3(xmlNode_t *parent, const char *name, const vec3_t pos)
add a Pos3 data to the XML Tree
Definition xml.cpp:243
xmlNode_t * XML_GetNode(xmlNode_t *parent, const char *name)
Get first Node of the XML tree by name.
Definition xml.cpp:496
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:273
#define MXML_TYPE_REAL
Definition xml.cpp:34
void XML_AddBoolValue(xmlNode_t *parent, const char *name, bool value)
add a non-false Boolean attribute to the XML Node
Definition xml.cpp:83
#define xmlNode_t
Definition xml.h:24