diff --git a/src/factory.c b/src/factory.c index 06bc61a1..c82f9015 100644 --- a/src/factory.c +++ b/src/factory.c @@ -62,9 +62,6 @@ char * Bound_script[10]; #include //for INT_MIN #include // strncpy() #include // for atof() -#if defined(WIN32) -#include -#endif #if defined(_MSC_VER) #define strdup _strdup #define putenv _putenv @@ -2002,30 +1999,16 @@ int L_GetFileName(lua_State* L) else { // convert Filename_unicode to UTF8 (Lua strings are 8bits) - size_t unicode_len = Unicode_strlen(Main.backups->Pages->Filename_unicode); - int len = WideCharToMultiByte(CP_UTF8, 0, Main.backups->Pages->Filename_unicode, unicode_len, NULL, 0, NULL, NULL); - if (len <= 0) + size_t len = 0; + char * utf8name = Unicode_to_utf8(Main.backups->Pages->Filename_unicode, &len); + if (utf8name == NULL) { - GFX2_Log(GFX2_WARNING, "WideCharToMultiByte() failed\n"); lua_pushstring(L, Main.backups->Pages->Filename); } else { - char * utf8name = (char *)GFX2_malloc(len); - if (utf8name == NULL) - lua_pushstring(L, Main.backups->Pages->Filename); - else - { - if (WideCharToMultiByte(CP_UTF8, 0, Main.backups->Pages->Filename_unicode, unicode_len, utf8name, len, NULL, NULL) > 0) - lua_pushlstring(L, utf8name, len); - else - { - DWORD error = GetLastError(); - GFX2_Log(GFX2_WARNING, "WideCharToMultiByte() failed error=%u\n", error); - lua_pushstring(L, Main.backups->Pages->Filename); - } - free(utf8name); - } + lua_pushlstring(L, utf8name, len); + free(utf8name); } } // still using the "short name" directory path at the moment diff --git a/src/osdep.c b/src/osdep.c index 79bbe33e..80445230 100644 --- a/src/osdep.c +++ b/src/osdep.c @@ -22,6 +22,11 @@ You should have received a copy of the GNU General Public License along with Grafx2; if not, see */ +////////////////////////////////////////////////////////////////////////////// +///@file osdep.c +/// OS Dependend code +////////////////////////////////////////////////////////////////////////////// + #if defined(USE_SDL) || defined(USE_SDL2) #include #elif !defined(WIN32) @@ -59,7 +64,9 @@ #endif #include "struct.h" +#include "unicode.h" #include "gfx2log.h" +#include "gfx2mem.h" dword GFX2_GetTicks(void) { @@ -253,3 +260,39 @@ unsigned long Memory_free(void) #endif } #endif + +#if defined(WIN32) +/** + * Converts an unicode string to UTF8 + * @param str the unicode string + * @param utf8len pointer to receive the utf8 string length (excluding null terminator) + * @return malloc'ed UTF8 string + */ +char * Unicode_to_utf8(const word * str, size_t * utf8len) +{ + size_t unicode_len = Unicode_strlen(str); + int len = WideCharToMultiByte(CP_UTF8, 0, str, unicode_len, NULL, 0, NULL, NULL); + if (len <= 0) + { + GFX2_Log(GFX2_WARNING, "WideCharToMultiByte() failed\n"); + return NULL; + } + else + { + char * utf8 = (char *)GFX2_malloc(len+1); + if (utf8 == NULL) + return NULL; + if (WideCharToMultiByte(CP_UTF8, 0, str, unicode_len, utf8, len, NULL, NULL) <= 0) + { + DWORD error = GetLastError(); + GFX2_Log(GFX2_WARNING, "WideCharToMultiByte() failed error=%u\n", error); + free(utf8); + return NULL; + } + if (utf8len != NULL) + *utf8len = (size_t)len; + utf8[len] = '\0'; + return utf8; + } +} +#endif diff --git a/src/osdep.h b/src/osdep.h index 2a6a6cfb..3446c55d 100644 --- a/src/osdep.h +++ b/src/osdep.h @@ -54,4 +54,8 @@ void Atari_Memory_free(unsigned long *stRam, unsigned long *ttRam); unsigned long Memory_free(void); #endif +#if defined(WIN32) +char * Unicode_to_utf8(const word * str, size_t * utf8len); +#endif + #endif