Fix debug mode compilation on 64-bit boxes.
[usmb.git] / xml.c
blob6499a88b67a8dfb6a5592ee689162222714ae5fd
1 /* usmb - mount SMB shares via FUSE and Samba
2 * Copyright (C) 2006-2010 Geoff Johnstone
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "config.h"
18 #include <libxml/xmlreader.h>
19 #include <libxml/xpath.h>
20 #include <libxml/xpathInternals.h>
21 #include <assert.h>
22 #include <stdbool.h>
23 #include <string.h>
24 #include "xml.h"
25 #include "utils.h"
28 bool xml_validate_relaxng (xmlDocPtr doc, const char *schema)
30 xmlRelaxNGParserCtxtPtr rngpcptr = NULL;
31 xmlRelaxNGPtr rngptr = NULL;
32 xmlRelaxNGValidCtxtPtr rngvptr = NULL;
33 bool ret = false;
35 assert (NULL != doc);
39 rngpcptr = xmlRelaxNGNewMemParserCtxt (schema, strlen (schema));
40 if (NULL == rngpcptr)
41 break;
43 rngptr = xmlRelaxNGParse (rngpcptr);
44 if (NULL == rngptr)
45 break;
47 rngvptr = xmlRelaxNGNewValidCtxt (rngptr);
48 if (NULL == rngvptr)
49 break;
51 ret = (0 == xmlRelaxNGValidateDoc (rngvptr, doc));
52 } while (false /*CONSTCOND*/);
54 if (NULL != rngvptr)
55 xmlRelaxNGFreeValidCtxt (rngvptr);
57 if (NULL != rngptr)
58 xmlRelaxNGFree (rngptr);
60 if (NULL != rngpcptr)
61 xmlRelaxNGFreeParserCtxt (rngpcptr);
63 return ret;
67 bool xml_xpath_attr_value (xmlXPathContextPtr ctx,
68 char *xpath,
69 const char *attr,
70 char **out)
72 xmlXPathObjectPtr obj;
73 xmlChar *tmp;
75 assert (NULL != ctx);
76 assert (NULL != xpath);
77 assert (NULL != out);
79 *out = NULL;
81 obj = xmlXPathEval (BAD_CAST xpath, ctx);
82 if (NULL == obj)
84 DEBUG (fputs ("XPath evaluation error\n", stderr));
85 return false;
90 if (XPATH_NODESET != obj->type)
92 DEBUG (fputs ("XPath evaluation didn't return a nodeset\n", stderr));
93 break;
96 if (NULL == obj->nodesetval)
98 DEBUG (fputs ("nodesetval is NULL\n", stderr));
99 break;
102 if (1 != obj->nodesetval->nodeNr)
104 DEBUG (fprintf (stderr, "Nodeset has %d elements\n",
105 obj->nodesetval->nodeNr));
106 break;
109 tmp = xmlGetProp (obj->nodesetval->nodeTab[0], BAD_CAST attr);
110 if (NULL == tmp)
111 break;
113 *out = xstrdup ((char *)tmp);
114 if (NULL == *out)
115 break;
117 xmlXPathFreeObject (obj);
118 return true;
119 /*NOTREACHED*/
120 } while (false /*CONSTCOND*/);
122 *out = NULL;
123 xmlXPathFreeObject (obj);
124 return false;
128 bool xml_xpath_text (xmlXPathContextPtr ctx, char *xpath, char **out)
130 xmlXPathObjectPtr obj;
132 assert (NULL != ctx);
133 assert (NULL != xpath);
134 assert (NULL != out);
136 *out = NULL;
138 DEBUG (fprintf (stderr, "xml_xpath_text (%s)\n", xpath));
140 obj = xmlXPathEval (BAD_CAST xpath, ctx);
141 if (NULL == obj)
143 DEBUG (fputs ("XPath evaluation error\n", stderr));
144 return false;
149 if (XPATH_NODESET != obj->type)
151 DEBUG (fputs ("XPath evaluation didn't return a nodeset\n", stderr));
152 break;
155 if (NULL == obj->nodesetval)
157 DEBUG (fputs ("nodesetval is NULL\n", stderr));
158 break;
161 if (1 != obj->nodesetval->nodeNr)
163 DEBUG (fprintf (stderr, "Nodeset has %d elements\n",
164 obj->nodesetval->nodeNr));
165 break;
168 if (NULL == obj->nodesetval->nodeTab[0]->content)
170 DEBUG (fputs ("Node has no text\n", stderr));
171 break;
174 *out = xstrdup ((char *)obj->nodesetval->nodeTab[0]->content);
175 if (NULL == *out)
176 break;
178 xmlXPathFreeObject (obj);
179 return true;
180 /*NOTREACHED*/
181 } while (false /*CONSTCOND*/);
183 *out = NULL;
184 xmlXPathFreeObject (obj);
185 return false;