[PATCH 2/2] cli: define config getters and setters using a macro

Jani Nikula jani at nikula.org
Sun Aug 18 08:20:05 PDT 2013


There's plenty of duplicated code in defining the functions for config
get/set. Add macros to define the functions.

---

This might be a bit too tricky for some people's tastes... let's see!
;)
---
 notmuch-config.c |  141 ++++++++++++++----------------------------------------
 1 file changed, 37 insertions(+), 104 deletions(-)

diff --git a/notmuch-config.c b/notmuch-config.c
index 305d213..fcee0fc 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -520,6 +520,18 @@ _config_set (notmuch_config_t *config, char **field,
     *field = NULL;
 }
 
+#define DEFINE_CONFIG_GET(group, key) \
+    const char *							\
+    notmuch_config_get_ ## group ## _ ## key (notmuch_config_t *config) { \
+	return _config_get (config, &config->group ## _ ## key, #group, #key); \
+    }
+
+#define DEFINE_CONFIG_SET(group, key)				\
+    void								\
+    notmuch_config_set_ ## group ## _ ## key (notmuch_config_t *config, const char *value) { \
+	_config_set (config, &config->group ## _ ## key, #group, #key, value); \
+    }
+
 static const char **
 _config_get_list (notmuch_config_t *config,
 		  const char *section, const char *key,
@@ -562,112 +574,33 @@ _config_set_list (notmuch_config_t *config,
     *config_var = NULL;
 }
 
-const char *
-notmuch_config_get_database_path (notmuch_config_t *config)
-{
-    return _config_get (config, &config->database_path, "database", "path");
-}
-
-void
-notmuch_config_set_database_path (notmuch_config_t *config,
-				  const char *database_path)
-{
-    _config_set (config, &config->database_path, "database", "path", database_path);
-}
-
-const char *
-notmuch_config_get_user_name (notmuch_config_t *config)
-{
-    return _config_get (config, &config->user_name, "user", "name");
-}
-
-void
-notmuch_config_set_user_name (notmuch_config_t *config,
-			      const char *user_name)
-{
-    _config_set (config, &config->user_name, "user", "name", user_name);
-}
-
-const char *
-notmuch_config_get_user_primary_email (notmuch_config_t *config)
-{
-    return _config_get (config, &config->user_primary_email, "user", "primary_email");
-}
-
-void
-notmuch_config_set_user_primary_email (notmuch_config_t *config,
-				       const char *primary_email)
-{
-    _config_set (config, &config->user_primary_email, "user", "primary_email", primary_email);
-}
-
-const char **
-notmuch_config_get_user_other_email (notmuch_config_t *config,   size_t *length)
-{
-    return _config_get_list (config, "user", "other_email",
-			     &(config->user_other_email),
-			     &(config->user_other_email_length), length);
-}
-
-const char **
-notmuch_config_get_new_tags (notmuch_config_t *config,   size_t *length)
-{
-    return _config_get_list (config, "new", "tags",
-			     &(config->new_tags),
-			     &(config->new_tags_length), length);
-}
-
-const char **
-notmuch_config_get_new_ignore (notmuch_config_t *config, size_t *length)
-{
-    return _config_get_list (config, "new", "ignore",
-			     &(config->new_ignore),
-			     &(config->new_ignore_length), length);
-}
-
-void
-notmuch_config_set_user_other_email (notmuch_config_t *config,
-				     const char *list[],
-				     size_t length)
-{
-    _config_set_list (config, "user", "other_email", list, length,
-		     &(config->user_other_email));
-}
-
-void
-notmuch_config_set_new_tags (notmuch_config_t *config,
-				     const char *list[],
-				     size_t length)
-{
-    _config_set_list (config, "new", "tags", list, length,
-		     &(config->new_tags));
-}
-
-void
-notmuch_config_set_new_ignore (notmuch_config_t *config,
-			       const char *list[],
-			       size_t length)
-{
-    _config_set_list (config, "new", "ignore", list, length,
-		     &(config->new_ignore));
-}
+#define DEFINE_CONFIG_GET_LIST(group, key)				\
+    const char **							\
+    notmuch_config_get_ ## group ## _ ## key (notmuch_config_t *config, size_t *length) { \
+	return _config_get_list (config, #group, #key, &config->group ## _ ## key, &config->group ## _ ## key ## _ ## length, length); \
+    }
 
-const char **
-notmuch_config_get_search_exclude_tags (notmuch_config_t *config, size_t *length)
-{
-    return _config_get_list (config, "search", "exclude_tags",
-			     &(config->search_exclude_tags),
-			     &(config->search_exclude_tags_length), length);
-}
+#define DEFINE_CONFIG_SET_LIST(group, key)				\
+    void								\
+    notmuch_config_set_ ## group ## _ ## key (notmuch_config_t *config, const char *list[], size_t length) { \
+	_config_set_list (config, #group, #key, list, length, &config->group ## _ ## key); \
+    }
 
-void
-notmuch_config_set_search_exclude_tags (notmuch_config_t *config,
-				      const char *list[],
-				      size_t length)
-{
-    _config_set_list (config, "search", "exclude_tags", list, length,
-		      &(config->search_exclude_tags));
-}
+DEFINE_CONFIG_GET(database, path);
+DEFINE_CONFIG_SET(database, path);
+DEFINE_CONFIG_GET(user, name);
+DEFINE_CONFIG_SET(user, name);
+DEFINE_CONFIG_GET(user, primary_email);
+DEFINE_CONFIG_SET(user, primary_email);
+
+DEFINE_CONFIG_GET_LIST(user, other_email);
+DEFINE_CONFIG_SET_LIST(user, other_email);
+DEFINE_CONFIG_GET_LIST(new, tags);
+DEFINE_CONFIG_SET_LIST(new, tags);
+DEFINE_CONFIG_GET_LIST(new, ignore);
+DEFINE_CONFIG_SET_LIST(new, ignore);
+DEFINE_CONFIG_GET_LIST(search, exclude_tags);
+DEFINE_CONFIG_SET_LIST(search, exclude_tags);
 
 /* Given a configuration item of the form <group>.<key> return the
  * component group and key. If any error occurs, print a message on
-- 
1.7.10.4



More information about the notmuch mailing list