string: Add string substitution function

And used it in config_loader

Signed-off-by: Otavio Pontes <otavio.pontes@intel.com>
This commit is contained in:
Otavio Pontes
2019-07-17 14:16:56 -07:00
committed by Castulo J. Martinez
parent ca0a8b2a11
commit 0c3cc3fb66
3 changed files with 24 additions and 8 deletions
+3 -8
View File
@@ -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) {
+12
View File
@@ -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;
}
+9
View File
@@ -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