From 700a7e9645775dae6caa033a393d393b17726650 Mon Sep 17 00:00:00 2001 From: Simeon Voelkel Date: Wed, 27 Aug 2008 11:00:10 +0200 Subject: [PATCH] Added directive-parser and tests for a content-check of a parsed configitem --- src/backend/configitem.c | 54 +++++++++++++++++++++++++++++++++++ tests/20config.tests | 56 +++++++++++++++++++++++++++++++++++++ tests/backend/test-config.c | 31 ++++++++++++++++++++ 3 files changed, 141 insertions(+) diff --git a/src/backend/configitem.c b/src/backend/configitem.c index a49694280..9ce36328b 100644 --- a/src/backend/configitem.c +++ b/src/backend/configitem.c @@ -208,6 +208,60 @@ lumiera_configitem_parse (LumieraConfigitem self, const char* line) else if (*itr == '@' ) { /*this is a directive*/ + + /*itr points now to @*/ + self->key = itr; + + /*check whether there are illegal whitespaces after @*/ + itr++; + if (*itr && !isspace(*itr)) + { + /*now look for the end of the directive and set the keysize*/ + self->key_size = strspn (itr, LUMIERA_CONFIG_KEY_CHARS); + + itr += self->key_size; + + /*we need a key with a length greather than zero and + * either the end of the line + * or a whitespace after the key */ + + if ( self->key_size && ( !*itr || (*itr && isspace(*itr)) )) + { + /*look for given arguments*/ + + /*skip blanks*/ + while (*itr && isspace (*itr)) + itr++; + + if (*itr) + { + /*there are arguments given, thus set delim*/ + self->delim = itr - 1; + } + else + { + /*no arguments were given*/ + self->delim = NULL; + } + } + else + { + /*malformed lines shall be treated like if they were comments*/ + self->key = NULL; + self->key_size = 0; + + LUMIERA_ERROR_SET (config_item, CONFIG_SYNTAX); + } + } + else + { + /*there occurred already an error right after the @!*/ + /*malformed lines shall be treated like if they were comments*/ + self->key = NULL; + self->key_size = 0; + + LUMIERA_ERROR_SET (config_item, CONFIG_SYNTAX); + } } else if (*itr == '[' ) { diff --git a/tests/20config.tests b/tests/20config.tests index 7a4ea2517..4f2ba9018 100644 --- a/tests/20config.tests +++ b/tests/20config.tests @@ -140,3 +140,59 @@ END TEST "create configitem with blank line" configitem_simple_ctor_dtor $' \t \t' <line = ' #comment bla' +END + +TEST "check content of configitem with section" configitem_simple_content_check $'[ key.foo suffix.bar ] ' << END +out: item->line = '[ key.foo suffix.bar ] ' +out: item->key_size = '7' +out: item->key = 'key.foo suffix.bar ] ' +out: item->delim = ' suffix.bar ] ' +END + +TEST "check content of configitem with directive (without argument)" configitem_simple_content_check $'\t @directive ' << END +out: item->line = ' @directive ' +out: item->key_size = '9' +out: item->key = '@directive ' +END + +TEST "check content of configitem with directive (with argument)" configitem_simple_content_check $'\t @directive \targument' << END +out: item->line = ' @directive argument' +out: item->key_size = '9' +out: item->key = '@directive argument' +out: item->delim = ' argument' +END + +TEST "check content of configitem with configentry" configitem_simple_content_check $' \t\t key.foo \t\t=\tbar' << END +out: item->line = ' key.foo = bar' +out: item->key_size = '7' +out: item->key = 'key.foo = bar' +out: item->delim = '= bar' +END + +TEST "check content of configitem with configentry (redirect)" configitem_simple_content_check $' \t\t key.foo \t\t<\tkey.bar' << END +out: item->line = ' key.foo < key.bar' +out: item->key_size = '7' +out: item->key = 'key.foo < key.bar' +out: item->delim = '< key.bar' +END diff --git a/tests/backend/test-config.c b/tests/backend/test-config.c index 1369aa8bc..eae982fc6 100644 --- a/tests/backend/test-config.c +++ b/tests/backend/test-config.c @@ -141,5 +141,36 @@ TEST ("configitem_simple_ctor_dtor") lumiera_config_destroy (); } +TEST ("configitem_simple_content_check") +{ + REQUIRE (argv[2]); + lumiera_config_init ("./"); + + LumieraConfigitem item; + + item = lumiera_configitem_new (argv[2]); + + if ( item->line ) + { + printf("item->line = \'%s\'\n", item->line); + } + if ( item->key_size ) + { + printf("item->key_size = \'%i\'\n", item->key_size); + } + if ( item->key ) + { + printf("item->key = \'%s\'\n", item->key); + } + if ( item->delim ) + { + printf("item->delim = \'%s\'\n", item->delim); + } + + lumiera_configitem_delete (item); + + lumiera_config_destroy (); +} + TESTS_END