diff --git a/src/backend/config.c b/src/backend/config.c index 630a656f2..60311af72 100644 --- a/src/backend/config.c +++ b/src/backend/config.c @@ -20,6 +20,7 @@ */ //TODO: Support library includes// +#include "lib/safeclib.h" //TODO: Lumiera header includes// @@ -29,7 +30,7 @@ //TODO: System includes// - +#include /** * @file @@ -40,25 +41,35 @@ NOBUG_DEFINE_FLAG_PARENT (config_all, backend); NOBUG_DEFINE_FLAG_PARENT (config, config_all); LUMIERA_ERROR_DEFINE (CONFIG_SYNTAX, "Syntax error in configfile"); +LUMIERA_ERROR_DEFINE (CONFIG_TYPE, "Config value has wrong type"); /* singleton config */ -static lumiera_config; +static LumieraConfig the_config = NULL; int lumiera_config_init (const char* path) { - UNIMPLEMENTED(); - return -1; + REQUIRE (!the_config, "Configuration subsystem already initialized"); + + the_config = malloc (sizeof (*the_config)); + the_config->path = lumiera_strndup (path, SIZE_MAX); + return 0; } -int -lumiera_config_destroy() +void +lumiera_config_destroy () { - UNIMPLEMENTED(); - return -1; + if (the_config) + { + lumiera_free (the_config->path); + lumiera_free (the_config); + the_config = NULL; + } + else + WARN (config, "Tried to destroy non initialized config subsystem"); } diff --git a/src/backend/config.h b/src/backend/config.h index d4f3ef6ce..89ce0caab 100644 --- a/src/backend/config.h +++ b/src/backend/config.h @@ -28,10 +28,12 @@ //TODO: Forward declarations// struct lumiera_config_struct; + NOBUG_DECLARE_FLAG (config_all); NOBUG_DECLARE_FLAG (config); LUMIERA_ERROR_DECLARE (CONFIG_SYNTAX); +LUMIERA_ERROR_DECLARE (CONFIG_TYPE); //TODO: Lumiera header includes// @@ -50,6 +52,7 @@ struct lumiera_config_struct { // cuckoo hash // configfile list + char* path; }; typedef struct lumiera_config_struct lumiera_config; @@ -69,17 +72,31 @@ typedef lumiera_config* LumieraConfig; // * does only initialize the variables, so that they get valid values, but does not allocate them as they will be allocated before as they are singleton. // * lumiera_config_init (const char* searchpath) searchpath is a buildin-default, can be changed via configure and can be appended and overridden by using a flag, e.g. {{{ --config-path-append="" }}} or {{{ --config-path="" }}} + +/** + * Initialize the configuration subsystem. + * @param path search path for config files. + * Must be called only once + */ int lumiera_config_init (const char* path); // * frees all space allocated by the ConfigLoader. -int -lumiera_config_destroy(); + +/** + * Destroys the configuration subsystem. + * Subsequent calls are no-ops. + */ +void +lumiera_config_destroy (); // * reads '''one''' single configuration file that will include all settings from other files. // * does not read itself but give delegates reading. The actual reading and parsing will be done in configfile object. s.later. +/** + * + */ int lumiera_config_load (const char* file); @@ -90,11 +107,17 @@ lumiera_config_load (const char* file); // * does initiate the actual saving procedure by delegating the save to the actual configfile objects, see below. // * empty user configuration files in RAM will be deleted from disk on write. // * checks whether the file has changed since last read, and will print out an error if necessary instead of overriding it without notification. +/** + * + */ int lumiera_config_save (); // * `lumiera_config_purge(const char* filename)` removes all configs loaded from filename +/** + * + */ int lumiera_config_purge (const char* filename); @@ -104,6 +127,9 @@ lumiera_config_purge (const char* filename); // * handles internally everything as string:string key:value pair. // * lowlevel function // * lumiera_config_integer_get (const char* key, int *value) will return integers instead of strings and return 0 if succeeded and -1 if it failed. +/** + * + */ int lumiera_config_get (const char* key, const char** value); @@ -114,6 +140,9 @@ lumiera_config_get (const char* key, const char** value); // * 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. +/** + * + */ int lumiera_config_set (const char* key, const char* value); @@ -127,6 +156,9 @@ lumiera_config_set (const char* key, const char* value); // if default is given then 'KEY_NOT_FOUND' is not a error here, if default is NULL then it is // NOTE2: default values are given as strings, the config loader remembers a given default value and checks if it got changed // when it is _set(). Thus a default value can be supressed when set/written +/** + * + */ #define LUMIERA_CONFIG_TYPE(name, type) \ int \ lumiera_config_##name##_get (const char* key, type* value, const char* def); @@ -137,6 +169,9 @@ LUMIERA_CONFIG_TYPES // * {{{ 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. +/** + * + */ #define LUMIERA_CONFIG_TYPE(name, type) \ int \ lumiera_config_##name##_set (const char* key, type* value, const char* fmt); @@ -146,11 +181,17 @@ LUMIERA_CONFIG_TYPES // * {{{ lumiera_config_reset(...) }}} // * reset a value by key to the system default values, thus removes a user's configuration line. +/** + * + */ int lumiera_config_reset(const char* key); // * Find exact place of a setting. +/** + * + */ int lumiera_config_info (const char* key, const char** filename, unsigned* line); diff --git a/tests/20config.tests b/tests/20config.tests new file mode 100644 index 000000000..060334900 --- /dev/null +++ b/tests/20config.tests @@ -0,0 +1,43 @@ +TESTING "test configuration system" ./test-config + +TEST "initializing config system" init < + + 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. +*/ +#include "lib/safeclib.h" + +#include "backend/config.h" + +#include "tests/test.h" + +TESTS_BEGIN + +TEST ("init") +{ + lumiera_config_init ("./"); + printf ("initialized\n"); + lumiera_config_destroy (); + printf ("destroyed\n"); +} + +TESTS_END