diff options
author | Wilmer van der Gaast <wilmer@gaast.net> | 2006-09-26 15:30:54 +0200 |
---|---|---|
committer | Wilmer van der Gaast <wilmer@gaast.net> | 2006-09-26 15:30:54 +0200 |
commit | 022df46b52683f49a1a48e15a440d1f6b81adfdd (patch) | |
tree | c70ac6f970fe44aa05376615ea31f0fb2a500422 /protocols/jabber/xmltree.c | |
parent | 8e6c7326667f7302bf622555d7c4164d24b89be0 (diff) |
Added xt_dup().
Diffstat (limited to 'protocols/jabber/xmltree.c')
-rw-r--r-- | protocols/jabber/xmltree.c | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/protocols/jabber/xmltree.c b/protocols/jabber/xmltree.c index 81e0dab5..da316f99 100644 --- a/protocols/jabber/xmltree.c +++ b/protocols/jabber/xmltree.c @@ -350,6 +350,47 @@ void xt_print( struct xt_node *node ) printf( "</%s>\n", node->name ); } +struct xt_node *xt_dup( struct xt_node *node ) +{ + struct xt_node *dup = g_new0( struct xt_node, 1 ); + struct xt_node *c, *dc = NULL; + int i; + + /* Let's NOT copy the parent element here BTW! Only do it for children. */ + + dup->name = g_strdup( node->name ); + dup->flags = node->flags; + if( node->text ) + { + dup->text = g_memdup( node->text, node->text_len + 1 ); + dup->text_len = node->text_len; + } + + /* Count the number of attributes and allocate the new array. */ + for( i = 0; node->attr[i].key; i ++ ); + dup->attr = g_new0( struct xt_attr, i + 1 ); + + /* Copy them all! */ + for( i --; i >= 0; i -- ) + { + dup->attr[i].key = g_strdup( node->attr[i].key ); + dup->attr[i].value = g_strdup( node->attr[i].value ); + } + + /* This nice mysterious loop takes care of the children. */ + for( c = node->children; c; c = c->next ) + { + if( dc == NULL ) + dc = dup->children = xt_dup( c ); + else + dc = ( dc->next = xt_dup( c ) ); + + dc->parent = dup; + } + + return dup; +} + /* Frees a node. This doesn't clean up references to itself from parents! */ void xt_free_node( struct xt_node *node ) { |