txml.c

filter: Statementstmt 67.2% 823 / 1224 Branchbran 51.0% 503 / 986 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 60.0% 1326 / 2210 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« const-xs.inc ↑ Summary const-c.inc »
1 /*
2 * tinyxml.c
3 *
4 * Created by xant on 2/17/06.
5 *
6 */
7  
8 #include "txml.h"
9 #include "string.h"
10 #include "stdlib.h"
11 #include "unistd.h"
12 #include "ctype.h"
13 #ifdef USE_ICONV
14 #include "iconv.h"
15 #endif
16 #include "errno.h"
17  
18 #define XML_ELEMENT_NONE 0
19 #define XML_ELEMENT_START 1
20 #define XML_ELEMENT_VALUE 2
21 #define XML_ELEMENT_END 3
22 #define XML_ELEMENT_UNIQUE 4
23  
24 //
25 // INTERNAL HELPERS
26 //
27 enum {
28 ENCODING_UTF8,
29 ENCODING_UTF16LE,
30 ENCODING_UTF16BE,
31 ENCODING_UTF32LE,
32 ENCODING_UTF32BE,
33 ENCODING_UTF7
34 } XML_ENCODING;
35  
36 static int
37 10 detect_encoding(char *buffer) {
38 10 ▶ if (buffer[0] == (char)0xef &&
Branch: 1 branch
BranchTrueFalseTotal
if (buffer[0] == (char)0xef && 0 10 10
39 0 ▶ buffer[1] == (char)0xbb &&
Branch: 1 branch
BranchTrueFalseTotal
buffer[1] == (char)0xbb && 0 0 0
40 0 ▶ buffer[2] == (char)0xbf)
Branch: 1 branch
BranchTrueFalseTotal
buffer[2] == (char)0xbf) 0 0 0
41 {
42 0 return ENCODING_UTF8;
43 10 ▶ } else if (buffer[0] == (char)0xff &&
Branch: 1 branch
BranchTrueFalseTotal
} else if (buffer[0] == (char)0xff && 0 10 10
44 0 ▶ buffer[1] == (char)0xfe &&
Branch: 1 branch
BranchTrueFalseTotal
buffer[1] == (char)0xfe && 0 0 0
45 0 ▶ buffer[3] != (char)0x00)
Branch: 1 branch
BranchTrueFalseTotal
buffer[3] != (char)0x00) 0 0 0
46 {
47 0 return ENCODING_UTF16LE; // utf-16le
48 10 ▶ } else if (buffer[0] == (char)0xfe &&
Branch: 1 branch
BranchTrueFalseTotal
} else if (buffer[0] == (char)0xfe && 0 10 10
49 0 ▶ buffer[1] == (char)0xff)
Branch: 1 branch
BranchTrueFalseTotal
buffer[1] == (char)0xff) 0 0 0
50 {
51 0 return ENCODING_UTF16BE; // utf-16be
52 10 ▶ } else if (buffer[0] == (char)0xff &&
Branch: 1 branch
BranchTrueFalseTotal
} else if (buffer[0] == (char)0xff && 0 10 10
53 0 ▶ buffer[1] == (char)0xfe &&
Branch: 1 branch
BranchTrueFalseTotal
buffer[1] == (char)0xfe && 0 0 0
54 0 ▶ buffer[2] == (char)0x00 &&
Branch: 1 branch
BranchTrueFalseTotal
buffer[2] == (char)0x00 && 0 0 0
55 0 ▶ buffer[3] == (char)0x00)
Branch: 1 branch
BranchTrueFalseTotal
buffer[3] == (char)0x00) 0 0 0
56 {
57 0 return ENCODING_UTF32LE; //utf-32le
58 10 ▶ } else if (buffer[0] == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
} else if (buffer[0] == 0 && 0 10 10
59 0 ▶ buffer[1] == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
buffer[1] == 0 && 0 0 0
60 0 ▶ buffer[2] == (char)0xfe &&
Branch: 1 branch
BranchTrueFalseTotal
buffer[2] == (char)0xfe && 0 0 0
61 0 ▶ buffer[3] == (char)0xff)
Branch: 1 branch
BranchTrueFalseTotal
buffer[3] == (char)0xff) 0 0 0
62 {
63 0 return ENCODING_UTF32BE; //utf-32be
64 10 ▶ } else if (buffer[0] == (char)0x2b &&
Branch: 1 branch
BranchTrueFalseTotal
} else if (buffer[0] == (char)0x2b && 0 10 10
65 0 ▶ buffer[1] == (char)0x2f &&
Branch: 1 branch
BranchTrueFalseTotal
buffer[1] == (char)0x2f && 0 0 0
66 0 ▶ buffer[2] == (char)0x76)
Branch: 1 branch
BranchTrueFalseTotal
buffer[2] == (char)0x76) 0 0 0
67 {
68 0 return ENCODING_UTF7;
69 }
70 10 return -1;
71 }
72  
73 int errno;
74  
75 static char *
76 202 dexmlize(char *string)
77 {
78 202 int i, p = 0;
79 202 int len = strlen(string);
80 202 char *unescaped = NULL;
81  
82 202 ▶ if (string) {
Branch: 1 branch
BranchTrueFalseTotal
if (string) { 202 0 202
83 202 unescaped = (char *)calloc(1, len+1); // inlude null-byte
84 1964 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 1762 202 1964
85 1762 ▶ switch (string[i]) {
Branch: 1 branch
BranchTrueFalseTotal
switch (string[i]) { 24 1738 1762
86 24 case '&':
87 24 ▶ if (string[i+1] == '#') {
Branch: 1 branch
BranchTrueFalseTotal
if (string[i+1] == '#') { 4 20 24
88 char *marker;
89 4 i+=2;
90 4 marker = &string[i];
91 4 ▶ if (string[i] >= '0' && string[i] <= '9' &&
Branch: 2 branches
BranchTrueFalseTotal
if (string[i] >= '0' && string[i] <= '9' && 4 0 4
if (string[i] >= '0' && string[i] <= '9' && 4 0 4
92 4 ▶ string[i+1] >= '0' && string[i+1] <= '9')
Branch: 2 branches
BranchTrueFalseTotal
string[i+1] >= '0' && string[i+1] <= '9') 4 0 4
string[i+1] >= '0' && string[i+1] <= '9') 4 0 4
93 {
94 4 char chr = 0;
95 4 i+=2;
96 4 ▶ if (string[i] >= '0' && string[i] <= '9' && string[i+1] == ';')
Branch: 3 branches
BranchTrueFalseTotal
if (string[i] >= '0' && string[i] <= '9' && string[i+1] == ';') 4 0 4
if (string[i] >= '0' && string[i] <= '9' && string[i+1] == ';') 2 2 4
if (string[i] >= '0' && string[i] <= '9' && string[i+1] == ';') 2 0 2
97 2 i++;
98 2 ▶ else if (string[i] == ';')
Branch: 1 branch
BranchTrueFalseTotal
else if (string[i] == ';') 0 2 2
99 ; // do nothing
100 else
101 0 return NULL;
102 4 chr = (char)strtol(marker, NULL, 0);
103 4 unescaped[p] = chr;
104 }
105 20 ▶ } else if (strncmp(&string[i], "&amp;", 5) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strncmp(&string[i], "&amp;", 5) == 0) { 1 19 20
106 1 i+=4;
107 1 unescaped[p] = '&';
108 19 ▶ } else if (strncmp(&string[i], "&lt;", 4) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strncmp(&string[i], "&lt;", 4) == 0) { 1 18 19
109 1 i+=3;
110 1 unescaped[p] = '<';
111 18 ▶ } else if (strncmp(&string[i], "&gt;", 4) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strncmp(&string[i], "&gt;", 4) == 0) { 1 17 18
112 1 i+=3;
113 1 unescaped[p] = '>';
114 17 ▶ } else if (strncmp(&string[i], "&quot;", 6) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strncmp(&string[i], "&quot;", 6) == 0) { 17 0 17
115 17 i+=5;
116 17 unescaped[p] = '"';
117 0 ▶ } else if (strncmp(&string[i], "&apos;", 6) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strncmp(&string[i], "&apos;", 6) == 0) { 0 0 0
118 0 i+=5;
119 0 unescaped[p] = '\'';
120 } else {
121 0 return NULL;
122 }
123 24 p++;
124 24 break;
125 1738 default:
126 1738 unescaped[p] = string[i];
127 1738 p++;
128 }
129 }
130 }
131 202 return unescaped;
132 }
133  
134 static char *
135 37 xmlize(char *string)
136 {
137 37 int i, p = 0;
138 int len;
139 int bufsize;
140 37 char *escaped = NULL;
141  
142 37 len = strlen(string);
143 37 ▶ if (string) {
Branch: 1 branch
BranchTrueFalseTotal
if (string) { 37 0 37
144 37 bufsize = len+1;
145 37 escaped = (char *)calloc(1, bufsize); // inlude null-byte
146 204 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 167 37 204
147 167 switch (string[i]) {
148 1 case '&':
149 1 bufsize += 5;
150 1 escaped = realloc(escaped, bufsize);
151 1 memset(escaped+p, 0, bufsize-p);
152 1 strcpy(&escaped[p], "&amp;");
153 1 p += 5;
154 1 break;
155 1 case '<':
156 1 bufsize += 4;
157 1 escaped = realloc(escaped, bufsize);
158 1 memset(escaped+p, 0, bufsize-p);
159 1 strcpy(&escaped[p], "&lt;");
160 1 p += 4;
161 1 break;
162 1 case '>':
163 1 bufsize += 4;
164 1 escaped = realloc(escaped, bufsize);
165 1 memset(escaped+p, 0, bufsize-p);
166 1 strcpy(&escaped[p], "&gt;");
167 1 p += 4;
168 1 break;
169 4 case '"':
170 4 bufsize += 6;
171 4 escaped = realloc(escaped, bufsize);
172 4 memset(escaped+p, 0, bufsize-p);
173 4 strcpy(&escaped[p], "&quot;");
174 4 p += 6;
175 4 break;
176 2 case '\'':
177 2 bufsize += 6;
178 2 escaped = realloc(escaped, bufsize);
179 2 memset(escaped+p, 0, bufsize-p);
180 2 strcpy(&escaped[p], "&apos;");
181 2 p += 6;
182 2 break;
183 /*
184 bufsize += 5;
185 escaped = realloc(escaped, bufsize);
186 memset(escaped+p, 0, bufsize-p);
187 sprintf(&escaped[p], "&#%02d;", string[i]);
188 p += 5;
189 break;
190 */
191 158 default:
192 158 escaped[p] = string[i];
193 158 p++;
194 }
195 }
196 }
197 37 return escaped;
198 }
199  
200 // reimplementing strcasestr since it's not present on all systems
201 // and we still need to be portable.
202 67 static char *txml_strcasestr (char *h, char *n)
203 {
204 67 char *hp, *np = n, *match = 0;
205 67 ▶ if(!*np) {
Branch: 1 branch
BranchTrueFalseTotal
if(!*np) { 0 67 67
206 0 return hp;
207 }
208  
209 343 ▶ for (hp = h; *hp; hp++) {
Branch: 1 branch
BranchTrueFalseTotal
for (hp = h; *hp; hp++) { 319 24 343
210 319 ▶ if (toupper(*hp) == toupper(*np)) {
Branch: 1 branch
BranchTrueFalseTotal
if (toupper(*hp) == toupper(*np)) { 215 104 319
211 215 ▶ if (!match) {
Branch: 1 branch
BranchTrueFalseTotal
if (!match) { 43 172 215
212 43 match = hp;
213 }
214 215 ▶ if(!*++np) {
Branch: 1 branch
BranchTrueFalseTotal
if(!*++np) { 43 172 215
215 43 return match;
216 }
217 } else {
218 104 ▶ if (match) {
Branch: 1 branch
BranchTrueFalseTotal
if (match) { 0 104 104
219 0 match = 0;
220 0 np = n;
221 }
222 }
223 }
224  
225 24 return NULL;
226 }
227  
228 //
229 // TXML IMPLEMENTATION
230 //
231  
232 TXml *
233 12 XmlCreateContext()
234 {
235 TXml *xml;
236  
237 12 xml = (TXml *)calloc(1, sizeof(TXml));
238 12 xml->cNode = NULL;
239 12 xml->ignoreWhiteSpaces = 1; // defaults to old behaviour (all blanks are not taken into account)
240 12 xml->ignoreBlanks = 1; // defaults to old behaviour (all blanks are not taken into account)
241 12 TAILQ_INIT(&xml->rootElements);
242 12 xml->head = NULL;
243 // default is UTF-8
244 12 sprintf(xml->outputEncoding, "utf-8");
245 12 sprintf(xml->documentEncoding, "utf-8");
246 12 return xml;
247 }
248  
249 void
250 22 XmlResetContext(TXml *xml)
251 {
252 XmlNode *rNode, *tmp;
253 35 ▶ TAILQ_FOREACH_SAFE(rNode, &xml->rootElements, siblings, tmp) {
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_FOREACH_SAFE(rNode, &xml->rootElements, siblings, tmp) { 13 22 35
254 13 ▶ TAILQ_REMOVE(&xml->rootElements, rNode, siblings);
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_REMOVE(&xml->rootElements, rNode, siblings); 0 13 13
255 13 XmlDestroyNode(rNode);
256 }
257 22 ▶ if(xml->head)
Branch: 1 branch
BranchTrueFalseTotal
if(xml->head) 10 12 22
258 10 free(xml->head);
259 22 xml->head = NULL;
260 22 }
261  
262 TXml *
263 0 XmlGetContext(XmlNode *node)
264 {
265 0 XmlNode *p = node;
266 do {
267 0 ▶ if (!p->parent)
Branch: 1 branch
BranchTrueFalseTotal
if (!p->parent) 0 0 0
268 0 return p->context;
269 0 p = p->parent;
270 0 ▶ } while (p);
Branch: 1 branch
BranchTrueFalseTotal
} while (p); 0 0 0
271 0 return NULL; // should never arrive here
272 }
273  
274 void
275 0 XmlSetDocumentEncoding(TXml *xml, char *encoding)
276 {
277 0 strncpy(xml->documentEncoding, encoding, sizeof(xml->documentEncoding)-1);
278 0 }
279  
280 void
281 0 XmlSetOutputEncoding(TXml *xml, char *encoding)
282 {
283 0 strncpy(xml->outputEncoding, encoding, sizeof(xml->outputEncoding)-1);
284 0 }
285  
286 void
287 11 XmlDestroyContext(TXml *xml)
288 {
289 11 XmlResetContext(xml);
290 11 free(xml);
291 11 }
292  
293 static void
294 375 XmlSetNodePath(XmlNode *node, XmlNode *parent)
295 {
296 unsigned int pathLen;
297  
298 375 ▶ if (node->path)
Branch: 1 branch
BranchTrueFalseTotal
if (node->path) 234 141 375
299 234 free(node->path);
300  
301 375 ▶ if(parent) {
Branch: 1 branch
BranchTrueFalseTotal
if(parent) { 232 143 375
302 232 ▶ if(parent->path) {
Branch: 1 branch
BranchTrueFalseTotal
if(parent->path) { 232 0 232
303 232 pathLen = (unsigned int)strlen(parent->path)+1+strlen(node->name)+1;
304 232 node->path = (char *)calloc(1, pathLen);
305 232 sprintf(node->path, "%s/%s", parent->path, node->name);
306 } else {
307 0 pathLen = (unsigned int)strlen(parent->name)+1+strlen(node->name)+1;
308 0 node->path = (char *)calloc(1, pathLen);
309 0 sprintf(node->path, "%s/%s", parent->name, node->name);
310 }
311 } else { /* root node */
312 143 node->path = (char *)calloc(1, strlen(node->name)+2);
313 143 sprintf(node->path, "/%s", node->name);
314 }
315  
316 375 }
317  
318 XmlNode *
319 141 XmlCreateNode(char *name, char *value, XmlNode *parent)
320 {
321 141 XmlNode *node = NULL;
322 141 node = (XmlNode *)calloc(1, sizeof(XmlNode));
323 141 ▶ if(!node || !name)
Branch: 2 branches
BranchTrueFalseTotal
if(!node || !name) 141 0 141
if(!node || !name) 0 141 141
324 0 return NULL;
325  
326 141 TAILQ_INIT(&node->attributes);
327 141 TAILQ_INIT(&node->children);
328 141 TAILQ_INIT(&node->namespaces);
329 141 TAILQ_INIT(&node->knownNamespaces);
330  
331 141 node->name = strdup(name);
332  
333 141 ▶ if (parent)
Branch: 1 branch
BranchTrueFalseTotal
if (parent) 105 36 141
334 105 XmlAddChildNode(parent, node);
335 else
336 36 XmlSetNodePath(node, NULL);
337  
338 141 ▶ if(value && strlen(value) > 0)
Branch: 2 branches
BranchTrueFalseTotal
if(value && strlen(value) > 0) 42 99 141
if(value && strlen(value) > 0) 32 10 42
339 32 node->value = strdup(value);
340 else
341 109 node->value = (char *)calloc(1, 1);
342 141 return node;
343 }
344  
345 void
346 139 XmlDestroyNode(XmlNode *node)
347 {
348 XmlNodeAttribute *attr, *attrTmp;
349 XmlNode *child, *childTmp;
350 XmlNamespace *ns, *nsTmp;
351 XmlNamespaceSet *item, *itemTmp;
352  
353 209 ▶ TAILQ_FOREACH_SAFE(attr, &node->attributes, list, attrTmp) {
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_FOREACH_SAFE(attr, &node->attributes, list, attrTmp) { 70 139 209
354 70 ▶ TAILQ_REMOVE(&node->attributes, attr, list);
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_REMOVE(&node->attributes, attr, list); 22 48 70
355 70 ▶ if(attr->name)
Branch: 1 branch
BranchTrueFalseTotal
if(attr->name) 70 0 70
356 70 free(attr->name);
357 70 ▶ if(attr->value)
Branch: 1 branch
BranchTrueFalseTotal
if(attr->value) 70 0 70
358 70 free(attr->value);
359 70 free(attr);
360 }
361  
362 264 ▶ TAILQ_FOREACH_SAFE(child, &node->children, siblings, childTmp) {
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_FOREACH_SAFE(child, &node->children, siblings, childTmp) { 125 139 264
363 125 ▶ TAILQ_REMOVE(&node->children, child, siblings);
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_REMOVE(&node->children, child, siblings); 79 46 125
364 125 XmlDestroyNode(child);
365 }
366  
367 442 ▶ TAILQ_FOREACH_SAFE(item, &node->knownNamespaces, next, itemTmp) {
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_FOREACH_SAFE(item, &node->knownNamespaces, next, itemTmp) { 303 139 442
368 303 ▶ TAILQ_REMOVE(&node->knownNamespaces, item, next);
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_REMOVE(&node->knownNamespaces, item, next); 188 115 303
369 303 free(item);
370 }
371  
372 183 ▶ TAILQ_FOREACH_SAFE(ns, &node->namespaces, list, nsTmp) {
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_FOREACH_SAFE(ns, &node->namespaces, list, nsTmp) { 44 139 183
373 44 ▶ TAILQ_REMOVE(&node->namespaces, ns, list);
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_REMOVE(&node->namespaces, ns, list); 21 23 44
374 44 XmlDestroyNamespace(ns);
375 }
376  
377 139 ▶ if(node->name)
Branch: 1 branch
BranchTrueFalseTotal
if(node->name) 139 0 139
378 139 free(node->name);
379 139 ▶ if(node->path)
Branch: 1 branch
BranchTrueFalseTotal
if(node->path) 139 0 139
380 139 free(node->path);
381 139 ▶ if(node->value)
Branch: 1 branch
BranchTrueFalseTotal
if(node->value) 139 0 139
382 139 free(node->value);
383 139 free(node);
384 139 }
385  
386 XmlErr
387 36 XmlSetNodeValue(XmlNode *node, char *val)
388 {
389 36 ▶ if(!val)
Branch: 1 branch
BranchTrueFalseTotal
if(!val) 0 36 36
390 0 return XML_BADARGS;
391  
392 36 ▶ if(node->value)
Branch: 1 branch
BranchTrueFalseTotal
if(node->value) 36 0 36
393 36 free(node->value);
394 36 node->value = strdup(val);
395 36 return XML_NOERR;
396 }
397  
398 /* quite useless */
399 char *
400 0 XmlGetNodeValue(XmlNode *node)
401 {
402 0 ▶ if(!node)
Branch: 1 branch
BranchTrueFalseTotal
if(!node) 0 0 0
403 0 return NULL;
404 0 return node->value;
405 }
406  
407 static void
408 107 XmlRemoveChildNode(XmlNode *parent, XmlNode *child)
409 {
410 int i;
411 XmlNode *p, *tmp;
412 294 ▶ TAILQ_FOREACH_SAFE(p, &parent->children, siblings, tmp) {
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_FOREACH_SAFE(p, &parent->children, siblings, tmp) { 294 0 294
413 294 ▶ if (p == child) {
Branch: 1 branch
BranchTrueFalseTotal
if (p == child) { 107 187 294
414 107 ▶ TAILQ_REMOVE(&parent->children, p, siblings);
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_REMOVE(&parent->children, p, siblings); 2 105 107
415 107 p->parent = NULL;
416 107 XmlSetNodePath(p, NULL);
417 107 break;
418 }
419 }
420 107 }
421  
422 static void
423 249 XmlUpdateKnownNamespaces(XmlNode *node)
424 {
425 XmlNode *p;
426 XmlNamespace *ns;
427 XmlNamespaceSet *newItem;
428
429 // first empty actual list
430 249 ▶ if (!TAILQ_EMPTY(&node->knownNamespaces)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!TAILQ_EMPTY(&node->knownNamespaces)) { 110 139 249
431 XmlNamespaceSet *oldItem;
432 388 ▶ while((oldItem = TAILQ_FIRST(&node->knownNamespaces))) {
Branch: 1 branch
BranchTrueFalseTotal
while((oldItem = TAILQ_FIRST(&node->knownNamespaces))) { 278 110 388
433 278 ▶ TAILQ_REMOVE(&node->knownNamespaces, oldItem, next);
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_REMOVE(&node->knownNamespaces, oldItem, next); 168 110 278
434 278 free(oldItem);
435 }
436 }
437  
438 // than start populating the list with actual default namespace
439 249 ▶ if (node->cns) {
Branch: 1 branch
BranchTrueFalseTotal
if (node->cns) { 22 227 249
440 22 newItem = (XmlNamespaceSet *)calloc(1, sizeof(XmlNamespaceSet));
441 22 newItem->ns = node->cns;
442 22 TAILQ_INSERT_TAIL(&node->knownNamespaces, newItem, next);
443 227 ▶ } else if (node->hns) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (node->hns) { 174 53 227
444 174 newItem = (XmlNamespaceSet *)calloc(1, sizeof(XmlNamespaceSet));
445 174 newItem->ns = node->hns;
446 174 TAILQ_INSERT_TAIL(&node->knownNamespaces, newItem, next);
447 }
448  
449 // add all namespaces defined by this node
450 294 ▶ TAILQ_FOREACH(ns, &node->namespaces, list) {
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_FOREACH(ns, &node->namespaces, list) { 45 249 294
451 45 ▶ if (ns->name) { // skip an eventual default namespace since has been handled earlier
Branch: 1 branch
BranchTrueFalseTotal
if (ns->name) { // skip an eventual default namespace since has been handled earlier 23 22 45
452 23 newItem = (XmlNamespaceSet *)calloc(1, sizeof(XmlNamespaceSet));
453 23 newItem->ns = ns;
454 23 TAILQ_INSERT_TAIL(&node->knownNamespaces, newItem, next);
455 }
456 }
457  
458 // and now import namespaces already valid in the scope of our parent
459 249 ▶ if (node->parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (node->parent) { 235 14 249
460 235 ▶ if (!TAILQ_EMPTY(&node->parent->knownNamespaces)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!TAILQ_EMPTY(&node->parent->knownNamespaces)) { 215 20 235
461 XmlNamespaceSet *parentItem;
462 759 ▶ TAILQ_FOREACH(parentItem, &node->parent->knownNamespaces, next) {
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_FOREACH(parentItem, &node->parent->knownNamespaces, next) { 544 215 759
463 544 ▶ if (parentItem->ns->name) { // skip the default namespace
Branch: 1 branch
BranchTrueFalseTotal
if (parentItem->ns->name) { // skip the default namespace 361 183 544
464 361 newItem = (XmlNamespaceSet *)calloc(1, sizeof(XmlNamespaceSet));
465 361 newItem->ns = parentItem->ns;
466 361 TAILQ_INSERT_TAIL(&node->knownNamespaces, newItem, next);
467 }
468 }
469 } else { // this shouldn't happen until knownNamespaces is properly kept synchronized
470 20 ▶ TAILQ_FOREACH(ns, &node->parent->namespaces, list) {
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_FOREACH(ns, &node->parent->namespaces, list) { 0 20 20
471 0 ▶ if (ns->name) { // skip the default namespace
Branch: 1 branch
BranchTrueFalseTotal
if (ns->name) { // skip the default namespace 0 0 0
472 0 newItem = (XmlNamespaceSet *)calloc(1, sizeof(XmlNamespaceSet));
473 0 newItem->ns = ns;
474 0 TAILQ_INSERT_TAIL(&node->knownNamespaces, newItem, next);
475 }
476 }
477 }
478 }
479 249 }
480  
481 // update the hinerited namespace across a branch.
482 // This happens if a node (with all its childnodes) is moved across
483 // 2 different documents. The hinerited namespace must be updated
484 // accordingly to the new context, so we traverse the branches
485 // under the moved node to update the the hinerited namespace where
486 // necessary.
487 // NOTE: if a node defines a new default itself, it's not necessary
488 // to go deeper in that same branch
489 static void
490 235 XmlUpdateBranchNamespace(XmlNode *node, XmlNamespace *ns)
491 {
492 XmlNode *child;
493 XmlNamespaceSet *nsItem;
494  
495 235 ▶ if (node->hns != ns && !node->cns) // skip update if not necessary
Branch: 2 branches
BranchTrueFalseTotal
if (node->hns != ns && !node->cns) // skip update if not necessary 91 144 235
if (node->hns != ns && !node->cns) // skip update if not necessary 90 1 91
496 90 node->hns = ns;
497  
498 235 XmlUpdateKnownNamespaces(node);
499  
500 235 ▶ if (node->ns) { // we are bound to a specific ns.... let's see if it's known
Branch: 1 branch
BranchTrueFalseTotal
if (node->ns) { // we are bound to a specific ns.... let's see if it's known 25 210 235
501 25 int missing = 1;
502  
503 107 ▶ TAILQ_FOREACH(nsItem, &node->knownNamespaces, next)
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_FOREACH(nsItem, &node->knownNamespaces, next) 82 25 107
504 82 ▶ if (strcmp(node->ns->uri, nsItem->ns->uri) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(node->ns->uri, nsItem->ns->uri) == 0) 28 54 82
505 28 ▶ if (!(node->ns->name && !nsItem->ns->name) && strcmp(node->ns->name, nsItem->ns->name) == 0)
Branch: 3 branches
BranchTrueFalseTotal
if (!(node->ns->name && !nsItem->ns->name) && strcmp(node->ns->name, nsItem->ns->name) == 0) 28 0 28
if (!(node->ns->name && !nsItem->ns->name) && strcmp(node->ns->name, nsItem->ns->name) == 0) 27 1 28
if (!(node->ns->name && !nsItem->ns->name) && strcmp(node->ns->name, nsItem->ns->name) == 0) 24 3 27
506 24 missing = 0;
507  
508 25 ▶ if (missing) {
Branch: 1 branch
BranchTrueFalseTotal
if (missing) { 1 24 25
509 XmlNamespace *newNS;
510 XmlNamespaceSet *newItem;
511 char *newAttr;
512  
513 1 newNS = XmlAddNamespace(node, node->ns->name, node->ns->uri);
514 1 node->ns = newNS;
515 1 newItem = (XmlNamespaceSet *)calloc(1, sizeof(XmlNamespaceSet));
516 1 newItem->ns = newNS;
517 1 TAILQ_INSERT_TAIL(&node->knownNamespaces, newItem, next);
518 1 newAttr = malloc(strlen(newNS->name)+7); // prefix + xmlns + :
519 1 sprintf(newAttr, "xmlns:%s", node->ns->name);
520 // enforce the definition for our namepsace in the new context
521 1 XmlAddAttribute(node, newAttr, node->ns->uri);
522 1 free(newAttr);
523 }
524 }
525  
526 238 ▶ TAILQ_FOREACH(child, &node->children, siblings) // update our descendants
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_FOREACH(child, &node->children, siblings) // update our descendants 3 235 238
527 3 ▶ XmlUpdateBranchNamespace(child, node->cns?node->cns:node->hns); // recursion here
Branch: 1 branch
BranchTrueFalseTotal
XmlUpdateBranchNamespace(child, node->cns?node->cns:node->hns); // recursion here 3 0 3
528 235 }
529  
530 XmlErr
531 232 XmlAddChildNode(XmlNode *parent, XmlNode *child)
532 {
533 TXml *srcCtx, *dstCtx;
534 232 ▶ if(!child)
Branch: 1 branch
BranchTrueFalseTotal
if(!child) 0 232 232
535 0 return XML_BADARGS;
536  
537 // now we can update the parent
538 232 ▶ if (child->parent)
Branch: 1 branch
BranchTrueFalseTotal
if (child->parent) 107 125 232
539 107 XmlRemoveChildNode(child->parent, child);
540  
541 232 TAILQ_INSERT_TAIL(&parent->children, child, siblings);
542 232 child->parent = parent;
543  
544 // udate/propagate the default namespace (if any) to the newly attached node
545 // (and all its descendants)
546 // Also scan for unknown namespaces defined/used in the newly attached branch
547 232 ▶ XmlUpdateBranchNamespace(child, parent->cns?parent->cns:parent->hns);
Branch: 1 branch
BranchTrueFalseTotal
XmlUpdateBranchNamespace(child, parent->cns?parent->cns:parent->hns); 150 82 232
548 232 XmlSetNodePath(child, parent);
549 232 return XML_NOERR;
550 }
551  
552 XmlNode *
553 5 XmlNextSibling(XmlNode *node)
554 {
555 5 return TAILQ_NEXT(node, siblings);
556 }
557  
558 XmlNode *
559 4 XmlPrevSibling(XmlNode *node)
560 {
561 4 return TAILQ_PREV(node, nodelistHead, siblings);
562 }
563  
564 XmlErr
565 16 XmlAddRootNode(TXml *xml, XmlNode *node)
566 {
567 16 ▶ if(!node)
Branch: 1 branch
BranchTrueFalseTotal
if(!node) 0 16 16
568 0 return XML_BADARGS;
569  
570 16 ▶ if (!TAILQ_EMPTY(&xml->rootElements) && !xml->allowMultipleRootNodes) {
Branch: 2 branches
BranchTrueFalseTotal
if (!TAILQ_EMPTY(&xml->rootElements) && !xml->allowMultipleRootNodes) { 3 13 16
if (!TAILQ_EMPTY(&xml->rootElements) && !xml->allowMultipleRootNodes) { 2 1 3
571 2 return XML_MROOT_ERR;
572 }
573  
574 14 TAILQ_INSERT_TAIL(&xml->rootElements, node, siblings);
575 14 node->context = xml;
576 14 XmlUpdateKnownNamespaces(node);
577 14 return XML_NOERR;
578 }
579  
580 XmlErr
581 70 XmlAddAttribute(XmlNode *node, char *name, char *val)
582 {
583 XmlNodeAttribute *attr;
584  
585 70 ▶ if(!name || !node)
Branch: 2 branches
BranchTrueFalseTotal
if(!name || !node) 70 0 70
if(!name || !node) 0 70 70
586 0 return XML_BADARGS;
587  
588 70 attr = (XmlNodeAttribute *)calloc(1, sizeof(XmlNodeAttribute));
589 70 attr->name = strdup(name);
590 70 ▶ attr->value = val?strdup(val):strdup("");
Branch: 1 branch
BranchTrueFalseTotal
attr->value = val?strdup(val):strdup(""); 70 0 70
591 70 attr->node = node;
592  
593 70 TAILQ_INSERT_TAIL(&node->attributes, attr, list);
594 70 return XML_NOERR;
595 }
596  
597 int
598 0 XmlRemoveAttribute(XmlNode *node, unsigned long index)
599 {
600 XmlNodeAttribute *attr, *tmp;
601 0 int count = 0;
602  
603 0 ▶ TAILQ_FOREACH_SAFE(attr, &node->attributes, list, tmp) {
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_FOREACH_SAFE(attr, &node->attributes, list, tmp) { 0 0 0
604 0 ▶ if (count++ == index) {
Branch: 1 branch
BranchTrueFalseTotal
if (count++ == index) { 0 0 0
605 0 ▶ TAILQ_REMOVE(&node->attributes, attr, list);
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_REMOVE(&node->attributes, attr, list); 0 0 0
606 0 free(attr->name);
607 0 free(attr->value);
608 0 free(attr);
609 0 return XML_NOERR;
610 }
611 }
612 0 return XML_GENERIC_ERR;
613 }
614  
615 void
616 0 XmlClearAttributes(XmlNode *node)
617 {
618 XmlNodeAttribute *attr, *tmp;
619 0 unsigned int nAttrs = 0;
620 int i;
621  
622 0 ▶ TAILQ_FOREACH_SAFE(attr, &node->attributes, list, tmp) {
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_FOREACH_SAFE(attr, &node->attributes, list, tmp) { 0 0 0
623 0 ▶ TAILQ_REMOVE(&node->attributes, attr, list);
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_REMOVE(&node->attributes, attr, list); 0 0 0
624 0 free(attr->name);
625 0 free(attr->value);
626 0 free(attr);
627 }
628 0 }
629  
630 XmlNodeAttribute
631 0 *XmlGetAttributeByName(XmlNode *node, char *name)
632 {
633 int i;
634 XmlNodeAttribute *attr;
635 0 ▶ TAILQ_FOREACH(attr, &node->attributes, list) {
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_FOREACH(attr, &node->attributes, list) { 0 0 0
636 0 ▶ if (strcmp(attr->name, name) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(attr->name, name) == 0) 0 0 0
637 0 return attr;
638 }
639 0 return NULL;
640 }
641  
642 XmlNodeAttribute
643 42 *XmlGetAttribute(XmlNode *node, unsigned long index)
644 {
645 XmlNodeAttribute *attr;
646 42 int count = 0;
647 52 ▶ TAILQ_FOREACH(attr, &node->attributes, list) {
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_FOREACH(attr, &node->attributes, list) { 52 0 52
648 52 ▶ if (count++ == index)
Branch: 1 branch
BranchTrueFalseTotal
if (count++ == index) 42 10 52
649 42 return attr;
650 }
651 0 return NULL;
652 }
653  
654 static XmlErr
655 17 XmlExtraNodeHandler(TXml *xml, char *content, char type)
656 {
657 17 XmlNode *newNode = NULL;
658 17 XmlErr res = XML_NOERR;
659 char fakeName[256];
660  
661 17 sprintf(fakeName, "_fakenode_%d_", type);
662 17 newNode = XmlCreateNode(fakeName, content, xml->cNode);
663 17 newNode->type = type;
664 17 ▶ if(!newNode || !newNode->name) {
Branch: 2 branches
BranchTrueFalseTotal
if(!newNode || !newNode->name) { 17 0 17
if(!newNode || !newNode->name) { 0 17 17
665 /* XXX - ERROR MESSAGES HERE */
666 0 res = XML_GENERIC_ERR;
667 0 goto _node_done;
668 }
669 17 ▶ if(xml->cNode) {
Branch: 1 branch
BranchTrueFalseTotal
if(xml->cNode) { 17 0 17
670 17 res = XmlAddChildNode(xml->cNode, newNode);
671 17 ▶ if(res != XML_NOERR) {
Branch: 1 branch
BranchTrueFalseTotal
if(res != XML_NOERR) { 0 17 17
672 0 XmlDestroyNode(newNode);
673 0 goto _node_done;
674 }
675 } else {
676 0 res = XmlAddRootNode(xml, newNode) ;
677 0 ▶ if(res != XML_NOERR) {
Branch: 1 branch
BranchTrueFalseTotal
if(res != XML_NOERR) { 0 0 0
678 0 XmlDestroyNode(newNode);
679 0 goto _node_done;
680 }
681 }
682 0 _node_done:
683 17 return res;
684 }
685  
686 static XmlErr
687 99 XmlStartHandler(TXml *xml, char *element, char **attr_names, char **attr_values)
688 {
689 99 XmlNode *newNode = NULL;
690 99 unsigned int offset = 0;
691 99 XmlErr res = XML_NOERR;
692 99 char *nodename = NULL;
693 99 char *nssep = NULL;
694 99 char *cnsUri = NULL;
695 99 XmlNamespace *cns = NULL;
696  
697 99 ▶ if(!element || strlen(element) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if(!element || strlen(element) == 0) 99 0 99
if(!element || strlen(element) == 0) 0 99 99
698 0 return XML_BADARGS;
699  
700 // unescape read element to be used as nodename
701 99 nodename = dexmlize(element);
702 99 ▶ if (!nodename)
Branch: 1 branch
BranchTrueFalseTotal
if (!nodename) 0 99 99
703 0 return XML_BAD_CHARS;
704  
705 99 ▶ if ((nssep = strchr(nodename, ':'))) { // a namespace is defined
Branch: 1 branch
BranchTrueFalseTotal
if ((nssep = strchr(nodename, ':'))) { // a namespace is defined 23 76 99
706 23 XmlNamespace *ns = NULL;
707 23 *nssep = 0; // nodename now starts with the null-terminated namespace
708 // followed by the real name (nssep + 1)
709 23 newNode = XmlCreateNode(nssep+1, NULL, xml->cNode);
710 23 ▶ if (xml->cNode)
Branch: 1 branch
BranchTrueFalseTotal
if (xml->cNode) 23 0 23
711 23 ns = XmlGetNamespaceByName(xml->cNode, nodename);
712 if (!ns) {
713 // TODO - Error condition
714 }
715 23 newNode->ns = ns;
716 } else {
717 76 newNode = XmlCreateNode(nodename, NULL, xml->cNode);
718 }
719 99 free(nodename);
720 99 ▶ if(!newNode || !newNode->name) {
Branch: 2 branches
BranchTrueFalseTotal
if(!newNode || !newNode->name) { 99 0 99
if(!newNode || !newNode->name) { 0 99 99
721 /* XXX - ERROR MESSAGES HERE */
722 0 return XML_MEMORY_ERR;
723 }
724 /* handle attributes if present */
725 99 ▶ if(attr_names && attr_values) {
Branch: 2 branches
BranchTrueFalseTotal
if(attr_names && attr_values) { 47 52 99
if(attr_names && attr_values) { 47 0 47
726 114 ▶ while(attr_names[offset] != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while(attr_names[offset] != NULL) { 67 47 114
727 67 char *nsp = NULL;
728 67 res = XmlAddAttribute(newNode, attr_names[offset], attr_values[offset]);
729 67 ▶ if(res != XML_NOERR) {
Branch: 1 branch
BranchTrueFalseTotal
if(res != XML_NOERR) { 0 67 67
730 0 XmlDestroyNode(newNode);
731 0 goto _start_done;
732 }
733 67 ▶ if ((nsp = txml_strcasestr(attr_names[offset], "xmlns"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((nsp = txml_strcasestr(attr_names[offset], "xmlns"))) { 43 24 67
734 43 ▶ if ((nssep = strchr(nsp, ':'))) { // declaration of a new namespace
Branch: 1 branch
BranchTrueFalseTotal
if ((nssep = strchr(nsp, ':'))) { // declaration of a new namespace 22 21 43
735 22 *nssep = 0;
736 22 XmlAddNamespace(newNode, nssep+1, attr_values[offset]);
737 } else { // definition of the default ns
738 21 newNode->cns = XmlAddNamespace(newNode, NULL, attr_values[offset]);
739 }
740 }
741 67 offset++;
742 }
743 }
744 99 ▶ if(xml->cNode) {
Branch: 1 branch
BranchTrueFalseTotal
if(xml->cNode) { 88 11 99
745 88 res = XmlAddChildNode(xml->cNode, newNode);
746 88 ▶ if(res != XML_NOERR) {
Branch: 1 branch
BranchTrueFalseTotal
if(res != XML_NOERR) { 0 88 88
747 0 XmlDestroyNode(newNode);
748 0 goto _start_done;
749 }
750 } else {
751 11 res = XmlAddRootNode(xml, newNode) ;
752 11 ▶ if(res != XML_NOERR) {
Branch: 1 branch
BranchTrueFalseTotal
if(res != XML_NOERR) { 0 11 11
753 0 XmlDestroyNode(newNode);
754 0 goto _start_done;
755 }
756 }
757 99 xml->cNode = newNode;
758  
759 99 _start_done:
760 99 return res;
761 }
762  
763 static XmlErr
764 99 XmlEndHandler(TXml *xml, char *element)
765 {
766 XmlNode *parent;
767 99 ▶ if(xml->cNode) {
Branch: 1 branch
BranchTrueFalseTotal
if(xml->cNode) { 99 0 99
768 99 parent = xml->cNode->parent;
769 99 xml->cNode = parent;
770 99 return XML_NOERR;
771 }
772 0 return XML_GENERIC_ERR;
773 }
774  
775 static XmlErr
776 36 XmlValueHandler(TXml *xml, char *text)
777 {
778 char *p;
779 36 ▶ if(text) {
Branch: 1 branch
BranchTrueFalseTotal
if(text) { 36 0 36
780 // remove heading blanks
781 36 ▶ if (xml->ignoreWhiteSpaces) { // first check if we want to ignore any kind of whitespace between nodes
Branch: 1 branch
BranchTrueFalseTotal
if (xml->ignoreWhiteSpaces) { // first check if we want to ignore any kind of whitespace between nodes 30 6 36
782 // (which means : 'no whitespace-only values' and 'any value will be trimmed')
783 30 ▶ while((*text == ' ' || *text == '\t' || *text == '\r' || *text == '\n') &&
Branch: 4 branches
BranchTrueFalseTotal
while((*text == ' ' || *text == '\t' || *text == '\r' || *text == '\n') && 30 0 30
while((*text == ' ' || *text == '\t' || *text == '\r' || *text == '\n') && 30 0 30
while((*text == ' ' || *text == '\t' || *text == '\r' || *text == '\n') && 30 0 30
while((*text == ' ' || *text == '\t' || *text == '\r' || *text == '\n') && 0 30 30
784 0 ▶ *text != 0)
Branch: 1 branch
BranchTrueFalseTotal
*text != 0) 0 0 0
785 {
786 0 text++;
787 }
788 6 ▶ } else if (xml->ignoreBlanks) { // or if perhaps we want to consider pure whitespaces: ' '
Branch: 1 branch
BranchTrueFalseTotal
} else if (xml->ignoreBlanks) { // or if perhaps we want to consider pure whitespaces: ' ' 3 3 6
789 // as part of the value. (but we still want to skip newlines and
790 // tabs, which are assumed to be there to prettify the text layout
791 3 ▶ while((*text == '\t' || *text == '\r' || *text == '\n') &&
Branch: 3 branches
BranchTrueFalseTotal
while((*text == '\t' || *text == '\r' || *text == '\n') && 3 0 3
while((*text == '\t' || *text == '\r' || *text == '\n') && 3 0 3
while((*text == '\t' || *text == '\r' || *text == '\n') && 0 3 3
792 0 ▶ *text != 0)
Branch: 1 branch
BranchTrueFalseTotal
*text != 0) 0 0 0
793 {
794 0 text++;
795 }
796 }
797  
798 36 p = text+strlen(text)-1;
799  
800 // remove trailing blanks
801 36 ▶ if (xml->ignoreWhiteSpaces) { // XXX - read above
Branch: 1 branch
BranchTrueFalseTotal
if (xml->ignoreWhiteSpaces) { // XXX - read above 30 6 36
802 32 ▶ while((*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n') &&
Branch: 5 branches
BranchTrueFalseTotal
while((*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n') && 30 2 32
while((*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n') && 30 0 30
while((*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n') && 30 0 30
while((*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n') && 0 30 30
while((*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n') && 2 0 2
803 p != text)
804 {
805 2 *p = 0;
806 2 p--;
807 }
808 6 ▶ } else if (xml->ignoreBlanks) { // XXX = read above
Branch: 1 branch
BranchTrueFalseTotal
} else if (xml->ignoreBlanks) { // XXX = read above 3 3 6
809 3 ▶ while((*p == '\t' || *p == '\r' || *p == '\n') &&
Branch: 4 branches
BranchTrueFalseTotal
while((*p == '\t' || *p == '\r' || *p == '\n') && 3 0 3
while((*p == '\t' || *p == '\r' || *p == '\n') && 3 0 3
while((*p == '\t' || *p == '\r' || *p == '\n') && 0 3 3
while((*p == '\t' || *p == '\r' || *p == '\n') && 0 0 0
810 p != text)
811 {
812 0 *p = 0;
813 0 p--;
814 }
815 }
816  
817 36 ▶ if(xml->cNode) {
Branch: 1 branch
BranchTrueFalseTotal
if(xml->cNode) { 36 0 36
818 36 char *rtext = dexmlize(text);
819 36 ▶ if (!rtext)
Branch: 1 branch
BranchTrueFalseTotal
if (!rtext) 0 36 36
820 0 return XML_BAD_CHARS;
821 36 XmlSetNodeValue(xml->cNode, rtext);
822 36 free(rtext);
823 } else {
824 0 fprintf(stderr, "cTag == NULL while handling a value!!");
825 }
826 36 return XML_NOERR;
827 }
828 0 return XML_GENERIC_ERR;
829 }
830  
831  
832 XmlErr
833 11 XmlParseBuffer(TXml *xml, char *buf)
834 {
835 11 XmlErr err = XML_NOERR;
836 11 int state = XML_ELEMENT_NONE;
837 11 char *p = buf;
838 unsigned int i;
839 11 char *start = NULL;
840 11 char *end = NULL;
841 11 char **attrs = NULL;
842 11 char **values = NULL;
843 11 unsigned int nAttrs = 0;
844 11 char *mark = NULL;
845 11 int quote = 0;
846  
847 11 XmlResetContext(xml); // reset the context if we are parsing a new document
848  
849 //unsigned int offset = fileStat.st_size;
850  
851 #define XML_FREE_ATTRIBUTES \
852 if(nAttrs>0) {\
853 for(i = 0; i < nAttrs; i++) {\
854 if(attrs[i]) \
855 free(attrs[i]);\
856 if(values[i]) \
857 free(values[i]);\
858 }\
859 free(attrs);\
860 attrs = NULL;\
861 free(values);\
862 values = NULL;\
863 nAttrs = 0;\
864 }\
865  
866 // skip tabs and new-lines
867 #define SKIP_BLANKS(__p) \
868 while((*__p == '\t' || *__p == '\r' || *__p == '\n') && *__p != 0) __p++;
869  
870 // skip any kind of whitespace
871 #define SKIP_WHITESPACES(__p) \
872 SKIP_BLANKS(__p); \
873 while(*__p == ' ') {\
874 __p++;\
875 SKIP_BLANKS(__p);\
876 if(*__p == 0) break;\
877 }
878  
879 #define ADVANCE_ELEMENT(__p) \
880 while(*__p != '>' && *__p != ' ' && *__p != '\t' && *__p != '\r' && *__p != '\n' && *__p != 0) __p++;
881  
882 #define ADVANCE_TO_ATTR_VALUE(__p) \
883 while(*__p != '=' && *__p != ' ' && *__p != '\t' && *__p != '\r' && *__p != '\n' && *__p != 0) __p++;\
884 SKIP_WHITESPACES(__p);
885  
886 270 ▶ while(*p != 0) {
Branch: 1 branch
BranchTrueFalseTotal
while(*p != 0) { 259 11 270
887 259 ▶ if (xml->ignoreWhiteSpaces) {
Branch: 1 branch
BranchTrueFalseTotal
if (xml->ignoreWhiteSpaces) { 216 43 259
888 507 ▶ SKIP_WHITESPACES(p);
Branch: 10 branches
BranchTrueFalseTotal
SKIP_WHITESPACES(p); 348 141 489
SKIP_WHITESPACES(p); 348 0 348
SKIP_WHITESPACES(p); 132 216 348
SKIP_WHITESPACES(p); 273 0 273
SKIP_WHITESPACES(p); 13 5 18
SKIP_WHITESPACES(p); 13 0 13
SKIP_WHITESPACES(p); 6 7 13
SKIP_WHITESPACES(p); 11 0 11
SKIP_WHITESPACES(p); 0 7 7
SKIP_WHITESPACES(p); 7 216 223
889 43 ▶ } else if (xml->ignoreBlanks) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (xml->ignoreBlanks) { 21 22 43
890 23 ▶ SKIP_BLANKS(p);
Branch: 4 branches
BranchTrueFalseTotal
SKIP_BLANKS(p); 23 0 23
SKIP_BLANKS(p); 23 0 23
SKIP_BLANKS(p); 2 21 23
SKIP_BLANKS(p); 2 0 2
891 }
892 259 ▶ if(*p == '<') { // an xml entity starts here
Branch: 1 branch
BranchTrueFalseTotal
if(*p == '<') { // an xml entity starts here 199 60 259
893 199 p++;
894 199 ▶ if(*p == '/') { // check if this is a closing node
Branch: 1 branch
BranchTrueFalseTotal
if(*p == '/') { // check if this is a closing node 73 126 199
895 73 p++;
896 73 ▶ SKIP_WHITESPACES(p);
Branch: 10 branches
BranchTrueFalseTotal
SKIP_WHITESPACES(p); 73 0 73
SKIP_WHITESPACES(p); 73 0 73
SKIP_WHITESPACES(p); 0 73 73
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 73 73
897 73 mark = p;
898 413 ▶ while(*p != '>' && *p != 0)
Branch: 2 branches
BranchTrueFalseTotal
while(*p != '>' && *p != 0) 340 73 413
while(*p != '>' && *p != 0) 340 0 340
899 340 p++;
900 73 ▶ if(*p == '>') {
Branch: 1 branch
BranchTrueFalseTotal
if(*p == '>') { 73 0 73
901 73 end = (char *)malloc(p-mark+1);
902 73 ▶ if(!end) {
Branch: 1 branch
BranchTrueFalseTotal
if(!end) { 0 73 73
903 0 err = XML_MEMORY_ERR;
904 0 goto _parser_err;
905 }
906 73 strncpy(end, mark, p-mark);
907 73 end[p-mark] = 0;
908 73 p++;
909 73 state = XML_ELEMENT_END;
910 73 err = XmlEndHandler(xml, end);
911 73 free(end);
912 73 ▶ if(err != XML_NOERR)
Branch: 1 branch
BranchTrueFalseTotal
if(err != XML_NOERR) 0 73 73
913 0 goto _parser_err;
914 }
915 126 ▶ } else if(strncmp(p, "!ENTITY", 8) == 0) { // XXX - IGNORING !ENTITY NODES
Branch: 1 branch
BranchTrueFalseTotal
} else if(strncmp(p, "!ENTITY", 8) == 0) { // XXX - IGNORING !ENTITY NODES 0 126 126
916 0 p += 8;
917 0 mark = p;
918 0 p = strstr(mark, ">");
919 0 ▶ if(!p) {
Branch: 1 branch
BranchTrueFalseTotal
if(!p) { 0 0 0
920 0 fprintf(stderr, "Can't find where the !ENTITY element ends\n");
921 0 err = XML_PARSER_GENERIC_ERR;
922 0 goto _parser_err;
923 }
924 0 p++;
925 126 ▶ } else if(strncmp(p, "!NOTATION", 9) == 0) { // XXX - IGNORING !NOTATION NODES
Branch: 1 branch
BranchTrueFalseTotal
} else if(strncmp(p, "!NOTATION", 9) == 0) { // XXX - IGNORING !NOTATION NODES 0 126 126
926 0 p += 9;
927 0 mark = p;
928 0 p = strstr(mark, ">");
929 0 ▶ if(!p) {
Branch: 1 branch
BranchTrueFalseTotal
if(!p) { 0 0 0
930 0 fprintf(stderr, "Can't find where the !NOTATION element ends\n");
931 0 err = XML_PARSER_GENERIC_ERR;
932 0 goto _parser_err;
933 }
934 0 p++;
935 126 ▶ } else if(strncmp(p, "!ATTLIST", 8) == 0) { // XXX - IGNORING !ATTLIST NODES
Branch: 1 branch
BranchTrueFalseTotal
} else if(strncmp(p, "!ATTLIST", 8) == 0) { // XXX - IGNORING !ATTLIST NODES 0 126 126
936 0 p += 8;
937 0 mark = p;
938 0 p = strstr(mark, ">");
939 0 ▶ if(!p) {
Branch: 1 branch
BranchTrueFalseTotal
if(!p) { 0 0 0
940 0 fprintf(stderr, "Can't find where the !NOTATION element ends\n");
941 0 err = XML_PARSER_GENERIC_ERR;
942 0 goto _parser_err;
943 }
944 0 p++;
945 126 ▶ } else if(strncmp(p, "!--", 3) == 0) { /* comment */
Branch: 1 branch
BranchTrueFalseTotal
} else if(strncmp(p, "!--", 3) == 0) { /* comment */ 11 115 126
946 11 char *comment = NULL;
947 11 p += 3; /* skip !-- */
948 11 mark = p;
949 11 p = strstr(mark, "-->");
950 if(!p) {
951 /* XXX - TODO - This error condition must be handled asap */
952 }
953 11 comment = (char *)calloc(1, p-mark+1);
954 11 ▶ if(!comment) {
Branch: 1 branch
BranchTrueFalseTotal
if(!comment) { 0 11 11
955 0 err = XML_MEMORY_ERR;
956 0 goto _parser_err;
957 }
958 11 strncpy(comment, mark, p-mark);
959 11 err = XmlExtraNodeHandler(xml, comment, XML_NODETYPE_COMMENT);
960 11 free(comment);
961 11 p+=3;
962 115 ▶ } else if(strncmp(p, "![", 2) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(strncmp(p, "![", 2) == 0) { 6 109 115
963 6 mark = p;
964 6 p += 2; /* skip ![ */
965 6 ▶ SKIP_WHITESPACES(p);
Branch: 10 branches
BranchTrueFalseTotal
SKIP_WHITESPACES(p); 6 0 6
SKIP_WHITESPACES(p); 6 0 6
SKIP_WHITESPACES(p); 0 6 6
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 6 6
966 //mark = p;
967 6 ▶ if(strncmp(p, "CDATA", 5) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(strncmp(p, "CDATA", 5) == 0) { 6 0 6
968 6 char *cdata = NULL;
969 6 p+=5;
970 6 ▶ SKIP_WHITESPACES(p);
Branch: 10 branches
BranchTrueFalseTotal
SKIP_WHITESPACES(p); 6 0 6
SKIP_WHITESPACES(p); 6 0 6
SKIP_WHITESPACES(p); 0 6 6
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 6 6
971 6 ▶ if(*p != '[') {
Branch: 1 branch
BranchTrueFalseTotal
if(*p != '[') { 0 6 6
972 0 fprintf(stderr, "Unsupported entity type at \"... -->%15s\"", mark);
973 0 err = XML_PARSER_GENERIC_ERR;
974 0 goto _parser_err;
975 }
976 6 mark = ++p;
977 6 p = strstr(mark, "]]>");
978 if(!p) {
979 /* XXX - TODO - This error condition must be handled asap */
980 }
981 6 cdata = (char *)calloc(1, p-mark+1);
982 6 ▶ if(!cdata) {
Branch: 1 branch
BranchTrueFalseTotal
if(!cdata) { 0 6 6
983 0 err = XML_MEMORY_ERR;
984 0 goto _parser_err;
985 }
986 6 strncpy(cdata, mark, p-mark);
987 6 err = XmlExtraNodeHandler(xml, cdata, XML_NODETYPE_CDATA);
988 6 free(cdata);
989 6 p+=3;
990 } else {
991 0 fprintf(stderr, "Unsupported entity type at \"... -->%15s\"", mark);
992 0 err = XML_PARSER_GENERIC_ERR;
993 0 goto _parser_err;
994 }
995 109 ▶ } else if(*p =='?') { /* head */
Branch: 1 branch
BranchTrueFalseTotal
} else if(*p =='?') { /* head */ 10 99 109
996 10 char *encoding = NULL;
997 10 p++;
998 10 mark = p;
999 10 p = strstr(mark, "?>");
1000 10 ▶ if(xml->head) // we are going to overwrite existing head (if any)
Branch: 1 branch
BranchTrueFalseTotal
if(xml->head) // we are going to overwrite existing head (if any) 0 10 10
1001 0 free(xml->head); /* XXX - should notify this behaviour? */
1002 10 xml->head = (char *)calloc(1, p-mark+1);
1003 10 strncpy(xml->head, mark, p-mark);
1004 10 encoding = strstr(xml->head, "encoding=");
1005 10 ▶ if (encoding) {
Branch: 1 branch
BranchTrueFalseTotal
if (encoding) { 10 0 10
1006 10 encoding += 9;
1007 10 ▶ if (*encoding == '"' || *encoding == '\'') {
Branch: 2 branches
BranchTrueFalseTotal
if (*encoding == '"' || *encoding == '\'') { 0 10 10
if (*encoding == '"' || *encoding == '\'') { 0 0 0
1008 10 int encoding_length = 0;
1009 10 quote = *encoding;
1010 10 encoding++;
1011 10 end = (char *)strchr(encoding, quote);
1012 10 ▶ if (!end) {
Branch: 1 branch
BranchTrueFalseTotal
if (!end) { 0 10 10
1013 0 fprintf(stderr, "Unquoted encoding string in the <?xml> section");
1014 0 err = XML_PARSER_GENERIC_ERR;
1015 0 goto _parser_err;
1016 }
1017 10 encoding_length = end - encoding;
1018 10 ▶ if (encoding_length < sizeof(xml->documentEncoding)) {
Branch: 1 branch
BranchTrueFalseTotal
if (encoding_length < sizeof(xml->documentEncoding)) { 10 0 10
1019 10 strncpy(xml->documentEncoding, encoding, encoding_length);
1020 // ensure to terminate it, if we are reusing a context we
1021 // could have still the old encoding there possibly with a
1022 // longer name (so poisoning the buffer)
1023 10 xml->documentEncoding[encoding_length] = 0;
1024 }
1025 }
1026 } else {
1027 }
1028 10 p+=2;
1029 } else { /* start tag */
1030 99 attrs = NULL;
1031 99 values = NULL;
1032 99 nAttrs = 0;
1033 99 state = XML_ELEMENT_START;
1034 99 ▶ SKIP_WHITESPACES(p);
Branch: 10 branches
BranchTrueFalseTotal
SKIP_WHITESPACES(p); 99 0 99
SKIP_WHITESPACES(p); 99 0 99
SKIP_WHITESPACES(p); 0 99 99
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 99 99
1035 99 mark = p;
1036 727 ▶ ADVANCE_ELEMENT(p);
Branch: 6 branches
BranchTrueFalseTotal
ADVANCE_ELEMENT(p); 675 52 727
ADVANCE_ELEMENT(p); 629 46 675
ADVANCE_ELEMENT(p); 629 0 629
ADVANCE_ELEMENT(p); 629 0 629
ADVANCE_ELEMENT(p); 628 1 629
ADVANCE_ELEMENT(p); 628 0 628
1037 99 start = (char *)malloc(p-mark+2);
1038 99 ▶ if(start == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(start == NULL) 0 99 99
1039 0 return XML_MEMORY_ERR;
1040 99 strncpy(start, mark, p-mark);
1041  
1042 99 ▶ if(*p == '>' && *(p-1) == '/') {
Branch: 2 branches
BranchTrueFalseTotal
if(*p == '>' && *(p-1) == '/') { 52 47 99
if(*p == '>' && *(p-1) == '/') { 26 26 52
1043 26 start[p-mark-1] = 0;
1044 26 state = XML_ELEMENT_UNIQUE;
1045 } else {
1046 73 start[p-mark] = 0;
1047 }
1048  
1049 147 ▶ SKIP_WHITESPACES(p);
Branch: 10 branches
BranchTrueFalseTotal
SKIP_WHITESPACES(p); 100 1 101
SKIP_WHITESPACES(p); 100 0 100
SKIP_WHITESPACES(p); 1 99 100
SKIP_WHITESPACES(p); 2 0 2
SKIP_WHITESPACES(p); 46 0 46
SKIP_WHITESPACES(p); 46 0 46
SKIP_WHITESPACES(p); 0 46 46
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 46 46
SKIP_WHITESPACES(p); 46 99 145
1050 99 ▶ if(*p == '>' || (*p == '/' && *(p+1) == '>')) {
Branch: 3 branches
BranchTrueFalseTotal
if(*p == '>' || (*p == '/' && *(p+1) == '>')) { 47 52 99
if(*p == '>' || (*p == '/' && *(p+1) == '>')) { 0 47 47
if(*p == '>' || (*p == '/' && *(p+1) == '>')) { 0 0 0
1051 52 ▶ if (*p == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '/') { 0 52 52
1052 0 state = XML_ELEMENT_UNIQUE;
1053 0 p++;
1054 }
1055 }
1056 166 ▶ while(*p != '>' && *p != 0) {
Branch: 2 branches
BranchTrueFalseTotal
while(*p != '>' && *p != 0) { 67 99 166
while(*p != '>' && *p != 0) { 67 0 67
1057 67 mark = p;
1058 545 ▶ ADVANCE_TO_ATTR_VALUE(p);
Branch: 16 branches
BranchTrueFalseTotal
ADVANCE_TO_ATTR_VALUE(p); 478 67 545
ADVANCE_TO_ATTR_VALUE(p); 478 0 478
ADVANCE_TO_ATTR_VALUE(p); 478 0 478
ADVANCE_TO_ATTR_VALUE(p); 478 0 478
ADVANCE_TO_ATTR_VALUE(p); 478 0 478
ADVANCE_TO_ATTR_VALUE(p); 478 0 478
ADVANCE_TO_ATTR_VALUE(p); 67 0 67
ADVANCE_TO_ATTR_VALUE(p); 67 0 67
ADVANCE_TO_ATTR_VALUE(p); 0 67 67
ADVANCE_TO_ATTR_VALUE(p); 0 0 0
ADVANCE_TO_ATTR_VALUE(p); 0 0 0
ADVANCE_TO_ATTR_VALUE(p); 0 0 0
ADVANCE_TO_ATTR_VALUE(p); 0 0 0
ADVANCE_TO_ATTR_VALUE(p); 0 0 0
ADVANCE_TO_ATTR_VALUE(p); 0 0 0
ADVANCE_TO_ATTR_VALUE(p); 0 67 67
1059 67 ▶ if(*p == '=') {
Branch: 1 branch
BranchTrueFalseTotal
if(*p == '=') { 67 0 67
1060 67 char *tmpAttr = (char *)malloc(p-mark+1);
1061 67 strncpy(tmpAttr, mark, p-mark);
1062 67 tmpAttr[p-mark] = 0;
1063 67 p++;
1064 67 ▶ SKIP_WHITESPACES(p);
Branch: 10 branches
BranchTrueFalseTotal
SKIP_WHITESPACES(p); 67 0 67
SKIP_WHITESPACES(p); 67 0 67
SKIP_WHITESPACES(p); 0 67 67
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 67 67
1065 67 ▶ if(*p == '"' || *p == '\'') {
Branch: 2 branches
BranchTrueFalseTotal
if(*p == '"' || *p == '\'') { 0 67 67
if(*p == '"' || *p == '\'') { 0 0 0
1066 67 quote = *p;
1067 67 p++;
1068 67 mark = p;
1069 845 ▶ while(*p != 0) {
Branch: 1 branch
BranchTrueFalseTotal
while(*p != 0) { 845 0 845
1070 845 ▶ if (*p == quote) {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == quote) { 67 778 845
1071 67 ▶ if (*(p+1) != quote) // handle quote escaping
Branch: 1 branch
BranchTrueFalseTotal
if (*(p+1) != quote) // handle quote escaping 67 0 67
1072 67 break;
1073 else
1074 0 p++;
1075 }
1076 778 p++;
1077 }
1078 67 ▶ if(*p == quote) {
Branch: 1 branch
BranchTrueFalseTotal
if(*p == quote) { 67 0 67
1079 char *dexmlized;
1080 67 char *tmpVal = (char *)malloc(p-mark+2);
1081 67 int i, j=0;
1082 845 ▶ for (i = 0; i < p-mark; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < p-mark; i++) { 778 67 845
1083 778 ▶ if ( mark[i] == quote && mark[i+1] == mark[i] )
Branch: 2 branches
BranchTrueFalseTotal
if ( mark[i] == quote && mark[i+1] == mark[i] ) 0 778 778
if ( mark[i] == quote && mark[i+1] == mark[i] ) 0 0 0
1084 0 i++;
1085 778 tmpVal[j++] = mark[i];
1086 }
1087 67 tmpVal[p-mark] = 0;
1088 /* add new attribute */
1089 67 nAttrs++;
1090 67 attrs = (char **)realloc(attrs, sizeof(char *)*(nAttrs+1));
1091 67 attrs[nAttrs-1] = tmpAttr;
1092 67 attrs[nAttrs] = NULL;
1093 67 values = (char **)realloc(values, sizeof(char *)*(nAttrs+1));
1094 67 dexmlized = dexmlize(tmpVal);
1095 67 free(tmpVal);
1096 67 values[nAttrs-1] = dexmlized;
1097 67 values[nAttrs] = NULL;
1098 67 p++;
1099 91 ▶ SKIP_WHITESPACES(p);
Branch: 10 branches
BranchTrueFalseTotal
SKIP_WHITESPACES(p); 70 2 72
SKIP_WHITESPACES(p); 70 0 70
SKIP_WHITESPACES(p); 3 67 70
SKIP_WHITESPACES(p); 5 0 5
SKIP_WHITESPACES(p); 19 0 19
SKIP_WHITESPACES(p); 19 0 19
SKIP_WHITESPACES(p); 0 19 19
SKIP_WHITESPACES(p); 0 0 0
SKIP_WHITESPACES(p); 0 19 19
SKIP_WHITESPACES(p); 19 67 86
1100 }
1101 else {
1102 0 free(tmpAttr);
1103 }
1104 } /* if(*p == '"' || *p == '\'') */
1105 else {
1106 0 free(tmpAttr);
1107 }
1108 } /* if(*p=='=') */
1109 67 ▶ if(*p == '/' && *(p+1) == '>') {
Branch: 2 branches
BranchTrueFalseTotal
if(*p == '/' && *(p+1) == '>') { 0 67 67
if(*p == '/' && *(p+1) == '>') { 0 0 0
1110 0 p++;
1111 0 state = XML_ELEMENT_UNIQUE;
1112 }
1113 } /* while(*p != '>' && *p != 0) */
1114 99 err = XmlStartHandler(xml, start, attrs, values);
1115 99 ▶ if(err != XML_NOERR) {
Branch: 1 branch
BranchTrueFalseTotal
if(err != XML_NOERR) { 0 99 99
1116 0 ▶ XML_FREE_ATTRIBUTES
Branch: 4 branches
BranchTrueFalseTotal
XML_FREE_ATTRIBUTES 0 0 0
XML_FREE_ATTRIBUTES 0 0 0
XML_FREE_ATTRIBUTES 0 0 0
XML_FREE_ATTRIBUTES 0 0 0
1117 0 free(start);
1118 0 return err;
1119 }
1120 99 ▶ if(state == XML_ELEMENT_UNIQUE) {
Branch: 1 branch
BranchTrueFalseTotal
if(state == XML_ELEMENT_UNIQUE) { 26 73 99
1121 26 err = XmlEndHandler(xml, start);
1122 26 ▶ if(err != XML_NOERR) {
Branch: 1 branch
BranchTrueFalseTotal
if(err != XML_NOERR) { 0 26 26
1123 0 ▶ XML_FREE_ATTRIBUTES
Branch: 4 branches
BranchTrueFalseTotal
XML_FREE_ATTRIBUTES 0 0 0
XML_FREE_ATTRIBUTES 0 0 0
XML_FREE_ATTRIBUTES 0 0 0
XML_FREE_ATTRIBUTES 0 0 0
1124 0 free(start);
1125 0 return err;
1126 }
1127 }
1128 166 ▶ XML_FREE_ATTRIBUTES
Branch: 4 branches
BranchTrueFalseTotal
XML_FREE_ATTRIBUTES 47 52 99
XML_FREE_ATTRIBUTES 67 0 67
XML_FREE_ATTRIBUTES 67 0 67
XML_FREE_ATTRIBUTES 67 47 114
1129 99 free(start);
1130 99 p++;
1131 } /* end of start tag */
1132 } /* if(*p == '<') */
1133 60 ▶ else if(state == XML_ELEMENT_START) {
Branch: 1 branch
BranchTrueFalseTotal
else if(state == XML_ELEMENT_START) { 36 24 60
1134 36 state = XML_ELEMENT_VALUE;
1135 36 mark = p;
1136 533 ▶ while(*p != '<' && *p != 0)
Branch: 2 branches
BranchTrueFalseTotal
while(*p != '<' && *p != 0) 497 36 533
while(*p != '<' && *p != 0) 497 0 497
1137 497 p++;
1138 36 ▶ if(*p == '<') { // p now points to the beginning of next node
Branch: 1 branch
BranchTrueFalseTotal
if(*p == '<') { // p now points to the beginning of next node 36 0 36
1139 36 char *value = (char *)malloc(p-mark+1);
1140 36 strncpy(value, mark, p-mark);
1141 36 value[p-mark] = 0;
1142 36 err = XmlValueHandler(xml, value);
1143 36 ▶ if(value)
Branch: 1 branch
BranchTrueFalseTotal
if(value) 36 0 36
1144 36 free(value);
1145 36 ▶ if(err != XML_NOERR)
Branch: 1 branch
BranchTrueFalseTotal
if(err != XML_NOERR) 0 36 36
1146 0 return(err);
1147 //p++;
1148 }
1149 }
1150 else {
1151 /* XXX */
1152 24 p++;
1153 }
1154 } // while(*p != 0)
1155  
1156 11 _parser_err:
1157 11 return err;
1158 }
1159  
1160 #ifdef WIN32
1161 //************************************************************************
1162 // BOOL W32LockFile (FILE* filestream)
1163 //
1164 // locks the specific file for exclusive access, nonblocking
1165 //
1166 // returns 0 on success
1167 //************************************************************************
1168 static BOOL
1169 W32LockFile (FILE* filestream)
1170 {
1171 BOOL res = TRUE;
1172 HANDLE hFile = INVALID_HANDLE_VALUE;
1173 unsigned long size = 0;
1174 int fd = 0;
1175  
1176 // check params
1177 if (!filestream)
1178 goto __exit;
1179  
1180 // get handle from stream
1181 fd = _fileno (filestream);
1182 hFile = (HANDLE)_get_osfhandle(fd);
1183  
1184 // lock file until access is permitted
1185 size = GetFileSize(hFile, NULL);
1186 res = LockFile (hFile, 0, 0, size, 0);
1187 if (res)
1188 res = 0;
1189 __exit:
1190 return res;
1191 }
1192  
1193 //************************************************************************
1194 // BOOL W32UnlockFile (FILE* filestream)
1195 //
1196 // unlocks the specific file locked by W32LockFile
1197 //
1198 // returns 0 on success
1199 //************************************************************************
1200 static BOOL
1201 W32UnlockFile (FILE* filestream)
1202 {
1203 BOOL res = TRUE;
1204 HANDLE hFile = INVALID_HANDLE_VALUE;
1205 unsigned long size = 0;
1206 int tries = 0;
1207 int fd = 0;
1208  
1209 // check params
1210 if (!filestream)
1211 goto __exit;
1212  
1213 // get handle from stream
1214 fd = _fileno (filestream);
1215 hFile = (HANDLE)_get_osfhandle(fd);
1216  
1217 // unlock
1218 size = GetFileSize(hFile, NULL);
1219 res = UnlockFile (hFile, 0, 0, size, 0);
1220 if (res)
1221 res = 0;
1222  
1223 __exit:
1224 return res;
1225 }
1226 #endif // #ifdef WIN32
1227  
1228 static XmlErr
1229 10 XmlFileLock(FILE *file)
1230 {
1231 10 int tries = 0;
1232 10 ▶ if(file) {
Branch: 1 branch
BranchTrueFalseTotal
if(file) { 10 0 10
1233 #ifdef WIN32
1234 while(W32LockFile(file) != 0) {
1235 #else
1236 10 ▶ while(ftrylockfile(file) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
while(ftrylockfile(file) != 0) { 0 10 10
1237 #endif
1238 // warning("can't obtain a lock on xml file %s... waiting (%d)", xmlFile, tries);
1239 0 tries++;
1240 0 ▶ if(tries>5) {
Branch: 1 branch
BranchTrueFalseTotal
if(tries>5) { 0 0 0
1241 0 fprintf(stderr, "sticky lock on xml file!!!");
1242 0 return XML_GENERIC_ERR;
1243 }
1244 0 sleep(1);
1245 }
1246 10 return XML_NOERR;
1247 }
1248 0 return XML_GENERIC_ERR;
1249 }
1250  
1251 static XmlErr
1252 10 XmlFileUnlock(FILE *file)
1253 {
1254 10 ▶ if(file) {
Branch: 1 branch
BranchTrueFalseTotal
if(file) { 10 0 10
1255 #ifdef WIN32
1256 if(W32UnlockFile(file) == 0)
1257 #else
1258 10 funlockfile(file);
1259  
1260 #endif
1261 10 return XML_NOERR;
1262 }
1263 0 return XML_GENERIC_ERR;
1264 }
1265  
1266 XmlErr
1267 10 XmlParseFile(TXml *xml, char *path)
1268 {
1269 FILE *inFile;
1270 char *buffer;
1271 XmlErr err;
1272 struct stat fileStat;
1273 10 int rc = 0;
1274  
1275 10 inFile = NULL;
1276 10 err = XML_NOERR;
1277 10 ▶ if(!path)
Branch: 1 branch
BranchTrueFalseTotal
if(!path) 0 10 10
1278 0 return XML_BADARGS;
1279 10 rc = stat(path, &fileStat);
1280 10 ▶ if (rc != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (rc != 0) 0 10 10
1281 0 return XML_BADARGS;
1282 10 xml->cNode = NULL;
1283 10 ▶ if(fileStat.st_size>0) {
Branch: 1 branch
BranchTrueFalseTotal
if(fileStat.st_size>0) { 10 0 10
1284 10 inFile = fopen(path, "r");
1285 10 ▶ if(inFile) {
Branch: 1 branch
BranchTrueFalseTotal
if(inFile) { 10 0 10
1286 #ifdef USE_ICONV
1287 iconv_t ich;
1288 char *iconvIn, *iconvOut;
1289 #endif
1290 char *out;
1291 size_t rb, cb, ilen, olen;
1292 10 char *encoding_from = NULL;
1293  
1294 10 ▶ if(XmlFileLock(inFile) != XML_NOERR) {
Branch: 1 branch
BranchTrueFalseTotal
if(XmlFileLock(inFile) != XML_NOERR) { 0 10 10
1295 0 fprintf(stderr, "Can't lock %s for opening ", path);
1296 0 return -1;
1297 }
1298 10 olen = ilen = fileStat.st_size;
1299 10 buffer = (char *)malloc(ilen+1);
1300 10 rb = fread(buffer, 1, ilen, inFile);
1301 10 ▶ if (ilen != rb) {
Branch: 1 branch
BranchTrueFalseTotal
if (ilen != rb) { 0 10 10
1302 0 fprintf(stderr, "Can't read %s content", path);
1303 0 return -1;
1304 }
1305 10 buffer[ilen] = 0;
1306 10 switch(detect_encoding(buffer)) {
1307 0 case ENCODING_UTF16LE:
1308 0 encoding_from = "UTF-16LE";
1309 0 break;
1310 0 case ENCODING_UTF16BE:
1311 0 encoding_from = "UTF-16BE";
1312 0 break;
1313 0 case ENCODING_UTF32LE:
1314 0 encoding_from = "UTF-32LE";
1315 0 break;
1316 0 case ENCODING_UTF32BE:
1317 0 encoding_from = "UTF-32BE";
1318 0 break;
1319 0 case ENCODING_UTF7:
1320 0 encoding_from = "UTF-7";
1321 0 olen = ilen*2; // we need a bigger output buffer
1322 0 break;
1323 }
1324 10 ▶ if (encoding_from) {
Branch: 1 branch
BranchTrueFalseTotal
if (encoding_from) { 0 10 10
1325 #ifdef USE_ICONV
1326 0 ich = iconv_open ("UTF-8", encoding_from);
1327 0 ▶ if (ich == (iconv_t)(-1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ich == (iconv_t)(-1)) { 0 0 0
1328 0 fprintf(stderr, "Can't init iconv: %s\n", strerror(errno));
1329 0 free(buffer);
1330 0 XmlFileUnlock(inFile);
1331 0 fclose(inFile);
1332 0 return -1;
1333 }
1334 0 out = (char *)calloc(1, olen);
1335 0 iconvIn = buffer;
1336 0 iconvOut = out;
1337 0 cb = iconv(ich, &iconvIn, &ilen, &iconvOut, &olen);
1338 0 ▶ if (cb == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (cb == -1) { 0 0 0
1339 0 fprintf(stderr, "Can't convert encoding: %s\n", strerror(errno));
1340 0 free(buffer);
1341 0 free(out);
1342 0 XmlFileUnlock(inFile);
1343 0 fclose(inFile);
1344 0 return -1;
1345 }
1346 0 free(buffer); // release initial buffer
1347 0 buffer = out; // point to the converted buffer
1348 0 iconv_close(ich);
1349 #else
1350 fprintf(stderr, "Iconv missing: can't open file %s encoded in %s. Convert it to utf8 and try again\n",
1351 path, encoding_from);
1352 free(buffer);
1353 XmlFileUnlock(inFile);
1354 fclose(inFile);
1355 return -1;
1356 #endif
1357 }
1358 10 err = XmlParseBuffer(xml, buffer);
1359 10 free(buffer); // release either the initial or the converted buffer
1360 10 XmlFileUnlock(inFile);
1361 10 fclose(inFile);
1362 } else {
1363 0 fprintf(stderr, "Can't open xmlfile %s\n", path);
1364 0 return -1;
1365 }
1366 } else {
1367 0 fprintf(stderr, "Can't stat xmlfile %s\n", path);
1368 0 return -1;
1369 }
1370 10 return XML_NOERR;
1371 }
1372  
1373 char *
1374 23 XmlDumpBranch(TXml *xml, XmlNode *rNode, unsigned int depth)
1375 {
1376 unsigned int i, n;
1377 23 char *out = NULL;
1378 23 int outOffset = 0;
1379 char *startTag;
1380 23 int startOffset = 0;
1381 char *endTag;
1382 23 int endOffset = 0;
1383 char *childDump;
1384 23 int childOffset = 0;
1385 23 char *value = NULL;
1386 23 int nameLen = 0;
1387 23 int nsNameLen = 0;
1388 XmlNodeAttribute *attr;
1389 XmlNode *child;
1390 unsigned long nAttrs;
1391  
1392  
1393 23 ▶ if (rNode->value) {
Branch: 1 branch
BranchTrueFalseTotal
if (rNode->value) { 23 0 23
1394 23 ▶ if (rNode->type == XML_NODETYPE_SIMPLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (rNode->type == XML_NODETYPE_SIMPLE) { 21 2 23
1395 21 value = xmlize(rNode->value);
1396 } else {
1397 2 value = strdup(rNode->value);
1398 }
1399 }
1400  
1401 23 ▶ if(rNode->name)
Branch: 1 branch
BranchTrueFalseTotal
if(rNode->name) 23 0 23
1402 23 nameLen = (unsigned int)strlen(rNode->name);
1403 else
1404 0 return NULL;
1405  
1406 /* First check if this is a special node (a comment or a CDATA) */
1407 23 ▶ if(rNode->type == XML_NODETYPE_COMMENT) {
Branch: 1 branch
BranchTrueFalseTotal
if(rNode->type == XML_NODETYPE_COMMENT) { 1 22 23
1408 1 out = malloc(strlen(value)+depth+9);
1409 1 *out = 0;
1410 1 ▶ if (xml->ignoreBlanks) {
Branch: 1 branch
BranchTrueFalseTotal
if (xml->ignoreBlanks) { 1 0 1
1411 2 ▶ for(n = 0; n < depth; n++)
Branch: 1 branch
BranchTrueFalseTotal
for(n = 0; n < depth; n++) 1 1 2
1412 1 out[n] = '\t';
1413 1 sprintf(out+depth, "<!--%s-->\n", value);
1414 } else {
1415 0 sprintf(out+depth, "<!--%s-->", value);
1416 }
1417 1 return out;
1418 22 ▶ } else if(rNode->type == XML_NODETYPE_CDATA) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(rNode->type == XML_NODETYPE_CDATA) { 1 21 22
1419 1 out = malloc(strlen(value)+depth+14);
1420 1 *out = 0;
1421 1 ▶ if (xml->ignoreBlanks) {
Branch: 1 branch
BranchTrueFalseTotal
if (xml->ignoreBlanks) { 1 0 1
1422 3 ▶ for(n = 0; n < depth; n++)
Branch: 1 branch
BranchTrueFalseTotal
for(n = 0; n < depth; n++) 2 1 3
1423 2 out[n] = '\t';
1424 1 sprintf(out+depth, "<![CDATA[%s]]>\n", value);
1425 } else {
1426 0 sprintf(out+depth, "<![CDATA[%s]]>", value);
1427 }
1428 1 return out;
1429 }
1430  
1431 21 childDump = (char *)calloc(1, 1);
1432  
1433 21 ▶ if (rNode->ns && rNode->ns->name)
Branch: 2 branches
BranchTrueFalseTotal
if (rNode->ns && rNode->ns->name) 4 17 21
if (rNode->ns && rNode->ns->name) 4 0 4
1434 4 nsNameLen = (unsigned int)strlen(rNode->ns->name)+1;
1435 21 startTag = (char *)calloc(1, depth+nameLen+nsNameLen+7); // :/<>\n
1436 21 endTag = (char *)calloc(1, depth+nameLen+nsNameLen+7);
1437  
1438 21 ▶ if (xml->ignoreBlanks) {
Branch: 1 branch
BranchTrueFalseTotal
if (xml->ignoreBlanks) { 11 10 21
1439 24 ▶ for(startOffset = 0; startOffset < depth; startOffset++)
Branch: 1 branch
BranchTrueFalseTotal
for(startOffset = 0; startOffset < depth; startOffset++) 13 11 24
1440 13 startTag[startOffset] = '\t';
1441 }
1442 21 startTag[startOffset++] = '<';
1443 21 ▶ if (rNode->ns && rNode->ns->name) {
Branch: 2 branches
BranchTrueFalseTotal
if (rNode->ns && rNode->ns->name) { 4 17 21
if (rNode->ns && rNode->ns->name) { 4 0 4
1444 // TODO - optimize
1445 4 strcpy(startTag + startOffset, rNode->ns->name);
1446 4 startOffset += nsNameLen;
1447 4 startTag[startOffset-1] = ':';
1448 }
1449 21 memcpy(startTag + startOffset, rNode->name, nameLen);
1450 21 startOffset += nameLen;
1451 21 nAttrs = XmlCountAttributes(rNode);
1452 21 ▶ if(nAttrs>0) {
Branch: 1 branch
BranchTrueFalseTotal
if(nAttrs>0) { 11 10 21
1453 27 ▶ for(i = 0; i < nAttrs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nAttrs; i++) { 16 11 27
1454 16 attr = XmlGetAttribute(rNode, i);
1455 16 ▶ if(attr) {
Branch: 1 branch
BranchTrueFalseTotal
if(attr) { 16 0 16
1456 int anLen, avLen;
1457 char *attr_value;
1458  
1459 16 attr_value = xmlize(attr->value);
1460 16 anLen = strlen(attr->name);
1461 16 avLen = strlen (attr_value);
1462 16 startTag = (char *)realloc(startTag, startOffset + anLen + avLen + 8);
1463 16 sprintf(startTag + startOffset, " %s=\"%s\"", attr->name, attr_value);
1464 16 startOffset += anLen + avLen + 4;
1465 16 ▶ if (attr_value)
Branch: 1 branch
BranchTrueFalseTotal
if (attr_value) 16 0 16
1466 16 free(attr_value);
1467 }
1468 }
1469 }
1470 21 ▶ if((value && *value) || !TAILQ_EMPTY(&rNode->children)) {
Branch: 3 branches
BranchTrueFalseTotal
if((value && *value) || !TAILQ_EMPTY(&rNode->children)) { 21 0 21
if((value && *value) || !TAILQ_EMPTY(&rNode->children)) { 14 7 21
if((value && *value) || !TAILQ_EMPTY(&rNode->children)) { 7 7 14
1471 14 ▶ if(!TAILQ_EMPTY(&rNode->children)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!TAILQ_EMPTY(&rNode->children)) { 7 7 14
1472 7 ▶ if (xml->ignoreBlanks) {
Branch: 1 branch
BranchTrueFalseTotal
if (xml->ignoreBlanks) { 4 3 7
1473 4 strcpy(startTag + startOffset, ">\n");
1474 4 startOffset += 2;
1475 7 ▶ for(endOffset = 0; endOffset < depth; endOffset++)
Branch: 1 branch
BranchTrueFalseTotal
for(endOffset = 0; endOffset < depth; endOffset++) 3 4 7
1476 3 endTag[endOffset] = '\t';
1477 } else {
1478 3 startTag[startOffset++] = '>';
1479 }
1480 27 ▶ TAILQ_FOREACH(child, &rNode->children, siblings) {
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_FOREACH(child, &rNode->children, siblings) { 20 7 27
1481 20 char *childBuff = XmlDumpBranch(xml, child, depth+1); /* let's recurse */
1482 20 ▶ if(childBuff) {
Branch: 1 branch
BranchTrueFalseTotal
if(childBuff) { 20 0 20
1483 20 int childBuffLen = strlen(childBuff);
1484 20 childDump = (char *)realloc(childDump, childOffset+childBuffLen+1);
1485 // ensure copying the null-terminating byte as well
1486 20 memcpy(childDump + childOffset, childBuff, childBuffLen + 1);
1487 20 childOffset += childBuffLen;
1488 20 free(childBuff);
1489 }
1490 }
1491 } else {
1492 // TODO - allow to specify a flag to determine if we want white spaces or not
1493 7 startTag[startOffset++] = '>';
1494 }
1495 14 startTag[startOffset] = 0; // ensure null-terminating the start-tag
1496 14 strcpy(endTag + endOffset, "</");
1497 14 endOffset += 2;
1498 14 ▶ if (rNode->ns && rNode->ns->name) {
Branch: 2 branches
BranchTrueFalseTotal
if (rNode->ns && rNode->ns->name) { 2 12 14
if (rNode->ns && rNode->ns->name) { 2 0 2
1499 // TODO - optimize
1500 2 strcpy(endTag + endOffset, rNode->ns->name);
1501 2 endOffset += nsNameLen;
1502 2 endTag[endOffset-1] = ':';
1503 }
1504 14 sprintf(endTag + endOffset, "%s>", rNode->name);
1505 14 endOffset += nameLen + 1;
1506 14 ▶ if (xml->ignoreBlanks)
Branch: 1 branch
BranchTrueFalseTotal
if (xml->ignoreBlanks) 8 6 14
1507 8 endTag[endOffset++] = '\n';
1508 14 endTag[endOffset] = 0; // ensure null-terminating
1509 28 out = (char *)malloc(depth+strlen(startTag)+strlen(endTag)+
1510 14 ▶ (value?strlen(value)+1:1)+strlen(childDump)+3);
Branch: 1 branch
BranchTrueFalseTotal
(value?strlen(value)+1:1)+strlen(childDump)+3); 14 0 14
1511 14 strcpy(out, startTag);
1512 14 outOffset += startOffset;
1513 14 ▶ if(value && *value) { // skip also if value is an empty string (not only if it's a null pointer)
Branch: 2 branches
BranchTrueFalseTotal
if(value && *value) { // skip also if value is an empty string (not only if it's a null pointer) 14 0 14
if(value && *value) { // skip also if value is an empty string (not only if it's a null pointer) 7 7 14
1514 7 ▶ if(!TAILQ_EMPTY(&rNode->children)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!TAILQ_EMPTY(&rNode->children)) { 0 7 7
1515 0 ▶ if (xml->ignoreBlanks) {
Branch: 1 branch
BranchTrueFalseTotal
if (xml->ignoreBlanks) { 0 0 0
1516 0 ▶ for(; outOffset < depth; outOffset++)
Branch: 1 branch
BranchTrueFalseTotal
for(; outOffset < depth; outOffset++) 0 0 0
1517 0 out[outOffset] = '\t';
1518 }
1519 0 ▶ if (value) {
Branch: 1 branch
BranchTrueFalseTotal
if (value) { 0 0 0
1520 0 sprintf(out + outOffset, "%s", value);
1521 0 outOffset += strlen(value);
1522 0 ▶ if (xml->ignoreBlanks)
Branch: 1 branch
BranchTrueFalseTotal
if (xml->ignoreBlanks) 0 0 0
1523 0 out[outOffset++] = '\n';
1524 }
1525 }
1526 else {
1527 7 ▶ if (value)
Branch: 1 branch
BranchTrueFalseTotal
if (value) 7 0 7
1528 7 strcpy(out + outOffset, value);
1529 7 outOffset += strlen(value);
1530 }
1531 }
1532 14 memcpy(out + outOffset, childDump, childOffset);
1533 14 outOffset += childOffset;
1534 14 strcpy(out + outOffset, endTag);
1535 }
1536 else {
1537 7 strcpy(startTag + startOffset, "/>");
1538 7 startOffset += 2;
1539 7 ▶ if (xml->ignoreBlanks)
Branch: 1 branch
BranchTrueFalseTotal
if (xml->ignoreBlanks) 3 4 7
1540 3 startTag[startOffset++] = '\n';
1541 7 startTag[startOffset] = 0;
1542 7 out = strdup(startTag);
1543 }
1544 21 free(startTag);
1545 21 free(endTag);
1546 21 free(childDump);
1547 21 ▶ if (value)
Branch: 1 branch
BranchTrueFalseTotal
if (value) 21 0 21
1548 21 free(value);
1549 21 return out;
1550 }
1551  
1552 char *
1553 3 XmlDump(TXml *xml, int *outlen)
1554 {
1555 char *dump;
1556 XmlNode *rNode;
1557 char *branch;
1558 unsigned int i;
1559 #ifdef USE_ICONV
1560 3 int doConversion = 0;
1561 #endif
1562 char head[256]; // should be enough
1563 int hLen;
1564 unsigned int offset;
1565  
1566 3 memset(head, 0, sizeof(head));
1567 3 ▶ if (xml->head) {
Branch: 1 branch
BranchTrueFalseTotal
if (xml->head) { 2 1 3
1568 int quote;
1569 char *start, *end, *encoding;
1570 2 char *initial = strdup(xml->head);
1571 2 start = strstr(initial, "encoding=");
1572 2 ▶ if (start) {
Branch: 1 branch
BranchTrueFalseTotal
if (start) { 2 0 2
1573 2 *start = 0;
1574 2 encoding = start+9;
1575 2 ▶ if (*encoding == '"' || *encoding == '\'') {
Branch: 2 branches
BranchTrueFalseTotal
if (*encoding == '"' || *encoding == '\'') { 0 2 2
if (*encoding == '"' || *encoding == '\'') { 0 0 0
1576 2 quote = *encoding;
1577 2 encoding++;
1578 2 end = (char *)strchr(encoding, quote);
1579 2 ▶ if (!end) {
Branch: 1 branch
BranchTrueFalseTotal
if (!end) { 2 0 2
1580 /* TODO - Error Messages */
1581 2 } else if ((end-encoding) >= sizeof(xml->outputEncoding)) {
1582 /* TODO - Error Messages */
1583 }
1584 2 *end = 0;
1585 // check if document encoding matches
1586 2 if (strncasecmp(encoding, xml->documentEncoding, end-encoding) != 0) {
1587 /* TODO - Error Messages */
1588 }
1589 2 ▶ if (strncasecmp(encoding, xml->outputEncoding, end-encoding) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strncasecmp(encoding, xml->outputEncoding, end-encoding) != 0) { 0 2 2
1590 #ifdef USE_ICONV
1591 0 snprintf(head, sizeof(head), "%sencoding=\"%s\"%s",
1592 0 initial, xml->outputEncoding, ++end);
1593 0 doConversion = 1;
1594 #else
1595 fprintf(stderr, "Iconv missing: will not convert output to %s\n", xml->outputEncoding);
1596 snprintf(head, sizeof(head), "%s", xml->head);
1597 #endif
1598 } else {
1599 2 snprintf(head, sizeof(head), "%s", xml->head);
1600 }
1601  
1602 }
1603 } else {
1604 #ifdef USE_ICONV
1605 0 ▶ if (xml->outputEncoding && strcasecmp(xml->outputEncoding, "utf-8") != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (xml->outputEncoding && strcasecmp(xml->outputEncoding, "utf-8") != 0) { 0 0 0
if (xml->outputEncoding && strcasecmp(xml->outputEncoding, "utf-8") != 0) { 0 0 0
1606 0 doConversion = 1;
1607 0 fprintf(stderr, "Iconv missing: will not convert output to %s\n", xml->outputEncoding);
1608 }
1609 0 snprintf(head, sizeof(head), "xml version=\"1.0\" encoding=\"%s\"",
1610 0 ▶ xml->outputEncoding?xml->outputEncoding:"utf-8");
Branch: 1 branch
BranchTrueFalseTotal
xml->outputEncoding?xml->outputEncoding:"utf-8"); 0 0 0
1611 #else
1612 if (xml->outputEncoding && strcasecmp(xml->outputEncoding, "utf-8") != 0) {
1613 fprintf(stderr, "Iconv missing: will not convert output to %s\n", xml->outputEncoding);
1614 }
1615 snprintf(head, sizeof(head), "xml version=\"1.0\" encoding=\"utf-8\"");
1616 #endif
1617 }
1618 2 free(initial);
1619 } else {
1620 #ifdef USE_ICONV
1621 1 ▶ if (xml->outputEncoding && strcasecmp(xml->outputEncoding, "utf-8") != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (xml->outputEncoding && strcasecmp(xml->outputEncoding, "utf-8") != 0) { 1 0 1
if (xml->outputEncoding && strcasecmp(xml->outputEncoding, "utf-8") != 0) { 0 1 1
1622 0 doConversion = 1;
1623 }
1624 1 snprintf(head, sizeof(head), "xml version=\"1.0\" encoding=\"%s\"",
1625 1 ▶ xml->outputEncoding?xml->outputEncoding:"utf-8");
Branch: 1 branch
BranchTrueFalseTotal
xml->outputEncoding?xml->outputEncoding:"utf-8"); 1 0 1
1626 #else
1627 if (xml->outputEncoding && strcasecmp(xml->outputEncoding, "utf-8") != 0) {
1628 fprintf(stderr, "Iconv missing: will not convert output to %s\n", xml->outputEncoding);
1629 }
1630 snprintf(head, sizeof(head), "xml version=\"1.0\" encoding=\"utf-8\"");
1631 #endif
1632 }
1633 3 hLen = strlen(head);
1634 3 dump = malloc(hLen+6);
1635 3 sprintf(dump, "<?%s?>\n", head);
1636 3 offset = hLen +5;
1637 6 ▶ TAILQ_FOREACH(rNode, &xml->rootElements, siblings) {
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_FOREACH(rNode, &xml->rootElements, siblings) { 3 3 6
1638 3 branch = XmlDumpBranch(xml, rNode, 0);
1639 3 ▶ if(branch) {
Branch: 1 branch
BranchTrueFalseTotal
if(branch) { 3 0 3
1640 3 int bLen = strlen(branch);
1641 3 dump = (char *)realloc(dump, offset + bLen + 1);
1642 // ensure copying the null-terminating byte as well
1643 3 memcpy(dump + offset, branch, bLen + 1);
1644 3 offset += bLen;
1645 3 free(branch);
1646 }
1647 }
1648 3 ▶ if (outlen) // check if we need to report the output size
Branch: 1 branch
BranchTrueFalseTotal
if (outlen) // check if we need to report the output size 3 0 3
1649 3 *outlen = strlen(dump);
1650 #ifdef USE_ICONV
1651 3 ▶ if (doConversion) {
Branch: 1 branch
BranchTrueFalseTotal
if (doConversion) { 0 3 3
1652 iconv_t ich;
1653 size_t ilen, olen, cb;
1654 char *out;
1655 char *iconvIn;
1656 char *iconvOut;
1657 0 ilen = strlen(dump);
1658 // the most expensive conversion would be from ascii to utf-32/ucs-4
1659 // ( 4 bytes for each char )
1660 0 olen = ilen * 4;
1661 // we still don't know how big the output buffer is going to be
1662 // we will update outlen later once iconv tell us the size
1663 0 ▶ if (outlen)
Branch: 1 branch
BranchTrueFalseTotal
if (outlen) 0 0 0
1664 0 *outlen = olen;
1665 0 out = (char *)calloc(1, olen);
1666 0 ich = iconv_open (xml->outputEncoding, xml->documentEncoding);
1667 0 ▶ if (ich == (iconv_t)(-1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ich == (iconv_t)(-1)) { 0 0 0
1668 0 free(dump);
1669 0 free(out);
1670 0 fprintf(stderr, "Can't init iconv: %s\n", strerror(errno));
1671 0 return NULL;
1672 }
1673 0 iconvIn = dump;
1674 0 iconvOut = out;
1675 0 cb = iconv(ich, &iconvIn, &ilen, &iconvOut, &olen);
1676 0 ▶ if (cb == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (cb == -1) { 0 0 0
1677 0 free(dump);
1678 0 free(out);
1679 0 fprintf(stderr, "Error from iconv: %s\n", strerror(errno));
1680 0 return NULL;
1681 }
1682 0 iconv_close(ich);
1683 0 free(dump); // release the old buffer (in the original encoding)
1684 0 dump = out;
1685 0 ▶ if (outlen) // update the outputsize if we have to
Branch: 1 branch
BranchTrueFalseTotal
if (outlen) // update the outputsize if we have to 0 0 0
1686 0 *outlen -= olen;
1687 }
1688 #endif
1689 3 return(dump);
1690 }
1691  
1692 XmlErr
1693 0 XmlSave(TXml *xml, char *xmlFile)
1694 {
1695 size_t rb;
1696 struct stat fileStat;
1697 0 FILE *saveFile = NULL;
1698 0 char *dump = NULL;
1699 0 int dumpLen = 0;
1700 0 char *backup = NULL;
1701 0 char *backupPath = NULL;
1702 0 FILE *backupFile = NULL;
1703  
1704  
1705 0 ▶ if (stat(xmlFile, &fileStat) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (stat(xmlFile, &fileStat) == 0) { 0 0 0
1706 0 ▶ if(fileStat.st_size>0) { /* backup old profiles */
Branch: 1 branch
BranchTrueFalseTotal
if(fileStat.st_size>0) { /* backup old profiles */ 0 0 0
1707 0 saveFile = fopen(xmlFile, "r");
1708 0 ▶ if(!saveFile) {
Branch: 1 branch
BranchTrueFalseTotal
if(!saveFile) { 0 0 0
1709 0 fprintf(stderr, "Can't open %s for reading !!", xmlFile);
1710 0 return XML_GENERIC_ERR;
1711 }
1712 0 ▶ if(XmlFileLock(saveFile) != XML_NOERR) {
Branch: 1 branch
BranchTrueFalseTotal
if(XmlFileLock(saveFile) != XML_NOERR) { 0 0 0
1713 0 fprintf(stderr, "Can't lock %s for reading ", xmlFile);
1714 0 return XML_GENERIC_ERR;
1715 }
1716 0 backup = (char *)malloc(fileStat.st_size+1);
1717 0 rb = fread(backup, 1, fileStat.st_size, saveFile);
1718 0 ▶ if (rb != fileStat.st_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (rb != fileStat.st_size) { 0 0 0
1719 0 fprintf(stderr, "Can't read %s content", xmlFile);
1720 0 return -1;
1721 }
1722 0 backup[fileStat.st_size] = 0;
1723 0 XmlFileUnlock(saveFile);
1724 0 fclose(saveFile);
1725 0 backupPath = (char *)malloc(strlen(xmlFile)+5);
1726 0 sprintf(backupPath, "%s.bck", xmlFile);
1727 0 backupFile = fopen(backupPath, "w+");
1728 0 ▶ if(backupFile) {
Branch: 1 branch
BranchTrueFalseTotal
if(backupFile) { 0 0 0
1729 0 ▶ if(XmlFileLock(backupFile) != XML_NOERR) {
Branch: 1 branch
BranchTrueFalseTotal
if(XmlFileLock(backupFile) != XML_NOERR) { 0 0 0
1730 0 fprintf(stderr, "Can't lock %s for writing ", backupPath);
1731 0 free(backupPath);
1732 0 free(backup);
1733 0 return XML_GENERIC_ERR;
1734 }
1735 0 fwrite(backup, 1, fileStat.st_size, backupFile);
1736 0 XmlFileUnlock(backupFile);
1737 0 fclose(backupFile);
1738 }
1739 else {
1740 0 fprintf(stderr, "Can't open backup file (%s) for writing! ", backupPath);
1741 0 free(backupPath);
1742 0 free(backup);
1743 0 return XML_GENERIC_ERR;
1744 }
1745 0 free(backupPath);
1746 0 free(backup);
1747 } /* end of backup */
1748 }
1749 0 dump = XmlDump(xml, &dumpLen);
1750 0 ▶ if(dump && dumpLen) {
Branch: 2 branches
BranchTrueFalseTotal
if(dump && dumpLen) { 0 0 0
if(dump && dumpLen) { 0 0 0
1751 0 saveFile = fopen(xmlFile, "w+");
1752 0 ▶ if(saveFile) {
Branch: 1 branch
BranchTrueFalseTotal
if(saveFile) { 0 0 0
1753 0 ▶ if(XmlFileLock(saveFile) != XML_NOERR) {
Branch: 1 branch
BranchTrueFalseTotal
if(XmlFileLock(saveFile) != XML_NOERR) { 0 0 0
1754 0 fprintf(stderr, "Can't lock %s for writing ", xmlFile);
1755 0 free(dump);
1756 0 return XML_GENERIC_ERR;
1757 }
1758 0 fwrite(dump, 1, dumpLen, saveFile);
1759 0 free(dump);
1760 0 XmlFileUnlock(saveFile);
1761 0 fclose(saveFile);
1762 }
1763 else {
1764 0 fprintf(stderr, "Can't open output file %s", xmlFile);
1765 0 ▶ if(!saveFile) {
Branch: 1 branch
BranchTrueFalseTotal
if(!saveFile) { 0 0 0
1766 0 free(dump);
1767 0 return XML_GENERIC_ERR;
1768 }
1769 }
1770 }
1771 0 return XML_NOERR;
1772 }
1773  
1774 unsigned long
1775 68 XmlCountAttributes(XmlNode *node)
1776 {
1777 XmlNodeAttribute *attr;
1778 68 int cnt = 0;
1779 146 ▶ TAILQ_FOREACH(attr, &node->attributes, list)
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_FOREACH(attr, &node->attributes, list) 78 68 146
1780 78 cnt++;
1781 68 return cnt;
1782 }
1783  
1784 unsigned long
1785 871 XmlCountChildren(XmlNode *node)
1786 {
1787 XmlNode *child;
1788 871 int cnt = 0;
1789 3631 ▶ TAILQ_FOREACH(child, &node->children, siblings)
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_FOREACH(child, &node->children, siblings) 2760 871 3631
1790 2760 cnt++;
1791 871 return cnt;
1792 }
1793  
1794 unsigned long
1795 255 XmlCountBranches(TXml *xml)
1796 {
1797 XmlNode *node;
1798 255 int cnt = 0;
1799 516 ▶ TAILQ_FOREACH(node, &xml->rootElements, siblings)
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_FOREACH(node, &xml->rootElements, siblings) 261 255 516
1800 261 cnt++;
1801 255 return cnt;
1802 }
1803  
1804 XmlErr
1805 0 XmlRemoveNode(TXml *xml, char *path)
1806 {
1807 /* XXX - UNIMPLEMENTED */
1808 0 return XML_GENERIC_ERR;
1809 }
1810  
1811 XmlErr
1812 1 XmlRemoveBranch(TXml *xml, unsigned long index)
1813 {
1814 1 int count = 0;
1815 XmlNode *branch, *tmp;
1816 1 ▶ TAILQ_FOREACH_SAFE(branch, &xml->rootElements, siblings, tmp) {
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_FOREACH_SAFE(branch, &xml->rootElements, siblings, tmp) { 1 0 1
1817 1 ▶ if (count++ == index) {
Branch: 1 branch
BranchTrueFalseTotal
if (count++ == index) { 1 0 1
1818 1 ▶ TAILQ_REMOVE(&xml->rootElements, branch, siblings);
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_REMOVE(&xml->rootElements, branch, siblings); 1 0 1
1819 1 XmlDestroyNode(branch);
1820 1 return XML_NOERR;
1821 }
1822 }
1823 0 return XML_GENERIC_ERR;
1824 }
1825  
1826 XmlNode
1827 491 *XmlGetChildNode(XmlNode *node, unsigned long index)
1828 {
1829 XmlNode *child;
1830 491 int count = 0;
1831 491 ▶ if(!node)
Branch: 1 branch
BranchTrueFalseTotal
if(!node) 0 491 491
1832 0 return NULL;
1833 1374 ▶ TAILQ_FOREACH(child, &node->children, siblings) {
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_FOREACH(child, &node->children, siblings) { 1374 0 1374
1834 1374 ▶ if (count++ == index) {
Branch: 1 branch
BranchTrueFalseTotal
if (count++ == index) { 491 883 1374
1835 491 return child;
1836 break;
1837 }
1838 }
1839 0 return NULL;
1840 }
1841  
1842 /* XXX - if multiple children shares the same name, only the first is returned */
1843 XmlNode
1844 29 *XmlGetChildNodeByName(XmlNode *node, char *name)
1845 {
1846 XmlNode *child;
1847 29 unsigned int i = 0;
1848 29 char *attrName = NULL;
1849 29 char *attrVal = NULL;
1850 29 char *nodeName = NULL;
1851 29 int nameLen = 0;
1852 char *p;
1853  
1854 29 ▶ if(!node)
Branch: 1 branch
BranchTrueFalseTotal
if(!node) 0 29 29
1855 0 return NULL;
1856  
1857 29 nodeName = strdup(name); // make a copy to avoid changing the provided buffer
1858 29 nameLen = strlen(nodeName);
1859  
1860 29 ▶ if (nodeName[nameLen-1] == ']') {
Branch: 1 branch
BranchTrueFalseTotal
if (nodeName[nameLen-1] == ']') { 6 23 29
1861 6 p = strchr(nodeName, '[');
1862 6 *p = 0;
1863 6 p++;
1864 6 ▶ if (sscanf(p, "%d]", &i) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (sscanf(p, "%d]", &i) == 1) { 6 0 6
1865 6 i--;
1866 0 ▶ } else if (*p == '@') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (*p == '@') { 0 0 0
1867 0 p++;
1868 0 p[strlen(p)-1] = 0;
1869 0 attrName = p;
1870 0 attrVal = strchr(p, '=');
1871 0 ▶ if (attrVal) {
Branch: 1 branch
BranchTrueFalseTotal
if (attrVal) { 0 0 0
1872 0 *attrVal = 0;
1873 0 attrVal++;
1874 0 ▶ if (*attrVal == '\'' || *attrVal == '"') {
Branch: 2 branches
BranchTrueFalseTotal
if (*attrVal == '\'' || *attrVal == '"') { 0 0 0
if (*attrVal == '\'' || *attrVal == '"') { 0 0 0
1875 0 char quote = *attrVal;
1876 0 int n, j=0;
1877 // inplace dequoting
1878 0 attrVal++;
1879 0 ▶ for (n = 0; attrVal[n] != 0; n++) {
Branch: 1 branch
BranchTrueFalseTotal
for (n = 0; attrVal[n] != 0; n++) { 0 0 0
1880 0 ▶ if (attrVal[n] == quote) {
Branch: 1 branch
BranchTrueFalseTotal
if (attrVal[n] == quote) { 0 0 0
1881 0 ▶ if (n && attrVal[n-1] == quote) { // quote escaping (XXX - perhaps out of spec)
Branch: 2 branches
BranchTrueFalseTotal
if (n && attrVal[n-1] == quote) { // quote escaping (XXX - perhaps out of spec) 0 0 0
if (n && attrVal[n-1] == quote) { // quote escaping (XXX - perhaps out of spec) 0 0 0
1882 0 ▶ if (j)
Branch: 1 branch
BranchTrueFalseTotal
if (j) 0 0 0
1883 0 j--;
1884 } else {
1885 0 attrVal[n] = 0;
1886 0 break;
1887 }
1888 }
1889 0 ▶ if (j != n)
Branch: 1 branch
BranchTrueFalseTotal
if (j != n) 0 0 0
1890 0 attrVal[j] = attrVal[n];
1891 0 j++;
1892 }
1893  
1894 }
1895 }
1896 }
1897 }
1898  
1899 102 ▶ TAILQ_FOREACH(child, &node->children, siblings) {
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_FOREACH(child, &node->children, siblings) { 102 0 102
1900 102 ▶ if(strcmp(child->name, nodeName) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(strcmp(child->name, nodeName) == 0) { 34 68 102
1901 34 ▶ if (attrName) {
Branch: 1 branch
BranchTrueFalseTotal
if (attrName) { 0 34 34
1902 0 XmlNodeAttribute *attr = XmlGetAttributeByName(child, attrName);
1903 0 ▶ if (attr) {
Branch: 1 branch
BranchTrueFalseTotal
if (attr) { 0 0 0
1904 0 ▶ if (attrVal) {
Branch: 1 branch
BranchTrueFalseTotal
if (attrVal) { 0 0 0
1905 0 char *dexmlized = dexmlize(attrVal);
1906 0 ▶ if (strcmp(attr->value, dexmlized) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(attr->value, dexmlized) != 0) { 0 0 0
1907 0 free(dexmlized);
1908 0 continue; // the attr value doesn't match .. let's skip to next matching node
1909 }
1910 0 free(dexmlized);
1911 }
1912 0 free(nodeName);
1913 0 return child;
1914 }
1915 34 ▶ } else if (i == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (i == 0) { 29 5 34
1916 29 free(nodeName);
1917 29 return child;
1918 } else {
1919 5 i--;
1920 }
1921 }
1922 }
1923 0 free(nodeName);
1924 0 return NULL;
1925 }
1926  
1927 XmlNode *
1928 19 XmlGetNode(TXml *xml, char *path)
1929 {
1930 char *buff, *walk;
1931 char *tag;
1932 19 unsigned long i = 0;
1933 19 XmlNode *cNode = NULL;
1934 19 XmlNode *wNode = NULL;
1935 //#ifndef WIN32
1936 char *brkb;
1937 //#endif
1938 19 ▶ if(!path)
Branch: 1 branch
BranchTrueFalseTotal
if(!path) 0 19 19
1939 0 return NULL;
1940  
1941 19 buff = strdup(path);
1942 19 walk = buff;
1943  
1944 // check if we are allowing multiple rootnodes to determine
1945 // if it's included in the path or not
1946 19 ▶ if (xml->allowMultipleRootNodes) {
Branch: 1 branch
BranchTrueFalseTotal
if (xml->allowMultipleRootNodes) { 2 17 19
1947 /* skip leading slashes '/' */
1948 4 ▶ while(*walk == '/')
Branch: 1 branch
BranchTrueFalseTotal
while(*walk == '/') 2 2 4
1949 2 walk++;
1950  
1951 /* select the root node */
1952 #ifndef WIN32
1953 2 tag = strtok_r(walk, "/", &brkb);
1954 #else
1955 tag = strtok(walk, "/");
1956 #endif
1957 2 ▶ if(!tag) {
Branch: 1 branch
BranchTrueFalseTotal
if(!tag) { 0 2 2
1958 0 free(buff);
1959 0 return NULL;
1960 }
1961  
1962 2 ▶ for(i = 0; i < XmlCountBranches(xml); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < XmlCountBranches(xml); i++) { 2 0 2
1963 2 wNode = XmlGetBranch(xml, i);
1964 2 ▶ if(strcmp(wNode->name, tag) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(strcmp(wNode->name, tag) == 0) { 2 0 2
1965 2 cNode = wNode;
1966 2 break;
1967 }
1968 }
1969 /* now cNode points to the root node ... let's find requested node */
1970 #ifndef WIN32
1971 2 tag = strtok_r(NULL, "/", &brkb);
1972 #else
1973 tag = strtok(NULL, "/");
1974 #endif
1975 } else { // no multiple rootnodes
1976 17 cNode = XmlGetBranch(xml, 0);
1977 // TODO - this could be done in a cleaner and more efficient way
1978 17 ▶ if (*walk != '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (*walk != '/') { 0 17 17
1979 0 buff = malloc(strlen(walk)+2);
1980 0 sprintf(buff, "/%s", walk);
1981 0 free(walk);
1982 0 walk = buff;
1983 }
1984 #ifndef WIN32
1985 17 tag = strtok_r(walk, "/", &brkb);
1986 #else
1987 tag = strtok(walk, "/");
1988 #endif
1989 }
1990  
1991 19 ▶ if(!cNode) {
Branch: 1 branch
BranchTrueFalseTotal
if(!cNode) { 0 19 19
1992 0 free(buff);
1993 0 return NULL;
1994 }
1995  
1996 44 ▶ while(tag) {
Branch: 1 branch
BranchTrueFalseTotal
while(tag) { 25 19 44
1997 XmlNode *tmp;
1998 25 wNode = XmlGetChildNodeByName(cNode, tag);
1999 25 ▶ if(!wNode) {
Branch: 1 branch
BranchTrueFalseTotal
if(!wNode) { 0 25 25
2000 0 free(buff);
2001 0 return NULL;
2002 }
2003 25 cNode = wNode; // update current node
2004 #ifndef WIN32
2005 25 tag = strtok_r(NULL, "/", &brkb);
2006 #else
2007 tag = strtok(NULL, "/");
2008 #endif
2009 }
2010  
2011 19 free(buff);
2012 19 return cNode;
2013 }
2014  
2015 XmlNode
2016 147 *XmlGetBranch(TXml *xml, unsigned long index)
2017 {
2018 XmlNode *node;
2019 147 int cnt = 0;
2020 147 ▶ if(!xml)
Branch: 1 branch
BranchTrueFalseTotal
if(!xml) 0 147 147
2021 0 return NULL;
2022 149 ▶ TAILQ_FOREACH(node, &xml->rootElements, siblings) {
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_FOREACH(node, &xml->rootElements, siblings) { 149 0 149
2023 149 ▶ if (cnt++ == index)
Branch: 1 branch
BranchTrueFalseTotal
if (cnt++ == index) 147 2 149
2024 147 return node;
2025 }
2026 0 return NULL;
2027 }
2028  
2029 XmlErr
2030 0 XmlSubstBranch(TXml *xml, unsigned long index, XmlNode *newBranch)
2031 {
2032 XmlNode *branch, *tmp;
2033 0 int cnt = 0;
2034 0 ▶ TAILQ_FOREACH_SAFE(branch, &xml->rootElements, siblings, tmp) {
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_FOREACH_SAFE(branch, &xml->rootElements, siblings, tmp) { 0 0 0
2035 0 ▶ if (cnt++ == index) {
Branch: 1 branch
BranchTrueFalseTotal
if (cnt++ == index) { 0 0 0
2036 0 TAILQ_INSERT_BEFORE(branch, newBranch, siblings);
2037 0 ▶ TAILQ_REMOVE(&xml->rootElements, branch, siblings);
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_REMOVE(&xml->rootElements, branch, siblings); 0 0 0
2038 0 return XML_NOERR;
2039 }
2040 }
2041 0 return XML_LINKLIST_ERR;
2042 }
2043  
2044 XmlNamespace *
2045 44 XmlCreateNamespace(char *nsName, char *nsUri) {
2046 XmlNamespace *newNS;
2047 44 newNS = (XmlNamespace *)calloc(1, sizeof(XmlNamespace));
2048 44 ▶ if (nsName)
Branch: 1 branch
BranchTrueFalseTotal
if (nsName) 23 21 44
2049 23 newNS->name = strdup(nsName);
2050 44 newNS->uri = strdup(nsUri);
2051 44 return newNS;
2052 }
2053  
2054 void
2055 44 XmlDestroyNamespace(XmlNamespace *ns)
2056 {
2057 44 ▶ if (ns) {
Branch: 1 branch
BranchTrueFalseTotal
if (ns) { 44 0 44
2058 44 ▶ if (ns->name)
Branch: 1 branch
BranchTrueFalseTotal
if (ns->name) 23 21 44
2059 23 free(ns->name);
2060 44 ▶ if (ns->uri)
Branch: 1 branch
BranchTrueFalseTotal
if (ns->uri) 44 0 44
2061 44 free(ns->uri);
2062 44 free(ns);
2063 }
2064 44 }
2065  
2066 XmlNamespace *
2067 44 XmlAddNamespace(XmlNode *node, char *nsName, char *nsUri) {
2068 44 XmlNamespace *newNS = NULL;
2069 44 ▶ if (!node || !nsUri)
Branch: 2 branches
BranchTrueFalseTotal
if (!node || !nsUri) 44 0 44
if (!node || !nsUri) 0 44 44
2070 0 return NULL;
2071  
2072 44 ▶ if ((newNS = XmlCreateNamespace(nsName, nsUri)))
Branch: 1 branch
BranchTrueFalseTotal
if ((newNS = XmlCreateNamespace(nsName, nsUri))) 44 0 44
2073 44 TAILQ_INSERT_TAIL(&node->namespaces, newNS, list);
2074 44 return newNS;
2075 }
2076  
2077 XmlNamespace *
2078 23 XmlGetNamespaceByName(XmlNode *node, char *nsName) {
2079 XmlNamespaceSet *item;
2080 // TODO - check if node->knownNamespaces needs to be updated
2081 45 ▶ TAILQ_FOREACH(item, &node->knownNamespaces, next) {
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_FOREACH(item, &node->knownNamespaces, next) { 45 0 45
2082 45 ▶ if (item->ns->name && strcmp(item->ns->name, nsName) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (item->ns->name && strcmp(item->ns->name, nsName) == 0) 27 18 45
if (item->ns->name && strcmp(item->ns->name, nsName) == 0) 23 4 27
2083 23 return item->ns;
2084 }
2085 0 return NULL;
2086 }
2087  
2088 XmlNamespace *
2089 0 XmlGetNamespaceByUri(XmlNode *node, char *nsUri) {
2090 XmlNamespaceSet *item;
2091 // TODO - check if node->knownNamespaces needs to be updated
2092 0 ▶ TAILQ_FOREACH(item, &node->knownNamespaces, next) {
Branch: 1 branch
BranchTrueFalseTotal
TAILQ_FOREACH(item, &node->knownNamespaces, next) { 0 0 0
2093 0 ▶ if (strcmp(item->ns->uri, nsUri) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(item->ns->uri, nsUri) == 0) 0 0 0
2094 0 return item->ns;
2095 }
2096 0 return NULL;
2097 }
2098  
2099 XmlNamespace *
2100 15 XmlGetNodeNamespace(XmlNode *node) {
2101 15 XmlNode *p = node->parent;
2102 15 ▶ if (node->ns) // my namespace
Branch: 1 branch
BranchTrueFalseTotal
if (node->ns) // my namespace 7 8 15
2103 7 return node->ns;
2104 8 ▶ if (node->hns) // hinerited namespace
Branch: 1 branch
BranchTrueFalseTotal
if (node->hns) // hinerited namespace 7 1 8
2105 7 return node->hns;
2106 // search for a default naspace defined in our hierarchy
2107 // this should happen only if a node has been moved across
2108 // multiple documents and it's hinerited namespace has been lost
2109 3 ▶ while (p) {
Branch: 1 branch
BranchTrueFalseTotal
while (p) { 2 1 3
2110 2 ▶ if (p->cns)
Branch: 1 branch
BranchTrueFalseTotal
if (p->cns) 0 2 2
2111 0 return p->cns;
2112 2 p = p->parent;
2113 }
2114 1 return NULL;
2115 }
2116  
2117 XmlErr
2118 0 XmlSetNodeCNamespace(XmlNode *node, XmlNamespace *ns) {
2119 0 ▶ if (!node || !ns)
Branch: 2 branches
BranchTrueFalseTotal
if (!node || !ns) 0 0 0
if (!node || !ns) 0 0 0
2120 0 return XML_BADARGS;
2121
2122 0 node->cns = ns;
2123 0 return XML_NOERR;
2124 }
2125  
2126 XmlErr
2127 0 XmlSetNodeNamespace(XmlNode *node, XmlNamespace *ns) {
2128 0 ▶ if (!node || !ns)
Branch: 2 branches
BranchTrueFalseTotal
if (!node || !ns) 0 0 0
if (!node || !ns) 0 0 0
2129 0 return XML_BADARGS;
2130
2131 0 node->ns = ns;
2132 0 return XML_NOERR;
2133 }
2134  
« const-xs.inc ↑ Summary const-c.inc »