Mercurial > pidgin.yaz
changeset 9837:dafebadcf8d2
[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 <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Mon, 23 Aug 2004 05:06:38 +0000 |
parents | d5a2232f83e4 |
children | ad7fab671e6f |
files | src/xmlnode.c src/xmlnode.h |
diffstat | 2 files changed, 33 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- 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, "</%s>", node_name); + if(tab && pretty && !has_data) + text = g_string_append(text, tab); + g_string_append_printf(text, "</%s>%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; };
--- 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);