From f0d3a824ed9acb9f6e3080683abe2bcc8b0cd6bd Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Mon, 21 Mar 2011 09:02:56 -0700 Subject: [PATCH] Merge in all changes related to being able to read configuration files. --- ChangeLog | 5 +++++ libmemcached/options.cc | 26 ++++++++++++++++++++++---- libmemcached/options/parser.yy | 6 ++++++ libmemcached/options/scanner.l | 6 +++++- support/example.cnf | 11 +++++++++++ tests/mem_functions.c | 1 + tests/parser.cc | 15 +++++++++++++++ tests/parser.h | 3 +++ 8 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 support/example.cnf diff --git a/ChangeLog b/ChangeLog index 538463c4..9c0bea9c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +0.49 + * Fix calls to auto methods so that if value is not passed in nothing bad happens. + * New parser calls for generating memcached_st objects. + * New error system. + 0.48 Tue Mar 15 23:05:18 PDT 2011 * Fix memory leak in server parse. * Move test framework out to be its own library (easier to work with Gearman). diff --git a/libmemcached/options.cc b/libmemcached/options.cc index 0d8c39f2..5a9feacb 100644 --- a/libmemcached/options.cc +++ b/libmemcached/options.cc @@ -66,10 +66,28 @@ memcached_return_t memcached_parse_options(memcached_st *self, char const *optio return MEMCACHED_SUCCESS; } -memcached_return_t memcached_parse_file_options(memcached_st *ptr, const char *filename) +memcached_return_t memcached_parse_file_options(memcached_st *self, const char *filename) { - (void)ptr; - (void)filename; + FILE *fp= fopen(filename, "r"); + + if (! fp) + return memcached_set_errno(self, errno, NULL); - return MEMCACHED_SUCCESS; + char buffer[BUFSIZ]; + memcached_return_t rc= MEMCACHED_INVALID_ARGUMENTS; + while (fgets(buffer, sizeof(buffer), fp)) + { + size_t length= strlen(buffer); + + if (length == 1 and buffer[0] == '\n') + continue; + + rc= memcached_parse_options(self, buffer, length); + + if (rc != MEMCACHED_SUCCESS) + break; + } + fclose(fp); + + return rc; } diff --git a/libmemcached/options/parser.yy b/libmemcached/options/parser.yy index 8cd16d53..5b140628 100644 --- a/libmemcached/options/parser.yy +++ b/libmemcached/options/parser.yy @@ -60,6 +60,8 @@ inline void libmemcached_error(YYLTYPE *locp, type_st *parser, yyscan_t *scanner %token SERVER %token SERVERS %token UNKNOWN +%token COMMENT +%token EMPTY_LINE %token DASH_OPTION @@ -143,6 +145,10 @@ statement: { } | statement ' ' DASH_OPTION expression { } + | COMMENT + { } + | EMPTY_LINE + { } ; diff --git a/libmemcached/options/scanner.l b/libmemcached/options/scanner.l index 3a135ff9..f5d09e7a 100644 --- a/libmemcached/options/scanner.l +++ b/libmemcached/options/scanner.l @@ -81,6 +81,10 @@ static void get_lex_chars(char* buffer, int& result, int max_size, struct type_s [\t\r\n] ; /* skip whitespace */ +^#.*$ { + return COMMENT; + } + "--" { return DASH_OPTION; } SERVER { return SERVER; } @@ -195,7 +199,7 @@ JENKINS { return JENKINS; } return STRING; } -\"[[:alnum:]]*\" { +\".*\" { yylval->string.c_str = yytext; yylval->string.length = yyleng; return QUOTED_STRING; diff --git a/support/example.cnf b/support/example.cnf new file mode 100644 index 00000000..3ccb796f --- /dev/null +++ b/support/example.cnf @@ -0,0 +1,11 @@ +# http://en.wikipedia.org/wiki/Consistent_hashing +--distribution=consistent + +# Store one additional copy on each node. +--number-of-replicas=2 +--prefix-key="my_foo" +--prefix-key="my_prefix" +--server=localhost:11211 +--server=localhost:11212 +--server=localhost:11213 +--verify-key diff --git a/tests/mem_functions.c b/tests/mem_functions.c index 3aff82fd..090d00cc 100644 --- a/tests/mem_functions.c +++ b/tests/mem_functions.c @@ -6320,6 +6320,7 @@ test_st parser_tests[] ={ {"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 9eb81714..8119c82b 100644 --- a/tests/parser.cc +++ b/tests/parser.cc @@ -332,3 +332,18 @@ test_return_t parser_key_prefix_test(memcached_st *junk) (void)junk; return _test_option(distribution_strings); } + +test_return_t memcached_parse_file_options_test(memcached_st *junk) +{ + (void)junk; + memcached_st memc; + memcached_st *memc_ptr= memcached_create(&memc); + + test_true(memc_ptr); + + memcached_return_t rc= memcached_parse_file_options(memc_ptr, "support/example.cnf"); + test_true_got(rc == MEMCACHED_SUCCESS, rc == MEMCACHED_INVALID_ARGUMENTS ? memcached_last_error_message(&memc) : memcached_strerror(NULL, rc)); + memcached_free(memc_ptr); + + return TEST_SUCCESS; +} diff --git a/tests/parser.h b/tests/parser.h index 5c5ad7f2..90a117a8 100644 --- a/tests/parser.h +++ b/tests/parser.h @@ -67,6 +67,9 @@ test_return_t parser_boolean_options_test(memcached_st *junk); LIBTEST_INTERNAL_API test_return_t parser_key_prefix_test(memcached_st *junk); +LIBTEST_INTERNAL_API +test_return_t memcached_parse_file_options_test(memcached_st *junk); + #ifdef __cplusplus } #endif -- 2.30.2