File: xmldom.cpp

package info (click to toggle)
c-munipack 2.1.38-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,888 kB
  • sloc: ansic: 200,762; cpp: 106,123; lex: 9,035; yacc: 4,916; sh: 4,074; fortran: 2,613; xml: 2,105; python: 1,182; makefile: 546; perl: 104
file content (530 lines) | stat: -rw-r--r-- 13,171 bytes parent folder | download | duplicates (2)
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
/**************************************************************

frameset.c (C-Munipack project)
Frame set context
Copyright (C) 2003 David Motl, dmotl@volny.cz

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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <expat.h>

#include "xmldom.h"
#include "comfun.h"

#define BUFFSIZE 4096

typedef struct _ParseContext
{
	int result;
	CmpackDocument *doc;
	CmpackNode *parent;
	char *cdata;
} ParseContext;

/*************************   HELPER FUNCTIONS   ***************/

static void node_clear(void *data)
{
	CmpackNode *ptr, *next;

	CmpackNode *node = (CmpackNode*)data;
	cmpack_free(node->nodeName);
	cmpack_free(node->nodeValue);

	ptr = node->firstChild; 
	while (ptr) {
		next = ptr->nextSibling;
		if (ptr->destroy)
			ptr->destroy(ptr);
		else
			node_clear(ptr);
		cmpack_free(ptr);
		ptr = next;
	}
}

static void element_clear(void *data)
{
	int i;
	CmpackElement *elem = (CmpackElement*)data;

	for (i=0; i<elem->attr.count; i++) {
		cmpack_free(elem->attr.list[i].name);
		cmpack_free(elem->attr.list[i].value);
	}
	cmpack_free(elem->attr.list);
	node_clear(&elem->node);
}

static CmpackElement *element_new(const char *name, const char **atlist)
{
	int i, j, count;

	CmpackElement *elem = (CmpackElement*)cmpack_calloc(1, sizeof(CmpackElement));
	elem->node.destroy  = element_clear;
	elem->node.nodeType = ELEMENT_NODE;
	elem->node.nodeName = cmpack_strdup(name);
	if (atlist && atlist[0]) {
		count = 0;
  		for (i=0; atlist[i]; i+=2)
			count++;
		elem->attr.list = (CmpackAttribute*)cmpack_malloc(count*sizeof(CmpackAttribute));
  		for (i=j=0; atlist[i]; i+=2) {
			elem->attr.list[j].name = cmpack_strdup(atlist[i]);
			elem->attr.list[j].value = cmpack_strdup(atlist[i+1]);
			j++;
		}
		elem->attr.count = j;
	}
	return elem;
}

static void node_add(CmpackNode *parent, CmpackNode *child)
{
	if (parent && child) {
		child->parentNode = parent;
		if (!parent->lastChild) {
			/* Parent's first child */
			parent->firstChild = child;
			parent->lastChild = child;
		} else {
			/* Parent's next child */
			parent->lastChild->nextSibling = child;
			parent->lastChild = child;
		}
	}
}

static CmpackDocument *document_new(void)
{
	return (CmpackDocument*)cmpack_calloc(1, sizeof(CmpackDocument));
}

static CmpackComment *comment_new(const char *data)
{
	CmpackComment *doc = (CmpackComment*)cmpack_calloc(1, sizeof(CmpackComment));
	doc->node.nodeType = COMMENT_NODE;
	doc->node.nodeValue = cmpack_strdup(data);
	return doc;
}

/* Strip leading and trailing white spaces */
static CmpackCData *cdata_new(char *str)
{
	CmpackCData *cd;

	if (str) {
		/* Remove leading spaces */
		while (*str>0 && *str<=32) 
			str++;
		if (*str==0)
			return NULL;
		/* Create character data node */
		cd = (CmpackCData*)cmpack_calloc(1, sizeof(CmpackCData));
		cd->node.nodeType = TEXT_NODE;
		cd->node.nodeValue = cmpack_strdup(str);
		/* Remove trailing spaces */
		if (cd->node.nodeValue[0]!=0) {
			char *ptr = cd->node.nodeValue + strlen(cd->node.nodeValue)-1;
			while (ptr>=str && *ptr>=0 && *ptr<=32)
				ptr--;
			*(ptr+1) = '\0';
		}
		return cd;
	}
	return NULL;
}

static char *merge_strings(char *str1, const char *str2)
{
	char *out;

	if (!str1) {
		if (str2) 
			return cmpack_strdup(str2);
		else
			return NULL;
	} else {
		if (str2) {
			size_t len1 = strlen(str1), len2 = strlen(str2);
			out = (char*)cmpack_malloc((len1+len2+1)*sizeof(char));
			memcpy(out, str1, len1);
			memcpy(out+len1, str2, len2);
			out[len1+len2] = '\0';
			cmpack_free(str1);
			return out;
		} else
			return str1;
	}
}

