File: xml.cpp

package info (click to toggle)
ufoai 2.5-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 82,128 kB
  • sloc: cpp: 225,227; python: 5,111; ansic: 4,133; php: 2,209; perl: 1,931; sh: 1,517; xml: 1,115; makefile: 401; sed: 11
file content (540 lines) | stat: -rw-r--r-- 15,900 bytes parent folder | download | duplicates (3)
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/**
 * @file
 * @brief UFO:AI interface functions to mxml
 */

/*
Copyright (C) 2002-2013 UFO: Alien Invasion.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#include "xml.h"
#include "../shared/cxx.h"

/**
 * @brief add a String attribute to the XML Node
 * @param[out] parent XML Node structure to add to
 * @param[in] name Name of the attribute
 * @param[in] value Value of the attribute
 */
void XML_AddString (xmlNode_t* parent, const char* name, const char* value)
{
	if (value)
		mxmlElementSetAttr(parent, name, value);
}

/**
 * @brief add a non-empty String attribute to the XML Node
 * @param[out] parent XML Node structure to add to
 * @param[in] name Name of the attribute
 * @param[in] value Value of the attribute
 * @note if the value is empty nothing will be added
 */
void XML_AddStringValue (xmlNode_t* parent, const char* name, const char* value)
{
	if (value && value[0] != '\0')
		mxmlElementSetAttr(parent, name, value);
}

/**
 * @brief add a Boolean attribute to the XML Node
 * @param[out] parent XML Node structure to add to
 * @param[in] name Name of the attribute
 * @param[in] value Value of the attribute
 */
void XML_AddBool (xmlNode_t* parent, const char* name, bool value)
{
	mxmlElementSetAttr(parent, name, value ? "true" : "false");
}

/**
 * @brief add a non-false Boolean attribute to the XML Node
 * @param[out] parent XML Node structure to add to
 * @param[in] name Name of the attribute
 * @param[in] value Value of the attribute
 * @note if the value is false nothing will be added
 */
void XML_AddBoolValue (xmlNode_t* parent, const char* name, bool value)
{
	if (value)
		mxmlElementSetAttr(parent, name, "true");
}

/**
 * @brief add a Float attribute to the XML Node
 * @param[out] parent XML Node structure to add to
 * @param[in] name Name of the attribute
 * @param[in] value Value of the attribute
 */
void XML_AddFloat (xmlNode_t* parent, const char* name, float value)
{
	XML_AddDouble(parent, name, value);
}

/**
 * @brief add a non-zero Float attribute to the XML Node
 * @param[out] parent XML Node structure to add to
 * @param[in] name Name of the attribute
 * @param[in] value Value of the attribute
 * @note if the value is zero nothing will be added
 */
void XML_AddFloatValue (xmlNode_t* parent, const char* name, float value)
{
	XML_AddDoubleValue(parent, name, value);
}

/**
 * @brief add a Double attribute to the XML Node
 * @param[out] parent XML Node structure to add to
 * @param[in] name Name of the attribute
 * @param[in] value Value of the attribute
 */
void XML_AddDouble (xmlNode_t* parent, const char* name, double value)
{
	char txt[50];
	snprintf(txt, sizeof(txt), "%f", value);
	mxmlElementSetAttr(parent, name, txt);
}

/**
 * @brief add a non-zero Double attribute to the XML Node
 * @param[out] parent XML Node structure to add to
 * @param[in] name Name of the attribute
 * @param[in] value Value of the attribute
 * @note if the value is zero nothing will be added
 */
void XML_AddDoubleValue (xmlNode_t* parent, const char* name, double value)
{
	if (value) {
		char txt[50];
		snprintf(txt, sizeof(txt), "%f", value);
		mxmlElementSetAttr(parent, name, txt);
	}
}

/**
 * @brief add a Byte attribute to the XML Node
 * @param[out] parent XML Node structure to add to
 * @param[in] name Name of the attribute
 * @param[in] value Value of the attribute
 */
void XML_AddByte (xmlNode_t* parent, const char* name, byte value)
{
	XML_AddLong(parent, name, value);
}

/**
 * @brief add a non-zero Byte attribute to the XML Node
 * @param[out] parent XML Node structure to add to
 * @param[in] name Name of the attribute
 * @param[in] value Value of the attribute
 * @note if the value is zero nothing will be added
 */
