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.
This commit is contained in:
Christian Thaeter 2008-09-22 22:38:17 +02:00
parent dd9e5051ba
commit f754e1521f
5 changed files with 145 additions and 1 deletions

View file

@ -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

View file

@ -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.

View file

@ -0,0 +1,95 @@
/*
config_wordlist.c - Lumiera wordlist access functions
Copyright (C) Lumiera.org
2008, Christian Thaeter <ct@pipapo.org>
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:
*/

View file

@ -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

View file

@ -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