1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
#include <stdlib.h>
#include "xml.h"
/* parse a string into a document */
struct kai_tag_t * kai_parse_string(const char *str)
{
yyscan_t scanner;
struct kai_tag_t *document;
kalmialex_init(&scanner);
YY_BUFFER_STATE buf = kalmia_scan_string(str, scanner);
int result = kalmiaparse(scanner, &document);
kalmia_delete_buffer(buf, scanner);
kalmialex_destroy(scanner);
if (result != 0) {
return NULL;
}
return document;
}
char * kai_tag_get_attr(struct kai_tag_t *t, const char *attr)
{
struct kai_attr_t *a = t->attrs;
while (a != NULL) {
if (strcmp(a->key, attr) == 0) {
return a->value;
}
a = a->next;
}
return NULL;
}
long kai_tag_attr_to_long(
struct kai_tag_t *t, const char *attr, long base)
{
char *val = kai_tag_get_attr(t, attr);
if (val == NULL) {
/* attribute not present, fall back to base */
return base;
}
else {
return strtol(val, NULL, 10);
}
}
char *kai_tag_attr_to_dup(struct kai_tag_t *t, const char *attr)
{
char *val = kai_tag_get_attr(t, attr);
if (val == NULL) {
return NULL;
}
else {
return strdup(val);
}
}
int kai_tag_num_children(struct kai_tag_t *t)
{
int count = 0;
struct kai_tag_t *child = t->children;
while (child != NULL) {
count += 1;
child = child->next;
}
return count;
}
|