X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=obt%2Fxml.c;h=223ad02b239df6dd0e173e7fb536e01b1027127a;hb=4e6c0086a657399d989f2e4849f7b397d7d4efbc;hp=92e234306fbaaf201f7475623b644a0e1c469c82;hpb=567fd15eebdd44e50cef140419dbf7a336708109;p=chaz%2Fopenbox diff --git a/obt/xml.c b/obt/xml.c index 92e23430..223ad02b 100644 --- a/obt/xml.c +++ b/obt/xml.c @@ -19,6 +19,7 @@ #include "obt/xml.h" #include "obt/paths.h" +#include #include #ifdef HAVE_STDLIB_H @@ -47,17 +48,22 @@ struct _ObtXmlInst { xmlDocPtr doc; xmlNodePtr root; gchar *path; + gchar *last_error_file; + gint last_error_line; + gchar *last_error_message; }; +static void obt_xml_save_last_error(ObtXmlInst* inst); + static void destfunc(struct Callback *c) { g_free(c->tag); - g_free(c); + g_slice_free(struct Callback, c); } ObtXmlInst* obt_xml_instance_new(void) { - ObtXmlInst *i = g_new(ObtXmlInst, 1); + ObtXmlInst *i = g_slice_new(ObtXmlInst); i->ref = 1; i->xdg_paths = obt_paths_new(); i->callbacks = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, @@ -65,6 +71,9 @@ ObtXmlInst* obt_xml_instance_new(void) i->doc = NULL; i->root = NULL; i->path = NULL; + i->last_error_file = NULL; + i->last_error_line = -1; + i->last_error_message = NULL; return i; } @@ -78,7 +87,9 @@ void obt_xml_instance_unref(ObtXmlInst *i) if (i && --i->ref == 0) { obt_paths_unref(i->xdg_paths); g_hash_table_destroy(i->callbacks); - g_free(i); + g_free(i->last_error_file); + g_free(i->last_error_message); + g_slice_free(ObtXmlInst, i); } } @@ -104,13 +115,18 @@ void obt_xml_register(ObtXmlInst *i, const gchar *tag, return; } - c = g_new(struct Callback, 1); + c = g_slice_new(struct Callback); c->tag = g_strdup(tag); c->func = func; c->data = data; g_hash_table_insert(i->callbacks, c->tag, c); } +void obt_xml_unregister(ObtXmlInst *i, const gchar *tag) +{ + g_hash_table_remove(i->callbacks, tag); +} + static gboolean load_file(ObtXmlInst *i, const gchar *domain, const gchar *filename, @@ -122,6 +138,8 @@ static gboolean load_file(ObtXmlInst *i, g_assert(i->doc == NULL); /* another doc isn't open already? */ + xmlResetLastError(); + for (it = paths; !r && it; it = g_slist_next(it)) { gchar *path; struct stat s; @@ -136,6 +154,8 @@ static gboolean load_file(ObtXmlInst *i, with extra nodes in it. */ i->doc = xmlReadFile(path, NULL, (XML_PARSE_NOBLANKS | XML_PARSE_RECOVER)); + xmlXIncludeProcessFlags(i->doc, (XML_PARSE_NOBLANKS | + XML_PARSE_RECOVER)); if (i->doc) { i->root = xmlDocGetRootElement(i->doc); if (!i->root) { @@ -161,6 +181,8 @@ static gboolean load_file(ObtXmlInst *i, g_free(path); } + obt_xml_save_last_error(i); + return r; } @@ -256,6 +278,8 @@ gboolean obt_xml_load_mem(ObtXmlInst *i, g_assert(i->doc == NULL); /* another doc isn't open already? */ + xmlResetLastError(); + i->doc = xmlParseMemory(data, len); if (i) { i->root = xmlDocGetRootElement(i->doc); @@ -274,9 +298,51 @@ gboolean obt_xml_load_mem(ObtXmlInst *i, else r = TRUE; /* ok ! */ } + + obt_xml_save_last_error(i); + return r; } +static void obt_xml_save_last_error(ObtXmlInst* inst) +{ + xmlErrorPtr error = xmlGetLastError(); + if (error) { + inst->last_error_file = g_strdup(error->file); + inst->last_error_line = error->line; + inst->last_error_message = g_strdup(error->message); + xmlResetError(error); + } +} + +gboolean obt_xml_last_error(ObtXmlInst *inst) +{ + return inst->last_error_file && + inst->last_error_line >= 0 && + inst->last_error_message; +} + +gchar* obt_xml_last_error_file(ObtXmlInst *inst) +{ + if (!obt_xml_last_error(inst)) + return NULL; + return inst->last_error_file; +} + +gint obt_xml_last_error_line(ObtXmlInst *inst) +{ + if (!obt_xml_last_error(inst)) + return -1; + return inst->last_error_line; +} + +gchar* obt_xml_last_error_message(ObtXmlInst *inst) +{ + if (!obt_xml_last_error(inst)) + return NULL; + return inst->last_error_message; +} + gboolean obt_xml_save_file(ObtXmlInst *inst, const gchar *path, gboolean pretty) @@ -300,8 +366,10 @@ void obt_xml_tree(ObtXmlInst *i, xmlNodePtr node) g_assert(i->doc); /* a doc is open? */ while (node) { - struct Callback *c = g_hash_table_lookup(i->callbacks, node->name); - if (c) c->func(node, c->data); + if (node->name) { + struct Callback *c = g_hash_table_lookup(i->callbacks, node->name); + if (c) c->func(node, c->data); + } node = node->next; } } @@ -311,16 +379,22 @@ void obt_xml_tree_from_root(ObtXmlInst *i) obt_xml_tree(i, i->root->children); } -gchar *obt_xml_node_string(xmlNodePtr node) +gchar *obt_xml_node_string_unstripped(xmlNodePtr node) { xmlChar *c = xmlNodeGetContent(node); gchar *s; - if (c) g_strstrip((char*)c); /* strip leading/trailing whitespace */ s = g_strdup(c ? (gchar*)c : ""); xmlFree(c); return s; } +gchar *obt_xml_node_string(xmlNodePtr node) +{ + gchar* result = obt_xml_node_string_unstripped(node); + g_strstrip(result); /* strip leading/trailing whitespace */ + return result; +} + gint obt_xml_node_int(xmlNodePtr node) { xmlChar *c = xmlNodeGetContent(node); @@ -403,13 +477,12 @@ gboolean obt_xml_attr_int(xmlNodePtr node, const gchar *name, gint *value) return r; } -gboolean obt_xml_attr_string(xmlNodePtr node, const gchar *name, - gchar **value) +gboolean obt_xml_attr_string_unstripped(xmlNodePtr node, const gchar *name, + gchar **value) { xmlChar *c = xmlGetProp(node, (const xmlChar*) name); gboolean r = FALSE; if (c) { - g_strstrip((char*)c); /* strip leading/trailing whitespace */ *value = g_strdup((gchar*)c); r = TRUE; } @@ -417,6 +490,15 @@ gboolean obt_xml_attr_string(xmlNodePtr node, const gchar *name, return r; } +gboolean obt_xml_attr_string(xmlNodePtr node, const gchar *name, + gchar **value) +{ + gboolean result = obt_xml_attr_string_unstripped(node, name, value); + if (result) + g_strstrip(*value); /* strip leading/trailing whitespace */ + return result; +} + gboolean obt_xml_attr_contains(xmlNodePtr node, const gchar *name, const gchar *val) {