cJSON_ConfigurationChangePrebufferSize

This commit is contained in:
Max Bruckner 2018-02-03 02:09:10 +01:00
parent 0474d4d85f
commit c4c52cfe58
3 changed files with 39 additions and 28 deletions

36
cJSON.c
View File

@ -62,10 +62,6 @@
#define true ((cJSON_bool)1)
#define false ((cJSON_bool)0)
#ifndef SIZE_MAX
#define SIZE_MAX ((size_t)-1)
#endif
typedef struct {
const unsigned char *json;
size_t position;
@ -2894,21 +2890,6 @@ CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item)
return (item->type & 0xFF) == cJSON_Raw;
}
static size_t get_size_from_number(const cJSON * const number)
{
if (number->valuedouble >= SIZE_MAX)
{
return SIZE_MAX;
}
if (number->valuedouble <= 0)
{
return 0;
}
return (size_t)number->valuedouble;
}
CJSON_PUBLIC(cJSON_Configuration) cJSON_CreateConfiguration(const cJSON * const json, const cJSON_Allocators * const allocators, void *allocator_userdata)
{
internal_configuration *configuration = NULL;
@ -2949,12 +2930,6 @@ CJSON_PUBLIC(cJSON_Configuration) cJSON_CreateConfiguration(const cJSON * const
/* then overwrite with other options if they exist */
option = get_object_item(json, "buffer_size", &global_configuration);
if (cJSON_IsNumber(option))
{
configuration->buffer_size = get_size_from_number(option);
}
option = get_object_item(json, "format", &global_configuration);
if (cJSON_IsTrue(option))
{
@ -3032,6 +3007,17 @@ CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeParseEnd(cJSON_Config
return configuration;
}
CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangePrebufferSize(cJSON_Configuration configuration, const size_t buffer_size)
{
if ((configuration == NULL) || (buffer_size == 0))
{
return NULL;
}
((internal_configuration*)configuration)->buffer_size = buffer_size;
return configuration;
}
static cJSON_bool compare(const cJSON * const a, const cJSON * const b, const internal_configuration * const configuration)
{
if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF)) || cJSON_IsInvalid(a))

View File

@ -155,7 +155,6 @@ CJSON_PUBLIC(const char*) cJSON_Version(void);
* If NULL is passed to a function that expects an object of type cJSON_Configuration,
* the following default configuration is used:
* {
* "buffer_size": 256,
* "format": true,
* "case_sensitive": true,
* "allow_data_after_json": true
@ -181,6 +180,8 @@ CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeAllocators(cJSON_Conf
CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeUserdata(cJSON_Configuration configuration, void *userdata);
/* Change the pointer where the end of parsing is written to */
CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeParseEnd(cJSON_Configuration configuration, size_t * const parse_end);
/* Set how many bytes should be initially allocated for printing */
CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangePrebufferSize(cJSON_Configuration configuration, const size_t buffer_size);
/* Supply malloc and free functions to cJSON globally */
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks);

View File

@ -34,13 +34,13 @@ static void create_configuration_should_create_a_configuration(void)
internal_configuration *configuration = NULL;
int userdata = 1;
json = cJSON_Parse("{\"buffer_size\":1024,\"format\":false,\"case_sensitive\":false,\"allow_data_after_json\":false}");
json = cJSON_Parse("{\"format\":false,\"case_sensitive\":false,\"allow_data_after_json\":false}");
TEST_ASSERT_NOT_NULL(json);
configuration = (internal_configuration*)cJSON_CreateConfiguration(json, NULL, &userdata);
cJSON_Delete(json);
json = NULL;
TEST_ASSERT_NOT_NULL(configuration);
TEST_ASSERT_EQUAL_MESSAGE(configuration->buffer_size, 1024, "buffer_size has an incorrect value.");
TEST_ASSERT_EQUAL_MESSAGE(configuration->buffer_size, 256, "buffer_size has an incorrect value.");
TEST_ASSERT_FALSE_MESSAGE(configuration->format, "format has an incorrect value.");
TEST_ASSERT_FALSE_MESSAGE(configuration->case_sensitive, "case_sensitive has an incorrect value.");
TEST_ASSERT_FALSE_MESSAGE(configuration->allow_data_after_json, "allow_data_after_json has an incorrect value.");
@ -143,6 +143,28 @@ static void configuration_change_parse_end_should_change_parse_end(void)
free(configuration);
}
static void configuration_change_prebuffer_size_should_change_buffer_size(void)
{
internal_configuration *configuration = (internal_configuration*)cJSON_CreateConfiguration(NULL, NULL, NULL);
TEST_ASSERT_NOT_NULL(configuration);
configuration = (internal_configuration*)cJSON_ConfigurationChangePrebufferSize(configuration, 1024);
TEST_ASSERT_NOT_NULL(configuration);
TEST_ASSERT_EQUAL_MESSAGE(configuration->buffer_size, 1024, "Didn't set the buffer size correctly.");
free(configuration);
}
static void configuration_change_prebuffer_size_should_not_allow_empty_sizes(void)
{
internal_configuration *configuration = (internal_configuration*)cJSON_CreateConfiguration(NULL, NULL, NULL);
TEST_ASSERT_NOT_NULL(configuration);
TEST_ASSERT_NULL(cJSON_ConfigurationChangePrebufferSize(configuration, 0));
free(configuration);
}
int main(void)
{
UNITY_BEGIN();
@ -153,6 +175,8 @@ int main(void)
RUN_TEST(configuration_change_allocators_should_change_allocators);
RUN_TEST(configuration_change_userdata_should_change_userdata);
RUN_TEST(configuration_change_parse_end_should_change_parse_end);
RUN_TEST(configuration_change_prebuffer_size_should_change_buffer_size);
RUN_TEST(configuration_change_prebuffer_size_should_not_allow_empty_sizes);
return UNITY_END();
}