void XML_AddByteValue (xmlNode_t* parent, const char* name, byte value)
{
	XML_AddLongValue(parent, name, value);
}

/**
 * @brief add a Short attribute to the XML Node
 * @param[out] parent XML Node structure to add to
 * @param[in] name Name of the attribute
 * @param[in] value Value of the attribute
 */
void XML_AddShort (xmlNode_t* parent, const char* name, short value)
{
	XML_AddLong(parent, name, value);
}

/**
 * @brief add a non-zero Short attribute to the XML Node
 * @param[out] parent XML Node structure to add to
 * @param[in] name Name of the attribute
 * @param[in] value Value of the attribute
 * @note if the value is zero nothing will be added
 */
void XML_AddShortValue (xmlNode_t* parent, const char* name, short value)
{
	XML_AddLongValue(parent, name, value);
}

/**
 * @brief add an Int attribute to the XML Node
 * @param[out] parent XML Node structure to add to
 * @param[in] name Name of the attribute
 * @param[in] value Value of the attribute
 */
void XML_AddInt (xmlNode_t* parent, const char* name, int value)
{
	XML_AddLong(parent, name, value);
}

/**
 * @brief add a non-zero Int attribute to the XML Node
 * @param[out] parent XML Node structure to add to
 * @param[in] name Name of the attribute
 * @param[in] value Value of the attribute
 * @note if the value is zero nothing will be added
 */
void XML_AddIntValue (xmlNode_t* parent, const char* name, int value)
{
	XML_AddLongValue(parent, name, value);
}

/**
 * @brief add a Long attribute to the XML Node
 * @param[out] parent XML Node structure to add to
 * @param[in] name Name of the attribute
 * @param[in] value Value of the attribute
 */
void XML_AddLong (xmlNode_t* parent, const char* name, long value)
{
	char txt[50];
	snprintf(txt, sizeof(txt), "%ld", value);
	mxmlElementSetAttr(parent, name, txt);
}

/**
 * @brief add a non-zero Long attribute to the XML Node
 * @param[out] parent XML Node structure to add to
 * @param[in] name Name of the attribute
 * @param[in] value Value of the attribute
 * @note if the value is zero nothing will be added
 */
void XML_AddLongValue (xmlNode_t* parent, const char* name, long value)
{
	if (value) {
		char txt[50];
		snprintf(txt, sizeof(txt), "%ld", value);
		mxmlElementSetAttr(parent, name, txt);
	}
}

/**
 * @brief add a Pos3 data to the XML Tree
 * @param[out] parent XML Node structure to add to
 * @param[in] name Name of the node to add
 * @param[in] pos Pos3 structure with position data
 * @note it creates a new node and adds coordinate(s) to the node as attributes
 */
void XML_AddPos3 (xmlNode_t* parent, const char* name, const vec3_t pos)
{
	xmlNode_t* t;
	t = mxmlNewElement(parent, name);
	XML_AddFloat(t, "x", pos[0]);
	XML_AddFloat(t, "y", pos[1]);
	XML_AddFloat(t, "z", pos[2]);
}

/**
 * @brief add a Pos2 data to the XML Tree
 * @param[out] parent XML Node structure to add to
 * @param[in] name Name of the node to add
 * @param[in] pos Pos2 structure with position data
 * @note it creates a new node and adds coordinate(s) to the node as attributes
 */
void XML_AddPos2 (xmlNode_t* parent, const char* name, const vec2_t pos)
{
	xmlNode_t* t;
	t = mxmlNewElement(parent, name);
	XML_AddFloat(t, "x", pos[0]);
	XML_AddFloat(t, "y", pos[1]);
}

/**
 * @brief add a date data to the XML Tree
 * @param[out] parent XML Node structure to add to
 * @param[in] name Name of the node to add
 * @param[in] day Day part of the date
 * @param[in] sec Second part of the date
 * @note it creates a new node and adds day/sec attributes to the node
 */
void XML_AddDate (xmlNode_t* parent, const char* name, const int day, const int sec)
{
	xmlNode_t* t;
	t = mxmlNewElement(parent, name);
	XML_AddInt(t, "day", day);
	XML_AddInt(t, "sec", sec);
}

/**
 * @brief add a new node to the XML tree
 * @param[out] parent XML Node structure to add to
 * @param[in] name Name of the new node
 * return pointer to the new XML Node structure
 */
xmlNode_t* XML_AddNode (xmlNode_t* parent, const char* name)
{
	return mxmlNewElement(parent,name);
}

