From 16c2fe9cc04a3f15fe56d3be2f3be19a1d731fb2 Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Mon, 21 Mar 2011 15:07:08 -0700 Subject: [PATCH] This adds a couple of new options for options parsing. --- libmemcached/callback.c | 2 +- libmemcached/constants.h | 1 + libmemcached/memcached.c | 16 ++++++++++++++-- libmemcached/memcached.h | 4 ++++ libmemcached/options.cc | 20 ++++++++++++++++++-- libmemcached/options.h | 3 +++ libmemcached/strerror.c | 2 ++ tests/mem_functions.c | 9 +++++---- tests/parser.cc | 31 +++++++++++++++++++++++++++++++ tests/parser.h | 6 ++++++ 10 files changed, 85 insertions(+), 9 deletions(-) diff --git a/libmemcached/callback.c b/libmemcached/callback.c index 69e47d4d..6d5285a5 100644 --- a/libmemcached/callback.c +++ b/libmemcached/callback.c @@ -39,7 +39,7 @@ memcached_return_t memcached_callback_set(memcached_st *ptr, memcached_array_free(ptr->prefix_key); ptr->prefix_key= memcached_strcpy(ptr, (const char *)data, strlen((const char*)data)); - f (! ptr->prefix_key) + if (! ptr->prefix_key) return memcached_set_error(ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, NULL); } else diff --git a/libmemcached/constants.h b/libmemcached/constants.h index 2ec8a9e8..3c9614d1 100644 --- a/libmemcached/constants.h +++ b/libmemcached/constants.h @@ -74,6 +74,7 @@ typedef enum { MEMCACHED_AUTH_PROBLEM, MEMCACHED_AUTH_FAILURE, MEMCACHED_AUTH_CONTINUE, + MEMCACHED_PARSE_ERROR, MEMCACHED_MAXIMUM_RETURN /* Always add new error code before */ } memcached_return_t; diff --git a/libmemcached/memcached.c b/libmemcached/memcached.c index 3d255fc0..a1f4b379 100644 --- a/libmemcached/memcached.c +++ b/libmemcached/memcached.c @@ -35,8 +35,8 @@ static const memcached_st global_copy= { .use_udp= false, .verify_key= false, .tcp_keepalive= false, - .load_from_file= false - + .load_from_file= false, + .ping_service= false } }; @@ -176,6 +176,18 @@ memcached_st *memcached_create(memcached_st *ptr) return ptr; } +memcached_st *memcached_create_with_options(const char *string, size_t length) +{ + memcached_st *self= memcached_create(NULL); + + if (! self) + return NULL; + + memcached_parse_options(self, string, length); + + return self; +} + void memcached_reset(memcached_st *ptr) { WATCHPOINT_ASSERT(ptr); diff --git a/libmemcached/memcached.h b/libmemcached/memcached.h index 9d0d2f0b..65b3b048 100644 --- a/libmemcached/memcached.h +++ b/libmemcached/memcached.h @@ -86,6 +86,7 @@ struct memcached_st { bool verify_key:1; bool tcp_keepalive:1; bool load_from_file:1; + bool ping_service:1; } flags; memcached_server_distribution_t distribution; hashkit_st hashkit; @@ -146,6 +147,9 @@ void memcached_servers_reset(memcached_st *ptr); LIBMEMCACHED_API memcached_st *memcached_create(memcached_st *ptr); +LIBMEMCACHED_API +memcached_st *memcached_create_with_options(const char *string, size_t length); + LIBMEMCACHED_API void memcached_free(memcached_st *ptr); diff --git a/libmemcached/options.cc b/libmemcached/options.cc index 5a9feacb..fefcb57d 100644 --- a/libmemcached/options.cc +++ b/libmemcached/options.cc @@ -42,6 +42,23 @@ int libmemcached_parse(type_st *, yyscan_t *); +memcached_return_t memcached_check_options(const char *option_string, size_t length, const char *error_buffer, size_t error_buffer_size) +{ + memcached_st memc; + if (! memcached_create(&memc)) + return MEMCACHED_MEMORY_ALLOCATION_FAILURE; + + memcached_return_t rc= memcached_parse_options(&memc, option_string, length); + if (rc != MEMCACHED_SUCCESS && error_buffer && error_buffer_size) + { + strncpy(error_buffer, error_buffer_size, memcached_last_error_message(&memc)); + } + + memcached_free(&memc); + + return rc; +} + memcached_return_t memcached_parse_options(memcached_st *self, char const *option_string, size_t length) { type_st pp; @@ -61,7 +78,7 @@ memcached_return_t memcached_parse_options(memcached_st *self, char const *optio libmemcached_lex_destroy(pp.yyscanner); if (not success) - return MEMCACHED_INVALID_ARGUMENTS; + return memcached_set_error(self, MEMCACHED_PARSE_ERROR, NULL); return MEMCACHED_SUCCESS; } @@ -83,7 +100,6 @@ memcached_return_t memcached_parse_file_options(memcached_st *self, const char * continue; rc= memcached_parse_options(self, buffer, length); - if (rc != MEMCACHED_SUCCESS) break; } diff --git a/libmemcached/options.h b/libmemcached/options.h index a2351548..b1b9b92a 100644 --- a/libmemcached/options.h +++ b/libmemcached/options.h @@ -41,6 +41,9 @@ extern "C" { #endif +LIBMEMCACHED_API + memcached_return_t memcached_check_options(const char *option_string, size_t length, const char *error_buffer, size_t error_buffer_size); + LIBMEMCACHED_API memcached_return_t memcached_parse_options(memcached_st *ptr, const char *option_string, size_t length); diff --git a/libmemcached/strerror.c b/libmemcached/strerror.c index d1c0f297..5fc365c2 100644 --- a/libmemcached/strerror.c +++ b/libmemcached/strerror.c @@ -91,6 +91,8 @@ const char *memcached_strerror(memcached_st *ptr, memcached_return_t rc) return "AUTHENTICATION FAILURE"; case MEMCACHED_AUTH_CONTINUE: return "CONTINUE AUTHENTICATION"; + case MEMCACHED_PARSE_ERROR: + return "ERROR OCCURED WHILE PARSING"; case MEMCACHED_MAXIMUM_RETURN: return "Gibberish returned!"; default: diff --git a/tests/mem_functions.c b/tests/mem_functions.c index 7eb8e83b..4b961911 100644 --- a/tests/mem_functions.c +++ b/tests/mem_functions.c @@ -401,10 +401,10 @@ static test_return_t error_test(memcached_st *memc) 2300930706U, 2943759320U, 674306647U, 2400528935U, 54481931U, 4186304426U, 1741088401U, 2979625118U, 4159057246U, 3425930182U, 2593724503U, 1868899624U, - 1769812374U, 2302537950U, 1110330676U }; + 1769812374U, 2302537950U, 1110330676U, 3365377466U }; // You have updated the memcache_error messages but not updated docs/tests. - test_true(MEMCACHED_MAXIMUM_RETURN == 43); + test_true(MEMCACHED_MAXIMUM_RETURN == 44); for (rc= MEMCACHED_SUCCESS; rc < MEMCACHED_MAXIMUM_RETURN; rc++) { uint32_t hash_val; @@ -6320,11 +6320,12 @@ test_st parser_tests[] ={ {"boolean_options", 0, (test_callback_fn)parser_boolean_options_test }, {"distribtions", 0, (test_callback_fn)parser_distribution_test }, {"hash", 0, (test_callback_fn)parser_hash_test }, + {"memcached_check_options", 0, (test_callback_fn)memcached_check_options_test }, + {"memcached_parse_file_options", 0, (test_callback_fn)memcached_parse_file_options_test }, {"number_options", 0, (test_callback_fn)parser_number_options_test }, + {"prefix_key", 0, (test_callback_fn)parser_key_prefix_test }, {"server", 0, (test_callback_fn)server_test }, {"servers", 0, (test_callback_fn)servers_test }, - {"prefix_key", 0, (test_callback_fn)parser_key_prefix_test }, - {"memcached_parse_file_options", 0, (test_callback_fn)memcached_parse_file_options_test }, {0, 0, (test_callback_fn)0} }; diff --git a/tests/parser.cc b/tests/parser.cc index 8119c82b..5a818755 100644 --- a/tests/parser.cc +++ b/tests/parser.cc @@ -347,3 +347,34 @@ test_return_t memcached_parse_file_options_test(memcached_st *junk) return TEST_SUCCESS; } + +test_return_t memcached_check_options_test(memcached_st *junk) +{ + (void)junk; + + memcached_return_t rc; + + rc= memcached_check_options(STRING_WITH_LEN("--server=localhost"), NULL, 0); + test_true(rc == MEMCACHED_SUCCESS); + + rc= memcached_check_options(STRING_WITH_LEN("--dude=localhost"), NULL, 0); + test_false(rc == MEMCACHED_SUCCESS); + test_true(rc == MEMCACHED_PARSE_ERROR); + + return TEST_SUCCESS; +} + +test_return_t memcached_create_with_options_test(memcached_st *junk) +{ + (void)junk; + + memcached_st *memc_ptr; + memc_ptr= memcached_create_with_options(STRING_WITH_LEN("--server=localhost")); + test_true(memc_ptr); + memcached_free(memc_ptr); + + memc_ptr= memcached_create_with_options(STRING_WITH_LEN("--dude=localhost")); + test_false(memc_ptr); + + return TEST_SUCCESS; +} diff --git a/tests/parser.h b/tests/parser.h index 90a117a8..d3dfbf64 100644 --- a/tests/parser.h +++ b/tests/parser.h @@ -70,6 +70,12 @@ test_return_t parser_key_prefix_test(memcached_st *junk); LIBTEST_INTERNAL_API test_return_t memcached_parse_file_options_test(memcached_st *junk); +LIBTEST_INTERNAL_API + test_return_t memcached_check_options_test(memcached_st *junk); + +LIBTEST_INTERNAL_API + test_return_t memcached_create_with_options_test(memcached_st *junk); + #ifdef __cplusplus } #endif -- 2.30.2