From 69f8bb777851449903b96c44ce185f903dbd701d Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Thu, 1 Feb 2018 02:01:17 +0100 Subject: [PATCH] default_configuration: Macro for the internal_configuration defaults --- cJSON.c | 24 +++++++++++++----------- tests/misc_tests.c | 4 ++-- 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 | 6 +++--- tests/print_number.c | 2 +- tests/print_object.c | 6 +++--- tests/print_string.c | 2 +- tests/print_value.c | 4 ++-- 12 files changed, 33 insertions(+), 31 deletions(-) diff --git a/cJSON.c b/cJSON.c index 31c9c12..fafb6a7 100644 --- a/cJSON.c +++ b/cJSON.c @@ -149,15 +149,17 @@ static void *internal_realloc(void *pointer, size_t size) #define internal_realloc realloc #endif -static internal_configuration global_configuration = { - 256, /* default buffer size */ - true, /* enable formatting by default */ - true, /* allow data after the JSON by default */ - true, /* case sensitive by default */ - internal_malloc, - internal_free, - internal_realloc -}; +#define default_configuration {\ + 256, /* default buffer size */\ + true, /* enable formatting by default */\ + true, /* allow data after the JSON by default */\ + true, /* case sensitive by default */\ + internal_malloc,\ + internal_free,\ + internal_realloc\ +} + +static internal_configuration global_configuration = default_configuration; static unsigned char* custom_strdup(const unsigned char* string, const internal_configuration * const configuration) { @@ -1013,7 +1015,7 @@ static parse_buffer *skip_utf8_bom(parse_buffer * const buffer) /* Parse an object - create a new root, and populate. */ static cJSON *parse(const char * const json, const internal_configuration * const configuration, size_t *end_position) { - parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0 } }; + parse_buffer buffer = { 0, 0, 0, 0, default_configuration }; cJSON *item = NULL; /* reset global error position */ @@ -1203,7 +1205,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, 0, 0, 0 } }; + printbuffer p = { 0, 0, 0, 0, 0, default_configuration}; if ((length < 0) || (buffer == NULL)) { diff --git a/tests/misc_tests.c b/tests/misc_tests.c index 969cde2..e0a129d 100644 --- a/tests/misc_tests.c +++ b/tests/misc_tests.c @@ -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, 0, 0, 0, 0 } }; + parse_buffer buffer = { 0, 0, 0, 0, default_configuration }; 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, 0, 0, 0, 0 } }; + parse_buffer buffer = { 0, 0, 0, 0, default_configuration }; buffer.content = string; buffer.length = sizeof(string); buffer.configuration = global_configuration; diff --git a/tests/parse_array.c b/tests/parse_array.c index d4c4f42..4143966 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, 0, 0, 0, 0 } }; + parse_buffer buffer = { 0, 0, 0, 0, default_configuration }; 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, 0, 0, 0, 0 } }; + parse_buffer buffer = { 0, 0, 0, 0, default_configuration }; 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 1751c5e..5850265 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, 0, 0, 0, 0 } }; + parse_buffer buffer = { 0, 0, 0, 0, default_configuration }; buffer.content = (const unsigned char*)string; buffer.length = strlen(string) + sizeof(""); diff --git a/tests/parse_object.c b/tests/parse_object.c index 415048a..0f46a22 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, 0, 0, 0, 0 } }; + parse_buffer parsebuffer = { 0, 0, 0, 0, default_configuration }; 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, 0, 0, 0, 0 } }; + parse_buffer parsebuffer = { 0, 0, 0, 0, default_configuration }; 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 c17b973..d22f7c8 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, 0, 0, 0, 0 } }; + parse_buffer buffer = { 0, 0, 0, 0, default_configuration }; 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, 0, 0, 0, 0 } }; + parse_buffer buffer = { 0, 0, 0, 0, default_configuration }; 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 5269a6d..2b17fc8 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, 0, 0, 0, 0 } }; + parse_buffer buffer = { 0, 0, 0, 0, default_configuration }; 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 70a984b..05636c3 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, 0, 0, 0 } }; - printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0 } }; + printbuffer formatted_buffer = { 0, 0, 0, 0, 0, default_configuration }; + printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, default_configuration }; - parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0 } }; + parse_buffer parsebuffer = { 0, 0, 0, 0, default_configuration }; parsebuffer.content = (const unsigned char*)input; parsebuffer.length = strlen(input) + sizeof(""); parsebuffer.configuration = global_configuration; diff --git a/tests/print_number.c b/tests/print_number.c index 4cb6d34..c5174c5 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, 0, 0, 0 } }; + printbuffer buffer = { 0, 0, 0, 0, 0, default_configuration }; buffer.buffer = printed; buffer.length = sizeof(printed); buffer.offset = 0; diff --git a/tests/print_object.c b/tests/print_object.c index d49a95c..582a10b 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, 0, 0, 0 } }; - printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0 } }; - parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0 } }; + printbuffer formatted_buffer = { 0, 0, 0, 0, 0, default_configuration }; + printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, default_configuration }; + parse_buffer parsebuffer = { 0, 0, 0, 0, default_configuration }; /* buffer for parsing */ parsebuffer.content = (const unsigned char*)input; diff --git a/tests/print_string.c b/tests/print_string.c index 37494a9..c0a8eee 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, 0, 0, 0 } }; + printbuffer buffer = { 0, 0, 0, 0, 0, default_configuration }; buffer.buffer = printed; buffer.length = sizeof(printed); buffer.offset = 0; diff --git a/tests/print_value.c b/tests/print_value.c index abb7c16..45a9d2c 100644 --- a/tests/print_value.c +++ b/tests/print_value.c @@ -32,8 +32,8 @@ 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, 0, 0, 0 } }; - parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0 } }; + printbuffer buffer = { 0, 0, 0, 0, 0, default_configuration }; + parse_buffer parsebuffer = { 0, 0, 0, 0, default_configuration }; buffer.buffer = printed; buffer.length = sizeof(printed); buffer.offset = 0;