/**
 * @brief retrieve a Boolean attribute from an XML Node
 * @param[in] parent XML Node structure to get from
 * @param[in] name Name of the attribute
 * @param[in] defaultval Default value to return if no such attribute defined
 */
bool XML_GetBool (xmlNode_t* parent, const char* name, const bool defaultval)
{
	const char* txt;
	txt = mxmlElementGetAttr(parent, name);
	if (!txt)
		return defaultval;

	if (!strcmp(txt, "true") || !strcmp(txt, "1"))
		return true;
	if (!strcmp(txt, "false") || !strcmp(txt, "0"))
		return false;

	return defaultval;
}

/**
 * @brief retrieve an Int attribute from an XML Node
 * @param[in] parent XML Node structure to get from
 * @param[in] name Name of the attribute
 * @param[in] defaultval Default value to return if no such attribute defined
 */
int XML_GetInt (xmlNode_t* parent, const char* name, const int defaultval)
{
	const char* txt;
	txt = mxmlElementGetAttr(parent, name);
	if (!txt)
		return defaultval;
	return atoi(txt);
}

/**
 * @brief retrieve a Short attribute from an XML Node
 * @param[in] parent XML Node structure to get from
 * @param[in] name Name of the attribute
 * @param[in] defaultval Default value to return if no such attribute defined
 */
short XML_GetShort (xmlNode_t* parent, const char* name, const short defaultval)
{
	const char* txt;
	txt = mxmlElementGetAttr(parent, name);
	if (!txt)
		return defaultval;
	return atoi(txt);
}

/**
 * @brief retrieve a Long attribute from an XML Node
 * @param[in] parent XML Node structure to get from
 * @param[in] name Name of the attribute
 * @param[in] defaultval Default value to return if no such attribute defined
 */
long XML_GetLong (xmlNode_t* parent, const char* name, const long defaultval)
{
	const char* txt;
	txt = mxmlElementGetAttr(parent, name);
	if (!txt)
		return defaultval;
	return atol(txt);
}

/**
 * @brief retrieve a String attribute from an XML Node
 * @param[in] parent XML Node structure to get from
 * @param[in] name Name of the attribute
 * @return empty string if no such attribute defined
 */
const char*  XML_GetString (xmlNode_t* parent, const char* name)
{
	const char* str = mxmlElementGetAttr(parent, name);
	if (!str)
		return "";
	return str;
}

/**
 * @brief retrieve a Float attribute from an XML Node
 * @param[in] parent XML Node structure to get from
 * @param[in] name Name of the attribute
 * @param[in] defaultval Default value to return if no such attribute defined
 */
float XML_GetFloat (xmlNode_t* parent, const char* name, const float defaultval)
{
	const char* txt;
	txt = mxmlElementGetAttr(parent, name);
	if (!txt)
		return defaultval;
	return atof(txt);
}

/**
 * @brief retrieve a Double attribute from an XML Node
 * @param[in] parent XML Node structure to get from
 * @param[in] name Name of the attribute
 * @param[in] defaultval Default value to return if no such attribute defined
 */
double XML_GetDouble (xmlNode_t* parent, const char* name, const double defaultval)
{
	const char* txt;
	txt = mxmlElementGetAttr(parent, name);
	if (!txt)
		return defaultval;
	return atof(txt);
}

/**
 * @brief retrieve the first Pos2 data from an XML Node
 * @param[in] parent XML Node structure to get child from
 * @param[in] name Name of the pos node
 * @param[out] pos vec2_t structure to fill
 * @return pointer to the node the data was retrieved from
 * @return nullptr if no node with name found
 */
xmlNode_t* XML_GetPos2 (xmlNode_t* parent, const char* name, vec2_t pos)
{
	xmlNode_t* p = XML_GetNode(parent, name);
	if (!p)
		return nullptr;
	pos[0] = XML_GetFloat(p, "x", 0);
	pos[1] = XML_GetFloat(p, "y", 0);
	return p;
}

/**
 * @brief retrieve the next Pos2 data from an XML Node
 * @param[in] actual XML Node pointer of the previous pos data
 * @param[in] parent XML Node structure to get child from
 * @param[in] name Name of the pos node
 * @param[out] pos vec2_t structure to fill
 * @return pointer to the node the data was retrieved from
 * @return nullptr if no Node with name found
 */
