reformatting: print_array

This commit is contained in:
Max Bruckner
2016-09-28 22:07:47 +07:00
parent ad711e6fab
commit 782770c6ba

124
cJSON.c
View File

@@ -1048,79 +1048,165 @@ static const char *parse_array(cJSON *item,const char *value,const char **ep)
static char *print_array(cJSON *item, int depth, int fmt, printbuffer *p) static char *print_array(cJSON *item, int depth, int fmt, printbuffer *p)
{ {
char **entries; char **entries;
char *out=0,*ptr,*ret;int len=5; char *out = 0;
char *ptr;
char *ret;
int len = 5;
cJSON *child = item->child; cJSON *child = item->child;
int numentries=0,i=0,fail=0; int numentries = 0;
int i = 0;
int fail = 0;
size_t tmplen = 0; size_t tmplen = 0;
/* How many entries in the array? */ /* How many entries in the array? */
while (child) numentries++,child=child->next; while (child)
{
numentries++;
child = child->next;
}
/* Explicitly handle numentries == 0 */ /* Explicitly handle numentries == 0 */
if (!numentries) if (!numentries)
{ {
if (p) out=ensure(p,3); if (p)
else out=(char*)cJSON_malloc(3); {
if (out) strcpy(out,"[]"); out = ensure(p, 3);
}
else
{
out = (char*)cJSON_malloc(3);
}
if (out)
{
strcpy(out,"[]");
}
return out; return out;
} }
if (p) if (p)
{ {
/* Compose the output array. */ /* Compose the output array. */
/* opening square bracket */
i = p->offset; i = p->offset;
ptr=ensure(p,1);if (!ptr) return 0; *ptr='['; p->offset++; ptr = ensure(p, 1);
if (!ptr)
{
return 0;
}
*ptr = '[';
p->offset++;
child = item->child; child = item->child;
while (child && !fail) while (child && !fail)
{ {
print_value(child, depth + 1, fmt, p); print_value(child, depth + 1, fmt, p);
p->offset = update(p); p->offset = update(p);
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;} 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;
}
child = child->next; child = child->next;
} }
ptr=ensure(p,2);if (!ptr) return 0; *ptr++=']';*ptr=0; ptr = ensure(p, 2);
if (!ptr)
{
return 0;
}
*ptr++ = ']';
*ptr = '\0';
out = (p->buffer) + i; out = (p->buffer) + i;
} }
else else
{ {
/* Allocate an array to hold the values for each */ /* Allocate an array to hold the pointers to all printed values */
entries = (char**)cJSON_malloc(numentries * sizeof(char*)); entries = (char**)cJSON_malloc(numentries * sizeof(char*));
if (!entries) return 0; if (!entries)
{
return 0;
}
memset(entries, 0, numentries * sizeof(char*)); memset(entries, 0, numentries * sizeof(char*));
/* Retrieve all the results: */ /* Retrieve all the results: */
child = item->child; child = item->child;
while (child && !fail) while (child && !fail)
{ {
ret = print_value(child, depth + 1, fmt, 0); ret = print_value(child, depth + 1, fmt, 0);
entries[i++] = ret; entries[i++] = ret;
if (ret) len+=strlen(ret)+2+(fmt?1:0); else fail=1; if (ret)
{
len += strlen(ret) + 2 + (fmt ? 1 : 0);
}
else
{
fail = 1;
}
child = child->next; child = child->next;
} }
/* If we didn't fail, try to malloc the output string */ /* If we didn't fail, try to malloc the output string */
if (!fail) out=(char*)cJSON_malloc(len); if (!fail)
{
out = (char*)cJSON_malloc(len);
}
/* If that fails, we fail. */ /* If that fails, we fail. */
if (!out) fail=1; if (!out)
{
fail = 1;
}
/* Handle failure. */ /* Handle failure. */
if (fail) if (fail)
{ {
for (i=0;i<numentries;i++) if (entries[i]) cJSON_free(entries[i]); /* free all the entries in the array */
for (i = 0; i < numentries; i++)
{
if (entries[i])
{
cJSON_free(entries[i]);
}
}
cJSON_free(entries); cJSON_free(entries);
return 0; return 0;
} }
/* Compose the output array. */ /* Compose the output array. */
*out='['; *out='[';
ptr=out+1;*ptr=0; ptr = out + 1;
*ptr = '\0';
for (i = 0; i < numentries; i++) for (i = 0; i < numentries; i++)
{ {
tmplen=strlen(entries[i]);memcpy(ptr,entries[i],tmplen);ptr+=tmplen; tmplen = strlen(entries[i]);
if (i!=numentries-1) {*ptr++=',';if(fmt)*ptr++=' ';*ptr=0;} memcpy(ptr, entries[i], tmplen);
ptr += tmplen;
if (i != (numentries - 1))
{
*ptr++ = ',';
if(fmt)
{
*ptr++ = ' ';
}
*ptr = 0;
}
cJSON_free(entries[i]); cJSON_free(entries[i]);
} }
cJSON_free(entries); cJSON_free(entries);
*ptr++=']';*ptr++=0; *ptr++ = ']';
*ptr++ = '\0';
} }
return out; return out;
} }