From bd4c9b3a7f55dda671454b0a853c7b28dda0bf33 Mon Sep 17 00:00:00 2001 From: Klaas Freitag Date: Fri, 12 Oct 2012 17:05:27 +0200 Subject: [PATCH] Added wide character to utf8 and vice versa conversion functions. Reviewed-by: Andreas Schneider --- src/std/c_string.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++ src/std/c_string.h | 35 ++++++++++++++++++++++ 2 files changed, 107 insertions(+) diff --git a/src/std/c_string.c b/src/std/c_string.c index c43df21e9..da753ba01 100644 --- a/src/std/c_string.c +++ b/src/std/c_string.c @@ -25,10 +25,20 @@ #include #include +#include +#include +#include + +#include "config.h" + #include "c_string.h" #include "c_alloc.h" #include "c_macro.h" +#ifdef _WIN32 +#include +#endif + int c_streq(const char *a, const char *b) { register const char *s1 = a; register const char *s2 = b; @@ -186,3 +196,65 @@ char *c_lowercase(const char* str) { return new; } +/* Convert a wide multibyte String to UTF8 */ +const char* c_utf8(const _TCHAR *wstr) +{ + const char *dst = NULL; + +#ifdef _WIN32 + char *mdst = NULL; + + if(!wstr) return NULL; + size_t len = _tcslen( wstr ); + /* Call once to get the required size. */ + int size_needed = WideCharToMultiByte(CP_UTF8, 0, wstr, len, NULL, 0, NULL, NULL); + if( size_needed > 0 ) { + mdst = c_malloc(1+size_needed); + memset( mdst, 0, 1+size_needed); + WideCharToMultiByte(CP_UTF8, 0, wstr, len, mdst, size_needed, NULL, NULL); + dst = mdst; + } +#else + dst = wstr; +#endif + return dst; +} + +/* Convert a an UTF8 string to multibyte */ +const _TCHAR* c_multibyte(const char *str) +{ +#ifdef _WIN32 + _TCHAR *wstrTo = NULL; + if(!str) return NULL; + + size_t len = strlen( str ); + int size_needed = MultiByteToWideChar(CP_UTF8, 0, str, len, NULL, 0); + if(size_needed > 0) { + int size_char = (size_needed+1)*sizeof(_TCHAR); + wstrTo = c_malloc(size_char); + memset( (void*)wstrTo, 0, size_char); + MultiByteToWideChar(CP_UTF8, 0, str, -1, wstrTo, size_needed); + } + return wstrTo; +#else + return str; +#endif +} + +void c_free_utf8(char* buf) +{ +#ifdef _WIN32 + SAFE_FREE(buf); +#else + (void)buf; +#endif +} + +void c_free_multibyte(const _TCHAR* buf) +{ +#ifdef _WIN32 + SAFE_FREE(buf); +#else + (void)buf; +#endif +} diff --git a/src/std/c_string.h b/src/std/c_string.h index c9244f77e..8a8ecdf67 100644 --- a/src/std/c_string.h +++ b/src/std/c_string.h @@ -33,6 +33,8 @@ #ifndef _C_STR_H #define _C_STR_H +#include "c_private.h" + struct c_strlist_s; typedef struct c_strlist_s c_strlist_t; /** @@ -137,6 +139,39 @@ char *c_uppercase(const char* str); */ char *c_lowercase(const char* str); +/** + * @brief Convert a multibyte string to utf8 (Win32). + * + * @param str The multibyte encoded string to convert + * + * @return The malloced converted string or NULL on error. + */ +const char* c_utf8(const _TCHAR *str); + +/** + * @brief Convert a utf8 encoded string to multibyte (Win32). + * + * @param str The utf8 string to convert. + * + * @return The malloced converted multibyte string or NULL on error. + */ +const _TCHAR* c_multibyte(const char *wstr); + +/** + * @brief Free buffer malloced by c_utf8. + * + * @param buf The buffer to free. + * + */ +void c_free_utf8(char* buf); + +/** + * @brief Free buffer malloced by c_multibyte. + * + * @param buf The buffer to free. + */ +void c_free_multibyte(const _TCHAR* buf); + /** * }@ */