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