Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id CE12C429E26 for ; Sat, 10 Dec 2011 10:23:01 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -0.7 X-Spam-Level: X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id XRRe73si9Nkj for ; Sat, 10 Dec 2011 10:23:01 -0800 (PST) Received: from dmz-mailsec-scanner-1.mit.edu (DMZ-MAILSEC-SCANNER-1.MIT.EDU [18.9.25.12]) by olra.theworths.org (Postfix) with ESMTP id 08111431FB6 for ; Sat, 10 Dec 2011 10:23:00 -0800 (PST) X-AuditID: 1209190c-b7f806d0000008d6-85-4ee3a384bb0b Received: from mailhub-auth-1.mit.edu ( [18.9.21.35]) by dmz-mailsec-scanner-1.mit.edu (Symantec Messaging Gateway) with SMTP id 2C.51.02262.483A3EE4; Sat, 10 Dec 2011 13:23:00 -0500 (EST) Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103]) by mailhub-auth-1.mit.edu (8.13.8/8.9.2) with ESMTP id pBAIMwsw023402; Sat, 10 Dec 2011 13:22:59 -0500 Received: from awakening.csail.mit.edu (awakening.csail.mit.edu [18.26.4.91]) (authenticated bits=0) (User authenticated as amdragon@ATHENA.MIT.EDU) by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id pBAIMtaV028096 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); Sat, 10 Dec 2011 13:22:56 -0500 (EST) Received: from amthrax by awakening.csail.mit.edu with local (Exim 4.77) (envelope-from ) id 1RZRbA-0002J9-4m; Sat, 10 Dec 2011 13:24:36 -0500 Date: Sat, 10 Dec 2011 13:24:36 -0500 From: Austin Clements To: David Bremner Subject: Re: [RFC PATCH] cli: factor out config handling code to get/set lists. Message-ID: <20111210182436.GE2760@mit.edu> References: <1323539438-15869-1-git-send-email-david@tethera.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1323539438-15869-1-git-send-email-david@tethera.net> User-Agent: Mutt/1.5.21 (2010-09-15) X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFupjleLIzCtJLcpLzFFi42IR4hRV1m1Z/NjPYPVPBYuNy34yW9xo7Wa0 uH5zJrMDs8evtrnMHs9W3WL22HLoPXMAcxSXTUpqTmZZapG+XQJXxsn1M5kLThpUbDm1hL2B 8YRyFyMnh4SAicSc29fZIWwxiQv31rN1MXJxCAnsY5RoujyRGcLZwCixcsdHFgjnJJPE5vtb 2CGcJYwSP968YwHpZxFQlZj28DUTiM0moCGxbf9yRhBbBCh+ddtkNhCbWcBeYtHsSWD7hAUC JXbe3ANWzyugLTHh5mpWEFtIwFGi6dA+Zoi4oMTJmU9YIHq1JG78ewlUzwFkS0ss/8cBEuYU cJI403UHbJWogIrElJPb2CYwCs1C0j0LSfcshO4FjMyrGGVTcqt0cxMzc4pTk3WLkxPz8lKL dA31cjNL9FJTSjcxgoNdkmcH45uDSocYBTgYlXh4Pb0e+QmxJpYVV+YeYpTkYFIS5f246LGf EF9SfkplRmJxRnxRaU5q8SFGCQ5mJRHe051AOd6UxMqq1KJ8mJQ0B4uSOG/1rod+QgLpiSWp 2ampBalFMFkZDg4lCd4PIEMFi1LTUyvSMnNKENJMHJwgw3mAhj8DqeEtLkjMLc5Mh8ifYlSU Euc9CZIQAElklObB9cKS0StGcaBXhHlfgFTxABMZXPcroMFMQIO/ZD8AGVySiJCSamAsuMDU E6D988UZOd4fpZozQzJjlJZ+U5933Cp3q8We0Ks/uP43512Yrb7ymeJup+Ko6Oys+b+0BFue nD4ZaKE5dfa3Bk6zs/rJWvX9uh8zs/l9TmSal/zJVNsRK5TNFJJbfqufJ5Ah5SS79NEV0cue WInI2q/Uun9Z7vnTf9tun96y8uCmyUeVWIozEg21mIuKEwHSAzUjIQMAAA== Cc: notmuch@notmuchmail.org, David Bremner X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 10 Dec 2011 18:23:02 -0000 Deduplicating this code seems like a great idea, but I don't think macros are the way to do it; especially not one that expands to an important top-level construct like a function definition. What about something like 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, length); } where config->user_other_email is a, say, struct _notmuch_config_list that combines together the cached string list and length? _config_get_list would look a lot like what you have now, but outlist would be a struct _notmuch_config_list* instead of a const char*** and it would always set *length (in addition to setting it in the cached value if necessary). Set could be similarly simple void notmuch_config_set_user_other_email (notmuch_config_t *config, const char *other_email[], size_t length) { _config_set_list (config, "user", "other_email", &config->user_other_email, other_email, length); } Quoth David Bremner on Dec 10 at 1:50 pm: > From: David Bremner > > The code is already duplicated once, and I want to add a third > configuration item that is also a list. > --- > > Mainly I am curious if people think using macros to declare these > "getters" and "setters" makes the code less maintainable. > > notmuch-config.c | 112 +++++++++++++++++++---------------------------------- > 1 files changed, 40 insertions(+), 72 deletions(-) > > diff --git a/notmuch-config.c b/notmuch-config.c > index 1a7ed58..33778a7 100644 > --- a/notmuch-config.c > +++ b/notmuch-config.c > @@ -520,93 +520,61 @@ notmuch_config_set_user_primary_email (notmuch_config_t *config, > config->user_primary_email = NULL; > } > > -const char ** > -notmuch_config_get_user_other_email (notmuch_config_t *config, > - size_t *length) > +static void > +_config_get_list (notmuch_config_t *config, > + const char *section, const char *key, > + const char ***outlist, size_t *length) > { > - char **emails; > - size_t emails_length; > + char **inlist; > unsigned int i; > > - if (config->user_other_email == NULL) { > - emails = g_key_file_get_string_list (config->key_file, > - "user", "other_email", > - &emails_length, NULL); > - if (emails) { > - config->user_other_email = talloc_size (config, > - sizeof (char *) * > - (emails_length + 1)); > - for (i = 0; i < emails_length; i++) > - config->user_other_email[i] = talloc_strdup (config->user_other_email, > - emails[i]); > - config->user_other_email[i] = NULL; > - > - g_strfreev (emails); > - > - config->user_other_email_length = emails_length; > + if (*outlist == NULL) { > + inlist = g_key_file_get_string_list (config->key_file, > + section, key, > + length, NULL); > + if (inlist) { > + *outlist = talloc_size (config, sizeof (char *) * > + (*length + 1)); > + for (i = 0; i < *length; i++) > + (*outlist)[i] = talloc_strdup (*outlist, inlist[i]); > + (*outlist)[i] = NULL; > + > + g_strfreev (inlist); > + > } > } > > - *length = config->user_other_email_length; > - return config->user_other_email; > } > > -void > -notmuch_config_set_user_other_email (notmuch_config_t *config, > - const char *other_email[], > - size_t length) > -{ > - g_key_file_set_string_list (config->key_file, > - "user", "other_email", > - other_email, length); > - > - talloc_free (config->user_other_email); > - config->user_other_email = NULL; > +#define _GET_LIST(list, group, name) \ > +const char ** \ > +notmuch_config_get_##list (notmuch_config_t *config, size_t *length) \ > +{ \ > + _config_get_list (config, group, name, &(config->list), \ > + &(config->list##_length)); \ > + *length = config->list##_length; \ > + return config->list; \ > } > > -const char ** > -notmuch_config_get_new_tags (notmuch_config_t *config, > - size_t *length) > -{ > - char **tags; > - size_t tags_length; > - unsigned int i; > +_GET_LIST (user_other_email, "user", "other_email"); > +_GET_LIST (new_tags, "new", "tags"); > > - if (config->new_tags == NULL) { > - tags = g_key_file_get_string_list (config->key_file, > - "new", "tags", > - &tags_length, NULL); > - if (tags) { > - config->new_tags = talloc_size (config, > - sizeof (char *) * > - (tags_length + 1)); > - for (i = 0; i < tags_length; i++) > - config->new_tags[i] = talloc_strdup (config->new_tags, > - tags[i]); > - config->new_tags[i] = NULL; > - > - g_strfreev (tags); > - > - config->new_tags_length = tags_length; > - } > - } > +#undef _GET_LIST > > - *length = config->new_tags_length; > - return config->new_tags; > +#define _SET_LIST(list, group, name) \ > +void \ > +notmuch_config_set_##list (notmuch_config_t *config, const char *list[], \ > + size_t length) \ > +{ \ > + g_key_file_set_string_list (config->key_file, group, name, list, length); \ > + talloc_free (config->list); \ > + config->list = NULL; \ > } > > -void > -notmuch_config_set_new_tags (notmuch_config_t *config, > - const char *new_tags[], > - size_t length) > -{ > - g_key_file_set_string_list (config->key_file, > - "new", "tags", > - new_tags, length); > +_SET_LIST(user_other_email, "user", "other_email"); > +_SET_LIST(new_tags, "new", "tags"); > > - talloc_free (config->new_tags); > - config->new_tags = NULL; > -} > +#undef _SET_LIST > > /* Given a configuration item of the form . return the > * component group and key. If any error occurs, print a message on