config api change, return pointers as truth value

NULL indicates some failure,
anything else success (with some usable value)
This commit is contained in:
Christian Thaeter 2008-09-09 21:34:40 +02:00
parent d26a92b67f
commit 056bb87f30
4 changed files with 77 additions and 76 deletions

View file

@ -171,14 +171,14 @@ lumiera_config_purge (const char* filename)
}
int
const char*
lumiera_config_get (const char* key, const char** value)
{
TRACE (config);
REQUIRE (key);
REQUIRE (value);
int ret = -1;
*value = NULL;
/* we translate the key for the env var override by making it uppercase and replace . with _,
as side effect, this also checks the key syntax */
@ -194,7 +194,6 @@ lumiera_config_get (const char* key, const char** value)
*value = getenv(env);
if (*value)
{
ret = 0;
NOTICE (config, "envvar override for config %s = %s", env, *value);
}
else
@ -205,7 +204,6 @@ lumiera_config_get (const char* key, const char** value)
if (item)
{
*value = item->delim+1;
ret = 0;
}
else
LUMIERA_ERROR_SET (config, CONFIG_NO_ENTRY);
@ -216,18 +214,18 @@ lumiera_config_get (const char* key, const char** value)
LUMIERA_ERROR_SET (config, CONFIG_SYNTAX_KEY);
}
return ret;
return *value;
}
int
const char*
lumiera_config_get_default (const char* key, const char** value)
{
TRACE (config);
REQUIRE (key);
REQUIRE (value);
int ret = -1;
*value = NULL;
TODO ("follow '<' delegates?");
TODO ("refactor _get and get_default to iterator access (return LList or Lookupentry)");
@ -236,39 +234,54 @@ lumiera_config_get_default (const char* key, const char** value)
if (item && item->parent == &lumiera_global_config->defaults)
{
*value = item->delim+1;
ret = 0;
}
return ret;
return *value;
}
int
LumieraConfigitem
lumiera_config_set (const char* key, const char* delim_value)
{
TRACE (config);
TODO ("if does this item already exist in a user writeable file?");
TODO (" replace delim_value");
LumieraConfigitem item = lumiera_config_lookup_item_find (&lumiera_global_config->keys, key);
if (item && item->parent != &lumiera_global_config->defaults)
{
TODO ("a user writeable file?");
TODO (" replace delim_value");
//LumieraConfigitem
lumiera_configitem_set_value (item, delim_value);
}
else
{
TODO ("create item");
TODO (" find matching prefix");
TODO (" find matching suffix");
TODO (" find proper prefix indentation, else use config.indent");
TODO (" create configitem with prefix/suffix removed");
TODO ("else");
TODO (" find matching prefix");
TODO (" find matching suffix");
TODO (" find proper prefix indentation, else use config.indent");
TODO (" create configitem with prefix/suffix removed");
char* line = lumiera_tmpbuf_snprintf (SIZE_MAX, "%s %s", key, delim_value);
item = lumiera_configitem_new (line);
if (item)
{
TODO ("next 2 ensure must generate runtime errors");
ENSURE (item->delim, "syntax error");
ENSURE (*item->delim == '=' || *item->delim == '<', "syntax error,");
// * set a value by key
// * handles internally everything as string:string key:value pair.
// * lowlevel function
// * tag file as dirty
// * set will create a new user configuration file if it does not exist yet or will append a line to the existing one in RAM. These files, tagged as 'dirty', will be only written if save() is called.
TODO ("insert in proper parent (file)");
llist_insert_tail (&lumiera_global_config->TODO_unknown.childs, &item->link);
item->parent = &lumiera_global_config->TODO_unknown;
lumiera_config_lookup_insert (&lumiera_global_config->keys, item);
TODO ("tag file as dirty");
}
}
TODO ("return item?");
UNIMPLEMENTED();
return -1;
return item;
}
@ -294,14 +307,14 @@ lumiera_config_setdefault (const char* line)
if (item)
{
ENSURE (item->delim, "default must be a configentry with key=value or key<delegate syntax")
ENSURE (item->delim, "default must be a configentry with key=value or key<delegate syntax");
ENSURE (*item->delim == '=' || *item->delim == '<', "default must be a configentry with key=value or key<delegate syntax");
TRACE (config, "registering default: '%s'", item->line);
llist_insert_head (&lumiera_global_config->defaults.childs, &item->link);
item->parent = &lumiera_global_config->defaults;
lumiera_config_lookup_insert_default (&lumiera_global_config->keys, item);
lumiera_config_lookup_insert (&lumiera_global_config->keys, item);
}
}
}

