put example files to dedicated dir
[monster.git] / xml.c
blob56ca64dc2f5db8aa3b84ddd874668d0e48f705ad
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * XML parsing helpers
6 * Copyright (C) 2006,2007 Nedko Arnaudov <nedko@arnaudov.name>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *****************************************************************************/
23 #include <libxml/parser.h>
24 #include <libxml/xpath.h>
25 #include <string.h>
27 #define DISABLE_DEBUG_OUTPUT
29 #include "xml.h"
30 #include "log.h"
32 int
33 xml_get_content(
34 xmlDocPtr doc_ptr,
35 const char * xpath,
36 xmlBufferPtr content_buffer_ptr)
38 int ret;
39 xmlXPathContextPtr xpath_ctx;
40 xmlXPathObjectPtr xpath_obj;
42 /* Create xpath evaluation context */
43 xpath_ctx = xmlXPathNewContext(doc_ptr);
44 if (xpath_ctx == NULL)
46 ERROR_OUT("Unable to create new XPath context");
47 ret = -1;
48 goto exit;
51 /* Evaluate xpath expression */
52 xpath_obj = xmlXPathEvalExpression((const xmlChar *)xpath, xpath_ctx);
53 if (xpath_obj == NULL)
55 ERROR_OUT("Unable to evaluate XPath expression \"%s\"", xpath);
56 ret = -1;
57 goto free_xpath_ctx;
60 if (xpath_obj->nodesetval == NULL || xpath_obj->nodesetval->nodeNr == 0)
62 ERROR_OUT("XPath \"%s\" evaluation returned no data", xpath);
63 ret = -1;
64 goto free_xpath_obj;
67 if (xpath_obj->nodesetval->nodeNr != 1)
69 ERROR_OUT("XPath \"%s\" evaluation returned multiple entries (%d)", xpath, xpath_obj->nodesetval->nodeNr);
70 ret = -1;
71 goto free_xpath_obj;
74 ret = xmlNodeBufGetContent(content_buffer_ptr, xpath_obj->nodesetval->nodeTab[0]);
75 if (ret != 0)
77 ERROR_OUT("xmlNodeBufGetContent() failed. (%d)", ret);
78 ret = -1;
79 goto free_xpath_obj;
82 ret = 0;
84 free_xpath_obj:
85 xmlXPathFreeObject(xpath_obj);
87 free_xpath_ctx:
88 xmlXPathFreeContext(xpath_ctx);
90 exit:
91 return ret;
94 int
95 xml_check_path(
96 xmlDocPtr doc_ptr,
97 const char * xpath)
99 int ret;
100 xmlXPathContextPtr xpath_ctx;
101 xmlXPathObjectPtr xpath_obj;
103 /* Create xpath evaluation context */
104 xpath_ctx = xmlXPathNewContext(doc_ptr);
105 if (xpath_ctx == NULL)
107 ERROR_OUT("Unable to create new XPath context");
108 ret = -1;
109 goto exit;
112 /* Evaluate xpath expression */
113 xpath_obj = xmlXPathEvalExpression((const xmlChar *)xpath, xpath_ctx);
114 if (xpath_obj == NULL)
116 ERROR_OUT("Unable to evaluate XPath expression \"%s\"", xpath);
117 ret = -1;
118 goto free_xpath_ctx;
121 if (xpath_obj->nodesetval == NULL || xpath_obj->nodesetval->nodeNr == 0)
123 DEBUG_OUT("XPath \"%s\" evaluation returned no data", xpath);
124 ret = 0;
125 goto free_xpath_obj;
128 if (xpath_obj->nodesetval->nodeNr != 1)
130 DEBUG_OUT("XPath \"%s\" evaluation returned multiple entries (%d)", xpath, xpath_obj->nodesetval->nodeNr);
131 ret = 2;
132 goto free_xpath_obj;
135 DEBUG_OUT("XPath \"%s\" evaluation returned one entry", xpath);
136 ret = 1;
138 free_xpath_obj:
139 xmlXPathFreeObject(xpath_obj);
141 free_xpath_ctx:
142 xmlXPathFreeContext(xpath_ctx);
144 exit:
145 return ret;
149 xml_get_string(
150 xmlDocPtr doc_ptr,
151 const char * xpath,
152 char * buffer_ptr,
153 size_t buffer_size)
155 int ret;
156 xmlBufferPtr content_buffer_ptr;
158 content_buffer_ptr = xmlBufferCreate();
159 if (content_buffer_ptr == NULL)
161 ERROR_OUT("xmlBufferCreate() failed.");
162 ret = -1;
163 goto exit;
166 ret = xml_get_content(doc_ptr, xpath, content_buffer_ptr);
167 if (ret != 0)
169 goto free_buffer;
172 DEBUG_OUT(
173 "XPath \"%s\" execution returned content \"%s\" (%u bytes)",
174 xpath,
175 xmlBufferContent(content_buffer_ptr),
176 xmlBufferLength(content_buffer_ptr));
178 if (buffer_size < xmlBufferLength(content_buffer_ptr) + 1)
180 ERROR_OUT(
181 "%u chars are requires to store \"%s\" but only %u are available",
182 xmlBufferLength(content_buffer_ptr) + 1,
183 xmlBufferContent(content_buffer_ptr),
184 buffer_size);
185 ret = -1;
186 goto free_buffer;
189 memcpy(buffer_ptr, xmlBufferContent(content_buffer_ptr), xmlBufferLength(content_buffer_ptr) + 1);
190 ret = 0;
192 free_buffer:
193 xmlBufferFree(content_buffer_ptr);
195 exit:
196 return ret;
200 xml_get_string_dup(
201 xmlDocPtr doc_ptr,
202 const char * xpath,
203 char ** buffer_ptr_ptr)
205 int ret;
206 xmlBufferPtr content_buffer_ptr;
207 char * buffer_ptr;
209 content_buffer_ptr = xmlBufferCreate();
210 if (content_buffer_ptr == NULL)
212 ERROR_OUT("xmlBufferCreate() failed.");
213 ret = -1;
214 goto exit;
217 ret = xml_get_content(doc_ptr, xpath, content_buffer_ptr);
218 if (ret != 0)
220 goto free_buffer;
223 DEBUG_OUT(
224 "XPath \"%s\" execution returned content \"%s\" (%u bytes)",
225 xpath,
226 xmlBufferContent(content_buffer_ptr),
227 xmlBufferLength(content_buffer_ptr));
229 buffer_ptr = strdup((const char *)xmlBufferContent(content_buffer_ptr));
230 if (buffer_ptr == NULL)
232 ERROR_OUT("strdup failed.");
233 ret = -1;
234 goto free_buffer;
237 *buffer_ptr_ptr = buffer_ptr;
238 ret = 0;
240 free_buffer:
241 xmlBufferFree(content_buffer_ptr);
243 exit:
244 return ret;
248 xml_get_int(
249 xmlDocPtr doc_ptr,
250 const char * xpath,
251 int * value_ptr)
253 int ret;
254 xmlBufferPtr content_buffer_ptr;
256 content_buffer_ptr = xmlBufferCreate();
257 if (content_buffer_ptr == NULL)
259 ERROR_OUT("xmlBufferCreate() failed.");
260 ret = -1;
261 goto exit;
264 ret = xml_get_content(doc_ptr, xpath, content_buffer_ptr);
265 if (ret != 0)
267 goto free_buffer;
270 DEBUG_OUT(
271 "XPath \"%s\" execution returned content \"%s\" (%u bytes)",
272 xpath,
273 xmlBufferContent(content_buffer_ptr),
274 xmlBufferLength(content_buffer_ptr));
276 *value_ptr = atoi((const char *)xmlBufferContent(content_buffer_ptr));
277 ret = 0;
279 free_buffer:
280 xmlBufferFree(content_buffer_ptr);
282 exit:
283 return ret;
287 xml_get_uint(
288 xmlDocPtr doc_ptr,
289 const char * xpath,
290 unsigned int * value_ptr)
292 int ret;
293 xmlBufferPtr content_buffer_ptr;
294 int value;
296 content_buffer_ptr = xmlBufferCreate();
297 if (content_buffer_ptr == NULL)
299 ERROR_OUT("xmlBufferCreate() failed.");
300 ret = -1;
301 goto exit;
304 ret = xml_get_content(doc_ptr, xpath, content_buffer_ptr);
305 if (ret != 0)
307 goto free_buffer;
310 DEBUG_OUT(
311 "XPath \"%s\" execution returned content \"%s\" (%u bytes)",
312 xpath,
313 xmlBufferContent(content_buffer_ptr),
314 xmlBufferLength(content_buffer_ptr));
316 value = atoi((const char *)xmlBufferContent(content_buffer_ptr));
317 if (value < 0)
319 ERROR_OUT("\"%s\" cannot be converted to unsigned int", xmlBufferContent(content_buffer_ptr));
320 return -1;
323 *value_ptr = (unsigned int)value;
324 ret = 0;
326 free_buffer:
327 xmlBufferFree(content_buffer_ptr);
329 exit:
330 return ret;