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 | |
| parent | 8e6c7326667f7302bf622555d7c4164d24b89be0 (diff) | |
Added xt_dup().
Diffstat (limited to 'protocols')
| -rw-r--r-- | protocols/jabber/xmltree.c | 41 | ||||
| -rw-r--r-- | protocols/jabber/xmltree.h | 1 | 
2 files changed, 42 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 )  { diff --git a/protocols/jabber/xmltree.h b/protocols/jabber/xmltree.h index cfefadca..55030d83 100644 --- a/protocols/jabber/xmltree.h +++ b/protocols/jabber/xmltree.h @@ -83,6 +83,7 @@ int xt_handle( struct xt_parser *xt, struct xt_node *node );  void xt_cleanup( struct xt_parser *xt, struct xt_node *node );  char *xt_to_string( struct xt_node *node );  void xt_print( struct xt_node *node ); +struct xt_node *xt_dup( struct xt_node *node );  void xt_free_node( struct xt_node *node );  void xt_free( struct xt_parser *xt );  struct xt_node *xt_find_node( struct xt_node *node, char *name ); | 