static void process_cdata(ParseContext *f)
{
	if (f->cdata && f->cdata[0]!='\0' && f->parent) {
		if (f->parent->lastChild && f->parent->lastChild->nodeType == TEXT_NODE) {
			/* Append to last text node */
			f->parent->lastChild->nodeValue = merge_strings(f->parent->lastChild->nodeValue, f->cdata);
		} else {
			/* Create a new text node */
			node_add(f->parent, (CmpackNode*)cdata_new(f->cdata));
		}
		cmpack_free(f->cdata);
		f->cdata = NULL;
	}
}

/**********************   XML PARSER HANDLERS   *****************/

/* Obligatory callback from XML parser */
static int XMLCALL XMLUnknownEncodingHandler(void *data, const XML_Char *encoding, XML_Encoding *info)
{
 	int i;
 	
 	for (i=0; i<256; i++) 
 		info->map[i] = i;
 	info->data = NULL;
 	info->convert = NULL;
 	info->release = NULL;
 	return XML_STATUS_OK;
}

/* Start element callback. This is called whenever the XML parser finds
 * a start of the element. The 'aname' parameter is name of the element,
 * 'atlist' is the list of attributes (key+val) pairs.
 */
static void XMLCALL XMLStartElementHandler(void *data, const char *name, const char **atlist)
{
  	ParseContext *f = (ParseContext*)data;

	CmpackElement *elem = element_new(name, atlist);
	if (!f->doc->root) 
		f->doc->root = (CmpackNode*)elem;
	if (f->parent)
		process_cdata(f);
	node_add(f->parent, (CmpackNode*)elem);
	f->parent = (CmpackNode*)elem;
}

/* End element handler. XML parser calls this function whenever the end of
 * an element is found.
 */
static void XMLCALL XMLEndElementHandler(void *adata, const char *aname)
{
  	ParseContext *f = (ParseContext*) adata;

	if (f->parent) {
		process_cdata(f);
		f->parent = f->parent->parentNode;
	}
}

/* Character data handler */
static void XMLCALL XMLCharacterDataHandler(void *adata, const char *data, int len)
{
	int dstlen;
  	ParseContext *f = (ParseContext*) adata;

	if (f->parent && len>0) {
		if (f->cdata) {
			dstlen = (int)strlen(f->cdata);
			f->cdata = (char*)cmpack_realloc(f->cdata, (dstlen+len+1)*sizeof(char));
			memcpy(f->cdata + dstlen, data, len);
			f->cdata[dstlen+len] = '\0';
		} else {
			f->cdata = (char*)cmpack_malloc((len+1)*sizeof(char));
			memcpy(f->cdata, data, len);
			f->cdata[len] = '\0';
		}
	}
}

/* Comment handler */
static void XMLCALL XMLCommentHandler(void *adata, const char *data)
{
  	ParseContext *f = (ParseContext*) adata;

	if (f->parent) {
		process_cdata(f);
		node_add(f->parent, (CmpackNode*)comment_new(data));
	}
}

/**********************   PUBLIC FUNCTIONS   *****************/

/* Load document from a file */
CmpackDocument *xml_doc_from_file(FILE *from)
{
	size_t len;
	int done, res = 0;
	char buf[BUFFSIZE];
	ParseContext p;
	XML_Parser parser;

	memset(&p, 0, sizeof(ParseContext));
	p.doc = document_new();

	parser = XML_ParserCreate(NULL);
	XML_SetUserData(parser, &p);
	XML_SetElementHandler(parser, XMLStartElementHandler, XMLEndElementHandler);
	XML_SetUnknownEncodingHandler(parser, XMLUnknownEncodingHandler, NULL);
	XML_SetCharacterDataHandler(parser, XMLCharacterDataHandler);
	XML_SetCommentHandler(parser, XMLCommentHandler);
	do {
		len = fread(buf, 1, BUFFSIZE, from);
		if (ferror(from)) {
			res = CMPACK_ERR_READ_ERROR;
			break;
		}
		done = feof(from);
		if (XML_Parse(parser, buf, (int)len, done)==XML_STATUS_ERROR) {
			res = p.result;
			break;
		}
	} while (!done);
	XML_ParserFree(parser);
	if (res!=0) {
		xml_doc_free(p.doc);
		return NULL;
	}
	return p.doc;
}

/* Destroy the XML document */
void xml_doc_free(CmpackDocument *doc)
{
	if (doc) {
		if (doc->root) {
			node_clear(doc->root);
			cmpack_free(doc->root);
		}
		cmpack_free(doc);
	}
}

/* Get document root */
CmpackElement *xml_doc_get_root(CmpackDocument *doc)
{
	return (CmpackElement*)doc->root;
}

/* Get first child element by name */
CmpackElement *xml_first_element(CmpackElement *elem, const char *name)
{
	CmpackNode *ptr;
	
	for (ptr=elem->node.firstChild; ptr!=NULL; ptr=ptr->nextSibling) {
		if (ptr->nodeType == ELEMENT_NODE && strcmp(ptr->nodeName, name)==0)
			return (CmpackElement*)ptr;
	}
	return NULL;
}

