Changes in lib/xmltree.c [222b440:0e788f5]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/xmltree.c
r222b440 r0e788f5 4 4 * Simple XML (stream) parse tree handling code (Jabber/XMPP, mainly) * 5 5 * * 6 * Copyright 2006 Wilmer van der Gaast <wilmer@gaast.net>*6 * Copyright 2006-2012 Wilmer van der Gaast <wilmer@gaast.net> * 7 7 * * 8 8 * This library is free software; you can redistribute it and/or * … … 163 163 int i; 164 164 165 /* Just in case someone likes infinite loops... */166 165 if( xt->root == NULL ) 167 return 0;166 return 1; 168 167 169 168 if( node == NULL ) … … 263 262 } 264 263 265 struct xt_node *xt_from_string( const char *in )264 struct xt_node *xt_from_string( const char *in, int len ) 266 265 { 267 266 struct xt_parser *parser; 268 struct xt_node *ret; 267 struct xt_node *ret = NULL; 268 269 if( len == 0 ) 270 len = strlen( in ); 269 271 270 272 parser = xt_new( NULL, NULL ); 271 xt_feed( parser, in, strlen( in ) ); 272 ret = parser->root; 273 parser->root = NULL; 273 xt_feed( parser, in, len ); 274 if( parser->cur == NULL ) 275 { 276 ret = parser->root; 277 parser->root = NULL; 278 } 274 279 xt_free( parser ); 275 280 … … 277 282 } 278 283 279 static void xt_to_string_real( struct xt_node *node, GString *str , int indent)284 static void xt_to_string_real( struct xt_node *node, GString *str ) 280 285 { 281 286 char *buf; … … 283 288 int i; 284 289 285 if( indent > 1 )286 g_string_append_len( str, "\n\t\t\t\t\t\t\t\t",287 indent < 8 ? indent : 8 );288 289 290 g_string_append_printf( str, "<%s", node->name ); 290 291 … … 311 312 312 313 for( c = node->children; c; c = c->next ) 313 xt_to_string_real( c, str, indent ? indent + 1 : 0 ); 314 315 if( indent > 0 && node->children ) 316 g_string_append_len( str, "\n\t\t\t\t\t\t\t\t", 317 indent < 8 ? indent : 8 ); 314 xt_to_string_real( c, str ); 318 315 319 316 g_string_append_printf( str, "</%s>", node->name ); … … 323 320 { 324 321 GString *ret; 322 char *real; 325 323 326 324 ret = g_string_new( "" ); 327 xt_to_string_real( node, ret, 0 ); 328 return g_string_free( ret, FALSE ); 329 } 330 331 /* WITH indentation! */ 332 char *xt_to_string_i( struct xt_node *node ) 333 { 334 GString *ret; 335 336 ret = g_string_new( "" ); 337 xt_to_string_real( node, ret, 1 ); 338 return g_string_free( ret, FALSE ); 325 xt_to_string_real( node, ret ); 326 327 real = ret->str; 328 g_string_free( ret, FALSE ); 329 330 return real; 339 331 } 340 332 341 333 void xt_print( struct xt_node *node ) 342 334 { 343 char *str = xt_to_string_i( node ); 344 fprintf( stderr, "%s", str ); 345 g_free( str ); 335 int i; 336 struct xt_node *c; 337 338 /* Indentation */ 339 for( c = node; c->parent; c = c->parent ) 340 fprintf( stderr, " " ); 341 342 /* Start the tag */ 343 fprintf( stderr, "<%s", node->name ); 344 345 /* Print the attributes */ 346 for( i = 0; node->attr[i].key; i ++ ) 347 { 348 char *v = g_markup_escape_text( node->attr[i].value, -1 ); 349 fprintf( stderr, " %s=\"%s\"", node->attr[i].key, v ); 350 g_free( v ); 351 } 352 353 /* /> in case there's really *nothing* inside this tag, otherwise 354 just >. */ 355 /* If this tag doesn't have any content at all... */ 356 if( node->text == NULL && node->children == NULL ) 357 { 358 fprintf( stderr, "/>\n" ); 359 return; 360 /* Then we're finished! */ 361 } 362 363 /* Otherwise... */ 364 fprintf( stderr, ">" ); 365 366 /* Only print the text if it contains more than whitespace (TEST). */ 367 if( node->text_len > 0 ) 368 { 369 for( i = 0; node->text[i] && isspace( node->text[i] ); i ++ ); 370 if( node->text[i] ) 371 { 372 char *v = g_markup_escape_text( node->text, -1 ); 373 fprintf( stderr, "%s", v ); 374 g_free( v ); 375 } 376 } 377 378 if( node->children ) 379 fprintf( stderr, "\n" ); 380 381 for( c = node->children; c; c = c->next ) 382 xt_print( c ); 383 384 if( node->children ) 385 for( c = node; c->parent; c = c->parent ) 386 fprintf( stderr, " " ); 387 388 /* Non-empty tag is now finished. */ 389 fprintf( stderr, "</%s>\n", node->name ); 346 390 } 347 391
Note: See TracChangeset
for help on using the changeset viewer.