From f754e1521f4145fbad6ff05bc4c228299d0324d9 Mon Sep 17 00:00:00 2001 From: Christian Thaeter Date: Mon, 22 Sep 2008 22:38:17 +0200 Subject: [PATCH] Start of a 'wordlist' type for the config subsystem wordlists are simple not quoted words delimited by semicolon, tab, space or commas. Some special functions will allow to access each of this words by index etc. --- src/backend/Makefile.am | 1 + src/backend/config.h | 7 ++- src/backend/config_wordlist.c | 95 ++++++++++++++++++++++++++++++++++ tests/22config_highlevel.tests | 22 ++++++++ tests/backend/test-config.c | 21 ++++++++ 5 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 src/backend/config_wordlist.c diff --git a/src/backend/Makefile.am b/src/backend/Makefile.am index ce63e44de..2cde41ae2 100644 --- a/src/backend/Makefile.am +++ b/src/backend/Makefile.am @@ -30,6 +30,7 @@ liblumibackend_a_SOURCES = \ $(liblumibackend_a_srcdir)/filehandlecache.c \ $(liblumibackend_a_srcdir)/config.c \ $(liblumibackend_a_srcdir)/config_typed.c \ + $(liblumibackend_a_srcdir)/config_wordlist.c \ $(liblumibackend_a_srcdir)/configentry.c \ $(liblumibackend_a_srcdir)/configitem.c \ $(liblumibackend_a_srcdir)/config_lookup.c diff --git a/src/backend/config.h b/src/backend/config.h index 80f33a6d5..a603c2cbf 100644 --- a/src/backend/config.h +++ b/src/backend/config.h @@ -96,6 +96,7 @@ typedef lumiera_config* LumieraConfig; LUMIERA_CONFIG_TYPE(number, signed long long) \ LUMIERA_CONFIG_TYPE(real, long double) \ LUMIERA_CONFIG_TYPE(string, const char*) \ + LUMIERA_CONFIG_TYPE(wordlist, const char*) \ LUMIERA_CONFIG_TYPE(word, const char*) \ LUMIERA_CONFIG_TYPE(bool, int) @@ -223,7 +224,11 @@ lumiera_config_setdefault (const char* line); LUMIERA_CONFIG_TYPES #undef LUMIERA_CONFIG_TYPE - +/** + * special functions for accessing wordlists + */ +const char* +lumiera_config_wordlist_get_nth (const char* key, unsigned nth); // * {{{ lumiera_config_TYPE_set (const char* key, TYPE*value, const char* fmt) }}} // Highlevel interface for different types, fmt is a printf format specifier for the desired format, when NULL, defaults apply. diff --git a/src/backend/config_wordlist.c b/src/backend/config_wordlist.c new file mode 100644 index 000000000..3bcbf1ca8 --- /dev/null +++ b/src/backend/config_wordlist.c @@ -0,0 +1,95 @@ +/* + config_wordlist.c - Lumiera wordlist access functions + + Copyright (C) Lumiera.org + 2008, Christian Thaeter + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +//TODO: Support library includes// +#include "lib/error.h" +#include "lib/safeclib.h" + + +//TODO: Lumiera header includes// +#include "backend/config.h" + +//TODO: internal/static forward declarations// + + +//TODO: System includes// + +/** + * return nth word of a wordlist + */ +const char* +lumiera_config_wordlist_get_nth (const char* key, unsigned nth) +{ + const char* value; + size_t len; + + if (!lumiera_config_wordlist_get (key, &value)) + return NULL; + + for (;;) + { + value += strspn (value, " \t,;"); + len = strcspn (value, " \t,;"); + if (!nth && *value) + break; + + --nth; + value += len; + + if (!*value) + return NULL; + } + + return lumiera_tmpbuf_strndup (value, len); +} + +#if 0 +int +lumiera_config_wordlist_find (const char* key, const char* value) +{ +} + + +LumieraConfigitem +lumiera_config_wordlist_set_nth (const char* key, const char** value, unsigned nth) +{ +} + +LumieraConfigitem +lumiera_config_wordlist_append (const char* key, const char** value, unsigned nth) +{ +} + +LumieraConfigitem +lumiera_config_wordlist_add (const char* key, const char** value, unsigned nth) +{ +} +#endif + + + +/* +// Local Variables: +// mode: C +// c-file-style: "gnu" +// indent-tabs-mode: nil +// End: +*/ diff --git a/tests/22config_highlevel.tests b/tests/22config_highlevel.tests index e0e4995c8..42e2561f3 100644 --- a/tests/22config_highlevel.tests +++ b/tests/22config_highlevel.tests @@ -192,3 +192,25 @@ out: item->key_size = '7' out: item->key = 'key.foo < key.bar' out: item->delim = '< key.bar' END + + +TEST "wordlist get item from empty list should fail" wordlist_get_nth 'foo.bar' '' 0 << END +out: 'NULL' +END + +TEST "wordlist get item past end should fail" wordlist_get_nth 'foo.bar' 'baz barf gnarf' 3 << END +out: 'NULL' +END + +TEST "wordlist get first item" wordlist_get_nth 'foo.bar' 'baz barf gnarf' 0 << END +out: 'baz' +END + +TEST "wordlist get last item" wordlist_get_nth 'foo.bar' 'baz barf; gnarf' 2 << END +out: 'gnarf' +END + +TEST "wordlist get middle" wordlist_get_nth 'foo.bar' 'baz barf, gnarf' 1 << END +out: 'barf' +END + diff --git a/tests/backend/test-config.c b/tests/backend/test-config.c index ea312a429..2ed4f4234 100644 --- a/tests/backend/test-config.c +++ b/tests/backend/test-config.c @@ -287,4 +287,25 @@ TEST ("configitem_simple_content_check") lumiera_config_destroy (); } + +TEST ("wordlist_get_nth") +{ + REQUIRE (argv[2]); + REQUIRE (argv[3]); + REQUIRE (argv[4]); + + lumiera_config_init ("./"); + + if (!lumiera_config_wordlist_set (argv[2], &argv[3])) + printf ("failed setting word '%s=%s': %s\n", argv[2], argv[3], lumiera_error ()); + + const char* word = lumiera_config_wordlist_get_nth (argv[2], atoi (argv[4])); + + printf ("'%s'\n", word?word:"NULL"); + + lumiera_config_destroy (); +} + + + TESTS_END