From 00323b19429cb62a0ac86b38f8d2c064bd6214f4 Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Mon, 13 Apr 2020 01:42:45 +0200 Subject: [PATCH] readline.c: fix 8b2e925c --- src/osdep.c | 16 ++++++++++++++++ src/osdep.h | 1 + src/readline.c | 10 +--------- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/osdep.c b/src/osdep.c index b9bb8fd7..ba609991 100644 --- a/src/osdep.c +++ b/src/osdep.c @@ -311,6 +311,22 @@ char * Unicode_to_utf8(const word * str, size_t * utf8len) return utf8; } } + +/** + * UTF8 to Unicode (16bits per character). + * use MultiByteToWideChar(CP_UTF8, ...) under WIN32. + * Note : For UTF-8, dwFlags must be set to either 0 or MB_ERR_INVALID_CHARS. + */ +void Unicode_from_utf8(const char * utf8, word * unicode, size_t unicodelen) +{ + int r = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, (LPWSTR)unicode, unicodelen); + if (r == 0) + { + GFX2_Log(GFX2_ERROR, "MultiByteToWideChar(CP_UTF8, \"%s\", ...) failed with error #%u\n", utf8, GetLastError()); + unicode[0] = 0; + } +} + #endif /** diff --git a/src/osdep.h b/src/osdep.h index c5ae1026..87ffdba9 100644 --- a/src/osdep.h +++ b/src/osdep.h @@ -56,6 +56,7 @@ unsigned long Memory_free(void); #if defined(WIN32) char * Unicode_to_utf8(const word * str, size_t * utf8len); +void Unicode_from_utf8(const char * utf8, word * unicode, size_t unicodelen); void GFX2_GetShortPathName(char * shortname, size_t shortname_len, const word * longname); #endif diff --git a/src/readline.c b/src/readline.c index 0e6646fd..effb00b7 100644 --- a/src/readline.c +++ b/src/readline.c @@ -709,15 +709,7 @@ byte Readline_ex_unicode(word x_pos, word y_pos, char * str, word * str_unicode, unicode_text[0] = 0; } #elif defined(WIN32) - // use MultiByteToWideChar(CP_UTF8, ...) under WIN32 - // Note : For UTF-8, dwFlags must be set to either 0 or - // MB_ERR_INVALID_CHARS. - i = MultiByteToWideChar(CP_UTF8, 0, Key_Text, -1, (LPWSTR)unicode_text, sizeof(unicode_text)/sizeof(word)); - if (i == 0) - { - GFX2_Log(GFX2_ERROR, "MultiByteToWideChar(CP_UTF8, \"%s\", ...) failed with error #%u\n", Key_Text, GetLastError()); - unicode_text[0] = 0; - } + Unicode_from_utf8(Key_Text, unicode_text, sizeof(unicode_text)/sizeof(word)); #else int j; for (i = 0, j = 0; i < (int)sizeof(Key_Text) && j < (int)sizeof(unicode_text) && Key_Text[i] != '\0'; i++)