MCPcopy Create free account
hub / github.com/Tencent/TAD_Sim / print_object

Function print_object

common/map_sdk/transmission/src/cJSON.cpp:573–652  ·  view source on GitHub ↗

Render an object to text. */

Source from the content-addressed store, hash-verified

571
572/* Render an object to text. */
573static char *print_object(cJSON *item, int depth, int fmt) {
574 char **entries = 0, **names = 0;
575 char *out = 0, *ptr, *ret, *str;
576 int len = 7, i = 0, j;
577 cJSON *child = item->child;
578 int numentries = 0, fail = 0;
579 /* Count the number of entries. */
580 while (child) numentries++, child = child->next;
581 /* Allocate space for the names and the objects */
582 entries = reinterpret_cast<char **>(cJSON_malloc(numentries * sizeof(char *)));
583 if (!entries) return 0;
584 names = reinterpret_cast<char **>(cJSON_malloc(numentries * sizeof(char *)));
585 if (!names) {
586 cJSON_free(entries);
587 return 0;
588 }
589 memset(entries, 0, sizeof(char *) * numentries);
590 memset(names, 0, sizeof(char *) * numentries);
591
592 /* Collect all the results into our arrays: */
593 child = item->child;
594 depth++;
595 if (fmt) len += depth;
596 while (child) {
597 names[i] = str = print_string_ptr(child->string);
598 entries[i++] = ret = print_value(child, depth, fmt);
599 if (str && ret) {
600 len += strlen(ret) + strlen(str) + 2 + (fmt ? 2 + depth : 0);
601 } else {
602 fail = 1;
603 }
604 child = child->next;
605 }
606
607 /* Try to allocate the output string */
608 if (!fail) out = reinterpret_cast<char *>(cJSON_malloc(len));
609 if (!out) fail = 1;
610
611 /* Handle failure */
612 if (fail) {
613 for (i = 0; i < numentries; i++) {
614 if (names[i]) cJSON_free(names[i]);
615 if (entries[i]) cJSON_free(entries[i]);
616 }
617 cJSON_free(names);
618 cJSON_free(entries);
619 return 0;
620 }
621
622 /* Compose the output: */
623 *out = '{';
624 ptr = out + 1;
625 if (fmt) *ptr++ = '\n';
626 *ptr = 0;
627 for (i = 0; i < numentries; i++) {
628 if (fmt) {
629 for (j = 0; j < depth; j++) *ptr++ = '\t';
630 }

Callers 1

print_valueFunction · 0.85

Calls 2

print_string_ptrFunction · 0.85
print_valueFunction · 0.85

Tested by

no test coverage detected