Changeset 194


Ignore:
Timestamp:
Feb 19, 2006 1:29:42 AM (18 years ago)
Author:
bennylp
Message:

Added pj_xml_clone()

Location:
pjproject/trunk/pjlib-util
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjlib-util/include/pjlib-util/xml.h

    r123 r194  
    9898 
    9999/** 
     100 * Clone XML node and all subnodes. 
     101 * 
     102 * @param pool      Pool to allocate memory for new nodes. 
     103 * @param rhs       The node to clone. 
     104 * 
     105 * @return          Cloned XML node, or NULL on fail. 
     106 */ 
     107PJ_DECL(pj_xml_node*) pj_xml_clone( pj_pool_t *pool, const pj_xml_node *rhs); 
     108 
     109 
     110/** 
    100111 * Create an empty node. 
    101112 * 
  • pjproject/trunk/pjlib-util/src/pjlib-util/xml.c

    r123 r194  
    4747static pj_xml_attr *alloc_attr( pj_pool_t *pool ) 
    4848{ 
    49     return pj_pool_calloc(pool, 1, sizeof(pj_xml_attr)); 
     49    return pj_pool_zalloc(pool, sizeof(pj_xml_attr)); 
    5050} 
    5151 
     
    115115        } 
    116116         
    117         pj_list_insert_before( &node->attr_head, attr ); 
     117        pj_list_push_back( &node->attr_head, attr ); 
    118118    } 
    119119 
     
    132132    while (*scanner->curptr == '<' && *(scanner->curptr+1) != '/') { 
    133133        pj_xml_node *sub_node = xml_parse_node(pool, scanner); 
    134         pj_list_insert_before( &node->node_head, sub_node ); 
     134        pj_list_push_back( &node->node_head, sub_node ); 
    135135    } 
    136136 
     
    332332PJ_DEF(void) pj_xml_add_node( pj_xml_node *parent, pj_xml_node *node ) 
    333333{ 
    334     pj_list_insert_before(&parent->node_head, node); 
     334    pj_list_push_back(&parent->node_head, node); 
    335335} 
    336336 
    337337PJ_DEF(void) pj_xml_add_attr( pj_xml_node *node, pj_xml_attr *attr ) 
    338338{ 
    339     pj_list_insert_before(&node->attr_head, attr); 
     339    pj_list_push_back(&node->attr_head, attr); 
    340340} 
    341341 
     
    410410} 
    411411 
     412 
     413PJ_DEF(pj_xml_node*) pj_xml_clone( pj_pool_t *pool, const pj_xml_node *rhs) 
     414{ 
     415    pj_xml_node *node; 
     416    const pj_xml_attr *r_attr; 
     417    const pj_xml_node *child; 
     418 
     419    node = alloc_node(pool); 
     420 
     421    pj_strdup(pool, &node->name, &rhs->name); 
     422    pj_strdup(pool, &node->content, &rhs->content); 
     423 
     424    /* Clone all attributes */ 
     425    r_attr = rhs->attr_head.next; 
     426    while (r_attr != &rhs->attr_head) { 
     427 
     428        pj_xml_attr *attr; 
     429 
     430        attr = alloc_attr(pool); 
     431        pj_strdup(pool, &attr->name, &r_attr->name); 
     432        pj_strdup(pool, &attr->value, &r_attr->value); 
     433 
     434        pj_list_push_back(&node->attr_head, attr); 
     435 
     436        r_attr = r_attr->next; 
     437    } 
     438 
     439    /* Clone all child nodes. */ 
     440    child = rhs->node_head.next; 
     441    while (child != (pj_xml_node*) &rhs->node_head) { 
     442        pj_xml_node *new_child; 
     443 
     444        new_child = pj_xml_clone(pool, child); 
     445        pj_list_push_back(&node->node_head, new_child); 
     446 
     447        child = child->next; 
     448    } 
     449 
     450    return node; 
     451} 
Note: See TracChangeset for help on using the changeset viewer.