xmlNode_t* XML_GetNextPos2 (xmlNode_t* actual, xmlNode_t* parent, const char* name, vec2_t pos)
{
	xmlNode_t* p = XML_GetNextNode(actual, parent, name);
	if (!p)
		return nullptr;
	pos[0] = XML_GetFloat(p, "x", 0);
	pos[1] = XML_GetFloat(p, "y", 0);
	return p;
}

/**
 * @brief retrieve the first Pos3 data from an XML Node
 * @param[in] parent XML Node structure to get child from
 * @param[in] name Name of the pos node
 * @param[out] pos vec3_t structure to fill
 * @return pointer to the node the data was retrieved from
 * @return nullptr if no node with name found
 */
xmlNode_t* XML_GetPos3 (xmlNode_t* parent, const char* name, vec3_t pos)
{
	xmlNode_t* p = XML_GetNode(parent, name);
	if (!p)
		return nullptr;
	pos[0] = XML_GetFloat(p, "x", 0);
	pos[1] = XML_GetFloat(p, "y", 0);
	pos[2] = XML_GetFloat(p, "z", 0);
	return p;
}

/**
 * @brief retrieve the next Pos3 data from an XML Node
 * @param[in] actual XML Node pointer of the previous pos data
 * @param[in] parent XML Node structure to get child from
 * @param[in] name Name of the pos node
 * @param[out] pos vec3_t structure to fill
 * @return pointer to the node the data was retrieved from
 * @return nullptr if no Node with name found
 */
xmlNode_t* XML_GetNextPos3 (xmlNode_t* actual, xmlNode_t* parent, const char* name, vec3_t pos)
{
	xmlNode_t* p = XML_GetNextNode(actual, parent, name);
	if (!p)
		return nullptr;
	pos[0] = XML_GetFloat(p, "x", 0);
	pos[1] = XML_GetFloat(p, "y", 0);
	pos[2] = XML_GetFloat(p, "z", 0);
	return p;
}

/**
 * @brief retrieve the date data from an XML Node
 * @param[in] parent XML Node structure to get child from
 * @param[in] name Name of the pos node
 * @param[out] day Day part of the date to fill
 * @param[out] sec Second part of the date to fill
 * @return pointer to the node the data was retrieved from
 * @return nullptr if no node with name found
 */
xmlNode_t* XML_GetDate (xmlNode_t* parent, const char* name, int* day, int* sec)
{
	xmlNode_t* p = XML_GetNode(parent, name);
	if (!p)
		return nullptr;
	*day = XML_GetInt(p, "day", 0);
	*sec = XML_GetInt(p, "sec", 0);
	return p;
}

/**
 * @brief Get first Node of the XML tree by name
 * @param[in] parent Parent XML Node structure
 * @param[in] name Name of the node to retrieve
 * @return pointer to the found XML Node structure or nullptr
 */
xmlNode_t* XML_GetNode (xmlNode_t* parent, const char* name)
{
	return mxmlFindElement(parent, parent, name, nullptr, nullptr, MXML_DESCEND_FIRST);
}

/**
 * @brief Get next Node of the XML tree by name
 * @param[in] current Pointer to the previous Node was found
 * @param[in] parent Parent XML Node structure
 * @param[in] name Name of the node to retrieve
 * @return pointer to the found XML Node structure or nullptr
 */
xmlNode_t* XML_GetNextNode (xmlNode_t* current, xmlNode_t* parent, const char* name)
{
	return mxmlFindElement(current, parent, name, nullptr, nullptr, MXML_NO_DESCEND);
}

/**
 * @brief callback function for parsing the node tree
 */
static mxml_type_t mxml_ufo_type_cb (xmlNode_t* node)
{
	/* You can lookup attributes and/or use the
	 * element name, hierarchy, etc... */
	const char* type = mxmlElementGetAttr(node, "type");
	if (type == nullptr)
		type = node->value.element.name;

	if (!strcmp(type, "int"))
		return MXML_INTEGER;
	else if (!strcmp(type, "opaque"))
		return MXML_OPAQUE;
	else if (!strcmp(type, "string"))
		return MXML_OPAQUE;
	else if (!strcmp(type, "double"))
		return MXML_REAL;
	else
		return MXML_TEXT;
}

xmlNode_t* XML_Parse (const char* buffer)
{
	return mxmlLoadString(nullptr, buffer, mxml_ufo_type_cb);
}