MCPcopy Create free account
hub / github.com/alibaba/CicadaPlayer / cJSON_Duplicate

Function cJSON_Duplicate

framework/utils/cJSON.c:2550–2627  ·  view source on GitHub ↗

Duplication */

Source from the content-addressed store, hash-verified

2548
2549/* Duplication */
2550CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse)
2551{
2552 cJSON *newitem = NULL;
2553 cJSON *child = NULL;
2554 cJSON *next = NULL;
2555 cJSON *newchild = NULL;
2556
2557 /* Bail on bad ptr */
2558 if (!item)
2559 {
2560 goto fail;
2561 }
2562 /* Create new item */
2563 newitem = cJSON_New_Item(&global_hooks);
2564 if (!newitem)
2565 {
2566 goto fail;
2567 }
2568 /* Copy over all vars */
2569 newitem->type = item->type & (~cJSON_IsReference);
2570 newitem->valueint = item->valueint;
2571 newitem->valuedouble = item->valuedouble;
2572 if (item->valuestring)
2573 {
2574 newitem->valuestring = (char*)cJSON_strdup((unsigned char*)item->valuestring, &global_hooks);
2575 if (!newitem->valuestring)
2576 {
2577 goto fail;
2578 }
2579 }
2580 if (item->string)
2581 {
2582 newitem->string = (item->type&cJSON_StringIsConst) ? item->string : (char*)cJSON_strdup((unsigned char*)item->string, &global_hooks);
2583 if (!newitem->string)
2584 {
2585 goto fail;
2586 }
2587 }
2588 /* If non-recursive, then we're done! */
2589 if (!recurse)
2590 {
2591 return newitem;
2592 }
2593 /* Walk the ->next chain for the child. */
2594 child = item->child;
2595 while (child != NULL)
2596 {
2597 newchild = cJSON_Duplicate(child, true); /* Duplicate (with recurse) each item in the ->next chain */
2598 if (!newchild)
2599 {
2600 goto fail;
2601 }
2602 if (next != NULL)
2603 {
2604 /* If newitem->child already set, then crosswire ->prev and ->next and move on */
2605 next->next = newchild;
2606 newchild->prev = next;
2607 next = newchild;

Callers 2

CicadaJSONItemMethod · 0.85
getJSONCopyMethod · 0.85

Calls 3

cJSON_New_ItemFunction · 0.85
cJSON_strdupFunction · 0.85
cJSON_DeleteFunction · 0.85

Tested by

no test coverage detected