MCPcopy Create free account
hub / github.com/SVF-tools/SVF / cJSON_Duplicate

Function cJSON_Duplicate

svf/lib/Util/cJSON.cpp:2720–2801  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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