mirror of
https://github.com/DaveGamble/cJSON.git
synced 2023-08-10 21:13:26 +03:00
reformatting: ensure
This commit is contained in:
parent
b53ce73709
commit
7f9a2691bf
45
cJSON.c
45
cJSON.c
@ -220,21 +220,40 @@ typedef struct
|
||||
int offset;
|
||||
} printbuffer;
|
||||
|
||||
static char* ensure(printbuffer *p,int needed)
|
||||
/* realloc printbuffer if necessary to have at least "needed" bytes more */
|
||||
static char* ensure(printbuffer *p, int needed)
|
||||
{
|
||||
char *newbuffer;int newsize;
|
||||
if (!p || !p->buffer) return 0;
|
||||
needed+=p->offset;
|
||||
if (needed<=p->length) return p->buffer+p->offset;
|
||||
char *newbuffer;
|
||||
int newsize;
|
||||
if (!p || !p->buffer)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
needed += p->offset;
|
||||
if (needed <= p->length)
|
||||
{
|
||||
return p->buffer + p->offset;
|
||||
}
|
||||
|
||||
newsize=pow2gt(needed);
|
||||
newbuffer=(char*)cJSON_malloc(newsize);
|
||||
if (!newbuffer) {cJSON_free(p->buffer);p->length=0,p->buffer=0;return 0;}
|
||||
if (newbuffer) memcpy(newbuffer,p->buffer,p->length);
|
||||
cJSON_free(p->buffer);
|
||||
p->length=newsize;
|
||||
p->buffer=newbuffer;
|
||||
return newbuffer+p->offset;
|
||||
newsize = pow2gt(needed);
|
||||
newbuffer = (char*)cJSON_malloc(newsize);
|
||||
if (!newbuffer)
|
||||
{
|
||||
cJSON_free(p->buffer);
|
||||
p->length = 0;
|
||||
p->buffer = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
if (newbuffer)
|
||||
{
|
||||
memcpy(newbuffer, p->buffer, p->length);
|
||||
}
|
||||
cJSON_free(p->buffer);
|
||||
p->length = newsize;
|
||||
p->buffer = newbuffer;
|
||||
|
||||
return newbuffer + p->offset;
|
||||
}
|
||||
|
||||
static int update(printbuffer *p)
|
||||
|
Loading…
Reference in New Issue
Block a user