mirror of
https://github.com/DaveGamble/cJSON.git
synced 2024-12-28 06:30:52 +08:00
cJSON_ConfigurationChangeParseEnd
Add a pointer to an end position of parsing to the cJSON_Configuration object. (Essentially like return_parse_end, but as offset instead of pointer).
This commit is contained in:
parent
9d801d64ea
commit
88c39fa2e4
33
cJSON.c
33
cJSON.c
@ -128,6 +128,7 @@ typedef struct internal_configuration
|
||||
cJSON_bool case_sensitive;
|
||||
cJSON_Allocators allocators;
|
||||
void *userdata;
|
||||
size_t *end_position;
|
||||
} internal_configuration;
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
@ -192,7 +193,8 @@ static void deallocate(const internal_configuration * const configuration, void
|
||||
global_deallocate_wrapper,\
|
||||
global_reallocate_wrapper\
|
||||
},\
|
||||
NULL /* no userdata */\
|
||||
NULL, /* no userdata */\
|
||||
NULL /* no end position */\
|
||||
}
|
||||
|
||||
/* this is necessary to assign the default configuration after initialization */
|
||||
@ -1058,7 +1060,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)
|
||||
static cJSON *parse(const char * const json, const internal_configuration * const configuration)
|
||||
{
|
||||
parse_buffer buffer = { 0, 0, 0, 0, default_configuration };
|
||||
cJSON *item = NULL;
|
||||
@ -1097,7 +1099,10 @@ static cJSON *parse(const char * const json, const internal_configuration * cons
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
*end_position = buffer.offset;
|
||||
if (configuration->end_position != NULL)
|
||||
{
|
||||
*configuration->end_position = buffer.offset;
|
||||
}
|
||||
|
||||
return item;
|
||||
|
||||
@ -1122,7 +1127,10 @@ fail:
|
||||
local_error.position = buffer.length - 1;
|
||||
}
|
||||
|
||||
*end_position = local_error.position;
|
||||
if (configuration->end_position != NULL)
|
||||
{
|
||||
*configuration->end_position = local_error.position;
|
||||
}
|
||||
global_error = local_error;
|
||||
}
|
||||
|
||||
@ -1137,7 +1145,8 @@ CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *json, const char **return_
|
||||
cJSON *item = NULL;
|
||||
|
||||
configuration.allow_data_after_json = !require_null_terminated;
|
||||
item = parse(json, &configuration, &end_position);
|
||||
configuration.end_position = &end_position;
|
||||
item = parse(json, &configuration);
|
||||
|
||||
if (return_parse_end != NULL)
|
||||
{
|
||||
@ -1150,8 +1159,7 @@ CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *json, const char **return_
|
||||
/* Default options for cJSON_Parse */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *json)
|
||||
{
|
||||
size_t end_position = 0;
|
||||
return parse(json, &global_configuration, &end_position);
|
||||
return parse(json, &global_configuration);
|
||||
}
|
||||
|
||||
#define cjson_min(a, b) (((a) < (b)) ? (a) : (b))
|
||||
@ -3012,6 +3020,17 @@ CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeUserdata(cJSON_Config
|
||||
return configuration;
|
||||
}
|
||||
|
||||
CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeParseEnd(cJSON_Configuration configuration, size_t * const parse_end)
|
||||
{
|
||||
if (configuration == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
((internal_configuration*)configuration)->end_position = parse_end;
|
||||
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))
|
||||
|
2
cJSON.h
2
cJSON.h
@ -179,6 +179,8 @@ CJSON_PUBLIC(cJSON_Configuration) cJSON_CreateConfiguration(const cJSON * const
|
||||
CJSON_PUBLIC(cJSON_Configuration) cJSON_ConfigurationChangeAllocators(cJSON_Configuration configuration, const cJSON_Allocators allocators);
|
||||
/* Change the allocator userdata attached to a cJSON_Configuration */
|
||||
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);
|
||||
|
||||
/* Supply malloc and free functions to cJSON globally */
|
||||
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks);
|
||||
|
@ -129,6 +129,20 @@ static void configuration_change_userdata_should_change_userdata(void)
|
||||
free(configuration);
|
||||
}
|
||||
|
||||
static void configuration_change_parse_end_should_change_parse_end(void)
|
||||
{
|
||||
size_t end_position = 0;
|
||||
internal_configuration *configuration = (internal_configuration*)cJSON_CreateConfiguration(NULL, NULL, NULL);
|
||||
TEST_ASSERT_NOT_NULL(configuration);
|
||||
|
||||
configuration = (internal_configuration*)cJSON_ConfigurationChangeParseEnd(configuration, &end_position);
|
||||
TEST_ASSERT_NOT_NULL(configuration);
|
||||
|
||||
TEST_ASSERT_TRUE_MESSAGE(configuration->end_position == &end_position, "Failed to set parse end.");
|
||||
|
||||
free(configuration);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
UNITY_BEGIN();
|
||||
@ -138,6 +152,7 @@ int main(void)
|
||||
RUN_TEST(create_configuration_should_take_custom_allocators);
|
||||
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);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
|
@ -419,7 +419,7 @@ static void *failing_realloc(void *pointer, size_t size, void *userdata)
|
||||
|
||||
static void ensure_should_fail_on_failed_realloc(void)
|
||||
{
|
||||
printbuffer buffer = {NULL, 10, 0, 0, false, {256, false, true, true, {global_allocate_wrapper, global_deallocate_wrapper, failing_realloc}, NULL } };
|
||||
printbuffer buffer = {NULL, 10, 0, 0, false, {256, false, true, true, {global_allocate_wrapper, global_deallocate_wrapper, failing_realloc}, NULL, NULL } };
|
||||
buffer.configuration.userdata = &buffer;
|
||||
buffer.buffer = (unsigned char*)malloc(100);
|
||||
TEST_ASSERT_NOT_NULL(buffer.buffer);
|
||||
|
Loading…
x
Reference in New Issue
Block a user