View file

@ -91,11 +91,11 @@ typedef lumiera_config* LumieraConfig;
*/
/* TODO: add here as 'LUMIERA_CONFIG_TYPE(name, ctype)' the _get/_set prototypes are declared automatically below, you still have to implement them in config.c */
#define LUMIERA_CONFIG_TYPES \
LUMIERA_CONFIG_TYPE(link, char*) \
LUMIERA_CONFIG_TYPE(link, const char*) \
LUMIERA_CONFIG_TYPE(number, signed long long) \
LUMIERA_CONFIG_TYPE(real, long double) \
LUMIERA_CONFIG_TYPE(string, char*) \
LUMIERA_CONFIG_TYPE(word, char*) \
LUMIERA_CONFIG_TYPE(string, const char*) \
LUMIERA_CONFIG_TYPE(word, const char*) \
LUMIERA_CONFIG_TYPE(bool, int)
@ -166,11 +166,11 @@ lumiera_config_dump (FILE* out);
/**
*
*/
int
const char*
lumiera_config_get (const char* key, const char** value);
int
const char*
lumiera_config_get_default (const char* key, const char** value);
@ -188,7 +188,7 @@ lumiera_config_get_default (const char* key, const char** value);
* @param delim_value delimiter (= or <) followed by the value to be set
*
*/
int
LumieraConfigitem
lumiera_config_set (const char* key, const char* delim_value);
@ -196,7 +196,7 @@ lumiera_config_set (const char* key, const char* delim_value);
* Installs a default value for a config key.
* Any key might have an associated default value which is used when
* no other configuration is available, this can be set once.
* Any subsequent call will be a no-op.
* Any subsequent call will be a no-op. This function writelocks the config system.
* @param line line with key, delimiter and value to store as default value
* @return NULL in case of an error, else a pointer to the default configitem
*/
@ -217,7 +217,7 @@ lumiera_config_setdefault (const char* line);
*
*/
#define LUMIERA_CONFIG_TYPE(name, type) \
int \
const char* \
lumiera_config_##name##_get (const char* key, type* value);
LUMIERA_CONFIG_TYPES
#undef LUMIERA_CONFIG_TYPE

View file

