diff options
Diffstat (limited to 'trace-xml.c')
-rw-r--r-- | trace-xml.c | 104 |
1 files changed, 103 insertions, 1 deletions
diff --git a/trace-xml.c b/trace-xml.c index 0bc130c..47cb3c9 100644 --- a/trace-xml.c +++ b/trace-xml.c | |||
@@ -25,13 +25,20 @@ | |||
25 | 25 | ||
26 | #include <libxml/xmlwriter.h> | 26 | #include <libxml/xmlwriter.h> |
27 | #include <libxml/parser.h> | 27 | #include <libxml/parser.h> |
28 | #include <libxml/encoding.h> | 28 | #include <libxml/xpath.h> |
29 | 29 | ||
30 | #include "trace-cmd.h" | 30 | #include "trace-cmd.h" |
31 | #include "trace-xml.h" | 31 | #include "trace-xml.h" |
32 | 32 | ||
33 | struct tracecmd_xml_handle { | 33 | struct tracecmd_xml_handle { |
34 | xmlTextWriterPtr writer; | 34 | xmlTextWriterPtr writer; |
35 | xmlDocPtr doc; | ||
36 | }; | ||
37 | |||
38 | struct tracecmd_xml_system { | ||
39 | struct tracecmd_xml_handle *handle; | ||
40 | xmlXPathObjectPtr result; | ||
41 | xmlNodePtr cur; | ||
35 | }; | 42 | }; |
36 | 43 | ||
37 | #define TRACE_ENCODING "UTF-8" | 44 | #define TRACE_ENCODING "UTF-8" |
@@ -122,6 +129,101 @@ void tracecmd_xml_close(struct tracecmd_xml_handle *handle) | |||
122 | xmlTextWriterEndDocument(handle->writer); | 129 | xmlTextWriterEndDocument(handle->writer); |
123 | xmlFreeTextWriter(handle->writer); | 130 | xmlFreeTextWriter(handle->writer); |
124 | } | 131 | } |
132 | if (handle->doc) { | ||
133 | xmlFreeDoc(handle->doc); | ||
134 | } | ||
135 | free(handle); | ||
136 | } | ||
137 | |||
138 | /***********************************************************/ | ||
139 | /*** Reading XML files ***/ | ||
140 | /***********************************************************/ | ||
141 | |||
142 | |||
143 | struct tracecmd_xml_handle *tracecmd_xml_open(const char *file) | ||
144 | { | ||
145 | struct tracecmd_xml_handle *handle; | ||
125 | 146 | ||
147 | handle = malloc_or_die(sizeof(*handle)); | ||
148 | memset(handle, 0, sizeof(*handle)); | ||
149 | |||
150 | handle->doc = xmlParseFile(file); | ||
151 | if (!handle->doc) | ||
152 | goto fail_free; | ||
153 | |||
154 | return handle; | ||
155 | |||
156 | fail_free: | ||
126 | free(handle); | 157 | free(handle); |
158 | return NULL; | ||
159 | } | ||
160 | |||
161 | struct tracecmd_xml_system * | ||
162 | tracecmd_xml_find_system(struct tracecmd_xml_handle *handle, | ||
163 | const char *system) | ||
164 | { | ||
165 | struct tracecmd_xml_system *sys; | ||
166 | xmlXPathContextPtr context; | ||
167 | xmlXPathObjectPtr result; | ||
168 | xmlChar *xpath; | ||
169 | char *path; | ||
170 | |||
171 | path = malloc_or_die(strlen(system) + 3); | ||
172 | sprintf(path, "//%s", system); | ||
173 | xpath = BAD_CAST path; | ||
174 | |||
175 | context = xmlXPathNewContext(handle->doc); | ||
176 | result = xmlXPathEvalExpression(xpath, context); | ||
177 | free(path); | ||
178 | |||
179 | if (xmlXPathNodeSetIsEmpty(result->nodesetval)) { | ||
180 | xmlXPathFreeObject(result); | ||
181 | return NULL; | ||
182 | } | ||
183 | |||
184 | sys = malloc_or_die(sizeof(*sys)); | ||
185 | sys->handle = handle; | ||
186 | sys->result = result; | ||
187 | sys->cur = result->nodesetval->nodeTab[0]->xmlChildrenNode; | ||
188 | |||
189 | return sys; | ||
190 | } | ||
191 | |||
192 | struct tracecmd_xml_system_node * | ||
193 | tracecmd_xml_system_node(struct tracecmd_xml_system *system) | ||
194 | { | ||
195 | return (struct tracecmd_xml_system_node *)system->cur; | ||
196 | } | ||
197 | |||
198 | const char *tracecmd_xml_node_type(struct tracecmd_xml_system_node *tnode) | ||
199 | { | ||
200 | xmlNodePtr node = (xmlNodePtr)tnode; | ||
201 | return (const char *)node->name; | ||
202 | } | ||
203 | |||
204 | struct tracecmd_xml_system_node * | ||
205 | tracecmd_xml_node_child(struct tracecmd_xml_system_node *tnode) | ||
206 | { | ||
207 | xmlNodePtr node = (xmlNodePtr)tnode; | ||
208 | return (struct tracecmd_xml_system_node *)node->xmlChildrenNode; | ||
209 | } | ||
210 | |||
211 | struct tracecmd_xml_system_node * | ||
212 | tracecmd_xml_node_next(struct tracecmd_xml_system_node *tnode) | ||
213 | { | ||
214 | xmlNodePtr node = (xmlNodePtr)tnode; | ||
215 | return (struct tracecmd_xml_system_node *)node->next; | ||
216 | } | ||
217 | |||
218 | const char *tracecmd_xml_node_value(struct tracecmd_xml_handle *handle, | ||
219 | struct tracecmd_xml_system_node *tnode) | ||
220 | { | ||
221 | xmlNodePtr node = (xmlNodePtr)tnode; | ||
222 | return (const char *)xmlNodeListGetString(handle->doc, node->xmlChildrenNode, 1); | ||
223 | } | ||
224 | |||
225 | void tracecmd_xml_free_system(struct tracecmd_xml_system *system) | ||
226 | { | ||
227 | xmlXPathFreeObject(system->result); | ||
228 | free(system); | ||
127 | } | 229 | } |