MCPcopy Create free account
hub / github.com/acl-dev/acl / print_array

Function print_array

lib_acl/samples/json/json5/cJSON.cpp:434–511  ·  view source on GitHub ↗

Render an array to text */

Source from the content-addressed store, hash-verified

432
433/* Render an array to text */
434static char *print_array(cJSON *item,int depth,int fmt,printbuffer *p)
435{
436 char **entries;
437 char *out=0,*ptr,*ret;int len=5;
438 cJSON *child=item->child;
439 int numentries=0,i=0,fail=0;
440 size_t tmplen=0;
441
442 /* How many entries in the array? */
443 while (child) numentries++,child=child->next;
444 /* Explicitly handle numentries==0 */
445 if (!numentries)
446 {
447 if (p) out=ensure(p,3);
448 else out=(char*)cJSON_malloc(3);
449 if (out) strcpy(out,"[]");
450 return out;
451 }
452
453 if (p)
454 {
455 /* Compose the output array. */
456 i=p->offset;
457 ptr=ensure(p,1);if (!ptr) return 0; *ptr='['; p->offset++;
458 child=item->child;
459 while (child && !fail)
460 {
461 print_value(child,depth+1,fmt,p);
462 p->offset=update(p);
463 if (child->next) {len=fmt?2:1;ptr=ensure(p,len+1);if (!ptr) return 0;*ptr++=',';if(fmt)*ptr++=' ';*ptr=0;p->offset+=len;}
464 child=child->next;
465 }
466 ptr=ensure(p,2);if (!ptr) return 0; *ptr++=']';*ptr=0;
467 out=(p->buffer)+i;
468 }
469 else
470 {
471 /* Allocate an array to hold the values for each */
472 entries=(char**)cJSON_malloc(numentries*sizeof(char*));
473 if (!entries) return 0;
474 memset(entries,0,numentries*sizeof(char*));
475 /* Retrieve all the results: */
476 child=item->child;
477 while (child && !fail)
478 {
479 ret=print_value(child,depth+1,fmt,0);
480 entries[i++]=ret;
481 if (ret) len+=strlen(ret)+2+(fmt?1:0); else fail=1;
482 child=child->next;
483 }
484
485 /* If we didn't fail, try to malloc the output string */
486 if (!fail) out=(char*)cJSON_malloc(len);
487 /* If that fails, we fail. */
488 if (!out) fail=1;
489
490 /* Handle failure. */
491 if (fail)

Callers 1

print_valueFunction · 0.85

Calls 4

ensureFunction · 0.85
memcpyFunction · 0.85
print_valueFunction · 0.70
updateFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…