Changeset e31e5b8 for lib


Ignore:
Timestamp:
2013-04-20T13:05:55Z (6 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
dd95ce4
Parents:
9b2a8c1 (diff), bfafb99 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merging "storage" branch which I wrote long ago. It separates generation of
XML-formatted user configs from disk I/O so we can try to start using other
mechanisms to store them (a REST API or something, for example).

Location:
lib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • lib/xmltree.c

    r9b2a8c1 re31e5b8  
    282282}
    283283
    284 static void xt_to_string_real( struct xt_node *node, GString *str )
     284static void xt_to_string_real( struct xt_node *node, GString *str, int indent )
    285285{
    286286        char *buf;
     
    288288        int i;
    289289       
     290        if( indent > 1 )
     291                g_string_append_len( str, "\n\t\t\t\t\t\t\t\t",
     292                                     indent < 8 ? indent : 8 );
     293       
    290294        g_string_append_printf( str, "<%s", node->name );
    291295       
     
    312316       
    313317        for( c = node->children; c; c = c->next )
    314                 xt_to_string_real( c, str );
     318                xt_to_string_real( c, str, indent ? indent + 1 : 0 );
     319       
     320        if( indent > 0 && node->children )
     321                g_string_append_len( str, "\n\t\t\t\t\t\t\t\t",
     322                                     indent < 8 ? indent : 8 );
    315323       
    316324        g_string_append_printf( str, "</%s>", node->name );
     
    320328{
    321329        GString *ret;
    322         char *real;
    323330       
    324331        ret = g_string_new( "" );
    325         xt_to_string_real( node, ret );
    326        
    327         real = ret->str;
    328         g_string_free( ret, FALSE );
    329        
    330         return real;
     332        xt_to_string_real( node, ret, 0 );
     333        return g_string_free( ret, FALSE );
     334}
     335
     336/* WITH indentation! */
     337char *xt_to_string_i( struct xt_node *node )
     338{
     339        GString *ret;
     340       
     341        ret = g_string_new( "" );
     342        xt_to_string_real( node, ret, 1 );
     343        return g_string_free( ret, FALSE );
    331344}
    332345
    333346void xt_print( struct xt_node *node )
    334347{
    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 );
     348        char *str = xt_to_string_i( node );
     349        fprintf( stderr, "%s", str );
     350        g_free( str );
    390351}
    391352
  • lib/xmltree.h

    r9b2a8c1 re31e5b8  
    8484struct xt_node *xt_from_string( const char *in, int text_len );
    8585char *xt_to_string( struct xt_node *node );
     86char *xt_to_string_i( struct xt_node *node );
    8687void xt_print( struct xt_node *node );
    8788struct xt_node *xt_dup( struct xt_node *node );
Note: See TracChangeset for help on using the changeset viewer.