Duplication */
| 2695 | |
| 2696 | /* Duplication */ |
| 2697 | CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse) |
| 2698 | { |
| 2699 | cJSON *newitem = NULL; |
| 2700 | cJSON *child = NULL; |
| 2701 | cJSON *next = NULL; |
| 2702 | cJSON *newchild = NULL; |
| 2703 | |
| 2704 | /* Bail on bad ptr */ |
| 2705 | if (!item) |
| 2706 | { |
| 2707 | goto fail; |
| 2708 | } |
| 2709 | /* Create new item */ |
| 2710 | newitem = cJSON_New_Item(&global_hooks); |
| 2711 | if (!newitem) |
| 2712 | { |
| 2713 | goto fail; |
| 2714 | } |
| 2715 | /* Copy over all vars */ |
| 2716 | newitem->type = item->type & (~cJSON_IsReference); |
| 2717 | newitem->valueint = item->valueint; |
| 2718 | newitem->valuedouble = item->valuedouble; |
| 2719 | if (item->valuestring) |
| 2720 | { |
| 2721 | newitem->valuestring = (char*)cJSON_strdup((unsigned char*)item->valuestring, &global_hooks); |
| 2722 | if (!newitem->valuestring) |
| 2723 | { |
| 2724 | goto fail; |
| 2725 | } |
| 2726 | } |
| 2727 | if (item->string) |
| 2728 | { |
| 2729 | newitem->string = (item->type&cJSON_StringIsConst) ? item->string : (char*)cJSON_strdup((unsigned char*)item->string, &global_hooks); |
| 2730 | if (!newitem->string) |
| 2731 | { |
| 2732 | goto fail; |
| 2733 | } |
| 2734 | } |
| 2735 | /* If non-recursive, then we're done! */ |
| 2736 | if (!recurse) |
| 2737 | { |
| 2738 | return newitem; |
| 2739 | } |
| 2740 | /* Walk the ->next chain for the child. */ |
| 2741 | child = item->child; |
| 2742 | while (child != NULL) |
| 2743 | { |
| 2744 | newchild = cJSON_Duplicate(child, true); /* Duplicate (with recurse) each item in the ->next chain */ |
| 2745 | if (!newchild) |
| 2746 | { |
| 2747 | goto fail; |
| 2748 | } |
| 2749 | if (next != NULL) |
| 2750 | { |
| 2751 | /* If newitem->child already set, then crosswire ->prev and ->next and move on */ |
| 2752 | next->next = newchild; |
| 2753 | newchild->prev = next; |
| 2754 | next = newchild; |
nothing calls this directly
no test coverage detected