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