diff options
author | José María Alonso <nimiux.gentoo.org> | 2012-02-05 19:24:07 +0100 |
---|---|---|
committer | José María Alonso <nimiux.gentoo.org> | 2012-02-05 19:24:07 +0100 |
commit | 273b48905e429d124ee79ea487f609a47df56e32 (patch) | |
tree | e4f4e1e25a970bc798f41de097923734c634a698 | |
parent | Fixed some memory leaks. Thanks to Vincent Huisman. (diff) | |
download | conf-update-273b48905e429d124ee79ea487f609a47df56e32.tar.gz conf-update-273b48905e429d124ee79ea487f609a47df56e32.tar.bz2 conf-update-273b48905e429d124ee79ea487f609a47df56e32.zip |
Code refactor read_config
-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); } |