From 8b901567ed44a20e9ff1b636eb674a9dbc6d8a44 Mon Sep 17 00:00:00 2001 From: Christian Thaeter Date: Fri, 8 Aug 2008 08:28:07 +0200 Subject: [PATCH] getting strings from the config a string can be either quoted (single or doublequoted), then the text between this quotes is returned, quotes are escaped by doubling themself, no chopping at either end is done, or not quoted then the the tabs and spaces are chopped from the value at either end. --- src/backend/config.h | 4 +- src/backend/config_typed.c | 113 ++++++++++++++++++++++++++++++++---- tests/20config.tests | 27 ++++++++- tests/backend/test-config.c | 19 ++++++ 4 files changed, 150 insertions(+), 13 deletions(-) diff --git a/src/backend/config.h b/src/backend/config.h index 8a6aa5198..29456ac02 100644 --- a/src/backend/config.h +++ b/src/backend/config.h @@ -85,8 +85,8 @@ typedef lumiera_config* LumieraConfig; #define LUMIERA_CONFIG_TYPES \ LUMIERA_CONFIG_TYPE(number, signed long long) \ LUMIERA_CONFIG_TYPE(real, long double) \ - LUMIERA_CONFIG_TYPE(string, const char*) \ - LUMIERA_CONFIG_TYPE(word, const char*) \ + LUMIERA_CONFIG_TYPE(string, char*) \ + LUMIERA_CONFIG_TYPE(word, char*) \ LUMIERA_CONFIG_TYPE(bool, int) diff --git a/src/backend/config_typed.c b/src/backend/config_typed.c index 259d2e225..34d46f109 100644 --- a/src/backend/config_typed.c +++ b/src/backend/config_typed.c @@ -31,6 +31,7 @@ extern LumieraConfig lumiera_global_config; //TODO: System includes// +#include /** * @file @@ -124,24 +125,116 @@ lumiera_config_real_set (const char* key, long double* value, const char* fmt) } + +/** + * helper function, takes a raw input string and give a tmpbuf with the string parsed back. + */ +static char* +scan_string (const char* in) +{ + /* chop leading blanks */ + in += strspn(in, " \t"); + + char quote = *in; + char* end; + char* ret = NULL; + + if (quote == '"' || quote == '\'') + { + /* quoted string */ + ++in; + end = strchr (in, quote); + while (end && end[1] == quote) + end = strchr (end + 2, quote); + + if (end) + { + ret = lumiera_tmpbuf_strndup (in, end-in); + + /* replace double quote chars with single one */ + char* wpos; + char* rpos; + for (wpos = rpos = ret; *rpos; ++rpos, ++wpos) + { + if (*rpos == quote) + ++rpos; + *wpos = *rpos; + } + *wpos = '\0'; + } + else + /* quotes doesnt match */ + LUMIERA_ERROR_SET (config_typed, CONFIG_SYNTAX_VALUE); + } + else + { + /* unquoted string */ + ret = lumiera_tmpbuf_strndup (in, SIZE_MAX); + + /* chop trailing blanks */ + end = ret + strlen (ret) - 1; + while (end > ret && (*end == ' ' || *end == '\t')) + *end-- = '\0'; + } + + return ret; +} + /** * String * either a string which covers the whole line - * or a quoted string until the ending quote - * suggestion: - * "doublequotes" allow normal C backslash escapes - * 'singlequotes' dont allow escapes except a double '' is the ' itself */ int -lumiera_config_string_get (const char* key, const char** value, const char* def) +lumiera_config_string_get (const char* key, char** value, const char* def) { TRACE (config_typed); - UNIMPLEMENTED(); - return 0; + + int ret = -1; + + const char* raw_value = NULL; + + LUMIERA_RDLOCK_SECTION (config_typed, lumiera_global_config->rh, &lumiera_global_config->lock) + { + if (!lumiera_config_get (key, &raw_value)) + { + if (raw_value) + { + *value = scan_string (raw_value); + + TRACE (config_typed, "RAW_VALUE %s, scanned .%s.", raw_value, *value); + + if (*value) + ret = 0; /* all ok */ + else if (def) + goto try_default; + } + else if (def) + { + ret = 0; + try_default: + + *value = scan_string (def); + TRACE (config_typed, "DEFAULT %s, scanned .%s.", def, *value); + if (*value) + { + TODO ("register default (writelock or mutex!)"); + } + else + { + ret = -1; + LUMIERA_ERROR_SET (config_typed, CONFIG_DEFAULT); + } + } + else + LUMIERA_ERROR_SET (config, CONFIG_NO_ENTRY); + } + } + + return ret; } int -lumiera_config_string_set (const char* key, const char** value, const char* fmt) +lumiera_config_string_set (const char* key, char** value, const char* fmt) { TRACE (config_typed); UNIMPLEMENTED(); @@ -155,7 +248,7 @@ lumiera_config_string_set (const char* key, const char** value, const char* fmt) * A single word, no quotes, nothing */ int -lumiera_config_word_get (const char* key, const char** value, const char* def) +lumiera_config_word_get (const char* key, char** value, const char* def) { TRACE (config_typed); UNIMPLEMENTED(); @@ -163,7 +256,7 @@ lumiera_config_word_get (const char* key, const char** value, const char* def) } int -lumiera_config_word_set (const char* key, const char** value, const char* fmt) +lumiera_config_word_set (const char* key, char** value, const char* fmt) { TRACE (config_typed); UNIMPLEMENTED(); diff --git a/tests/20config.tests b/tests/20config.tests index d4eebff47..c51cd6015 100644 --- a/tests/20config.tests +++ b/tests/20config.tests @@ -79,7 +79,32 @@ PLANNED "real set" <