From 94266b99d7a804008912f2354f8d8e7ed55d60a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sat, 13 Feb 2021 11:42:21 +0100 Subject: [PATCH] config: refactor: add str_to_wchars(), use it when parsing word-delimiters --- config.c | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/config.c b/config.c index 56acb0cf..939a8c7e 100644 --- a/config.c +++ b/config.c @@ -397,6 +397,25 @@ str_to_double(const char *s, double *res) return errno == 0 && *end == '\0'; } +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, @@ -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) {