MCPcopy Create free account
hub / github.com/PointCloudLibrary/pcl / print_object

Function print_object

outofcore/src/cJSON.cpp:430–492  ·  view source on GitHub ↗

Render an object to text. */

Source from the content-addressed store, hash-verified

428
429/* Render an object to text. */
430static char *print_object(cJSON *item,int depth,int fmt)
431{
432 char *out=nullptr;
433 std::size_t len=7;
434 cJSON *child=item->child;
435 std::size_t numentries=0,fail=0;
436 /* Count the number of entries. */
437 while (child) numentries++,child=child->next;
438 /* Allocate space for the names and the objects */
439 char **entries = static_cast<char**>(cJSON_malloc(numentries*sizeof(char*)));
440 if (!entries) return nullptr;
441 char **names=static_cast<char**>(cJSON_malloc(numentries*sizeof(char*)));
442 if (!names) {cJSON_free(entries);return nullptr;}
443 std::fill_n(entries, numentries, nullptr);
444 std::fill_n(names, numentries, nullptr);
445
446 /* Collect all the results into our arrays: */
447 child=item->child;depth++;if (fmt) len+=depth;
448 int childId = 0;
449 while (child)
450 {
451 char *str = print_string_ptr(child->string);
452 names[childId] = str;
453 char *ret = print_value(child,depth,fmt);
454 entries[childId++] = ret;
455 if (str && ret) len+=strlen(ret)+strlen(str)+2+(fmt?2+depth:0); else fail=1;
456 child=child->next;
457 }
458
459 /* Try to allocate the output string */
460 if (!fail) out=static_cast<char*> (cJSON_malloc(len));
461 if (!out) fail=1;
462
463 /* Handle failure */
464 if (fail)
465 {
466 for (std::size_t i=0;i<numentries;i++) {if (names[i]) cJSON_free(names[i]);if (entries[i]) cJSON_free(entries[i]);}
467 cJSON_free(names);cJSON_free(entries);
468 return nullptr;
469 }
470
471 /* Compose the output: */
472 *out='{';
473 char *ptr = out+1;
474 if (fmt)*ptr++='\n';
475 *ptr=0;
476 for (std::size_t i=0;i<numentries;i++)
477 {
478 if (fmt) for (int j=0; j < depth; j++) *ptr++='\t';
479 strcpy(ptr,names[i]);ptr+=strlen(names[i]);
480 *ptr++=':';if (fmt) *ptr++='\t';
481 strcpy(ptr,entries[i]);ptr+=strlen(entries[i]);
482 if (i!=numentries-1) *ptr++=',';
483 if (fmt) *ptr++='\n';
484 *ptr=0;
485 cJSON_free(names[i]);cJSON_free(entries[i]);
486 }
487

Callers 1

print_valueFunction · 0.85

Calls 2

print_string_ptrFunction · 0.85
print_valueFunction · 0.85

Tested by

no test coverage detected