Config UI: leave other_browser_cmd unset instead of configuring an empty value
[browser-switch] / config.c
index 727287f..8928a3d 100644 (file)
--- a/config.c
+++ b/config.c
@@ -21,6 +21,7 @@
  */
 
 #include <stdlib.h>
  */
 
 #include <stdlib.h>
+#include <stddef.h>
 #include <string.h>
 
 #include "configfile.h"
 #include <string.h>
 
 #include "configfile.h"
@@ -72,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) {
 /* 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)
                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;
        }
 
        cfg->flags = 0;
@@ -96,26 +99,26 @@ void swb_config_free(struct swb_config *cfg) {
 /* Load a value into the part of a struct swb_config indicated by name */
 static int swb_config_load_option(struct swb_config *cfg,
                                  char *name, char *value) {
 /* Load a value into the part of a struct swb_config indicated by name */
 static int swb_config_load_option(struct swb_config *cfg,
                                  char *name, char *value) {
-       int i;
-       struct swb_config_option opt;
+       struct swb_config_option *opt;
+       ptrdiff_t i;
        int retval = 0;
 
        int retval = 0;
 
-       for (i = 0; swb_config_options[i].name; ++i) {
-               opt = swb_config_options[i];
-               if (strcmp(name, opt.name))
+       for (opt = swb_config_options; opt->name; ++opt) {
+               if (strcmp(name, opt->name))
                        continue;
 
                        continue;
 
-               if (!(cfg->flags & opt.set_mask)) {
-                       switch (opt.type) {
+               if (!(cfg->flags & opt->set_mask)) {
+                       i = opt - swb_config_options;
+                       switch (opt->type) {
                          case SWB_CONFIG_OPT_STRING:
                          case SWB_CONFIG_OPT_STRING:
-                                 *(char **)cfg->entries[i] = value;
-                                 break;
+                               *(char **)cfg->entries[i] = value;
+                               break;
                          case SWB_CONFIG_OPT_INT:
                          case SWB_CONFIG_OPT_INT:
-                                 *(int *)cfg->entries[i] = atoi(value);
-                                 free(value);
-                                 break;
+                               *(int *)cfg->entries[i] = atoi(value);
+                               free(value);
+                               break;
                        }
                        }
-                       cfg->flags |= opt.set_mask;
+                       cfg->flags |= opt->set_mask;
                }
                retval = 1;
                break;
                }
                retval = 1;
                break;