diff --git a/legacy/evil/ChangeLog b/legacy/evil/ChangeLog index 325b1766de..07c2a8cc4a 100644 --- a/legacy/evil/ChangeLog +++ b/legacy/evil/ChangeLog @@ -1,3 +1,11 @@ +2011-11-02 Vincent Torri + + * src/lib/evil_util.c: + * src/lib/evil_util.h: + Add evil_utf16_to_utf8() function, display error messages + and fix a bit the documentation of the other conversion + functions. + 2011-10-12 Vincent Torri * src/lib/evil_util.c: diff --git a/legacy/evil/src/lib/evil_util.c b/legacy/evil/src/lib/evil_util.c index 59c386ac11..1ffd9ba9f9 100644 --- a/legacy/evil/src/lib/evil_util.c +++ b/legacy/evil/src/lib/evil_util.c @@ -21,15 +21,26 @@ evil_char_to_wchar(const char *text) wchar_t *wtext; int wsize; + if (!text) + return NULL; + wsize = MultiByteToWideChar(CP_ACP, 0, text, (int)strlen(text) + 1, NULL, 0); if ((wsize == 0) || (wsize > (int)(ULONG_MAX / sizeof(wchar_t)))) - return NULL; + { + if (wsize == 0) + _evil_last_error_display(__FUNCTION__); + return NULL; + } wtext = malloc(wsize * sizeof(wchar_t)); if (wtext) if (!MultiByteToWideChar(CP_ACP, 0, text, (int)strlen(text) + 1, wtext, wsize)) - return NULL; + { + if (wsize == 0) + _evil_last_error_display(__FUNCTION__); + return NULL; + } return wtext; } @@ -40,9 +51,15 @@ evil_wchar_to_char(const wchar_t *text) char *atext; int asize; + if (!text) + return NULL; + asize = WideCharToMultiByte(CP_ACP, 0, text, -1, NULL, 0, NULL, NULL); if (asize == 0) - return NULL; + { + _evil_last_error_display(__FUNCTION__); + return NULL; + } atext = (char*)malloc(asize * sizeof(char)); if (!atext) @@ -50,11 +67,49 @@ evil_wchar_to_char(const wchar_t *text) asize = WideCharToMultiByte(CP_ACP, 0, text, -1, atext, asize, NULL, NULL); if (asize == 0) - return NULL; + { + _evil_last_error_display(__FUNCTION__); + return NULL; + } return atext; } +char * +evil_utf16_to_utf8(const wchar_t *text16) +{ + char *text8; + DWORD flag = 0; + int size8; + + if (!text16) + return NULL; + +#if _WIN32_WINNT >= 0x0600 + flag = WC_ERR_INVALID_CHARS;; +#endif + + size8 = WideCharToMultiByte(CP_UTF8, flag, text16, -1, NULL, 0, NULL, NULL); + if (size8 == 0) + { + _evil_last_error_display(__FUNCTION__); + return NULL; + } + + text8 = (char*)malloc(size8 * sizeof(char)); + if (!text8) + return NULL; + + size8 = WideCharToMultiByte(CP_UTF8, flag, text16, -1, text8, size8, NULL, NULL); + if (size8 == 0) + { + _evil_last_error_display(__FUNCTION__); + return NULL; + } + + return text8; +} + char * evil_format_message(long err) { diff --git a/legacy/evil/src/lib/evil_util.h b/legacy/evil/src/lib/evil_util.h index eaefdec225..0170dd0f2a 100644 --- a/legacy/evil/src/lib/evil_util.h +++ b/legacy/evil/src/lib/evil_util.h @@ -10,7 +10,7 @@ * * Convert a string from char * to wchar_t * and return it. If the * allocation or conversion fails, NULL is returned. On success, the - * returned value must be freed. + * returned value must be freed when it is not used anymore. * * Conformity: Non applicable. * @@ -29,7 +29,7 @@ EAPI wchar_t *evil_char_to_wchar(const char *text); * * Convert a string from wchar_t * to char * and return it. If the * allocation or conversion fails, NULL is returned. On success, the - * returned value must be freed. + * returned value must be freed when it is not used anymore. * * Conformity: Non applicable. * @@ -40,6 +40,25 @@ EAPI wchar_t *evil_char_to_wchar(const char *text); */ EAPI char *evil_wchar_to_char(const wchar_t *text); +/** + * @brief Convert a string from UTF-16 to UTF-8. + * + * @param text The string to convert in UTF-16. + * @return The converted string in UTF-8. + * + * Convert a string from UTF-16 to UTF-8 and return it. If the + * allocation or conversion fails, NULL is returned. On success, the + * returned value must be freed when it is not used anymore. + * + * Conformity: Non applicable. + * + * Supported OS: Windows 95, Windows 98, Windows Me, Windows NT, Windows 2000, + * Windows XP. + * + * @ingroup Evil + */ +EAPI char *evil_utf16_to_utf8(const wchar_t *text); + EAPI char *evil_format_message(long err); EAPI char *evil_last_error_get(void);