mirror of
https://github.com/DaveGamble/cJSON.git
synced 2023-08-10 21:13:26 +03:00
reformatting: cJSON_Duplicate
This commit is contained in:
parent
e6fd236f00
commit
9adc6e7fe1
66
cJSON.c
66
cJSON.c
@ -2054,28 +2054,74 @@ cJSON *cJSON_CreateStringArray(const char **strings, int count)
|
|||||||
/* Duplication */
|
/* Duplication */
|
||||||
cJSON *cJSON_Duplicate(cJSON *item, int recurse)
|
cJSON *cJSON_Duplicate(cJSON *item, int recurse)
|
||||||
{
|
{
|
||||||
cJSON *newitem,*cptr,*nptr=0,*newchild;
|
cJSON *newitem;
|
||||||
|
cJSON *cptr;
|
||||||
|
cJSON *nptr = 0;
|
||||||
|
cJSON *newchild;
|
||||||
|
|
||||||
/* Bail on bad ptr */
|
/* Bail on bad ptr */
|
||||||
if (!item) return 0;
|
if (!item)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
/* Create new item */
|
/* Create new item */
|
||||||
newitem = cJSON_New_Item();
|
newitem = cJSON_New_Item();
|
||||||
if (!newitem) return 0;
|
if (!newitem)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
/* Copy over all vars */
|
/* Copy over all vars */
|
||||||
newitem->type=item->type&(~cJSON_IsReference),newitem->valueint=item->valueint,newitem->valuedouble=item->valuedouble;
|
newitem->type = item->type & (~cJSON_IsReference);
|
||||||
if (item->valuestring) {newitem->valuestring=cJSON_strdup(item->valuestring); if (!newitem->valuestring) {cJSON_Delete(newitem);return 0;}}
|
newitem->valueint = item->valueint;
|
||||||
if (item->string) {newitem->string=cJSON_strdup(item->string); if (!newitem->string) {cJSON_Delete(newitem);return 0;}}
|
newitem->valuedouble = item->valuedouble;
|
||||||
|
if (item->valuestring)
|
||||||
|
{
|
||||||
|
newitem->valuestring = cJSON_strdup(item->valuestring);
|
||||||
|
if (!newitem->valuestring)
|
||||||
|
{
|
||||||
|
cJSON_Delete(newitem);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (item->string)
|
||||||
|
{
|
||||||
|
newitem->string = cJSON_strdup(item->string);
|
||||||
|
if (!newitem->string)
|
||||||
|
{
|
||||||
|
cJSON_Delete(newitem);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
/* If non-recursive, then we're done! */
|
/* If non-recursive, then we're done! */
|
||||||
if (!recurse) return newitem;
|
if (!recurse)
|
||||||
|
{
|
||||||
|
return newitem;
|
||||||
|
}
|
||||||
/* Walk the ->next chain for the child. */
|
/* Walk the ->next chain for the child. */
|
||||||
cptr = item->child;
|
cptr = item->child;
|
||||||
while (cptr)
|
while (cptr)
|
||||||
{
|
{
|
||||||
newchild = cJSON_Duplicate(cptr, 1); /* Duplicate (with recurse) each item in the ->next chain */
|
newchild = cJSON_Duplicate(cptr, 1); /* Duplicate (with recurse) each item in the ->next chain */
|
||||||
if (!newchild) {cJSON_Delete(newitem);return 0;}
|
if (!newchild)
|
||||||
if (nptr) {nptr->next=newchild,newchild->prev=nptr;nptr=newchild;} /* If newitem->child already set, then crosswire ->prev and ->next and move on */
|
{
|
||||||
else {newitem->child=newchild;nptr=newchild;} /* Set newitem->child and move to it */
|
cJSON_Delete(newitem);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (nptr)
|
||||||
|
{
|
||||||
|
/* If newitem->child already set, then crosswire ->prev and ->next and move on */
|
||||||
|
nptr->next = newchild;
|
||||||
|
newchild->prev = nptr;
|
||||||
|
nptr = newchild;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Set newitem->child and move to it */
|
||||||
|
newitem->child = newchild; nptr = newchild;
|
||||||
|
}
|
||||||
cptr = cptr->next;
|
cptr = cptr->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
return newitem;
|
return newitem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user