Remove printing without buffer

This commit is contained in:
Max Bruckner 2017-02-19 04:16:57 +01:00
parent 80354bdb06
commit 9bf531ca05

276
cJSON.c
View File

@ -328,6 +328,12 @@ static unsigned char *print_number(const cJSON *item, printbuffer *p)
{ {
unsigned char *str = NULL; unsigned char *str = NULL;
double d = item->valuedouble; double d = item->valuedouble;
if (p == NULL)
{
return NULL;
}
/* special case for 0. */ /* special case for 0. */
if (d == 0) if (d == 0)
{ {
@ -678,6 +684,11 @@ static unsigned char *print_string_ptr(const unsigned char *str, printbuffer *p)
cjbool flag = false; cjbool flag = false;
unsigned char token = '\0'; unsigned char token = '\0';
if (p == NULL)
{
return NULL;
}
/* empty string */ /* empty string */
if (!str) if (!str)
{ {
@ -1028,8 +1039,12 @@ static unsigned char *print_value(const cJSON *item, size_t depth, cjbool fmt, p
{ {
return NULL; return NULL;
} }
if (p)
if (p == NULL)
{ {
return NULL;
}
switch ((item->type) & 0xFF) switch ((item->type) & 0xFF)
{ {
case cJSON_NULL: case cJSON_NULL:
@ -1090,40 +1105,6 @@ static unsigned char *print_value(const cJSON *item, size_t depth, cjbool fmt, p
out = NULL; out = NULL;
break; break;
} }
}
else
{
switch ((item->type) & 0xFF)
{
case cJSON_NULL:
out = cJSON_strdup((const unsigned char*)"null");
break;
case cJSON_False:
out = cJSON_strdup((const unsigned char*)"false");
break;
case cJSON_True:
out = cJSON_strdup((const unsigned char*)"true");
break;
case cJSON_Number:
out = print_number(item, 0);
break;
case cJSON_Raw:
out = cJSON_strdup((unsigned char*)item->valuestring);
break;
case cJSON_String:
out = print_string(item, 0);
break;
case cJSON_Array:
out = print_array(item, depth, fmt, 0);
break;
case cJSON_Object:
out = print_object(item, depth, fmt, 0);
break;
default:
out = NULL;
break;
}
}
return out; return out;
} }
@ -1209,16 +1190,18 @@ fail:
/* Render an array to text */ /* Render an array to text */
static unsigned char *print_array(const cJSON *item, size_t depth, cjbool fmt, printbuffer *p) static unsigned char *print_array(const cJSON *item, size_t depth, cjbool fmt, printbuffer *p)
{ {
unsigned char **entries;
unsigned char *out = NULL; unsigned char *out = NULL;
unsigned char *ptr = NULL; unsigned char *ptr = NULL;
unsigned char *ret = NULL;
size_t len = 5; size_t len = 5;
cJSON *child = item->child; cJSON *child = item->child;
size_t numentries = 0; size_t numentries = 0;
size_t i = 0; size_t i = 0;
cjbool fail = false; cjbool fail = false;
size_t tmplen = 0;
if (p == NULL)
{
return NULL;
}
/* How many entries in the array? */ /* How many entries in the array? */
while (child) while (child)
@ -1239,8 +1222,6 @@ static unsigned char *print_array(const cJSON *item, size_t depth, cjbool fmt, p
return out; return out;
} }
if (p)
{
/* Compose the output array. */ /* Compose the output array. */
/* opening square bracket */ /* opening square bracket */
i = p->offset; i = p->offset;
@ -1286,84 +1267,6 @@ static unsigned char *print_array(const cJSON *item, size_t depth, cjbool fmt, p
*ptr++ = ']'; *ptr++ = ']';
*ptr = '\0'; *ptr = '\0';
out = (p->buffer) + i; out = (p->buffer) + i;
}
else
{
/* Allocate an array to hold the pointers to all printed values */
entries = (unsigned char**)cJSON_malloc(numentries * sizeof(unsigned char*));
if (!entries)
{
return NULL;
}
memset(entries, '\0', numentries * sizeof(unsigned char*));
/* Retrieve all the results: */
child = item->child;
while (child && !fail)
{
ret = print_value(child, depth + 1, fmt, 0);
entries[i++] = ret;
if (ret)
{
len += strlen((char*)ret) + 2 + (fmt ? 1 : 0);
}
else
{
fail = true;
}
child = child->next;
}
/* If we didn't fail, try to malloc the output string */
if (!fail)
{
out = (unsigned char*)cJSON_malloc(len);
}
/* If that fails, we fail. */
if (!out)
{
fail = true;
}
/* Handle failure. */
if (fail)
{
/* free all the entries in the array */
for (i = 0; i < numentries; i++)
{
if (entries[i])
{
cJSON_free(entries[i]);
}
}
cJSON_free(entries);
return NULL;
}
/* Compose the output array. */
*out='[';
ptr = out + 1;
*ptr = '\0';
for (i = 0; i < numentries; i++)
{
tmplen = strlen((char*)entries[i]);
memcpy(ptr, entries[i], tmplen);
ptr += tmplen;
if (i != (numentries - 1))
{
*ptr++ = ',';
if(fmt)
{
*ptr++ = ' ';
}
*ptr = '\0';
}
cJSON_free(entries[i]);
}
cJSON_free(entries);
*ptr++ = ']';
*ptr++ = '\0';
}
return out; return out;
} }
@ -1466,19 +1369,18 @@ fail:
/* Render an object to text. */ /* Render an object to text. */
static unsigned char *print_object(const cJSON *item, size_t depth, cjbool fmt, printbuffer *p) static unsigned char *print_object(const cJSON *item, size_t depth, cjbool fmt, printbuffer *p)
{ {
unsigned char **entries = NULL;
unsigned char **names = NULL;
unsigned char *out = NULL; unsigned char *out = NULL;
unsigned char *ptr = NULL; unsigned char *ptr = NULL;
unsigned char *ret = NULL;
unsigned char *str = NULL;
size_t len = 7; size_t len = 7;
size_t i = 0; size_t i = 0;
size_t j = 0; size_t j = 0;
cJSON *child = item->child; cJSON *child = item->child;
size_t numentries = 0; size_t numentries = 0;
cjbool fail = false;
size_t tmplen = 0; if (p == NULL)
{
return NULL;
}
/* Count the number of entries. */ /* Count the number of entries. */
while (child) while (child)
@ -1510,8 +1412,6 @@ static unsigned char *print_object(const cJSON *item, size_t depth, cjbool fmt,
return out; return out;
} }
if (p)
{
/* Compose the output: */ /* Compose the output: */
i = p->offset; i = p->offset;
len = fmt ? 2 : 1; /* fmt: {\n */ len = fmt ? 2 : 1; /* fmt: {\n */
@ -1571,7 +1471,7 @@ static unsigned char *print_object(const cJSON *item, size_t depth, cjbool fmt,
if (!print_value(child, depth, fmt, p)) if (!print_value(child, depth, fmt, p))
{ {
return NULL; return NULL;
}; }
p->offset = update(p); p->offset = update(p);
/* print comma if not last */ /* print comma if not last */
@ -1611,128 +1511,6 @@ static unsigned char *print_object(const cJSON *item, size_t depth, cjbool fmt,
*ptr++ = '}'; *ptr++ = '}';
*ptr = '\0'; *ptr = '\0';
out = (p->buffer) + i; out = (p->buffer) + i;
}
else
{
/* Allocate space for the names and the objects */
entries = (unsigned char**)cJSON_malloc(numentries * sizeof(unsigned char*));
if (!entries)
{
return NULL;
}
names = (unsigned char**)cJSON_malloc(numentries * sizeof(unsigned char*));
if (!names)
{
cJSON_free(entries);
return NULL;
}
memset(entries, '\0', sizeof(unsigned char*) * numentries);
memset(names, '\0', sizeof(unsigned char*) * numentries);
/* Collect all the results into our arrays: */
child = item->child;
depth++;
if (fmt)
{
len += depth;
}
while (child && !fail)
{
names[i] = str = print_string_ptr((unsigned char*)child->string, 0); /* print key */
entries[i++] = ret = print_value(child, depth, fmt, 0);
if (str && ret)
{
len += strlen((char*)ret) + strlen((char*)str) + 2 + (fmt ? 2 + depth : 0);
}
else
{
fail = true;
}
child = child->next;
}
/* Try to allocate the output string */
if (!fail)
{
out = (unsigned char*)cJSON_malloc(len);
}
if (!out)
{
fail = true;
}
/* Handle failure */
if (fail)
{
/* free all the printed keys and values */
for (i = 0; i < numentries; i++)
{
if (names[i])
{
cJSON_free(names[i]);
}
if (entries[i])
{
cJSON_free(entries[i]);
}
}
cJSON_free(names);
cJSON_free(entries);
return NULL;
}
/* Compose the output: */
*out = '{';
ptr = out + 1;
if (fmt)
{
*ptr++ = '\n';
}
*ptr = '\0';
for (i = 0; i < numentries; i++)
{
if (fmt)
{
for (j = 0; j < depth; j++)
{
*ptr++='\t';
}
}
tmplen = strlen((char*)names[i]);
memcpy(ptr, names[i], tmplen);
ptr += tmplen;
*ptr++ = ':';
if (fmt)
{
*ptr++ = '\t';
}
strcpy((char*)ptr, (char*)entries[i]);
ptr += strlen((char*)entries[i]);
if (i != (numentries - 1))
{
*ptr++ = ',';
}
if (fmt)
{
*ptr++ = '\n';
}
*ptr = '\0';
cJSON_free(names[i]);
cJSON_free(entries[i]);
}
cJSON_free(names);
cJSON_free(entries);
if (fmt)
{
for (i = 0; i < (depth - 1); i++)
{
*ptr++ = '\t';
}
}
*ptr++ = '}';
*ptr++ = '\0';
}
return out; return out;
} }