From: Steven Luo Date: Sun, 30 May 2010 05:20:24 +0000 (-0700) Subject: Get rid of boilerplate code in swb_config_free() X-Git-Tag: v3.3b1~8 X-Git-Url: http://git.maemo.org/git/?p=browser-switch;a=commitdiff_plain;h=8e8375e7af5af21d1741dce1fc5ebc39a2aa270b Get rid of boilerplate code in swb_config_free() We can loop over swb_config_options[] instead of identifying every string config option explicitly here. --- diff --git a/config.c b/config.c index 289374b..8928a3d 100644 --- a/config.c +++ b/config.c @@ -73,22 +73,24 @@ void swb_config_init(struct swb_config *cfg) { /* Free all heap memory used in an swb_config struct This MUST NOT be done if any of the strings are being used elsewhere! */ void swb_config_free(struct swb_config *cfg) { + int i; + if (!cfg) return; if (!(cfg->flags & SWB_CONFIG_INITIALIZED)) return; - if (cfg->flags & SWB_CONFIG_DEFAULT_BROWSER_SET) { - free(cfg->default_browser); - cfg->default_browser = NULL; - } - if (cfg->flags & SWB_CONFIG_OTHER_BROWSER_CMD_SET) { - free(cfg->other_browser_cmd); - cfg->other_browser_cmd = NULL; - } - if (cfg->flags & SWB_CONFIG_LOGGING_SET) { - free(cfg->logging); - cfg->logging = NULL; + for (i = 0; swb_config_options[i].name; ++i) { + if (cfg->flags & swb_config_options[i].set_mask) { + switch (swb_config_options[i].type) { + case SWB_CONFIG_OPT_STRING: + free(*(char **)cfg->entries[i]); + *(char **)cfg->entries[i] = NULL; + break; + default: + break; + } + } } cfg->flags = 0;