1 |
commit: 273b48905e429d124ee79ea487f609a47df56e32 |
2 |
Author: José María Alonso <nimiux.gentoo.org> |
3 |
AuthorDate: Sun Feb 5 18:24:07 2012 +0000 |
4 |
Commit: José María Alonso <nimiux <AT> gentoo <DOT> org> |
5 |
CommitDate: Sun Feb 5 18:24:07 2012 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/conf-update.git;a=commit;h=273b4890 |
7 |
|
8 |
Code refactor read_config |
9 |
|
10 |
--- |
11 |
config.c | 56 +++++++++++++++++++++++++++++++------------------------- |
12 |
1 files changed, 31 insertions(+), 25 deletions(-) |
13 |
|
14 |
diff --git a/config.c b/config.c |
15 |
index 4637ed0..b59a949 100644 |
16 |
--- a/config.c |
17 |
+++ b/config.c |
18 |
@@ -1,9 +1,33 @@ |
19 |
#include "conf-update.h" |
20 |
|
21 |
+bool get_boolean(GKeyFile *conffile, const char *key, bool default_value) { |
22 |
+ GError *error = NULL; |
23 |
+ bool value, invalid_value, key_not_found; |
24 |
+ |
25 |
+ value = (bool) g_key_file_get_boolean(conffile, PROG_NAME, key, &error); |
26 |
+ invalid_value = (bool) g_error_matches(error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE); |
27 |
+ key_not_found = (bool) g_error_matches(error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND); |
28 |
+ g_clear_error(&error); |
29 |
+ if (invalid_value || key_not_found) { |
30 |
+ return default_value; |
31 |
+ } else { |
32 |
+ return value; |
33 |
+ } |
34 |
+} |
35 |
+ |
36 |
+char *get_string(GKeyFile *conffile, const char *key, char *default_value) { |
37 |
+ char * value; |
38 |
+ |
39 |
+ if (!(value = g_key_file_get_string(conffile, PROG_NAME, key, NULL))) { |
40 |
+ return default_value; |
41 |
+ } else { |
42 |
+ return value; |
43 |
+ } |
44 |
+} |
45 |
+ |
46 |
void read_config() { |
47 |
extern struct configuration config; |
48 |
GKeyFile *conffile; |
49 |
- GError *error = NULL; |
50 |
|
51 |
// set reasonable defaults |
52 |
config.check_actions = TRUE; |
53 |
@@ -21,30 +45,12 @@ void read_config() { |
54 |
fprintf(stderr, "!!! ERROR: Could not load config file %s\n", CONFIG_FILE); |
55 |
exit(EXIT_FAILURE); |
56 |
} else { |
57 |
- config.automerge_trivial = g_key_file_get_boolean(conffile, PROG_NAME, "autoreplace_trivial", &error); |
58 |
- 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)) { |
59 |
- config.automerge_trivial = TRUE; |
60 |
- g_clear_error(&error); |
61 |
- } |
62 |
- config.automerge_unmodified = g_key_file_get_boolean(conffile, PROG_NAME, "autoreplace_unmodified", &error); |
63 |
- 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)) { |
64 |
- config.automerge_unmodified = FALSE; |
65 |
- g_clear_error(&error); |
66 |
- } |
67 |
- config.check_actions = g_key_file_get_boolean(conffile, PROG_NAME, "confirm_actions", &error); |
68 |
- 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)) { |
69 |
- config.check_actions = TRUE; |
70 |
- g_clear_error(&error); |
71 |
- } |
72 |
- if (!(config.diff_tool = g_key_file_get_string(conffile, PROG_NAME, "diff_tool", NULL))) { |
73 |
- config.diff_tool = strdup("diff -u"); |
74 |
- } |
75 |
- if (!(config.pager = g_key_file_get_string(conffile, PROG_NAME, "pager", NULL))) { |
76 |
- config.pager = strdup(""); |
77 |
- } |
78 |
- if (!(config.merge_tool = g_key_file_get_string(conffile, PROG_NAME, "merge_tool", NULL))) { |
79 |
- config.merge_tool = strdup("sdiff -s -o"); |
80 |
- } |
81 |
+ config.automerge_trivial = get_boolean(conffile, "autoreplace_trivial", TRUE); |
82 |
+ config.automerge_unmodified = get_boolean(conffile, "autoreplace_unmodified", FALSE); |
83 |
+ config.check_actions = get_boolean(conffile, "confirm_actions", TRUE); |
84 |
+ config.diff_tool = get_string(conffile, "diff_tool", strdup("diff -u")); |
85 |
+ config.pager = get_string(conffile, "pager", strdup("")); |
86 |
+ config.merge_tool = get_string(conffile, "merge_tool", strdup("sdiff -s -o")); |
87 |
} |
88 |
g_key_file_free(conffile); |
89 |
} |