forked from lantus/Cannonball-C
-
Notifications
You must be signed in to change notification settings - Fork 3
/
xmlutils.c
154 lines (117 loc) · 3.69 KB
/
xmlutils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/***************************************************************************
Xml Helper Functions
Copyright Chris White.
See license.txt for more details.
***************************************************************************/
#include "xmlutils.h"
#include "utils.h"
XMLNode* GetXmlDocNode(XMLDoc* doc, const char* path)
{
XMLSearch search;
XMLSearch_init_from_XPath(path, &search);
int loop = 0;
for (loop = 0; loop < doc->n_nodes; loop++)
{
XMLNode* node = XMLSearch_next(doc->nodes[loop], &search);
if (node != NULL)
{
return node;
}
}
return NULL;
}
const char* GetXMLDocValueString(XMLDoc* doc, const char* path, const char* defaultValue)
{
XMLNode* node = GetXmlDocNode(doc, path);
if (node != 0)
{
return node->text;
}
return defaultValue;
}
int GetXMLDocValueInt(XMLDoc* doc, const char* path, int defaultValue)
{
XMLNode* node = GetXmlDocNode(doc, path);
if (node != 0)
{
int value;
sscanf(node->text, "%d", &value);
return value;
}
return defaultValue;
}
const char* GetXmlDocAttributeString(XMLDoc* doc, const char* path, const char* attribute, const char* defaultValue)
{
XMLNode* node = GetXmlDocNode(doc, path);
if (node)
{
SXML_CHAR* attributeValue;
XMLNode_get_attribute_with_default(node, attribute, &attributeValue, defaultValue);
return attributeValue;
}
return defaultValue;
}
int GetXmlDocAttributeInt(XMLDoc* doc, const char* path, const char* attribute, int defaultValue)
{
const char* result = GetXmlDocAttributeString(doc, path, attribute, Utils_int_to_string(defaultValue));
int value;
sscanf(result, "%d", &value);
return value;
}
void AddXmlHeader(XMLDoc* doc)
{
XMLNode* node = XMLNode_alloc();
XMLNode_set_tag(node, "xml version=\"1.0\" encoding=\"utf-8\"");
XMLNode_set_type(node, TAG_INSTR);
XMLDoc_add_node(doc, node);
}
XMLNode* AddXmlFatherNode(XMLDoc* doc, const char* nodeName)
{
XMLNode* node = XMLNode_alloc();
XMLNode_set_tag(node, nodeName);
XMLNode_set_type(node, TAG_FATHER);
XMLDoc_add_node(doc, node);
return node;
}
void AddXmlChildNodeRootString(XMLDoc* doc, const char* nodeName, const char* text)
{
XMLNode* node = XMLNode_alloc();
XMLNode_set_tag(node, nodeName);
XMLNode_set_text(node, text);
XMLDoc_add_child_root(doc, node);
}
void AddXmlChildNodeRootInt(XMLDoc* doc, const char* nodeName, int value)
{
AddXmlChildNodeRootString(doc, nodeName, Utils_int_to_string(value));
}
XMLNode* AddNode(XMLDoc* doc, XMLNode* parent, const char* nodeName)
{
XMLNode* node = XMLNode_alloc();
XMLNode_set_tag(node, nodeName);
if (parent == NULL)
{
parent = doc->nodes[doc->i_root];
}
XMLNode_add_child(parent, node);
return node;
}
XMLNode* AddNodeString(XMLDoc* doc, XMLNode* parent, const char* nodeName, const char* text)
{
XMLNode* node = AddNode(doc, parent, nodeName);
XMLNode_set_text(node, text);
return node;
}
XMLNode* AddNodeInt(XMLDoc* doc, XMLNode* parent, const char* nodeName, int value)
{
return AddNodeString(doc, parent, nodeName, Utils_int_to_string(value));
}
XMLNode* AddNodeAttributeString(XMLDoc* doc, XMLNode* parent, const char* nodeName, const char* attributeName, const char* text)
{
XMLNode* node = AddNode(doc, parent, nodeName);
XMLNode_set_attribute(node, attributeName, text);
return node;
}
XMLNode* AddNodeAttributeInt(XMLDoc* doc, XMLNode* parent, const char* nodeName, const char* attributeName, int value)
{
return AddNodeAttributeString(doc, parent, nodeName, attributeName, Utils_int_to_string(value));
}