diff options
-rw-r--r-- | config.c | 56 |
1 files changed, 31 insertions, 25 deletions
@@ -1,9 +1,33 @@ #include "conf-update.h" +bool get_boolean(GKeyFile *conffile, const char *key, bool default_value) { + GError *error = NULL; + bool value, invalid_value, key_not_found; + + value = (bool) g_key_file_get_boolean(conffile, PROG_NAME, key, &error); + invalid_value = (bool) g_error_matches(error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE); + key_not_found = (bool) g_error_matches(error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND); + g_clear_error(&error); + if (invalid_value || key_not_found) { + return default_value; + } else { + return value; + } +} + +char *get_string(GKeyFile *conffile, const char *key, char *default_value) { + char * value; + + if (!(value = g_key_file_get_string(conffile, PROG_NAME, key, NULL))) { + return default_value; + } else { + return value; + } +} + void read_config() { extern struct configuration config; GKeyFile *conffile; - GError *error = NULL; // set reasonable defaults config.check_actions = TRUE; @@ -21,30 +45,12 @@ void read_config() { fprintf(stderr, "!!! ERROR: Could not load config file %s\n", CONFIG_FILE); exit(EXIT_FAILURE); } else { - config.automerge_trivial = g_key_file_get_boolean(conffile, PROG_NAME, "autoreplace_trivial", &error); - if (g_error_matches(error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE) || g_error_matches(error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND)) { - config.automerge_trivial = TRUE; - g_clear_error(&error); - } - config.automerge_unmodified = g_key_file_get_boolean(conffile, PROG_NAME, "autoreplace_unmodified", &error); - if (g_error_matches(error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE) || g_error_matches(error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND)) { - config.automerge_unmodified = FALSE; - g_clear_error(&error); - } - config.check_actions = g_key_file_get_boolean(conffile, PROG_NAME, "confirm_actions", &error); - if (g_error_matches(error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE) || g_error_matches(error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND)) { - config.check_actions = TRUE; - g_clear_error(&error); - } - if (!(config.diff_tool = g_key_file_get_string(conffile, PROG_NAME, "diff_tool", NULL))) { - config.diff_tool = strdup("diff -u"); - } - if (!(config.pager = g_key_file_get_string(conffile, PROG_NAME, "pager", NULL))) { - config.pager = strdup(""); - } - if (!(config.merge_tool = g_key_file_get_string(conffile, PROG_NAME, "merge_tool", NULL))) { - config.merge_tool = strdup("sdiff -s -o"); - } + config.automerge_trivial = get_boolean(conffile, "autoreplace_trivial", TRUE); + config.automerge_unmodified = get_boolean(conffile, "autoreplace_unmodified", FALSE); + config.check_actions = get_boolean(conffile, "confirm_actions", TRUE); + config.diff_tool = get_string(conffile, "diff_tool", strdup("diff -u")); + config.pager = get_string(conffile, "pager", strdup("")); + config.merge_tool = get_string(conffile, "merge_tool", strdup("sdiff -s -o")); } g_key_file_free(conffile); } |