Put format into internal_configuration

This commit is contained in:
Max Bruckner
2018-03-25 23:32:03 +02:00
parent 677f0cb1bb
commit 27977adc93
13 changed files with 51 additions and 49 deletions
+24 -23
View File
@@ -118,6 +118,7 @@ static int case_insensitive_strcmp(const unsigned char *string1, const unsigned
typedef struct internal_configuration
{
cJSON_bool format;
void *(*allocate)(size_t size);
void (*deallocate)(void *pointer);
void *(*reallocate)(void *pointer, size_t size);
@@ -143,7 +144,7 @@ static void *internal_realloc(void *pointer, size_t size)
#define internal_realloc realloc
#endif
static internal_configuration global_configuration = { internal_malloc, internal_free, internal_realloc };
static internal_configuration global_configuration = { true, internal_malloc, internal_free, internal_realloc };
static unsigned char* custom_strdup(const unsigned char* string, const internal_configuration * const configuration)
{
@@ -353,7 +354,6 @@ typedef struct
size_t offset;
size_t depth; /* current nesting depth (for formatted printing) */
cJSON_bool noalloc;
cJSON_bool format; /* is this print a formatted print */
internal_configuration configuration;
} printbuffer;
@@ -994,7 +994,7 @@ static parse_buffer *skip_utf8_bom(parse_buffer * const buffer)
/* Parse an object - create a new root, and populate. */
CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated)
{
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } };
cJSON *item = NULL;
/* reset error position */
@@ -1079,7 +1079,7 @@ CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value)
#define cjson_min(a, b) (((a) < (b)) ? (a) : (b))
static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_configuration * const configuration)
static unsigned char *print(const cJSON * const item, const internal_configuration * const configuration)
{
static const size_t default_buffer_size = 256;
printbuffer buffer[1];
@@ -1090,7 +1090,6 @@ static unsigned char *print(const cJSON * const item, cJSON_bool format, const i
/* create buffer */
buffer->buffer = (unsigned char*) configuration->allocate(default_buffer_size);
buffer->length = default_buffer_size;
buffer->format = format;
buffer->configuration = *configuration;
if (buffer->buffer == NULL)
{
@@ -1148,17 +1147,19 @@ fail:
/* Render a cJSON item/entity/structure to text. */
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item)
{
return (char*)print(item, true, &global_configuration);
return (char*)print(item, &global_configuration);
}
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item)
{
return (char*)print(item, false, &global_configuration);
internal_configuration configuration = global_configuration;
configuration.format = false;
return (char*)print(item, &configuration);
}
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt)
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool format)
{
printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
printbuffer p = { 0, 0, 0, 0, 0, { 0, 0, 0, 0 } };
if (prebuffer < 0)
{
@@ -1174,8 +1175,8 @@ CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON
p.length = (size_t)prebuffer;
p.offset = 0;
p.noalloc = false;
p.format = fmt;
p.configuration = global_configuration;
p.configuration.format = format;
if (!print_value(item, &p))
{
@@ -1188,7 +1189,7 @@ CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON
CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format)
{
printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
printbuffer p = { 0, 0, 0, 0, 0, { 0, 0, 0, 0 } };
if ((length < 0) || (buffer == NULL))
{
@@ -1199,8 +1200,8 @@ CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, cons
p.length = (size_t)length;
p.offset = 0;
p.noalloc = true;
p.format = format;
p.configuration = global_configuration;
p.configuration.format = format;
return print_value(item, &p);
}
@@ -1485,14 +1486,14 @@ static cJSON_bool print_array(const cJSON * const item, printbuffer * const outp
update_offset(output_buffer);
if (current_element->next)
{
length = (size_t) (output_buffer->format ? 2 : 1);
length = (size_t) (output_buffer->configuration.format ? 2 : 1);
output_pointer = ensure(output_buffer, length + 1);
if (output_pointer == NULL)
{
return false;
}
*output_pointer++ = ',';
if(output_buffer->format)
if(output_buffer->configuration.format)
{
*output_pointer++ = ' ';
}
@@ -1636,7 +1637,7 @@ static cJSON_bool print_object(const cJSON * const item, printbuffer * const out
}
/* Compose the output: */
length = (size_t) (output_buffer->format ? 2 : 1); /* fmt: {\n */
length = (size_t) (output_buffer->configuration.format ? 2 : 1); /* fmt: {\n */
output_pointer = ensure(output_buffer, length + 1);
if (output_pointer == NULL)
{
@@ -1645,7 +1646,7 @@ static cJSON_bool print_object(const cJSON * const item, printbuffer * const out
*output_pointer++ = '{';
output_buffer->depth++;
if (output_buffer->format)
if (output_buffer->configuration.format)
{
*output_pointer++ = '\n';
}
@@ -1653,7 +1654,7 @@ static cJSON_bool print_object(const cJSON * const item, printbuffer * const out
while (current_item)
{
if (output_buffer->format)
if (output_buffer->configuration.format)
{
size_t i;
output_pointer = ensure(output_buffer, output_buffer->depth);
@@ -1675,14 +1676,14 @@ static cJSON_bool print_object(const cJSON * const item, printbuffer * const out
}
update_offset(output_buffer);
length = (size_t) (output_buffer->format ? 2 : 1);
length = (size_t) (output_buffer->configuration.format ? 2 : 1);
output_pointer = ensure(output_buffer, length);
if (output_pointer == NULL)
{
return false;
}
*output_pointer++ = ':';
if (output_buffer->format)
if (output_buffer->configuration.format)
{
*output_pointer++ = '\t';
}
@@ -1696,7 +1697,7 @@ static cJSON_bool print_object(const cJSON * const item, printbuffer * const out
update_offset(output_buffer);
/* print comma if not last */
length = (size_t) ((output_buffer->format ? 1 : 0) + (current_item->next ? 1 : 0));
length = (size_t) ((output_buffer->configuration.format ? 1 : 0) + (current_item->next ? 1 : 0));
output_pointer = ensure(output_buffer, length + 1);
if (output_pointer == NULL)
{
@@ -1707,7 +1708,7 @@ static cJSON_bool print_object(const cJSON * const item, printbuffer * const out
*output_pointer++ = ',';
}
if (output_buffer->format)
if (output_buffer->configuration.format)
{
*output_pointer++ = '\n';
}
@@ -1717,12 +1718,12 @@ static cJSON_bool print_object(const cJSON * const item, printbuffer * const out
current_item = current_item->next;
}
output_pointer = ensure(output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2);
output_pointer = ensure(output_buffer, output_buffer->configuration.format ? (output_buffer->depth + 1) : 2);
if (output_pointer == NULL)
{
return false;
}
if (output_buffer->format)
if (output_buffer->configuration.format)
{
size_t i;
for (i = 0; i < (output_buffer->depth - 1); i++)