diff options
author | Lukas Fleischer <calcurse@cryptocrack.de> | 2012-02-16 09:29:52 +0100 |
---|---|---|
committer | Lukas Fleischer <calcurse@cryptocrack.de> | 2012-02-16 10:39:14 +0100 |
commit | 4c5d6fe612f8e80dbc6279b1b1cdf6eb568dd4da (patch) | |
tree | 8ac3c547bfc715986f3e8db6ecfd8c778f9a5835 /src | |
parent | f73f50055987a5ba644ab9721741ef1b9861b079 (diff) | |
download | calcurse-4c5d6fe612f8e80dbc6279b1b1cdf6eb568dd4da.zip |
src/config.c: Make config file reading more flexible
This adds one level of abstraction to config_load() by splitting out the
actual reading routine and the variable setter into two separate
functions. config_file_walk() can be used to read the configuration
file, strip comments and pass every key/value pair to a callback.
config_load_cb() is the new callback used in config_load().
Rationale: It makes sense to reuse the key/value parser to allow for a
much saner config_save() routine that changes single values only instead
of rewriting (and overwriting) the whole configuration file every time.
Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
Diffstat (limited to 'src')
-rw-r--r-- | src/config.c | 38 |
1 files changed, 27 insertions, 11 deletions
diff --git a/src/config.c b/src/config.c index 8af8e68..14294cc 100644 --- a/src/config.c +++ b/src/config.c @@ -38,6 +38,8 @@ #include "calcurse.h" +typedef int (*config_fn_walk_cb_t) (const char *, const char *, void *); + static int config_parse_bool (unsigned *dest, const char *val) { @@ -225,16 +227,14 @@ config_set_conf (const char *key, const char *value) return -1; } -/* Load the user configuration. */ -void -config_load (void) +static void +config_file_walk (config_fn_walk_cb_t fn_cb, void *data) { FILE *data_file; char *mesg_line1 = _("Failed to open config file"); char *mesg_line2 = _("Press [ENTER] to continue"); char buf[BUFSIZ], e_conf[BUFSIZ]; char *key, *value; - int result; data_file = fopen (path_conf, "r"); if (data_file == NULL) @@ -272,18 +272,34 @@ config_load (void) value = e_conf; } - result = config_set_conf (key, value); - if (result < 0) - EXIT (_("configuration variable unknown: \"%s\""), key); - /* NOTREACHED */ - else if (result == 0) - EXIT (_("wrong configuration variable format for \"%s\""), key); - /* NOTREACHED */ + fn_cb (key, value, data); } file_close (data_file, __FILE_POS__); pthread_mutex_unlock (&nbar.mutex); } +static int +config_load_cb (const char *key, const char *value, void *dummy) +{ + int result = config_set_conf (key, value); + + if (result < 0) + EXIT (_("configuration variable unknown: \"%s\""), key); + /* NOTREACHED */ + else if (result == 0) + EXIT (_("wrong configuration variable format for \"%s\""), key); + /* NOTREACHED */ + + return 1; +} + +/* Load the user configuration. */ +void +config_load (void) +{ + config_file_walk (config_load_cb, NULL); +} + /* * Return a string defining the color theme in the form: * foreground color 'on' background color |