From 9d995d59d4868a0ed02dc37892d5f670bd4eb86b Mon Sep 17 00:00:00 2001 From: Tomas Bzatek Date: Sun, 12 Dec 2010 17:18:53 +0100 Subject: xml-parser: Cleanup --- src/items.c | 6 ++-- src/setup.c | 2 +- src/xml-parser.c | 95 ++++++++++++++++++++++++++------------------------------ src/xml-parser.h | 4 +-- 4 files changed, 50 insertions(+), 57 deletions(-) (limited to 'src') diff --git a/src/items.c b/src/items.c index b31e9cd..06864dd 100644 --- a/src/items.c +++ b/src/items.c @@ -279,7 +279,7 @@ parse_album_xml (const gchar *filename) g_free (node_name); } - xml_parser_close (xml); + xml_parser_free (xml); return index; } @@ -327,7 +327,7 @@ get_album_objects_count (const gchar *filename) count = xml_file_node_get_children_count (xml, "/gallery/items/item"); hidden = xml_file_node_get_children_count (xml, "/gallery/items/item/hidden"); - xml_parser_close (xml); + xml_parser_free (xml); return (count - hidden); } @@ -355,5 +355,5 @@ get_album_titles (const gchar *filename, gchar **title, gchar **description, gch *thumbnail = xml_file_get_node_attribute (xml, "/gallery/items/item[1]", "preview"); } - xml_parser_close (xml); + xml_parser_free (xml); } diff --git a/src/setup.c b/src/setup.c index a639348..21ae26d 100644 --- a/src/setup.c +++ b/src/setup.c @@ -165,7 +165,7 @@ parse_setup_xml (const gchar *filename) setup->squared_thumbnail_type = THUMBNAIL_SQUARE_TYPE_NONE; g_free (s); - xml_parser_close (xml); + xml_parser_free (xml); return setup; } 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; diff --git a/src/xml-parser.h b/src/xml-parser.h index c750b57..77900e6 100644 --- a/src/xml-parser.h +++ b/src/xml-parser.h @@ -38,9 +38,9 @@ typedef struct { TXMLFile * 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); +void xml_parser_free (TXMLFile *file); /* * xml_file_get_node_name: retrieve name of the XPath node -- cgit v1.2.3