diff --git a/src/config_loader.c b/src/config_loader.c index 4f4f943b..f3ccb6d4 100644 --- a/src/config_loader.c +++ b/src/config_loader.c @@ -56,14 +56,6 @@ bool config_loader_set_opt(char *section, char *opt, char *value) } options = cl.available_opts; - /* replace all '_' used in config options with '-' used in flags */ - flag = strdup_or_die(opt); - for (unsigned int i = 0; flag[i]; i++) { - if (flag[i] == '_') { - flag[i] = '-'; - } - } - /* options within a section are by default considered to be global */ if (section) { lsection = str_tolower(section); @@ -75,6 +67,9 @@ bool config_loader_set_opt(char *section, char *opt, char *value) } } + /* replace all '_' used in config options with '-' used in flags */ + flag = str_subchar(opt, '_', '-'); + /* search the option from within the available options */ while (options->name != NULL) { if (strcmp(flag, options->name) == 0) { diff --git a/src/lib/strings.c b/src/lib/strings.c index 59bad77b..8b37293c 100644 --- a/src/lib/strings.c +++ b/src/lib/strings.c @@ -179,3 +179,15 @@ bool strtobool(const char *str) free_string(&str_lower); return ret; } + +char *str_subchar(const char *str, char c1, char c2) +{ + char *new = strdup_or_die(str); + for (unsigned int i = 0; new[i]; i++) { + if (new[i] == c1) { + new[i] = c2; + } + } + + return new; +} diff --git a/src/lib/strings.h b/src/lib/strings.h index 95d2c0d0..6e20c9fd 100644 --- a/src/lib/strings.h +++ b/src/lib/strings.h @@ -88,6 +88,15 @@ char *str_tolower(const char *str); */ bool strtobool(const char *str); +/** + * Returns a copy of str with all chars c1 replaced with c2. + * + * @param str The original string. + * @param c1 Character to look for. + * @param c2 The character to replace c1 to. + */ +char *str_subchar(const char *str, char c1, char c2); + #ifdef __cplusplus } #endif