summaryrefslogtreecommitdiff
path: root/src/xml-parser.c
diff options
context:
space:
mode:
authorTomas Bzatek <tbzatek@users.sourceforge.net>2010-12-12 17:18:53 +0100
committerTomas Bzatek <tbzatek@users.sourceforge.net>2010-12-12 17:18:53 +0100
commit9d995d59d4868a0ed02dc37892d5f670bd4eb86b (patch)
tree7758b8c834e1601d1a6bc0188b9d437742144c2b /src/xml-parser.c
parent9f32e7a3eb5559d1e1b3a5de2c12e3e92ac6af0c (diff)
downloadcataract-9d995d59d4868a0ed02dc37892d5f670bd4eb86b.tar.xz
xml-parser: Cleanup
Diffstat (limited to 'src/xml-parser.c')
-rw-r--r--src/xml-parser.c95
1 files changed, 44 insertions, 51 deletions
diff --git a/src/xml-parser.c b/src/xml-parser.c
index 70c7528..3eae5b1 100644
--- a/src/xml-parser.c
+++ b/src/xml-parser.c
@@ -43,20 +43,19 @@ xml_parser_load (const gchar *filename)
file = g_malloc0 (sizeof (TXMLFile));
- /* Load XML document */
+ /* Load XML document */
file->doc = xmlParseFile (filename);
- if (file->doc == NULL) {
- log_error ("Error: unable to parse file \"%s\"\n", filename);
- g_free (file);
- return NULL;
+ if (! file->doc) {
+ log_error ("Error: unable to parse file \"%s\"\n", filename);
+ xml_parser_free (file);
+ return NULL;
}
- /* Create xpath evaluation context */
+ /* Create xpath evaluation context */
file->xpathCtx = xmlXPathNewContext (file->doc);
- if (file->xpathCtx == NULL) {
+ if (! file->xpathCtx) {
log_error ("Error: unable to create new XPath context\n");
- xmlFreeDoc (file->doc);
- g_free (file);
+ xml_parser_free (file);
return FALSE;
}
@@ -65,17 +64,17 @@ xml_parser_load (const gchar *filename)
/*
- * xml_parser_close: close the XML document parser
+ * xml_parser_free: close the XML document parser and frees all memory
*/
void
-xml_parser_close (TXMLFile *file)
+xml_parser_free (TXMLFile *file)
{
- if (file)
- {
- xmlXPathFreeContext (file->xpathCtx);
- xmlFreeDoc (file->doc);
+ if (file) {
+ if (file->xpathCtx)
+ xmlXPathFreeContext (file->xpathCtx);
+ if (file->doc)
+ xmlFreeDoc (file->doc);
g_free (file);
- file = NULL;
}
}
@@ -89,21 +88,21 @@ xml_file_get_node_name (TXMLFile *file, const gchar *x_path)
xmlNodePtr cur;
gchar *attrv;
- if ((! file) || (! x_path))
+ if (! file || ! x_path)
return NULL;
/* Evaluate xpath expression */
xpathObj = xmlXPathEvalExpression ((const xmlChar *) x_path, file->xpathCtx);
if (xpathObj == NULL) {
- log_error ("Error: unable to evaluate xpath expression \"%s\"\n", x_path);
- return NULL;
+ log_error ("Error: unable to evaluate xpath expression \"%s\"\n", x_path);
+ return NULL;
}
attrv = NULL;
if ((xpathObj->nodesetval) && (xpathObj->nodesetval->nodeNr > 0)) {
- cur = xpathObj->nodesetval->nodeTab[0];
- if (cur->name)
- attrv = g_strdup ((const gchar *) cur->name);
+ cur = xpathObj->nodesetval->nodeTab[0];
+ if (cur->name)
+ attrv = g_strdup ((const gchar *) cur->name);
}
xmlXPathFreeObject (xpathObj);
@@ -124,36 +123,30 @@ xml_file_get_node_value (TXMLFile *file, const gchar *x_path)
gchar *val, *valx;
int i;
- if ((! file) || (! x_path))
+ if (! file || ! x_path)
return NULL;
/* Evaluate xpath expression */
xpathObj = xmlXPathEvalExpression ((const xmlChar *) x_path, file->xpathCtx);
if (xpathObj == NULL) {
- log_error ("Error: unable to evaluate xpath expression \"%s\"\n", x_path);
- return NULL;
+ log_error ("Error: unable to evaluate xpath expression \"%s\"\n", x_path);
+ return NULL;
}
val = NULL;
- if ((xpathObj->nodesetval) && (xpathObj->nodesetval->nodeNr > 0)) {
- for (i = 0; i < xpathObj->nodesetval->nodeNr; i++)
- {
- cur = xpathObj->nodesetval->nodeTab[i];
- if (cur->content)
- {
- if (val == NULL)
- {
- val = g_strdup ((gchar *) cur->content);
- }
- else
- {
- valx = g_strconcat (val, (gchar *) cur->content, NULL);
- g_free (val);
- val = valx;
- }
- }
+ if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0)
+ for (i = 0; i < xpathObj->nodesetval->nodeNr; i++) {
+ cur = xpathObj->nodesetval->nodeTab[i];
+ if (cur->content) {
+ if (val == NULL) {
+ val = g_strdup ((gchar *) cur->content);
+ } else {
+ valx = g_strconcat (val, (gchar *) cur->content, NULL);
+ g_free (val);
+ val = valx;
+ }
}
- }
+ }
xmlXPathFreeObject (xpathObj);
return val;
@@ -171,18 +164,18 @@ xml_file_get_node_attribute (TXMLFile *file, const gchar *x_path, const gchar *a
xmlChar *attrvx;
gchar *attrv;
- if ((! file) || (! x_path))
+ if (! file || ! x_path)
return NULL;
/* Evaluate xpath expression */
xpathObj = xmlXPathEvalExpression ((const xmlChar *) x_path, file->xpathCtx);
if (xpathObj == NULL) {
- log_error ("Error: unable to evaluate xpath expression \"%s\"\n", x_path);
- return NULL;
+ log_error ("Error: unable to evaluate xpath expression \"%s\"\n", x_path);
+ return NULL;
}
attrv = NULL;
- if ((xpathObj->nodesetval) && (xpathObj->nodesetval->nodeNr > 0)) {
+ if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
cur = xpathObj->nodesetval->nodeTab[0];
attrvx = xmlGetProp (cur, (const xmlChar *) attr);
if (attrvx) {
@@ -242,19 +235,19 @@ xml_file_node_get_children_count (TXMLFile *file, const gchar *x_path)
xmlXPathObjectPtr xpathObj;
int count;
- if ((! file) || (! x_path))
+ if (! file || ! x_path)
return 0;
/* Evaluate xpath expression */
xpathObj = xmlXPathEvalExpression ((const xmlChar *) x_path, file->xpathCtx);
if (xpathObj == NULL) {
- log_error ("Error: unable to evaluate xpath expression \"%s\"\n", x_path);
- return 0;
+ log_error ("Error: unable to evaluate xpath expression \"%s\"\n", x_path);
+ return 0;
}
count = 0;
if (xpathObj->nodesetval)
- count = xpathObj->nodesetval->nodeNr;
+ count = xpathObj->nodesetval->nodeNr;
xmlXPathFreeObject (xpathObj);
return count;