comparison src/xmlnode.c @ 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 35f69749b226
children ad7fab671e6f
comparison
equal deleted inserted replaced
9836:d5a2232f83e4 9837:dafebadcf8d2
244 } 244 }
245 245
246 return ret; 246 return ret;
247 } 247 }
248 248
249 char *xmlnode_to_str(xmlnode *node, int *len) 249 static char *xmlnode_to_str_helper(xmlnode *node, int *len, gboolean pretty, int depth)
250 { 250 {
251 char *ret; 251 char *ret;
252 GString *text = g_string_new(""); 252 GString *text = g_string_new("");
253 xmlnode *c; 253 xmlnode *c;
254 char *node_name, *esc, *esc2; 254 char *node_name, *esc, *esc2, *tab = NULL;
255 gboolean need_end = FALSE; 255 gboolean need_end = FALSE, has_data = FALSE;
256 #ifdef _WIN32
257 static const char *newline = "\r\n";
258 #else
259 static const char *newline = "\n";
260 #endif
261
262 if(pretty && depth) {
263 tab = g_strnfill(depth, '\t');
264 text = g_string_append(text, tab);
265 }
256 266
257 node_name = g_markup_escape_text(node->name, -1); 267 node_name = g_markup_escape_text(node->name, -1);
258 g_string_append_printf(text, "<%s", node_name); 268 g_string_append_printf(text, "<%s", node_name);
259
260 269
261 for(c = node->child; c; c = c->next) 270 for(c = node->child; c; c = c->next)
262 { 271 {
263 if(c->type == XMLNODE_TYPE_ATTRIB) { 272 if(c->type == XMLNODE_TYPE_ATTRIB) {
264 esc = g_markup_escape_text(c->name, -1); 273 esc = g_markup_escape_text(c->name, -1);
265 esc2 = g_markup_escape_text(c->data, -1); 274 esc2 = g_markup_escape_text(c->data, -1);
266 g_string_append_printf(text, " %s='%s'", esc, esc2); 275 g_string_append_printf(text, " %s='%s'", esc, esc2);
267 g_free(esc); 276 g_free(esc);
268 g_free(esc2); 277 g_free(esc2);
269 } else if(c->type == XMLNODE_TYPE_TAG || c->type == XMLNODE_TYPE_DATA) { 278 } else if(c->type == XMLNODE_TYPE_TAG || c->type == XMLNODE_TYPE_DATA) {
279 if(c->type == XMLNODE_TYPE_DATA)
280 has_data = TRUE;
270 need_end = TRUE; 281 need_end = TRUE;
271 } 282 }
272 } 283 }
273 284
274 if(need_end) { 285 if(need_end) {
275 text = g_string_append_c(text, '>'); 286 g_string_append_printf(text, ">%s", (pretty && !has_data) ? newline : "");
276 287
277 for(c = node->child; c; c = c->next) 288 for(c = node->child; c; c = c->next)
278 { 289 {
279 if(c->type == XMLNODE_TYPE_TAG) { 290 if(c->type == XMLNODE_TYPE_TAG) {
280 int esc_len; 291 int esc_len;
281 esc = xmlnode_to_str(c, &esc_len); 292 esc = xmlnode_to_str_helper(c, &esc_len, (pretty && !has_data), depth+1);
282 text = g_string_append_len(text, esc, esc_len); 293 text = g_string_append_len(text, esc, esc_len);
283 g_free(esc); 294 g_free(esc);
284 } else if(c->type == XMLNODE_TYPE_DATA) { 295 } else if(c->type == XMLNODE_TYPE_DATA) {
285 esc = g_markup_escape_text(c->data, c->data_sz); 296 esc = g_markup_escape_text(c->data, c->data_sz);
286 text = g_string_append(text, esc); 297 text = g_string_append(text, esc);
287 g_free(esc); 298 g_free(esc);
288 } 299 }
289 } 300 }
290 301
291 g_string_append_printf(text, "</%s>", node_name); 302 if(tab && pretty && !has_data)
303 text = g_string_append(text, tab);
304 g_string_append_printf(text, "</%s>%s", node_name, pretty ? newline : "");
292 } else { 305 } else {
293 g_string_append_printf(text, "/>"); 306 g_string_append_printf(text, "/>%s", pretty ? newline : "");
294 } 307 }
295 308
296 g_free(node_name); 309 g_free(node_name);
310
311 if(tab)
312 g_free(tab);
297 313
298 ret = text->str; 314 ret = text->str;
299 if(len) 315 if(len)
300 *len = text->len; 316 *len = text->len;
301 g_string_free(text, FALSE); 317 g_string_free(text, FALSE);
302 return ret; 318 return ret;
319 }
320
321 char *xmlnode_to_str(xmlnode *node, int *len) {
322 return xmlnode_to_str_helper(node, len, FALSE, 0);
323 }
324
325 char *xmlnode_to_formatted_str(xmlnode *node, int *len) {
326 return xmlnode_to_str_helper(node, len, TRUE, 0);
303 } 327 }
304 328
305 struct _xmlnode_parser_data { 329 struct _xmlnode_parser_data {
306 xmlnode *current; 330 xmlnode *current;
307 }; 331 };