/* Get next child element of the same name */
CmpackElement *xml_next_element(CmpackElement *elem)
{
	CmpackNode *ptr;

	for (ptr=elem->node.nextSibling; ptr!=NULL; ptr=ptr->nextSibling) {
		if (ptr->nodeType == ELEMENT_NODE && strcmp(ptr->nodeName, elem->node.nodeName)==0)
			return (CmpackElement*)ptr;
	}
	return NULL;
}

const char *xml_comment(CmpackNode *node)
{
	CmpackNode *ptr;
	
	for (ptr=node->firstChild; ptr!=NULL; ptr=ptr->nextSibling) {
		if (ptr->nodeType == COMMENT_NODE && ptr->nodeValue!=NULL)
			return ptr->nodeValue;
	}
	return NULL;
}

/* Get number of child nodes of given name */
int xml_get_n_children(CmpackElement *elem, const char *name)
{
	int count = 0;
	CmpackNode *ptr;

	for (ptr=elem->node.firstChild; ptr!=NULL; ptr=ptr->nextSibling) {
		if (ptr->nodeType == ELEMENT_NODE && strcmp(ptr->nodeName, name)==0)
			count++;
	}
	return count;
}

/* Returns nonzero if the element has the attribute */
int xml_has_attribute(CmpackElement *elem, const char *attr)
{
	int i;

	for (i=0; i<elem->attr.count; i++) {
		if (strcmp(elem->attr.list[i].name, attr)==0)
			return 1;
	}
	return 0;
}

const char *xml_attr_s(CmpackElement *elem, const char *attr, const char *defval)
{
	int i;

	for (i=0; i<elem->attr.count; i++) {
		if (strcmp(elem->attr.list[i].name, attr)==0)
			return elem->attr.list[i].value;
	}
	return defval;
}

int xml_attr_i(CmpackElement *elem, const char *attr, int defval)
{
	char *endptr;

	const char *str = xml_attr_s(elem, attr, NULL);
	if (str) {
		int value = strtol(str, &endptr, 10);
		if (endptr!=str)
			return value;
	}
	return defval;
}

double xml_attr_d(CmpackElement *elem, const char *attr, double defval)
{
	char *endptr;

	const char *str = xml_attr_s(elem, attr, NULL);
	if (str) {
		double value = strtod(str, &endptr);
		if (endptr!=str)
			return value;
	}
	return defval;
}

int xml_value_s(CmpackElement *elem, char *buf, int bufsize)
{
	CmpackNode *child = elem->node.firstChild;
	while (child) {
		if (child->nodeType == TEXT_NODE) {
			strcpy_truncate(buf, bufsize, child->nodeValue);
			return 1;
		}
		child = child->nextSibling;
	}
	return 0;
}

const char *xml_value(CmpackElement *elem, const char *defval)
{
	CmpackNode *child = elem->node.firstChild;
	while (child) {
		if (child->nodeType == TEXT_NODE)
			return child->nodeValue;
		child = child->nextSibling;
	}
	return NULL;
}

int xml_value_i(CmpackElement *elem, int defval)
{
	char *endptr;

	const char *str = xml_value(elem, NULL);
	if (str) {
		int value = strtol(str, &endptr, 10);
		if (endptr!=str)
			return value;
	}
	return defval;
}

double xml_value_d(CmpackElement *elem, double defval)
{
	char *endptr;

	const char *str = xml_value(elem, NULL);
	if (str) {
		double value = strtod(str, &endptr);
		if (endptr!=str)
			return value;
	}
	return defval;
}

int xml_value_tm(CmpackElement *elem, struct tm *t)
{
	const char *str, *ptr;
	
	memset(t, 0, sizeof(struct tm));
	str = xml_value(elem, NULL);
	if (str && sscanf(str, " %4d-%2d-%2d", &t->tm_year, &t->tm_mon, &t->tm_mday)==3) {
		t->tm_year -= 1900;
		t->tm_mon--;
		ptr = strchr(str, ' ');
		if (ptr && sscanf(ptr, " %2d:%2d:%2d", &t->tm_hour, &t->tm_min, &t->tm_sec)==3) 
			return 1;
	}
	return 0;
}

/* Get child node's value */
const char *xml_child_value(CmpackElement *elem, const char *name, const char *defval)
{
	CmpackElement *child = xml_first_element(elem, name);
	if (child)
		return xml_value(child, defval);
	return defval;
}

int xml_child_value_s(CmpackElement *elem, const char *name, char *buf, int bufsize)
{
	CmpackElement *child = xml_first_element(elem, name);
	if (child)
		return xml_value_s(child, buf, bufsize);
	return 0;
}

int xml_child_value_i(CmpackElement *elem, const char *name, int defval)
{
	CmpackElement *child = xml_first_element(elem, name);
	if (child)
		return xml_value_i(child, defval);
	return defval;
}

double xml_child_value_d(CmpackElement *elem, const char *name, double defval)
{
	CmpackElement *child = xml_first_element(elem, name);
	if (child)
		return xml_value_d(child, defval);
	return defval;
}

int xml_child_value_tm(CmpackElement *elem, const char *name, struct tm *t)
{
	CmpackElement *child = xml_first_element(elem, name);
	if (child)
		return xml_value_tm(child, t);
	return 0;
}