-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsxml.c
545 lines (470 loc) · 14.1 KB
/
bsxml.c
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
541
542
543
544
545
/* bsxml.c - Simple DOM xml tree
*
* Copyright (C) 2014 Borislav Sapundzhiev
*
* 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.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include "bsstr.h"
#include "bsxml.h"
/* initial size */
#define XMLTREE_CHILDSIZE 8
#define XMLTREE_ATTRSIZE 4
#define XMLTREE_STACKSIZE 32
#define ENC_TYPE_UTF8 "UTF-8"
XmlNode * XmlNode_Create(const String tag)
{
XmlNode * node = (XmlNode *) malloc( sizeof(struct XmlNode) );
if (!node) return NULL;
node->m_tag = strdup( tag );
node->m_type = NODE_ROOT;
node->m_parent = 0;
node->m_content = NULL;
node->m_childs = cpo_array_create(XMLTREE_CHILDSIZE, sizeof(struct XmlNode));
node->m_attributes = cpo_array_create(XMLTREE_ATTRSIZE, sizeof( struct XmlAttribute) );
return node;
}
XmlAttributes *XmlNode_getAttributes(struct XmlNode * node)
{
return node->m_attributes;
}
asize_t XmlNode_getAttributesCount(struct XmlNode * node)
{
return node->m_attributes->num;
}
String XmlNode_getTag(struct XmlNode * node)
{
return node->m_tag;
}
asize_t XmlNode_getChildCount(struct XmlNode * node)
{
return node->m_childs->num;
}
XmlNodeRef XmlNode_getParent(struct XmlNode * node)
{
return node->m_parent;
}
String XmlNode_getContent(struct XmlNode * node)
{
return node->m_content;
}
int XmlNode_getLine(struct XmlNode * node)
{
return node->m_line;
}
void XmlNode_setLine(struct XmlNode * node, int line )
{
node->m_line = line;
}
void XmlNode_delete(struct XmlNode *node)
{
asize_t i;
if (node == NULL) return;
//printf("delete %s\n", node->m_tag);
for (i=0; i < node->m_attributes->num; i++) {
XmlAttribute *attr = cpo_array_get_at(node->m_attributes, i);
free(attr->key);
free(attr->value);
}
if (node->m_childs) {
cpo_array_destroy(node->m_childs);
}
if (node->m_attributes) {
cpo_array_destroy(node->m_attributes);
}
if (node->m_content)
free(node->m_content);
free(node->m_tag);
}
void XmlNode_deleteTree(struct XmlNode *root)
{
asize_t i;
if (root == NULL) return;
for (i=0 ; i < root->m_childs->num; i++) {
XmlNode *node = cpo_array_get_at(root->m_childs, i);
XmlNode_deleteTree(node);
}
XmlNode_delete(root);
if (root->m_type == NODE_ROOT) {
free(root);
}
}
void XmlNode_print(struct XmlNode *root)
{
asize_t i;
for (i=0 ; i < root->m_childs->num; i++) {
XmlNode *node = cpo_array_get_at(root->m_childs, i);
XmlNode_print(node);
}
printf("Node %s content:%s", root->m_tag, root->m_content != NULL ? root->m_content : NULL);
}
int XmlNode_isTag(struct XmlNode *node, const String tag )
{
return strcasecmp(tag, node->m_tag) == XML_NOK;
}
static int XmlAttribute_comparer(const void *a, const void *b)
{
return strcmp(((XmlAttribute *) a)->key, ((XmlAttribute *) b)->key);
}
XmlAttribute *XmlNode_getAttribute(struct XmlNode *node, const String key)
{
XmlAttribute a;
a.key = key;
return (XmlAttribute*)cpo_array_bsearch(node->m_attributes, &a, XmlAttribute_comparer);
}
String XmlNode_getAttributeValue(struct XmlNode *node, const String key)
{
String value = NULL;
XmlAttribute *attr = XmlNode_getAttribute(node, key);
if(attr) {
value = attr->value;
}
return value;
}
int XmlNode_haveAttribute(struct XmlNode *node, const String key )
{
return ( XmlNode_getAttribute(node, key ) ) ? XML_OK : XML_NOK;
}
void XmlNode_setAttribute(struct XmlNode *node, const String key, const String value )
{
XmlAttribute *a = cpo_array_push( node->m_attributes );
a->key = strdup(key);
a->value = strdup(value);
}
static int XmlNode_comparer(const void *a, const void *b)
{
return strcmp(((XmlNode *) a)->m_tag, ((XmlNode *) b)->m_tag);
}
XmlNodeRef XmlNode_findChild(struct XmlNode *node, const String tag )
{
XmlNode tmpNode = {NODE_CHILD, 0, tag};
XmlNodeRef ret = (XmlNodeRef)cpo_array_bsearch(node->m_childs, &tmpNode, XmlNode_comparer);
return ret;
}
XmlNode * XmlNode_createChild(struct XmlNode *node, const String tag, const String text)
{
XmlNodeRef child = cpo_array_push( node->m_childs );
child->m_tag = strdup( tag );
child->m_type = NODE_CHILD;
child->m_content = NULL;
child->m_parent = node;
if (text) {
XmlNode_setValue(child, text );
}
child->m_childs = cpo_array_create(XMLTREE_CHILDSIZE, sizeof(struct XmlNode));
child->m_attributes = cpo_array_create(XMLTREE_ATTRSIZE, sizeof( struct XmlAttribute) );
return child;
}
void XmlNode_addChild(struct XmlNode *node, const XmlNodeRef child )
{
XmlNodeRef ref = cpo_array_push( node->m_childs );
if (ref) {
*ref = *child;
ref->m_parent = node;
}
}
XmlNodeRef XmlNode_getChild(struct XmlNode *node, asize_t i)
{
assert( i < node->m_childs->num );
return cpo_array_get_at(node->m_childs, i);
}
void XmlNode_getValue(struct XmlNode *node, String *value )
{
*value = node->m_content;
}
void XmlNode_getValueInt(struct XmlNode *node, int *value )
{
*value = atoi(node->m_content);
}
void XmlNode_getValueFloat(struct XmlNode *node, float *value )
{
*value = (float)atof(node->m_content);
}
static int XmlNode_isEmptyValue(const char * s)
{
const char *p;
for (p=s; *p != '\0'; p++) {
if ( *p != ' ' && *p != '\r' && *p != '\n' && *p != '\t' ) {
return XML_NOK;
}
}
return XML_OK;
}
void XmlNode_setValue(struct XmlNode *node, const String value )
{
if (!isNullorEmpty(value) && !XmlNode_isEmptyValue(value)) {
//printf("value for %s = '%s'\n",node->m_tag, value );
if (node->m_content != NULL) {
size_t end = strlen(node->m_content);
size_t len = strlen(value);
unsigned last = isAlphaNumeric(*(node->m_content + end-1)) ? 2:1;
char *new = realloc(node->m_content,(end + len + last));
if (!new) return;
node->m_content = new;
if (last == 2) {
strcat(node->m_content, " ");
end+=1;
}
strncpy(node->m_content + end, value, len+1);
} else {
node->m_content = strdup(value);
}
}
}
void XmlNode_setValueInt(struct XmlNode *node, int value )
{
char str[128];
snprintf( str, sizeof(str),"%d",value );
XmlNode_setValue(node, str);
}
void XmlNode_setValueFloat(struct XmlNode *node, float value )
{
char str[128];
snprintf( str,sizeof(str),"%f",value );
XmlNode_setValue(node, str);
}
int XmlNode_getSubNodeValue(struct XmlNode *node, const String tag, String *value )
{
XmlNodeRef child = XmlNode_findChild(node, tag );
if (child) {
XmlNode_getValue(child, value );
return XML_OK;
}
return XML_NOK;
}
int XmlNode_getSubNodeValueInt(struct XmlNode *node, const String tag, int *value )
{
XmlNodeRef child = XmlNode_findChild(node, tag );
if (child) {
XmlNode_getValueInt(child, value );
return XML_OK;
}
return XML_NOK;
}
int XmlNode_getSubNodeValueFloat(struct XmlNode *node, const String tag, float *value )
{
XmlNodeRef child = XmlNode_findChild(node, tag );
if (child) {
XmlNode_getValueFloat(child, value );
return XML_OK;
}
return XML_NOK;
}
void XmlNode_setSubNodeValue(struct XmlNode *node, const String tag, const String value )
{
XmlNodeRef child = XmlNode_findChild(node, tag );
if (!child) {
child = XmlNode_createChild(node, tag, NULL);
}
XmlNode_setValue(child, value);
}
void XmlNode_setSubNodeValueInt(struct XmlNode *node, const String tag, int value )
{
XmlNodeRef child = XmlNode_findChild(node, tag );
if (!child) {
child = XmlNode_createChild(node, tag, NULL);
}
XmlNode_setValueInt(child, value);
}
void XmlNode_setSubNodeValueFloat(struct XmlNode *node, const String tag, float value )
{
XmlNodeRef child = XmlNode_findChild(node, tag );
if (!child) {
child = XmlNode_createChild(node, tag, NULL);
}
XmlNode_setValueFloat(child, value);
}
/* return allocated string */
String XmlNode_getXML(struct XmlNode *node)
{
String xmlStr = NULL;
bsstr *buff = bsstr_create("");
asize_t i;
if (node->m_attributes->num == 0) {
bsstr_printf(buff, "<%s>", node->m_tag);
} else {
bsstr_printf(buff, "<%s ", node->m_tag);
// Put attributes.
for (i =0; i< node->m_attributes->num; i++) {
XmlAttribute *a = cpo_array_get_at(node->m_attributes, i);
bsstr_printf(buff, "%s =\"%s\" ",a->key, a->value);
}
if (isNullorEmpty(node->m_content) && !node->m_childs->num) {
bsstr_printf(buff, "/>\n");
xmlStr = bsstr_release(buff);
return xmlStr;
}
bsstr_printf(buff, ">");
}
if (node->m_childs->num) {
bsstr_add(buff, "\n");
}
if (!isNullorEmpty(node->m_content)) {
size_t pos;
char *p = node->m_content;
for (pos = 0; pos < strlen(node->m_content); ++pos) {
switch (p[pos]) {
case '&':
bsstr_printf(buff,"&");
break;
case '\"':
bsstr_printf(buff,""");
break;
case '\'':
bsstr_printf(buff,"'");
break;
case '<':
bsstr_printf(buff,"<");
break;
case '>':
bsstr_printf(buff,">");
break;
default:
bsstr_printf(buff,"%c", p[pos]);
break;
}
}
}
for (i = 0; i < node->m_childs->num; i++) {
XmlNodeRef child = cpo_array_get_at(node->m_childs, i);
String childXML = XmlNode_getXML(child);
bsstr_add(buff, childXML);
free(childXML);
}
// End tag.
bsstr_printf(buff, "</%s>\n", node->m_tag);
xmlStr = bsstr_release(buff);
return xmlStr;
}
void XmlNode_toFile(struct XmlNode *node, const char *fileName)
{
FILE *f = fopen (fileName, "w+b");
if (f) {
char *buffer = XmlNode_getXML(node);
if (buffer) {
size_t len = strlen(buffer);
fprintf(f, "<?xml version=\"1.0\" encoding=\"%s\"?>\n", ENC_TYPE_UTF8);
fwrite(buffer, sizeof(char),len, f);
free(buffer);
}
fclose (f);
} else {
printf("error: cannot write to %s \n", fileName);
}
}
/*parser */
static void startElement(void *userData, const char *name, const char **atts)
{
asize_t i = 0;
void *ptr = NULL;
XmlNodeRef parent= NULL, node=NULL;
XmlParser *parser = (XmlParser *)userData;
if (parser->m_nodeStack->num > 0) {
ptr = stack_back(parser->m_nodeStack);
parent = (XmlNodeRef) ARR_VAL(ptr);
} else {
parser->m_root = XmlNode_Create((const String) name);
}
if (parent) {
node = XmlNode_createChild(parent, (const String)name, NULL);
} else {
node = parser->m_root;
}
ptr = stack_push_back(parser->m_nodeStack);
if (ptr != NULL) {
ARR_VAL(ptr) = ARR_VAL2PTR(node);
}
XmlNode_setLine(node, XML_GetCurrentLineNumber( parser->m_parser ) );
// Call start element callback.
while (atts[i] != 0) {
XmlNode_setAttribute(node, (const String)atts[i], (const String)atts[i+1] );
i += 2;
}
}
static void endElement(void *userData, const char *name )
{
XmlParser *parser = (XmlParser *)userData;
assert( parser->m_nodeStack->num > 0 );
if (parser->m_nodeStack->num > 0) {
stack_pop_back(parser->m_nodeStack);
}
}
static void characterData( void *userData, const char *s, int len )
{
XmlParser *parser = (XmlParser *)userData;
if (parser->m_nodeStack->num > 0) {
void *ptr = stack_back(parser->m_nodeStack);
XmlNode *node = (XmlNode*) ARR_VAL(ptr);
char *str = (char*)malloc(len + 1);
if (!str) return;
strncpy(str,s,len);
str[len] = 0;
XmlNode_setValue(node, str);
free(str);
}
}
const String XmlParser_getErrorString(struct XmlParser *parser)
{
return parser->m_errorString;
}
/* return root elem */
XmlNodeRef XmlParser_parse(XmlParser *parser, const char * xml )
{
XmlNodeRef root = NULL;
parser->m_errorString = NULL;
parser->m_nodeStack= cpo_array_create(XMLTREE_STACKSIZE, sizeof(void*));
/*expat parser*/
parser->m_parser = XML_ParserCreate(NULL);
XML_SetUserData(parser->m_parser, parser );
XML_SetElementHandler(parser->m_parser, startElement, endElement );
XML_SetCharacterDataHandler(parser->m_parser, characterData );
if (XML_Parse(parser->m_parser, xml, strlen(xml), XML_TRUE)) {
root = parser->m_root;
} else {
parser->m_errorString = (char*) XML_ErrorString(XML_GetErrorCode(parser->m_parser));
printf("XML Error: %s at line %ld\n",
XmlParser_getErrorString(parser),
XML_GetCurrentLineNumber(parser->m_parser));
}
XML_ParserFree(parser->m_parser);
cpo_array_destroy(parser->m_nodeStack);
return root;
}
XmlNodeRef XmlParser_parse_file(struct XmlParser *parser, const String fileName )
{
XmlNodeRef root = NULL;
FILE *f = fopen (fileName, "rb");
if (f) {
char * buffer;
long length;
size_t read = 0;
fseek (f, 0, SEEK_END);
length = ftell (f);
fseek (f, 0, SEEK_SET);
buffer = (char*) malloc (length + 1);
if (buffer) {
read = fread (buffer, sizeof(char), length, f);
buffer[read] = '\0';
}
fclose (f);
if (read == length) {
root = XmlParser_parse(parser, buffer);
} else {
parser->m_errorString = strerror(errno);
}
free(buffer);
} else {
parser->m_errorString = strerror(errno);
printf("error: cannot read %s \n", fileName);
}
return root;
}