@ -38,8 +38,8 @@ extern LumieraConfig lumiera_global_config;
* Here are the high level typed configuration interfaces defined.
*/
int
lumiera_config_link_get (const char* key, char** value)
const char*
lumiera_config_link_get (const char* key, const char** value)
{
TRACE (config_typed);
UNIMPLEMENTED();
@ -47,7 +47,7 @@ lumiera_config_link_get (const char* key, char** value)
}
int
lumiera_config_link_set (const char* key, char** value)
lumiera_config_link_set (const char* key, const char** value)
{
TRACE (config_typed);
UNIMPLEMENTED();
@ -59,26 +59,23 @@ lumiera_config_link_set (const char* key, char** value)
* Number
* signed integer numbers, in different formats (decimal, hex, oct, binary(for masks))
*/
int
const char*
lumiera_config_number_get (const char* key, long long* value)
{
TRACE (config_typed);
int ret = -1;
const char* raw_value = NULL;
LUMIERA_RDLOCK_SECTION (config_typed, &lumiera_global_config->lock)
{
if (!lumiera_config_get (key, &raw_value))
if (lumiera_config_get (key, &raw_value))
{
if (raw_value)
{
/* got it, scan it */
if (sscanf (raw_value, "%Li", value) == 1)
ret = 0; /* all ok */
else
if (sscanf (raw_value, "%Li", value) != 1)
{
raw_value = NULL;
LUMIERA_ERROR_SET (config_typed, CONFIG_SYNTAX_VALUE);
}
}
@ -87,7 +84,7 @@ lumiera_config_number_get (const char* key, long long* value)
}
}
return ret;
return raw_value;
}
int
@ -103,7 +100,7 @@ lumiera_config_number_set (const char* key, long long* value)
* Real
* floating point number in standard formats (see printf/scanf)
*/
int
const char*
lumiera_config_real_get (const char* key, long double* value)
{
TRACE (config_typed);
@ -183,36 +180,31 @@ scan_string (const char* in)
return ret;
}
int
lumiera_config_string_get (const char* key, char** value)
const char*
lumiera_config_string_get (const char* key, const char** value)
{
TRACE (config_typed);
int ret = -1;
const char* raw_value = NULL;
const char* raw_value = *value = NULL;
LUMIERA_RDLOCK_SECTION (config_typed, &lumiera_global_config->lock)
{
if (!lumiera_config_get (key, &raw_value))
if (lumiera_config_get (key, &raw_value))
{
if (raw_value)
{
*value = scan_string (raw_value);
if (*value)
ret = 0; /* all ok */
/* else error was raised by scan_string */
}
else
LUMIERA_ERROR_SET (config, CONFIG_NO_ENTRY);
}
}
return ret;
return *value;
}
int
lumiera_config_string_set (const char* key, char** value)
lumiera_config_string_set (const char* key, const char** value)
{
TRACE (config_typed);
UNIMPLEMENTED();
@ -247,35 +239,31 @@ scan_word (const char* in)
return ret;
}
int
lumiera_config_word_get (const char* key, char** value)
const char*
lumiera_config_word_get (const char* key, const char** value)
{
TRACE (config_typed, "KEY %s", key);
int ret = -1;
const char* raw_value = NULL;
const char* raw_value = *value = NULL;
LUMIERA_RDLOCK_SECTION (config_typed, &lumiera_global_config->lock)
{
if (!lumiera_config_get (key, &raw_value))
if (lumiera_config_get (key, &raw_value))
{
if (raw_value)
{
*value = scan_word (raw_value);
if (*value)
ret = 0; /* all ok */
}
else
LUMIERA_ERROR_SET (config, CONFIG_NO_ENTRY);
}
}
return ret;
return *value;
}
int
lumiera_config_word_set (const char* key, char** value)
lumiera_config_word_set (const char* key, const char** value)
{
TRACE (config_typed);
UNIMPLEMENTED();
@ -287,7 +275,7 @@ lumiera_config_word_set (const char* key, char** value)
* Bool
* Bool in various formats, (0,1(!1), yes/no, true/false, on/off, set/clear)
*/
int
const char*
lumiera_config_bool_get (const char* key, int* value)
{
TRACE (config_typed);

View file

@ -94,7 +94,7 @@ TEST ("number_get")
lumiera_config_setdefault (lumiera_tmpbuf_snprintf (SIZE_MAX, "%s = %s", argv[2], argv[3]));
if (!lumiera_config_number_get (argv[2], &number))
if (lumiera_config_number_get (argv[2], &number))
printf ("%lld\n", number);
else
printf ("%s, %lld\n", lumiera_error (), number);
@ -111,7 +111,7 @@ TEST ("number_get_nodefault")
long long number = 0;
if (!lumiera_config_number_get (argv[2], &number))
if (lumiera_config_number_get (argv[2], &number))
printf ("%lld\n", number);
else
printf ("%s\n", lumiera_error ());
@ -127,11 +127,11 @@ TEST ("string_get")
lumiera_config_init ("./");
char* string;
const char* string;
lumiera_config_setdefault (lumiera_tmpbuf_snprintf (SIZE_MAX, "%s = %s", argv[2], argv[3]));
if (!lumiera_config_string_get (argv[2], &string))
if (lumiera_config_string_get (argv[2], &string))
printf ("'%s'\n", string);
else
printf ("%s, '%s'\n", lumiera_error (), string);
@ -150,7 +150,7 @@ TEST ("string_set")
FIXME ("handle error");
const char* string;
if (!lumiera_config_get (argv[2], &string))
if (lumiera_config_get (argv[2], &string))
printf ("'%s'\n", string);
else
printf ("%s, '%s'\n", lumiera_error (), string);
@ -166,11 +166,11 @@ TEST ("word_get")
lumiera_config_init ("./");
char* word;
const char* word;
lumiera_config_setdefault (lumiera_tmpbuf_snprintf (SIZE_MAX, "%s = %s", argv[2], argv[3]));
if (!lumiera_config_word_get (argv[2], &word))
if (lumiera_config_word_get (argv[2], &word))
printf ("'%s'\n", word);
else
printf ("%s, '%s'\n", lumiera_error (), word);