MCPcopy Create free account
hub / github.com/esnet/iperf / cJSON_Duplicate

Function cJSON_Duplicate

src/cjson.c:2717–2798  ·  view source on GitHub ↗

Duplication */

Source from the content-addressed store, hash-verified

2715
2716/* Duplication */
2717CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse)
2718{
2719 cJSON *newitem = NULL;
2720 cJSON *child = NULL;
2721 cJSON *next = NULL;
2722 cJSON *newchild = NULL;
2723
2724 /* Bail on bad ptr */
2725 if (!item)
2726 {
2727 goto fail;
2728 }
2729 /* Create new item */
2730 newitem = cJSON_New_Item(&global_hooks);
2731 if (!newitem)
2732 {
2733 goto fail;
2734 }
2735 /* Copy over all vars */
2736 newitem->type = item->type & (~cJSON_IsReference);
2737 newitem->valueint = item->valueint;
2738 newitem->valuedouble = item->valuedouble;
2739 if (item->valuestring)
2740 {
2741 newitem->valuestring = (char*)cJSON_strdup((unsigned char*)item->valuestring, &global_hooks);
2742 if (!newitem->valuestring)
2743 {
2744 goto fail;
2745 }
2746 }
2747 if (item->string)
2748 {
2749 newitem->string = (item->type&cJSON_StringIsConst) ? item->string : (char*)cJSON_strdup((unsigned char*)item->string, &global_hooks);
2750 if (!newitem->string)
2751 {
2752 goto fail;
2753 }
2754 }
2755 /* If non-recursive, then we're done! */
2756 if (!recurse)
2757 {
2758 return newitem;
2759 }
2760 /* Walk the ->next chain for the child. */
2761 child = item->child;
2762 while (child != NULL)
2763 {
2764 newchild = cJSON_Duplicate(child, true); /* Duplicate (with recurse) each item in the ->next chain */
2765 if (!newchild)
2766 {
2767 goto fail;
2768 }
2769 if (next != NULL)
2770 {
2771 /* If newitem->child already set, then crosswire ->prev and ->next and move on */
2772 next->next = newchild;
2773 newchild->prev = next;
2774 next = newchild;

Callers

nothing calls this directly

Calls 3

cJSON_New_ItemFunction · 0.85
cJSON_strdupFunction · 0.85
cJSON_DeleteFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…