# HG changeset patch # User Nathan Walp # Date 1093237598 0 # Node ID dafebadcf8d2792c5a40f6ed5b7aac8c5beb45a7 # Parent d5a2232f83e40288cd6dd713330c605167d9d3ff [gaim-migrate @ 10714] this should at least mostly work... adda xmlnode_to_formatted_str() function that makes the XML readable, rather than spitting it out all on 1 line, which the parser may be OK with, but most of us humans have a harder time with this is the result of grim kicking my ass into gear, and much discussion with him, so he gets a sizeable chunk of the credit for this, if it works. committer: Tailor Script diff -r d5a2232f83e4 -r dafebadcf8d2 src/xmlnode.c --- a/src/xmlnode.c Mon Aug 23 00:47:45 2004 +0000 +++ b/src/xmlnode.c Mon Aug 23 05:06:38 2004 +0000 @@ -246,18 +246,27 @@ return ret; } -char *xmlnode_to_str(xmlnode *node, int *len) +static char *xmlnode_to_str_helper(xmlnode *node, int *len, gboolean pretty, int depth) { char *ret; GString *text = g_string_new(""); xmlnode *c; - char *node_name, *esc, *esc2; - gboolean need_end = FALSE; + char *node_name, *esc, *esc2, *tab = NULL; + gboolean need_end = FALSE, has_data = FALSE; +#ifdef _WIN32 + static const char *newline = "\r\n"; +#else + static const char *newline = "\n"; +#endif + + if(pretty && depth) { + tab = g_strnfill(depth, '\t'); + text = g_string_append(text, tab); + } node_name = g_markup_escape_text(node->name, -1); g_string_append_printf(text, "<%s", node_name); - for(c = node->child; c; c = c->next) { if(c->type == XMLNODE_TYPE_ATTRIB) { @@ -267,18 +276,20 @@ g_free(esc); g_free(esc2); } else if(c->type == XMLNODE_TYPE_TAG || c->type == XMLNODE_TYPE_DATA) { + if(c->type == XMLNODE_TYPE_DATA) + has_data = TRUE; need_end = TRUE; } } if(need_end) { - text = g_string_append_c(text, '>'); + g_string_append_printf(text, ">%s", (pretty && !has_data) ? newline : ""); for(c = node->child; c; c = c->next) { if(c->type == XMLNODE_TYPE_TAG) { int esc_len; - esc = xmlnode_to_str(c, &esc_len); + esc = xmlnode_to_str_helper(c, &esc_len, (pretty && !has_data), depth+1); text = g_string_append_len(text, esc, esc_len); g_free(esc); } else if(c->type == XMLNODE_TYPE_DATA) { @@ -288,13 +299,18 @@ } } - g_string_append_printf(text, "", node_name); + if(tab && pretty && !has_data) + text = g_string_append(text, tab); + g_string_append_printf(text, "%s", node_name, pretty ? newline : ""); } else { - g_string_append_printf(text, "/>"); + g_string_append_printf(text, "/>%s", pretty ? newline : ""); } g_free(node_name); + if(tab) + g_free(tab); + ret = text->str; if(len) *len = text->len; @@ -302,6 +318,14 @@ return ret; } +char *xmlnode_to_str(xmlnode *node, int *len) { + return xmlnode_to_str_helper(node, len, FALSE, 0); +} + +char *xmlnode_to_formatted_str(xmlnode *node, int *len) { + return xmlnode_to_str_helper(node, len, TRUE, 0); +} + struct _xmlnode_parser_data { xmlnode *current; }; diff -r d5a2232f83e4 -r dafebadcf8d2 src/xmlnode.h --- a/src/xmlnode.h Mon Aug 23 00:47:45 2004 +0000 +++ b/src/xmlnode.h Mon Aug 23 05:06:38 2004 +0000 @@ -54,6 +54,7 @@ const char *xmlnode_get_attrib(xmlnode *node, const char *attr); void xmlnode_remove_attrib(xmlnode *node, const char *attr); char *xmlnode_to_str(xmlnode *node, int *len); +char *xmlnode_to_formatted_str(xmlnode *node, int *len); xmlnode *xmlnode_from_str(const char *str, size_t size); xmlnode *xmlnode_copy(xmlnode *src);