aboutsummaryrefslogtreecommitdiff
path: root/src/config.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.c')
-rw-r--r--src/config.c52
1 files changed, 31 insertions, 21 deletions
diff --git a/src/config.c b/src/config.c
index 740b18e..e4151eb 100644
--- a/src/config.c
+++ b/src/config.c
@@ -23,9 +23,32 @@ static void ftag_config_read(enum ftag_config_e key)
}
}
-void ftag_config_init(char *config_file)
+static int ftag_config_load_file(const char *config_file)
+{
+ if (!(config_file && file_exists(config_file)))
+ return 0;
+
+ uconfig = uconfig_new(config_file);
+ if (uconfig == NULL) {
+ fprintf(stderr,
+ "%s: error during configuration file parsing\n",
+ __func__);
+ exit(EXIT_FAILURE);
+ }
+ for (enum ftag_config_e key = FC_FIRST; key < FC_COUNT; key++) {
+ ftag_config_read(key);
+ }
+ return 1;
+}
+
+void ftag_config_init(const char *config_file)
{
const char *env_home = getenv("HOME");
+ char default_config_file[512];
+
+ /* TODO: prioritize XDG_CONFIG_HOME */
+ strbuild(default_config_file,
+ "%s/.config/ftag/ftag.conf", env_home);
/* Keys */
ftag_config_keys[FC_DATABASE_PATH] = "database_path";
@@ -41,27 +64,14 @@ void ftag_config_init(char *config_file)
strcpy(ftag_config_values[FC_REMOTE_HOST], "localhost");
strcpy(ftag_config_values[FC_REMOTE_ROOT], "ftag");
- if (config_file == NULL) {
- /* Use default config file */
- /* TODO: prioritize XDG_CONFIG_HOME */
- config_file = malloc(CONFIG_STR_SIZE);
- strbuild_with_size(config_file, CONFIG_STR_SIZE,
- "%s/.config/ftag/ftag.conf", env_home);
- }
-
uconfig = NULL;
- if (file_exists(config_file)) {
- uconfig = uconfig_new(config_file);
- if (uconfig == NULL) {
- fprintf(stderr,
- "%s: error during configuration file parsing\n",
- __func__);
- exit(EXIT_FAILURE);
- }
- for (enum ftag_config_e key = FC_FIRST; key < FC_COUNT; key++) {
- ftag_config_read(key);
- }
- }
+ int config_loaded = 0;
+
+ config_loaded = ftag_config_load_file(config_file);
+ if (config_loaded)
+ return;
+
+ ftag_config_load_file(default_config_file);
}
const char *ftag_config_get(enum ftag_config_e key)