summaryrefslogtreecommitdiff
path: root/libpurple/xmlnode.c
diff options
context:
space:
mode:
authorMarcus Lundblad <malu@pidgin.im>2009-03-02 18:47:27 +0000
committerMarcus Lundblad <malu@pidgin.im>2009-03-02 18:47:27 +0000
commitfaf2dc3f086b8e2486c1ad6a389140b11c311d0b (patch)
tree251678823b2173ceb7f55fdcb0adc11487f534a3 /libpurple/xmlnode.c
parent965851333b7a36ab43d43513a1ab92eeab85a1d3 (diff)
parent8f4cd05bc2cbeb9d8975548858e65558600115bd (diff)
downloadpidgin-faf2dc3f086b8e2486c1ad6a389140b11c311d0b.tar.gz
propagate from branch 'im.pidgin.pidgin' (head 133e5eca834c5d4adad0937da2cb93df4e9c8b30)
to branch 'im.pidgin.pidgin.vv' (head 46202497d8c3543ee9c8d91aa15cb7ff5d19e9a6)
Diffstat (limited to 'libpurple/xmlnode.c')
-rw-r--r--libpurple/xmlnode.c110
1 files changed, 84 insertions, 26 deletions
diff --git a/libpurple/xmlnode.c b/libpurple/xmlnode.c
index 90cbb9dd0d..52cac8fb1c 100644
--- a/libpurple/xmlnode.c
+++ b/libpurple/xmlnode.c
@@ -129,7 +129,7 @@ xmlnode_remove_attrib(xmlnode *node, const char *attr)
for(attr_node = node->child; attr_node; attr_node = attr_node->next)
{
if(attr_node->type == XMLNODE_TYPE_ATTRIB &&
- !strcmp(attr_node->name, attr))
+ purple_strequal(attr_node->name, attr))
{
if(sibling == NULL) {
node->child = attr_node->next;
@@ -146,20 +146,6 @@ xmlnode_remove_attrib(xmlnode *node, const char *attr)
}
}
-/* Compare two nullable xmlns strings.
- * They are considered equal if they're both NULL or the strings are equal
- */
-static gboolean _xmlnode_compare_xmlns(const char *xmlns1, const char *xmlns2) {
- gboolean equal = FALSE;
-
- if (xmlns1 == NULL && xmlns2 == NULL)
- equal = TRUE;
- else if (xmlns1 != NULL && xmlns2 != NULL && !strcmp(xmlns1, xmlns2))
- equal = TRUE;
-
- return equal;
-}
-
void
xmlnode_remove_attrib_with_namespace(xmlnode *node, const char *attr, const char *xmlns)
{
@@ -171,8 +157,8 @@ xmlnode_remove_attrib_with_namespace(xmlnode *node, const char *attr, const char
for(attr_node = node->child; attr_node; attr_node = attr_node->next)
{
if(attr_node->type == XMLNODE_TYPE_ATTRIB &&
- !strcmp(attr_node->name, attr) &&
- _xmlnode_compare_xmlns(xmlns, attr_node->xmlns))
+ purple_strequal(attr, attr_node->name) &&
+ purple_strequal(xmlns, attr_node->xmlns))
{
if(sibling == NULL) {
node->child = attr_node->next;
@@ -252,7 +238,7 @@ xmlnode_get_attrib(xmlnode *node, const char *attr)
g_return_val_if_fail(attr != NULL, NULL);
for(x = node->child; x; x = x->next) {
- if(x->type == XMLNODE_TYPE_ATTRIB && !strcmp(attr, x->name)) {
+ if(x->type == XMLNODE_TYPE_ATTRIB && purple_strequal(attr, x->name)) {
return x->data;
}
}
@@ -270,8 +256,8 @@ xmlnode_get_attrib_with_namespace(xmlnode *node, const char *attr, const char *x
for(x = node->child; x; x = x->next) {
if(x->type == XMLNODE_TYPE_ATTRIB &&
- !strcmp(attr, x->name) &&
- _xmlnode_compare_xmlns(xmlns, x->xmlns)) {
+ purple_strequal(attr, x->name) &&
+ purple_strequal(xmlns, x->xmlns)) {
return x->data;
}
}
@@ -388,8 +374,8 @@ xmlnode_get_child_with_namespace(const xmlnode *parent, const char *name, const
if(ns)
xmlns = xmlnode_get_namespace(x);
- if(x->type == XMLNODE_TYPE_TAG && name && !strcmp(parent_name, x->name)
- && (!ns || (xmlns && !strcmp(ns, xmlns)))) {
+ if(x->type == XMLNODE_TYPE_TAG && purple_strequal(parent_name, x->name)
+ && purple_strequal(ns, xmlns)) {
ret = x;
break;
}
@@ -477,7 +463,7 @@ xmlnode_to_str_helper(const xmlnode *node, int *len, gboolean formatting, int de
g_hash_table_foreach(node->namespace_map,
(GHFunc)xmlnode_to_str_foreach_append_ns, text);
} else if (node->xmlns) {
- if(!node->parent || !node->parent->xmlns || strcmp(node->xmlns, node->parent->xmlns))
+ if(!node->parent || !purple_strequal(node->xmlns, node->parent->xmlns))
{
char *xmlns = g_markup_escape_text(node->xmlns, -1);
g_string_append_printf(text, " xmlns='%s'", xmlns);
@@ -648,7 +634,7 @@ xmlnode_parser_element_text_libxml(void *user_data, const xmlChar *text, int tex
if(!xpd->current || xpd->error)
return;
-
+
if(!text || !text_len)
return;
@@ -736,6 +722,78 @@ xmlnode_from_str(const char *str, gssize size)
return ret;
}
+xmlnode *
+xmlnode_from_file(const char *dir,const char *filename, const char *description, const char *process)
+{
+ gchar *filename_full;
+ GError *error = NULL;
+ gchar *contents = NULL;
+ gsize length;
+ xmlnode *node = NULL;
+
+ g_return_val_if_fail(dir != NULL, NULL);
+
+ purple_debug_info(process, "Reading file %s from directory %s\n",
+ filename, dir);
+
+ filename_full = g_build_filename(dir, filename, NULL);
+
+ if (!g_file_test(filename_full, G_FILE_TEST_EXISTS))
+ {
+ purple_debug_info(process, "File %s does not exist (this is not "
+ "necessarily an error)\n", filename_full);
+ g_free(filename_full);
+ return NULL;
+ }
+
+ if (!g_file_get_contents(filename_full, &contents, &length, &error))
+ {
+ purple_debug_error(process, "Error reading file %s: %s\n",
+ filename_full, error->message);
+ g_error_free(error);
+ }
+
+ if ((contents != NULL) && (length > 0))
+ {
+ node = xmlnode_from_str(contents, length);
+
+ /* If we were unable to parse the file then save its contents to a backup file */
+ if (node == NULL)
+ {
+ gchar *filename_temp, *filename_temp_full;
+
+ filename_temp = g_strdup_printf("%s~", filename);
+ filename_temp_full = g_build_filename(dir, filename_temp, NULL);
+
+ purple_debug_error("util", "Error parsing file %s. Renaming old "
+ "file to %s\n", filename_full, filename_temp);
+ purple_util_write_data_to_file_absolute(filename_temp_full, contents, length);
+
+ g_free(filename_temp_full);
+ g_free(filename_temp);
+ }
+
+ g_free(contents);
+ }
+
+ /* If we could not parse the file then show the user an error message */
+ if (node == NULL)
+ {
+ gchar *title, *msg;
+ title = g_strdup_printf(_("Error Reading %s"), filename);
+ msg = g_strdup_printf(_("An error was encountered reading your "
+ "%s. The file has not been loaded, and the old file "
+ "has been renamed to %s~."), description, filename_full);
+ purple_notify_error(NULL, NULL, title, msg);
+ g_free(title);
+ g_free(msg);
+ }
+
+ g_free(filename_full);
+
+ return node;
+}
+
static void
xmlnode_copy_foreach_ns(gpointer key, gpointer value, gpointer user_data)
{
@@ -800,8 +858,8 @@ xmlnode_get_next_twin(xmlnode *node)
if(ns)
xmlns = xmlnode_get_namespace(sibling);
- if(sibling->type == XMLNODE_TYPE_TAG && !strcmp(node->name, sibling->name) &&
- (!ns || (xmlns && !strcmp(ns, xmlns))))
+ if(sibling->type == XMLNODE_TYPE_TAG && purple_strequal(node->name, sibling->name) &&
+ purple_strequal(ns, xmlns))
return sibling;
}