MCPcopy Index your code
hub / github.com/armink/struct2json / print_array

Function print_array

struct2json/src/cJSON.c:435–512  ·  view source on GitHub ↗

Render an array to text */

Source from the content-addressed store, hash-verified

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

Callers 1

print_valueFunction · 0.85

Calls 3

ensureFunction · 0.85
print_valueFunction · 0.85
updateFunction · 0.85

Tested by

no test coverage detected