From 479909d0a6b2b1b94a059f4b2291b3adc1854c06 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Wed, 31 Jan 2018 23:28:48 +0100 Subject: [PATCH] Put format into internal_configuration --- cJSON.c | 47 ++++++++++++++++++++++---------------------- cJSON.h | 2 +- tests/misc_tests.c | 6 +++--- tests/parse_array.c | 4 ++-- tests/parse_number.c | 2 +- tests/parse_object.c | 4 ++-- tests/parse_string.c | 4 ++-- tests/parse_value.c | 2 +- tests/print_array.c | 10 +++++----- tests/print_number.c | 2 +- tests/print_object.c | 10 +++++----- tests/print_string.c | 2 +- tests/print_value.c | 5 +++-- 13 files changed, 51 insertions(+), 49 deletions(-) diff --git a/cJSON.c b/cJSON.c index b4fc65f..54738df 100644 --- a/cJSON.c +++ b/cJSON.c @@ -120,6 +120,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); @@ -145,7 +146,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) { @@ -355,7 +356,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; @@ -996,7 +996,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 */ @@ -1081,7 +1081,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]; @@ -1092,7 +1092,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) { @@ -1150,17 +1149,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) { @@ -1176,8 +1177,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)) { @@ -1190,7 +1191,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)) { @@ -1201,8 +1202,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); } @@ -1491,14 +1492,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++ = ' '; } @@ -1642,7 +1643,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) { @@ -1651,7 +1652,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'; } @@ -1659,7 +1660,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); @@ -1681,14 +1682,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'; } @@ -1702,7 +1703,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) { @@ -1713,7 +1714,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'; } @@ -1723,12 +1724,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++) diff --git a/cJSON.h b/cJSON.h index 878f657..26e5be9 100644 --- a/cJSON.h +++ b/cJSON.h @@ -147,7 +147,7 @@ CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item); /* Render a cJSON entity to text for transfer/storage without any formatting. */ CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item); /* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */ -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); /* Render a cJSON entity to text using a buffer already allocated in memory with given length. Returns 1 on success and 0 on failure. */ /* NOTE: cJSON is not always 100% accurate in estimating how much memory it will use, so to be safe allocate 5 bytes more than you actually need */ CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format); diff --git a/tests/misc_tests.c b/tests/misc_tests.c index e97e96c..efcc3ca 100644 --- a/tests/misc_tests.c +++ b/tests/misc_tests.c @@ -419,7 +419,7 @@ static void *failing_realloc(void *pointer, size_t size) static void ensure_should_fail_on_failed_realloc(void) { - printbuffer buffer = {NULL, 10, 0, 0, false, false, {&malloc, &free, &failing_realloc}}; + printbuffer buffer = {NULL, 10, 0, 0, false, {false, &malloc, &free, &failing_realloc}}; buffer.buffer = (unsigned char*)malloc(100); TEST_ASSERT_NOT_NULL(buffer.buffer); @@ -429,7 +429,7 @@ static void ensure_should_fail_on_failed_realloc(void) static void skip_utf8_bom_should_skip_bom(void) { const unsigned char string[] = "\xEF\xBB\xBF{}"; - parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } }; buffer.content = string; buffer.length = sizeof(string); buffer.configuration = global_configuration; @@ -441,7 +441,7 @@ static void skip_utf8_bom_should_skip_bom(void) static void skip_utf8_bom_should_not_skip_bom_if_not_at_beginning(void) { const unsigned char string[] = " \xEF\xBB\xBF{}"; - parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } }; buffer.content = string; buffer.length = sizeof(string); buffer.configuration = global_configuration; diff --git a/tests/parse_array.c b/tests/parse_array.c index dcf2d63..0ece51d 100644 --- a/tests/parse_array.c +++ b/tests/parse_array.c @@ -44,7 +44,7 @@ static void assert_is_array(cJSON *array_item) static void assert_not_array(const char *json) { - parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } }; buffer.content = (const unsigned char*)json; buffer.length = strlen(json) + sizeof(""); buffer.configuration = global_configuration; @@ -55,7 +55,7 @@ static void assert_not_array(const char *json) static void assert_parse_array(const char *json) { - parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } }; buffer.content = (const unsigned char*)json; buffer.length = strlen(json) + sizeof(""); buffer.configuration = global_configuration; diff --git a/tests/parse_number.c b/tests/parse_number.c index f499ab6..c83d85e 100644 --- a/tests/parse_number.c +++ b/tests/parse_number.c @@ -45,7 +45,7 @@ static void assert_is_number(cJSON *number_item) static void assert_parse_number(const char *string, int integer, double real) { - parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } }; buffer.content = (const unsigned char*)string; buffer.length = strlen(string) + sizeof(""); diff --git a/tests/parse_object.c b/tests/parse_object.c index 990758d..2535d87 100644 --- a/tests/parse_object.c +++ b/tests/parse_object.c @@ -52,7 +52,7 @@ static void assert_is_child(cJSON *child_item, const char *name, int type) static void assert_not_object(const char *json) { - parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } }; + parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } }; parsebuffer.content = (const unsigned char*)json; parsebuffer.length = strlen(json) + sizeof(""); parsebuffer.configuration = global_configuration; @@ -64,7 +64,7 @@ static void assert_not_object(const char *json) static void assert_parse_object(const char *json) { - parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } }; + parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } }; parsebuffer.content = (const unsigned char*)json; parsebuffer.length = strlen(json) + sizeof(""); parsebuffer.configuration = global_configuration; diff --git a/tests/parse_string.c b/tests/parse_string.c index 66e96ea..6e34362 100644 --- a/tests/parse_string.c +++ b/tests/parse_string.c @@ -45,7 +45,7 @@ static void assert_is_string(cJSON *string_item) static void assert_parse_string(const char *string, const char *expected) { - parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } }; buffer.content = (const unsigned char*)string; buffer.length = strlen(string) + sizeof(""); buffer.configuration = global_configuration; @@ -59,7 +59,7 @@ static void assert_parse_string(const char *string, const char *expected) static void assert_not_parse_string(const char * const string) { - parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } }; buffer.content = (const unsigned char*)string; buffer.length = strlen(string) + sizeof(""); buffer.configuration = global_configuration; diff --git a/tests/parse_value.c b/tests/parse_value.c index 3e8fb27..4036546 100644 --- a/tests/parse_value.c +++ b/tests/parse_value.c @@ -43,7 +43,7 @@ static void assert_is_value(cJSON *value_item, int type) static void assert_parse_value(const char *string, int type) { - parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } }; buffer.content = (const unsigned char*) string; buffer.length = strlen(string) + sizeof(""); buffer.configuration = global_configuration; diff --git a/tests/print_array.c b/tests/print_array.c index 5cf556c..399e23e 100644 --- a/tests/print_array.c +++ b/tests/print_array.c @@ -31,10 +31,10 @@ static void assert_print_array(const char * const expected, const char * const i cJSON item[1]; - printbuffer formatted_buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; - printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; + printbuffer formatted_buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0 } }; + printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0 } }; - parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } }; + parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } }; parsebuffer.content = (const unsigned char*)input; parsebuffer.length = strlen(input) + sizeof(""); parsebuffer.configuration = global_configuration; @@ -56,11 +56,11 @@ static void assert_print_array(const char * const expected, const char * const i memset(item, 0, sizeof(item)); TEST_ASSERT_TRUE_MESSAGE(parse_array(item, &parsebuffer), "Failed to parse array."); - unformatted_buffer.format = false; + unformatted_buffer.configuration.format = false; TEST_ASSERT_TRUE_MESSAGE(print_array(item, &unformatted_buffer), "Failed to print unformatted string."); TEST_ASSERT_EQUAL_STRING_MESSAGE(input, printed_unformatted, "Unformatted array is not correct."); - formatted_buffer.format = true; + formatted_buffer.configuration.format = true; TEST_ASSERT_TRUE_MESSAGE(print_array(item, &formatted_buffer), "Failed to print formatted string."); TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed_formatted, "Formatted array is not correct."); diff --git a/tests/print_number.c b/tests/print_number.c index 79c4e6b..2d00d34 100644 --- a/tests/print_number.c +++ b/tests/print_number.c @@ -28,7 +28,7 @@ static void assert_print_number(const char *expected, double input) { unsigned char printed[1024]; cJSON item[1]; - printbuffer buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; + printbuffer buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0 } }; buffer.buffer = printed; buffer.length = sizeof(printed); buffer.offset = 0; diff --git a/tests/print_object.c b/tests/print_object.c index 5b9b0a5..15e6188 100644 --- a/tests/print_object.c +++ b/tests/print_object.c @@ -31,9 +31,9 @@ static void assert_print_object(const char * const expected, const char * const cJSON item[1]; - printbuffer formatted_buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; - printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; - parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } }; + printbuffer formatted_buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0 } }; + printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0 } }; + parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } }; /* buffer for parsing */ parsebuffer.content = (const unsigned char*)input; @@ -57,11 +57,11 @@ static void assert_print_object(const char * const expected, const char * const memset(item, 0, sizeof(item)); TEST_ASSERT_TRUE_MESSAGE(parse_object(item, &parsebuffer), "Failed to parse object."); - unformatted_buffer.format = false; + unformatted_buffer.configuration.format = false; TEST_ASSERT_TRUE_MESSAGE(print_object(item, &unformatted_buffer), "Failed to print unformatted string."); TEST_ASSERT_EQUAL_STRING_MESSAGE(input, printed_unformatted, "Unformatted object is not correct."); - formatted_buffer.format = true; + formatted_buffer.configuration.format = true; TEST_ASSERT_TRUE_MESSAGE(print_object(item, &formatted_buffer), "Failed to print formatted string."); TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed_formatted, "Formatted ojbect is not correct."); diff --git a/tests/print_string.c b/tests/print_string.c index 8b69680..e7d46aa 100644 --- a/tests/print_string.c +++ b/tests/print_string.c @@ -27,7 +27,7 @@ static void assert_print_string(const char *expected, const char *input) { unsigned char printed[1024]; - printbuffer buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; + printbuffer buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0 } }; buffer.buffer = printed; buffer.length = sizeof(printed); buffer.offset = 0; diff --git a/tests/print_value.c b/tests/print_value.c index 1c8599f..a0c929d 100644 --- a/tests/print_value.c +++ b/tests/print_value.c @@ -32,13 +32,14 @@ static void assert_print_value(const char *input) { unsigned char printed[1024]; cJSON item[1]; - printbuffer buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; - parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } }; + printbuffer buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0 } }; + parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } }; buffer.buffer = printed; buffer.length = sizeof(printed); buffer.offset = 0; buffer.noalloc = true; buffer.configuration = global_configuration; + buffer.configuration.format = false; parsebuffer.content = (const unsigned char*)input; parsebuffer.length = strlen(input) + sizeof("");