reformatting: cJSON_Duplicate

This commit is contained in:
Max Bruckner 2016-09-30 10:53:58 +07:00
parent e6fd236f00
commit 9adc6e7fe1

94
cJSON.c
View File

@ -2052,31 +2052,77 @@ 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;
/* Bail on bad ptr */ cJSON *cptr;
if (!item) return 0; cJSON *nptr = 0;
/* Create new item */ cJSON *newchild;
newitem=cJSON_New_Item();
if (!newitem) return 0; /* Bail on bad ptr */
/* Copy over all vars */ if (!item)
newitem->type=item->type&(~cJSON_IsReference),newitem->valueint=item->valueint,newitem->valuedouble=item->valuedouble; {
if (item->valuestring) {newitem->valuestring=cJSON_strdup(item->valuestring); if (!newitem->valuestring) {cJSON_Delete(newitem);return 0;}} 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! */ /* Create new item */
if (!recurse) return newitem; newitem = cJSON_New_Item();
/* Walk the ->next chain for the child. */ if (!newitem)
cptr=item->child; {
while (cptr) return 0;
{ }
newchild=cJSON_Duplicate(cptr,1); /* Duplicate (with recurse) each item in the ->next chain */ /* Copy over all vars */
if (!newchild) {cJSON_Delete(newitem);return 0;} newitem->type = item->type & (~cJSON_IsReference);
if (nptr) {nptr->next=newchild,newchild->prev=nptr;nptr=newchild;} /* If newitem->child already set, then crosswire ->prev and ->next and move on */ newitem->valueint = item->valueint;
else {newitem->child=newchild;nptr=newchild;} /* Set newitem->child and move to it */ newitem->valuedouble = item->valuedouble;
cptr=cptr->next; if (item->valuestring)
} {
return newitem; 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 (!recurse)
{
return newitem;
}
/* Walk the ->next chain for the child. */
cptr = item->child;
while (cptr)
{
newchild = cJSON_Duplicate(cptr, 1); /* Duplicate (with recurse) each item in the ->next chain */
if (!newchild)
{
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;
}
return newitem;
} }
void cJSON_Minify(char *json) void cJSON_Minify(char *json)