From: Max Kellermann Date: Mon, 19 Oct 2009 06:18:43 +0000 (+0200) Subject: charset: added "replace" functions X-Git-Tag: release-0.16~75 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=87e8d7f3e8fa5a005c2aae06b6e00c4ba47d0c7e;p=ncmpc.git charset: added "replace" functions These functions are a shortcut for a common use case: convert an allocated string in-place. --- diff --git a/src/charset.c b/src/charset.c index 85cb891..59d1a26 100644 --- a/src/charset.c +++ b/src/charset.c @@ -107,3 +107,34 @@ locale_to_utf8(const char *localestr) return g_strdup(localestr); #endif } + +char * +replace_utf8_to_locale(char *src) +{ +#ifdef ENABLE_LOCALE + assert(src != NULL); + + if (noconvert) + return src; + + return utf8_to_locale(src); +#else + return src; +#endif +} + + +char * +replace_locale_to_utf8(char *src) +{ +#ifdef ENABLE_LOCALE + assert(src != NULL); + + if (noconvert) + return src; + + return locale_to_utf8(src); +#else + return src; +#endif +} diff --git a/src/charset.h b/src/charset.h index d1b3f75..a08054b 100644 --- a/src/charset.h +++ b/src/charset.h @@ -41,4 +41,19 @@ utf8_width(const char *str); char *utf8_to_locale(const char *str); char *locale_to_utf8(const char *str); +/** + * Converts the UTF-8 string to the locale, and frees the source + * pointer. Returns the source pointer as-is if no conversion is + * required. + */ +char * +replace_utf8_to_locale(char *src); + +/** + * Converts the locale string to UTF-8, and frees the source pointer. + * Returns the source pointer as-is if no conversion is required. + */ +char * +replace_locale_to_utf8(char *src); + #endif diff --git a/src/wreadln.c b/src/wreadln.c index ad1047f..66a1f1c 100644 --- a/src/wreadln.c +++ b/src/wreadln.c @@ -93,8 +93,7 @@ byte_to_screen(const gchar *data, size_t x) dup = g_strdup(data); dup[x] = 0; - p = locale_to_utf8(dup); - g_free(dup); + p = replace_locale_to_utf8(dup); width = utf8_width(p); g_free(p);