From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1Ru6pm-0003Pw-Ua for garchives@archives.gentoo.org; Sun, 05 Feb 2012 18:29:08 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id DE2F3E084F; Sun, 5 Feb 2012 18:28:48 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id A0148E084F for ; Sun, 5 Feb 2012 18:28:48 +0000 (UTC) Received: from pelican.gentoo.org (unknown [66.219.59.40]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 0B7951B4010 for ; Sun, 5 Feb 2012 18:28:48 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by pelican.gentoo.org (Postfix) with ESMTP id 2AA8F80043 for ; Sun, 5 Feb 2012 18:28:47 +0000 (UTC) From: "José María Alonso" To: gentoo-commits@lists.gentoo.org Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "José María Alonso" Message-ID: <273b48905e429d124ee79ea487f609a47df56e32.nimiux@gentoo> Subject: [gentoo-commits] proj/conf-update:master commit in: / X-VCS-Repository: proj/conf-update X-VCS-Files: config.c X-VCS-Directories: / X-VCS-Committer: nimiux X-VCS-Committer-Name: José María Alonso X-VCS-Revision: 273b48905e429d124ee79ea487f609a47df56e32 Date: Sun, 5 Feb 2012 18:28:47 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: 03f7ccd6-588b-4c25-8c75-e6ec0f30604c X-Archives-Hash: c10655a7282b519ed5ac7d5503e1e6b1 commit: 273b48905e429d124ee79ea487f609a47df56e32 Author: Jos=C3=A9 Mar=C3=ADa Alonso AuthorDate: Sun Feb 5 18:24:07 2012 +0000 Commit: Jos=C3=A9 Mar=C3=ADa Alonso gentoo org> CommitDate: Sun Feb 5 18:24:07 2012 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/conf-update.g= it;a=3Dcommit;h=3D273b4890 Code refactor read_config --- config.c | 56 +++++++++++++++++++++++++++++++------------------------- 1 files changed, 31 insertions(+), 25 deletions(-) diff --git a/config.c b/config.c index 4637ed0..b59a949 100644 --- a/config.c +++ b/config.c @@ -1,9 +1,33 @@ #include "conf-update.h" =20 +bool get_boolean(GKeyFile *conffile, const char *key, bool default_value= ) { + GError *error =3D NULL; + bool value, invalid_value, key_not_found; + + value =3D (bool) g_key_file_get_boolean(conffile, PROG_NAME, key, &erro= r); + invalid_value =3D (bool) g_error_matches(error, G_KEY_FILE_ERROR, G_KEY= _FILE_ERROR_INVALID_VALUE); + key_not_found =3D (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_valu= e) { + char * value; + + if (!(value =3D 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 =3D NULL; =09 // set reasonable defaults config.check_actions =3D TRUE; @@ -21,30 +45,12 @@ void read_config() { fprintf(stderr, "!!! ERROR: Could not load config file %s\n", CONFIG_F= ILE); exit(EXIT_FAILURE); } else { - config.automerge_trivial =3D g_key_file_get_boolean(conffile, PROG_NAM= E, "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_N= OT_FOUND)) { - config.automerge_trivial =3D TRUE; - g_clear_error(&error); - } - config.automerge_unmodified =3D 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_N= OT_FOUND)) { - config.automerge_unmodified =3D FALSE; - g_clear_error(&error); - } - config.check_actions =3D 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_N= OT_FOUND)) { - config.check_actions =3D TRUE; - g_clear_error(&error); - } - if (!(config.diff_tool =3D g_key_file_get_string(conffile, PROG_NAME, = "diff_tool", NULL))) { - config.diff_tool =3D strdup("diff -u"); - } - if (!(config.pager =3D g_key_file_get_string(conffile, PROG_NAME, "pag= er", NULL))) { - config.pager =3D strdup(""); - } - if (!(config.merge_tool =3D g_key_file_get_string(conffile, PROG_NAME,= "merge_tool", NULL))) { - config.merge_tool =3D strdup("sdiff -s -o"); - } + config.automerge_trivial =3D get_boolean(conffile, "autoreplace_trivia= l", TRUE); + config.automerge_unmodified =3D get_boolean(conffile, "autoreplace_unm= odified", FALSE); + config.check_actions =3D get_boolean(conffile, "confirm_actions", TRUE= ); + config.diff_tool =3D get_string(conffile, "diff_tool", strdup("diff -u= ")); + config.pager =3D get_string(conffile, "pager", strdup("")); + config.merge_tool =3D get_string(conffile, "merge_tool", strdup("sdiff= -s -o")); } g_key_file_free(conffile); }