From 4c5d6fe612f8e80dbc6279b1b1cdf6eb568dd4da Mon Sep 17 00:00:00 2001
From: Lukas Fleischer <calcurse@cryptocrack.de>
Date: Thu, 16 Feb 2012 09:29:52 +0100
Subject: src/config.c: Make config file reading more flexible

This adds one level of abstraction to config_load() by splitting out the
actual reading routine and the variable setter into two separate
functions. config_file_walk() can be used to read the configuration
file, strip comments and pass every key/value pair to a callback.
config_load_cb() is the new callback used in config_load().

Rationale: It makes sense to reuse the key/value parser to allow for a
much saner config_save() routine that changes single values only instead
of rewriting (and overwriting) the whole configuration file every time.

Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
---
 src/config.c | 38 +++++++++++++++++++++++++++-----------
 1 file changed, 27 insertions(+), 11 deletions(-)

(limited to 'src')

diff --git a/src/config.c b/src/config.c
index 8af8e68..14294cc 100644
--- a/src/config.c
+++ b/src/config.c
@@ -38,6 +38,8 @@
 
 #include "calcurse.h"
 
+typedef int (*config_fn_walk_cb_t) (const char *, const char *, void *);
+
 static int
 config_parse_bool (unsigned *dest, const char *val)
 {
@@ -225,16 +227,14 @@ config_set_conf (const char *key, const char *value)
   return -1;
 }
 
-/* Load the user configuration. */
-void
-config_load (void)
+static void
+config_file_walk (config_fn_walk_cb_t fn_cb, void *data)
 {
   FILE *data_file;
   char *mesg_line1 = _("Failed to open config file");
   char *mesg_line2 = _("Press [ENTER] to continue");
   char buf[BUFSIZ], e_conf[BUFSIZ];
   char *key, *value;
-  int result;
 
   data_file = fopen (path_conf, "r");
   if (data_file == NULL)
@@ -272,18 +272,34 @@ config_load (void)
           value = e_conf;
         }
 
-      result = config_set_conf (key, value);
-      if (result < 0)
-        EXIT (_("configuration variable unknown: \"%s\""), key);
-        /* NOTREACHED */
-      else if (result == 0)
-        EXIT (_("wrong configuration variable format for \"%s\""), key);
-        /* NOTREACHED */
+      fn_cb (key, value, data);
     }
   file_close (data_file, __FILE_POS__);
   pthread_mutex_unlock (&nbar.mutex);
 }
 
+static int
+config_load_cb (const char *key, const char *value, void *dummy)
+{
+  int result = config_set_conf (key, value);
+
+  if (result < 0)
+    EXIT (_("configuration variable unknown: \"%s\""), key);
+    /* NOTREACHED */
+  else if (result == 0)
+    EXIT (_("wrong configuration variable format for \"%s\""), key);
+    /* NOTREACHED */
+
+  return 1;
+}
+
+/* Load the user configuration. */
+void
+config_load (void)
+{
+  config_file_walk (config_load_cb, NULL);
+}
+
 /*
  * Return a string defining the color theme in the form:
  *       foreground color 'on' background color
-- 
cgit v1.2.3-70-g09d2