Render an object to text. */
| 703 | |
| 704 | /* Render an object to text. */ |
| 705 | static char *print_object(cJSON *item, int depth, int fmt) |
| 706 | { |
| 707 | char **entries = 0, **names = 0; |
| 708 | char *out = 0, *ptr, *ret, *str; |
| 709 | int len = 7, i = 0, j; |
| 710 | cJSON *child = item->child; |
| 711 | int numentries = 0, fail = 0; |
| 712 | /* Count the number of entries. */ |
| 713 | while (child) |
| 714 | numentries++, child = child->next; |
| 715 | /* Allocate space for the names and the objects */ |
| 716 | entries = (char**) cJSON_malloc(numentries * sizeof(char*)); |
| 717 | if (!entries) |
| 718 | return 0; |
| 719 | names = (char**) cJSON_malloc(numentries * sizeof(char*)); |
| 720 | if (!names) |
| 721 | { |
| 722 | cJSON_free(entries); |
| 723 | return 0; |
| 724 | } |
| 725 | memset(entries, 0, sizeof(char*) * numentries); |
| 726 | memset(names, 0, sizeof(char*) * numentries); |
| 727 | |
| 728 | /* Collect all the results into our arrays: */ |
| 729 | child = item->child; |
| 730 | depth++; |
| 731 | if (fmt) |
| 732 | len += depth; |
| 733 | while (child) |
| 734 | { |
| 735 | names[i] = str = print_string_ptr(child->string); |
| 736 | entries[i++] = ret = print_value(child, depth, fmt); |
| 737 | if (str && ret) |
| 738 | len += strlen(ret) + strlen(str) + 2 + (fmt ? 2 + depth : 0); |
| 739 | else |
| 740 | fail = 1; |
| 741 | child = child->next; |
| 742 | } |
| 743 | |
| 744 | /* Try to allocate the output string */ |
| 745 | if (!fail) |
| 746 | out = (char*) cJSON_malloc(len); |
| 747 | if (!out) |
| 748 | fail = 1; |
| 749 | |
| 750 | /* Handle failure */ |
| 751 | if (fail) |
| 752 | { |
| 753 | for (i = 0; i < numentries; i++) |
| 754 | { |
| 755 | if (names[i]) |
| 756 | cJSON_free(names[i]); |
| 757 | if (entries[i]) |
| 758 | cJSON_free(entries[i]); |
| 759 | } |
| 760 | cJSON_free(names); |
| 761 | cJSON_free(entries); |
| 762 | return 0; |
no test coverage detected