config: refactor: add str_to_wchars(), use it when parsing word-delimiters

This commit is contained in:
Daniel Eklöf 2021-02-13 11:42:21 +01:00
parent b09606e343
commit 94266b99d7
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F

View file

@ -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) {