MCPcopy Create free account
hub / github.com/OpenIPC/firmware / cJSON_Duplicate

Function cJSON_Duplicate

general/package/xmdp/src/cjson/cJSON.c:2687–2764  ·  view source on GitHub ↗

Duplication */

Source from the content-addressed store, hash-verified

2685
2686/* Duplication */
2687CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse)
2688{
2689 cJSON *newitem = NULL;
2690 cJSON *child = NULL;
2691 cJSON *next = NULL;
2692 cJSON *newchild = NULL;
2693
2694 /* Bail on bad ptr */
2695 if (!item)
2696 {
2697 goto fail;
2698 }
2699 /* Create new item */
2700 newitem = cJSON_New_Item(&global_hooks);
2701 if (!newitem)
2702 {
2703 goto fail;
2704 }
2705 /* Copy over all vars */
2706 newitem->type = item->type & (~cJSON_IsReference);
2707 newitem->valueint = item->valueint;
2708 newitem->valuedouble = item->valuedouble;
2709 if (item->valuestring)
2710 {
2711 newitem->valuestring = (char*)cJSON_strdup((unsigned char*)item->valuestring, &global_hooks);
2712 if (!newitem->valuestring)
2713 {
2714 goto fail;
2715 }
2716 }
2717 if (item->string)
2718 {
2719 newitem->string = (item->type&cJSON_StringIsConst) ? item->string : (char*)cJSON_strdup((unsigned char*)item->string, &global_hooks);
2720 if (!newitem->string)
2721 {
2722 goto fail;
2723 }
2724 }
2725 /* If non-recursive, then we're done! */
2726 if (!recurse)
2727 {
2728 return newitem;
2729 }
2730 /* Walk the ->next chain for the child. */
2731 child = item->child;
2732 while (child != NULL)
2733 {
2734 newchild = cJSON_Duplicate(child, true); /* Duplicate (with recurse) each item in the ->next chain */
2735 if (!newchild)
2736 {
2737 goto fail;
2738 }
2739 if (next != NULL)
2740 {
2741 /* If newitem->child already set, then crosswire ->prev and ->next and move on */
2742 next->next = newchild;
2743 newchild->prev = next;
2744 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