MCPcopy Create free account
hub / github.com/beefytech/Beef / print_array

Function print_array

BeefySysLib/util/Json.cpp:363–418  ·  view source on GitHub ↗

Render an array to text */

Source from the content-addressed store, hash-verified

361
362/* Render an array to text */
363static char *print_array(Json* item,int depth,int fmt)
364{
365 char **entries;
366 char *out=0,*ptr,*ret;int len=5;
367 Json* child=item->mChild;
368 int numentries=0,i=0,fail=0;
369
370 /* How many entries in the array? */
371 while (child) numentries++,child=child->mNext;
372 /* Explicitly handle numentries==0 */
373 if (!numentries)
374 {
375 out=(char*)Json::malloc(3);
376 if (out) strcpy(out,"[]");
377 return out;
378 }
379 /* Allocate an array to hold the values for each */
380 entries=(char**)Json::malloc(numentries*sizeof(char*));
381 if (!entries) return 0;
382 memset(entries,0,numentries*sizeof(char*));
383 /* Retrieve all the results: */
384 child=item->mChild;
385 while (child && !fail)
386 {
387 ret=print_value(child,depth+1,fmt);
388 entries[i++]=ret;
389 if (ret) len+=(int)strlen(ret)+2+(fmt?1:0); else fail=1;
390 child=child->mNext;
391 }
392
393 /* If we didn't fail, try to malloc the output string */
394 if (!fail) out=(char*)Json::malloc(len);
395 /* If that fails, we fail. */
396 if (!out) fail=1;
397
398 /* Handle failure. */
399 if (fail)
400 {
401 for (i=0;i<numentries;i++) if (entries[i]) Json::free(entries[i]);
402 Json::free(entries);
403 return 0;
404 }
405
406 /* Compose the output array. */
407 *out='[';
408 ptr=out+1;*ptr=0;
409 for (i=0;i<numentries;i++)
410 {
411 strcpy(ptr,entries[i]);ptr+=strlen(entries[i]);
412 if (i!=numentries-1) {*ptr++=',';if(fmt)*ptr++=' ';*ptr=0;}
413 Json::free(entries[i]);
414 }
415 Json::free(entries);
416 *ptr++=']';*ptr++=0;
417 return out;
418}
419
420/* Build an object from the text. */

Callers 1

print_valueFunction · 0.85

Calls 3

print_valueFunction · 0.85
mallocFunction · 0.50
freeFunction · 0.50

Tested by

no test coverage detected