changed to cJSON_PrintPreallocated, added flag in printbuffer

This commit is contained in:
Kyle Chisholm
2016-11-25 13:33:10 -05:00
committed by Max Bruckner
parent 602c241a0b
commit de93d76d0b
3 changed files with 61 additions and 5 deletions

15
cJSON.c
View File

@@ -244,6 +244,7 @@ typedef struct
char *buffer;
int length;
int offset;
cjbool noalloc;
} printbuffer;
/* realloc printbuffer if necessary to have at least "needed" bytes more */
@@ -261,6 +262,10 @@ static char* ensure(printbuffer *p, int needed)
return p->buffer + p->offset;
}
if (p->noalloc) {
return NULL;
}
newsize = pow2gt(needed);
newbuffer = (char*)cJSON_malloc(newsize);
if (!newbuffer)
@@ -882,17 +887,19 @@ char *cJSON_PrintBuffered(const cJSON *item, int prebuffer, cjbool fmt)
}
p.length = prebuffer;
p.offset = 0;
p.noalloc = false;
return print_value(item, 0, fmt, &p);
}
int cJSON_PrintMallocedBuffer(cJSON *item,char *buf,const int len, cjbool fmt)
int cJSON_PrintPreallocated(cJSON *item,char *buf, const int len, const cjbool fmt)
{
char *out;
printbuffer p;
p.buffer = buf;
p.length = len;
p.offset = 0;
p.noalloc = true;
out = print_value(item,0,fmt,&p);
return (out != buf ? -1 : 0);
}
@@ -1147,7 +1154,11 @@ static char *print_array(const cJSON *item, int depth, cjbool fmt, printbuffer *
child = item->child;
while (child && !fail)
{
print_value(child, depth + 1, fmt, p);
ptr = print_value(child, depth + 1, fmt, p);
if (!ptr)
{
return NULL;
}
p->offset = update(p);
if (child->next)
{