diff options
| author | Daniel Eklöf <daniel@ekloef.se> | 2021-02-13 10:42:21 +0000 |
|---|---|---|
| committer | Daniel Eklöf <daniel@ekloef.se> | 2021-02-13 10:42:21 +0000 |
| commit | 94266b99d7a804008912f2354f8d8e7ed55d60a4 (patch) | |
| tree | 52ad8793b298426d01fb6f66ba8df1a288418290 | |
| parent | url-mode: do case insensitive comparison when matching key sequence (diff) | |
config: refactor: add str_to_wchars(), use it when parsing word-delimiters
| -rw-r--r-- | config.c | 32 |
1 files changed, 24 insertions, 8 deletions
@@ -398,6 +398,25 @@ str_to_double(const char *s, double *res) } static bool +str_to_wchars(const char *s, wchar_t **res, struct config *conf, + const char *path, int lineno, + const char *section, const char *key) +{ + *res = NULL; + + size_t chars = mbstowcs(NULL, s, 0); + if (chars == (size_t)-1) { + LOG_AND_NOTIFY_ERR("%s:%d: [%s]: %s: invalid string: %s", + path, lineno, section, key, s); + return false; + } + + *res = xmalloc((chars + 1) * sizeof(wchar_t)); + mbstowcs(*res, s, chars + 1); + return true; +} + +static bool str_to_color(const char *s, uint32_t *color, bool allow_alpha, struct config *conf, const char *path, int lineno, const char *section, const char *key) @@ -712,18 +731,15 @@ parse_section_main(const char *key, const char *value, struct config *conf, } else if (strcmp(key, "word-delimiters") == 0) { - size_t chars = mbstowcs(NULL, value, 0); - if (chars == (size_t)-1) { - LOG_AND_NOTIFY_ERR( - "%s:%d: [default]: word-delimiters: invalid string: %s", - path, lineno, value); + wchar_t *word_delimiters; + if (!str_to_wchars(value, &word_delimiters, conf, path, lineno, + "default", "word-delimiters")) + { return false; } - free(conf->word_delimiters); + conf->word_delimiters = word_delimiters; - conf->word_delimiters = xmalloc((chars + 1) * sizeof(wchar_t)); - mbstowcs(conf->word_delimiters, value, chars + 1); } else if (strcmp(key, "notify") == 0) { |
