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

Function print_array

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

Render an array to text */

Source from the content-addressed store, hash-verified

463
464/* Render an array to text */
465static char *print_array(cJSON *item, int depth, int fmt) {
466 char **entries;
467 char *out = 0, *ptr, *ret;
468 int len = 5;
469 cJSON *child = item->child;
470 int numentries = 0, i = 0, fail = 0;
471
472 /* How many entries in the array? */
473 while (child) numentries++, child = child->next;
474 /* Allocate an array to hold the values for each */
475 entries = reinterpret_cast<char **>(cJSON_malloc(numentries * sizeof(char *)));
476 if (!entries) return 0;
477 memset(entries, 0, numentries * sizeof(char *));
478 /* Retrieve all the results: */
479 child = item->child;
480 while (child && !fail) {
481 ret = print_value(child, depth + 1, fmt);
482 entries[i++] = ret;
483 if (ret) {
484 len += strlen(ret) + 2 + (fmt ? 1 : 0);
485 } else {
486 fail = 1;
487 }
488 child = child->next;
489 }
490
491 /* If we didn't fail, try to malloc the output string */
492 if (!fail) out = reinterpret_cast<char *>(cJSON_malloc(len));
493 /* If that fails, we fail. */
494 if (!out) fail = 1;
495
496 /* Handle failure. */
497 if (fail) {
498 for (i = 0; i < numentries; i++)
499 if (entries[i]) cJSON_free(entries[i]);
500 cJSON_free(entries);
501 return 0;
502 }
503
504 /* Compose the output array. */
505 *out = '[';
506 ptr = out + 1;
507 *ptr = 0;
508 for (i = 0; i < numentries; i++) {
509 strcpy(ptr, entries[i]);
510 ptr += strlen(entries[i]);
511 if (i != numentries - 1) {
512 *ptr++ = ',';
513 if (fmt) *ptr++ = ' ';
514 *ptr = 0;
515 }
516 cJSON_free(entries[i]);
517 }
518 cJSON_free(entries);
519 *ptr++ = ']';
520 *ptr++ = 0;
521 return out;
522}

Callers 1

print_valueFunction · 0.85

Calls 1

print_valueFunction · 0.85

Tested by

no test coverage detected