michaelrsweet/Mini-XML — Issue #1: mxmlNewCopy Function
Author: michaelrsweet
Created: 2012-01-01 08:02:20+00:00
State: open
Labels: P4 - Feature

[Originally submitted by David_Descamps]

/*
* 'mxmlCopyElement()'
*
* The new element node is copied from an original and added to the end of the
* specified parent's child list. The constant MXML_NO_PARENT can be used to
* specify that the new element node has no parent.
*
*/
mxml_node_t *
mxmlNewCopy(mxml_node_t *parent, /* I - Parent node */
int descend, /* I - Descend into tree - MXML_DESCEND, MXML_NO_DESCEND, or MXML_DESCEND_FIRST */
mxml_node_t *original) /* I - Node to copy and add */
{
mxml_node_t *node; /* New node */
int i; /* Looping var */
mxml_node_t *child = NULL;
switch (original->type) {
case MXML_ELEMENT:
{
if(original->value.opaque && '?' == original->value.opaque[0])
{
node = mxmlNewElement(parent, original->value.opaque);
break;
}
node = mxmlNewElement(parent, original->value.element.name);

for ( i = 0; i < original->value.element.num_attrs; i ++)
{
mxmlElementSetAttr(node, original->value.element.attrs[i].name,
original->value.element.attrs[i].value);
}
break;
}
case MXML_INTEGER:
{
node = mxmlNewInteger(parent, original->value.integer);
break;
}
case MXML_OPAQUE:
{
node = mxmlNewOpaque(parent, original->value.opaque);
break;
}
case MXML_REAL:
{
node = mxmlNewReal(parent, original->value.real);
break;
}
case MXML_TEXT:
{
node = mxmlNewText(parent, original->value.text.whitespace,
original->value.text.string);
break;
}
case MXML_CUSTOM:
{
node = mxmlNewCustom(parent, original->value.custom.data,
original->value.custom.destroy);
break;
}
default:
break;
}

if(MXML_NO_DESCEND!=descend && original->child)
{
if(MXML_DESCEND_FIRST==descend)
descend=MXML_NO_DESCEND;
mxmlNewCopy(node, descend, original->child);
child = mxmlWalkNext(original->child, original, MXML_NO_DESCEND);
/*
* Loop all children...
*/
while (child != NULL)
{
mxmlNewCopy(node, descend, child);
child = mxmlWalkNext(child, original, MXML_NO_DESCEND);
}
}
return (node);
}