From 98c381468b97317b6280e40aa16216cf568e41f2 Mon Sep 17 00:00:00 2001 From: Lynix Date: Sun, 2 Apr 2017 16:57:50 +0200 Subject: [PATCH 1/4] Add support of unicode to all file operations --- src/host/os_chdir.c | 3 ++- src/host/os_copyfile.c | 3 ++- src/host/os_getcwd.c | 8 +++++++- src/host/os_isdir.c | 8 +++++++- src/host/os_isfile.c | 10 ++++++--- src/host/os_islink.c | 4 +++- src/host/os_locate.c | 2 +- src/host/os_match.c | 15 ++++++++------ src/host/os_pathsearch.c | 2 +- src/host/os_rmdir.c | 3 ++- src/host/os_writefile_ifnotequal.c | 2 +- src/host/premake.c | 8 ++++++-- src/host/premake.h | 7 ++++++- src/host/utf8handling.c | 33 ++++++++++++++++++++++++++++++ 14 files changed, 87 insertions(+), 21 deletions(-) create mode 100644 src/host/utf8handling.c diff --git a/src/host/os_chdir.c b/src/host/os_chdir.c index 5d09c16586..3b930127f6 100644 --- a/src/host/os_chdir.c +++ b/src/host/os_chdir.c @@ -14,7 +14,8 @@ int do_chdir(lua_State* L, const char* path) (void)(L); /* warning: unused parameter */ #if PLATFORM_WINDOWS - z = SetCurrentDirectoryA(path); + z = SetCurrentDirectoryW(utf8_towide(L, path)); + lua_pop(L, 1); #else z = !chdir(path); #endif diff --git a/src/host/os_copyfile.c b/src/host/os_copyfile.c index 526afb5c95..434694a704 100644 --- a/src/host/os_copyfile.c +++ b/src/host/os_copyfile.c @@ -14,7 +14,8 @@ int os_copyfile(lua_State* L) const char* dst = luaL_checkstring(L, 2); #if PLATFORM_WINDOWS - z = CopyFileA(src, dst, FALSE); + z = CopyFileW(utf8_towide(L, src), utf8_towide(L, dst), FALSE); + lua_pop(L, 2); #else lua_pushfstring(L, "cp \"%s\" \"%s\"", src, dst); z = (system(lua_tostring(L, -1)) == 0); diff --git a/src/host/os_getcwd.c b/src/host/os_getcwd.c index 3262865b3a..4c4d745391 100644 --- a/src/host/os_getcwd.c +++ b/src/host/os_getcwd.c @@ -5,6 +5,7 @@ */ #include "premake.h" +#include "assert.h" int os_getcwd(lua_State* L) { @@ -24,8 +25,13 @@ int do_getcwd(char* buffer, size_t size) int result; #if PLATFORM_WINDOWS - result = (GetCurrentDirectoryA(size, buffer) != 0); + assert(size == 0x4000); + wchar_t wbuffer[0x4000]; + + result = (GetCurrentDirectoryW(size, wbuffer) != 0); if (result) { + WideCharToMultiByte(CP_UTF8, 0, wbuffer, -1, buffer, size, NULL, NULL); + do_translate(buffer, '/'); } #else diff --git a/src/host/os_isdir.c b/src/host/os_isdir.c index d7e73c9cdb..171cdf6df3 100644 --- a/src/host/os_isdir.c +++ b/src/host/os_isdir.c @@ -18,6 +18,8 @@ int os_isdir(lua_State* L) const char* path = luaL_checkstring(L, 1); #ifdef _WIN32 int attr; + + const wchar_t* wide_path = utf8_towide(L, path); #endif /* empty path is equivalent to ".", must be true */ @@ -27,7 +29,7 @@ int os_isdir(lua_State* L) } #ifdef _WIN32 // Use Windows-specific GetFileAttributes since it deals with symbolic links. - else if ((attr = GetFileAttributesA(path)) != INVALID_FILE_ATTRIBUTES) + else if ((attr = GetFileAttributesW(wide_path)) != INVALID_FILE_ATTRIBUTES) { int isdir = (attr & FILE_ATTRIBUTE_DIRECTORY) != 0; lua_pushboolean(L, isdir); @@ -42,6 +44,10 @@ int os_isdir(lua_State* L) { lua_pushboolean(L, 0); } + +#ifdef _WIN32 + lua_pop(L, -2); /* pop wide string */ +#endif return 1; } diff --git a/src/host/os_isfile.c b/src/host/os_isfile.c index 34c5c5278e..2c737e1e82 100644 --- a/src/host/os_isfile.c +++ b/src/host/os_isfile.c @@ -11,15 +11,19 @@ int os_isfile(lua_State* L) { const char* filename = luaL_checkstring(L, 1); - lua_pushboolean(L, do_isfile(filename)); + lua_pushboolean(L, do_isfile(L, filename)); return 1; } -int do_isfile(const char* filename) +int do_isfile(lua_State* L, const char* filename) { + (void)(L); /* warning: unused parameter */ + #if PLATFORM_WINDOWS - DWORD attrib = GetFileAttributesA(filename); + DWORD attrib = GetFileAttributesW(utf8_towide(L, filename)); + lua_pop(L, 1); + if (attrib != INVALID_FILE_ATTRIBUTES) { return (attrib & FILE_ATTRIBUTE_DIRECTORY) == 0; diff --git a/src/host/os_islink.c b/src/host/os_islink.c index ff495c344a..61b845fc9a 100644 --- a/src/host/os_islink.c +++ b/src/host/os_islink.c @@ -14,7 +14,9 @@ int os_islink(lua_State* L) #if PLATFORM_WINDOWS { - DWORD attr = GetFileAttributesA(path); + DWORD attr = GetFileAttributesW(utf8_towide(L, path)); + lua_pop(L, 1); + if (attr != INVALID_FILE_ATTRIBUTES) { lua_pushboolean(L, (attr & FILE_ATTRIBUTE_REPARSE_POINT) != 0); return 1; diff --git a/src/host/os_locate.c b/src/host/os_locate.c index 6313f7a9ec..b72b1576dd 100644 --- a/src/host/os_locate.c +++ b/src/host/os_locate.c @@ -37,7 +37,7 @@ int os_locate(lua_State* L) const char* name = lua_tostring(L, i); /* Direct path to file? Return as absolute path */ - if (do_isfile(name)) { + if (do_isfile(L, name)) { lua_pushcfunction(L, path_getabsolute); lua_pushvalue(L, i); lua_call(L, 1, 1); diff --git a/src/host/os_match.c b/src/host/os_match.c index dd1e7f2713..109d1c1adf 100644 --- a/src/host/os_match.c +++ b/src/host/os_match.c @@ -15,14 +15,17 @@ typedef struct struct_MatchInfo { HANDLE handle; int is_first; - WIN32_FIND_DATAA entry; + WIN32_FIND_DATAW entry; } MatchInfo; int os_matchstart(lua_State* L) { const char* mask = luaL_checkstring(L, 1); MatchInfo* m = (MatchInfo*)malloc(sizeof(MatchInfo)); - m->handle = FindFirstFileA(mask, &m->entry); + + m->handle = FindFirstFileW(utf8_towide(L, mask), &m->entry); + lua_pop(L, 1); + m->is_first = 1; lua_pushlightuserdata(L, m); return 1; @@ -40,7 +43,7 @@ int os_matchdone(lua_State* L) int os_matchname(lua_State* L) { MatchInfo* m = (MatchInfo*)lua_touserdata(L, 1); - lua_pushstring(L, m->entry.cFileName); + utf8_fromwide(L, m->entry.cFileName); return 1; } @@ -64,11 +67,11 @@ int os_matchnext(lua_State* L) m->is_first = 0; else { - if (!FindNextFileA(m->handle, &m->entry)) + if (!FindNextFileW(m->handle, &m->entry)) return 0; } - if (strcmp(m->entry.cFileName, ".") != 0 && strcmp(m->entry.cFileName, "..") != 0) + if (wcscmp(m->entry.cFileName, L".") != 0 && wcscmp(m->entry.cFileName, L"..") != 0) { lua_pushboolean(L, 1); return 1; @@ -159,7 +162,7 @@ int os_matchisfile(lua_State* L) fname = lua_tostring(L, -1); lua_pop(L, 1); - lua_pushboolean(L, do_isfile(fname)); + lua_pushboolean(L, do_isfile(L, fname)); } return 1; } diff --git a/src/host/os_pathsearch.c b/src/host/os_pathsearch.c index ec15225884..85e49a026e 100644 --- a/src/host/os_pathsearch.c +++ b/src/host/os_pathsearch.c @@ -58,7 +58,7 @@ int do_pathsearch(lua_State* L, const char* filename, const char* path) lua_concat(L, 3); /* test it - if it exists, return the absolute path */ - if (do_isfile(lua_tostring(L, -1))) + if (do_isfile(L, lua_tostring(L, -1))) { lua_pop(L, 1); lua_pushcfunction(L, path_getabsolute); diff --git a/src/host/os_rmdir.c b/src/host/os_rmdir.c index 7de669a95a..01e6d1cce5 100644 --- a/src/host/os_rmdir.c +++ b/src/host/os_rmdir.c @@ -14,7 +14,8 @@ int os_rmdir(lua_State* L) const char* path = luaL_checkstring(L, 1); #if PLATFORM_WINDOWS - z = RemoveDirectoryA(path); + z = RemoveDirectoryW(utf8_towide(L, path)); + lua_pop(L, 1); #else z = (0 == rmdir(path)); #endif diff --git a/src/host/os_writefile_ifnotequal.c b/src/host/os_writefile_ifnotequal.c index 9d4dcbb76f..88265d2951 100644 --- a/src/host/os_writefile_ifnotequal.c +++ b/src/host/os_writefile_ifnotequal.c @@ -75,7 +75,7 @@ int os_writefile_ifnotequal(lua_State* L) const char* dst = luaL_checkstring(L, 2); // if destination exist, and they are the same, no need to copy. - if (do_isfile(dst) && compare_file(content, length, dst)) + if (do_isfile(L, dst) && compare_file(content, length, dst)) { lua_pushinteger(L, 0); return 1; diff --git a/src/host/premake.c b/src/host/premake.c index 8c26b306be..aa8774ecbc 100644 --- a/src/host/premake.c +++ b/src/host/premake.c @@ -229,9 +229,13 @@ int premake_locate_executable(lua_State* L, const char* argv0) const char* path = NULL; #if PLATFORM_WINDOWS - DWORD len = GetModuleFileNameA(NULL, buffer, PATH_MAX); + wchar_t widebuffer[PATH_MAX]; + + DWORD len = GetModuleFileNameW(NULL, widebuffer, PATH_MAX); if (len > 0) { + WideCharToMultiByte(CP_UTF8, 0, widebuffer, len, buffer, PATH_MAX, NULL, NULL); + buffer[len] = 0; path = buffer; } @@ -320,7 +324,7 @@ int premake_locate_executable(lua_State* L, const char* argv0) int premake_test_file(lua_State* L, const char* filename, int searchMask) { if (searchMask & TEST_LOCAL) { - if (do_isfile(filename)) { + if (do_isfile(L, filename)) { lua_pushcfunction(L, path_getabsolute); lua_pushstring(L, filename); lua_call(L, 1, 1); diff --git a/src/host/premake.h b/src/host/premake.h index d9bb7b33f9..b4b20a3a8d 100644 --- a/src/host/premake.h +++ b/src/host/premake.h @@ -80,12 +80,17 @@ unsigned long do_hash(const char* str, int seed); void do_getabsolute(char* result, const char* value, const char* relative_to); int do_getcwd(char* buffer, size_t size); int do_isabsolute(const char* path); -int do_isfile(const char* filename); +int do_isfile(lua_State* L, const char* filename); int do_locate(lua_State* L, const char* filename, const char* path); void do_normalize(lua_State* L, char* buffer, const char* path); int do_pathsearch(lua_State* L, const char* filename, const char* path); void do_translate(char* value, const char sep); +/* Unicode conversion helper functions (required for Windows systems) */ +#ifdef PLATFORM_WINDOWS +const char* utf8_fromwide(lua_State* L, const wchar_t* wstr); +const wchar_t* utf8_towide(lua_State* L, const char* str); +#endif /* Built-in functions */ int criteria_compile(lua_State* L); diff --git a/src/host/utf8handling.c b/src/host/utf8handling.c new file mode 100644 index 0000000000..e9bb9374a5 --- /dev/null +++ b/src/host/utf8handling.c @@ -0,0 +1,33 @@ +/** + * \file utf8handking.c + * \brief Handles conversions between Unicode (UTF-8) and system native encoding (wide chars on Windows) + * \author Copyright (c) 2017 Jérôme Leclercq and the Premake project + */ + +#include "premake.h" +#include "stdlib.h" + +#ifdef PLATFORM_WINDOWS +const char* utf8_fromwide(lua_State* L, const wchar_t* wstr) +{ + int size_required = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL); + + char* unicode_str = (char*) malloc(size_required * sizeof(char)); + WideCharToMultiByte(CP_UTF8, 0, wstr, -1, unicode_str, size_required, NULL, NULL); + + lua_pushstring(L, unicode_str); + free(unicode_str); + + return unicode_str; +} + +const wchar_t* utf8_towide(lua_State* L, const char* str) +{ + int size_required = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); + + wchar_t* wide_string = (wchar_t*) lua_newuserdata(L, size_required * sizeof(wchar_t)); + MultiByteToWideChar(CP_UTF8, 0, str, -1, wide_string, size_required); + + return wide_string; +} +#endif From 0cc98c5f668a1b640f176cfb69b69880e9d90150 Mon Sep 17 00:00:00 2001 From: Lynix Date: Sun, 2 Apr 2017 17:06:09 +0200 Subject: [PATCH 2/4] Fix problematic assert --- src/host/os_getcwd.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/host/os_getcwd.c b/src/host/os_getcwd.c index 58c49e10f9..5cd9bab1ff 100644 --- a/src/host/os_getcwd.c +++ b/src/host/os_getcwd.c @@ -5,7 +5,6 @@ */ #include "premake.h" -#include "assert.h" int os_getcwd(lua_State* L) { @@ -25,10 +24,9 @@ int do_getcwd(char* buffer, size_t size) int result; #if PLATFORM_WINDOWS - assert(size == 0x4000); - wchar_t wbuffer[0x4000]; + wchar_t wbuffer[MAX_PATH]; - result = (GetCurrentDirectoryW((DWORD)size, wbuffer) != 0); + result = (GetCurrentDirectoryW(MAX_PATH, wbuffer) != 0); if (result) { WideCharToMultiByte(CP_UTF8, 0, wbuffer, -1, buffer, size, NULL, NULL); From d83689d7ee12808b0d1a1eef8977162fee0d250f Mon Sep 17 00:00:00 2001 From: Lynix Date: Mon, 3 Apr 2017 09:15:59 +0200 Subject: [PATCH 3/4] Fix Lua stack corruption with os.isdir --- src/host/os_isdir.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/host/os_isdir.c b/src/host/os_isdir.c index 171cdf6df3..91cdd6fb0c 100644 --- a/src/host/os_isdir.c +++ b/src/host/os_isdir.c @@ -46,7 +46,7 @@ int os_isdir(lua_State* L) } #ifdef _WIN32 - lua_pop(L, -2); /* pop wide string */ + lua_remove(L, -2); /* pop wide string */ #endif return 1; From 9c4c998a2b3424c9047acec5186f23d2e4c10db6 Mon Sep 17 00:00:00 2001 From: Lynix Date: Tue, 4 Apr 2017 09:03:29 +0200 Subject: [PATCH 4/4] Rework unicode encoding/decoding (Windows) --- src/host/os_chdir.c | 10 ++++++++-- src/host/os_copyfile.c | 18 ++++++++++++++++-- src/host/os_getcwd.c | 6 +++--- src/host/os_isdir.c | 13 +++++++------ src/host/os_isfile.c | 10 ++++++++-- src/host/os_islink.c | 9 +++++++-- src/host/os_match.c | 22 ++++++++++++++++++---- src/host/os_rmdir.c | 10 ++++++++-- src/host/premake.h | 6 ------ src/host/utf8handling.c | 33 --------------------------------- 10 files changed, 75 insertions(+), 62 deletions(-) delete mode 100644 src/host/utf8handling.c diff --git a/src/host/os_chdir.c b/src/host/os_chdir.c index 3b930127f6..28aa0fcd2e 100644 --- a/src/host/os_chdir.c +++ b/src/host/os_chdir.c @@ -14,8 +14,14 @@ int do_chdir(lua_State* L, const char* path) (void)(L); /* warning: unused parameter */ #if PLATFORM_WINDOWS - z = SetCurrentDirectoryW(utf8_towide(L, path)); - lua_pop(L, 1); + wchar_t wide_buffer[PATH_MAX]; + if (MultiByteToWideChar(CP_UTF8, 0, path, -1, wide_buffer, PATH_MAX) == 0) + { + lua_pushstring(L, "unable to encode path"); + return lua_error(L); + } + + z = SetCurrentDirectoryW(wide_buffer); #else z = !chdir(path); #endif diff --git a/src/host/os_copyfile.c b/src/host/os_copyfile.c index 434694a704..e15c93ddbb 100644 --- a/src/host/os_copyfile.c +++ b/src/host/os_copyfile.c @@ -14,8 +14,22 @@ int os_copyfile(lua_State* L) const char* dst = luaL_checkstring(L, 2); #if PLATFORM_WINDOWS - z = CopyFileW(utf8_towide(L, src), utf8_towide(L, dst), FALSE); - lua_pop(L, 2); + wchar_t wide_src[PATH_MAX]; + wchar_t wide_dst[PATH_MAX]; + + if (MultiByteToWideChar(CP_UTF8, 0, src, -1, wide_src, PATH_MAX) == 0) + { + lua_pushstring(L, "unable to encode source path"); + return lua_error(L); + } + + if (MultiByteToWideChar(CP_UTF8, 0, dst, -1, wide_dst, PATH_MAX) == 0) + { + lua_pushstring(L, "unable to encode source path"); + return lua_error(L); + } + + z = CopyFileW(wide_src, wide_dst, FALSE); #else lua_pushfstring(L, "cp \"%s\" \"%s\"", src, dst); z = (system(lua_tostring(L, -1)) == 0); diff --git a/src/host/os_getcwd.c b/src/host/os_getcwd.c index 5cd9bab1ff..5a3e2931ba 100644 --- a/src/host/os_getcwd.c +++ b/src/host/os_getcwd.c @@ -24,9 +24,9 @@ int do_getcwd(char* buffer, size_t size) int result; #if PLATFORM_WINDOWS - wchar_t wbuffer[MAX_PATH]; - - result = (GetCurrentDirectoryW(MAX_PATH, wbuffer) != 0); + wchar_t wbuffer[PATH_MAX]; + + result = (GetCurrentDirectoryW(PATH_MAX, wbuffer) != 0); if (result) { WideCharToMultiByte(CP_UTF8, 0, wbuffer, -1, buffer, size, NULL, NULL); diff --git a/src/host/os_isdir.c b/src/host/os_isdir.c index 91cdd6fb0c..b3e2ccdd37 100644 --- a/src/host/os_isdir.c +++ b/src/host/os_isdir.c @@ -18,8 +18,13 @@ int os_isdir(lua_State* L) const char* path = luaL_checkstring(L, 1); #ifdef _WIN32 int attr; - - const wchar_t* wide_path = utf8_towide(L, path); + + wchar_t wide_path[PATH_MAX]; + if (MultiByteToWideChar(CP_UTF8, 0, path, -1, wide_path, PATH_MAX) == 0) + { + lua_pushstring(L, "unable to encode path"); + return lua_error(L); + } #endif /* empty path is equivalent to ".", must be true */ @@ -44,10 +49,6 @@ int os_isdir(lua_State* L) { lua_pushboolean(L, 0); } - -#ifdef _WIN32 - lua_remove(L, -2); /* pop wide string */ -#endif return 1; } diff --git a/src/host/os_isfile.c b/src/host/os_isfile.c index 2c737e1e82..2bad6f04d7 100644 --- a/src/host/os_isfile.c +++ b/src/host/os_isfile.c @@ -21,9 +21,15 @@ int do_isfile(lua_State* L, const char* filename) (void)(L); /* warning: unused parameter */ #if PLATFORM_WINDOWS - DWORD attrib = GetFileAttributesW(utf8_towide(L, filename)); - lua_pop(L, 1); + wchar_t wide_path[PATH_MAX]; + if (MultiByteToWideChar(CP_UTF8, 0, filename, -1, wide_path, PATH_MAX) == 0) + { + lua_pushstring(L, "unable to encode filepath"); + return lua_error(L); + } + + DWORD attrib = GetFileAttributesW(wide_path); if (attrib != INVALID_FILE_ATTRIBUTES) { return (attrib & FILE_ATTRIBUTE_DIRECTORY) == 0; diff --git a/src/host/os_islink.c b/src/host/os_islink.c index 61b845fc9a..1c2dee91a4 100644 --- a/src/host/os_islink.c +++ b/src/host/os_islink.c @@ -14,9 +14,14 @@ int os_islink(lua_State* L) #if PLATFORM_WINDOWS { - DWORD attr = GetFileAttributesW(utf8_towide(L, path)); - lua_pop(L, 1); + wchar_t wide_path[PATH_MAX]; + if (MultiByteToWideChar(CP_UTF8, 0, path, -1, wide_path, PATH_MAX) == 0) + { + lua_pushstring(L, "unable to encode path"); + return lua_error(L); + } + DWORD attr = GetFileAttributesW(wide_path); if (attr != INVALID_FILE_ATTRIBUTES) { lua_pushboolean(L, (attr & FILE_ATTRIBUTE_REPARSE_POINT) != 0); return 1; diff --git a/src/host/os_match.c b/src/host/os_match.c index 109d1c1adf..2a25df686a 100644 --- a/src/host/os_match.c +++ b/src/host/os_match.c @@ -21,11 +21,17 @@ typedef struct struct_MatchInfo int os_matchstart(lua_State* L) { const char* mask = luaL_checkstring(L, 1); - MatchInfo* m = (MatchInfo*)malloc(sizeof(MatchInfo)); - m->handle = FindFirstFileW(utf8_towide(L, mask), &m->entry); - lua_pop(L, 1); + wchar_t wide_mask[PATH_MAX]; + if (MultiByteToWideChar(CP_UTF8, 0, mask, -1, wide_mask, PATH_MAX) == 0) + { + lua_pushstring(L, "unable to encode mask"); + return lua_error(L); + } + + MatchInfo* m = (MatchInfo*)malloc(sizeof(MatchInfo)); + m->handle = FindFirstFileW(wide_mask, &m->entry); m->is_first = 1; lua_pushlightuserdata(L, m); return 1; @@ -43,7 +49,15 @@ int os_matchdone(lua_State* L) int os_matchname(lua_State* L) { MatchInfo* m = (MatchInfo*)lua_touserdata(L, 1); - utf8_fromwide(L, m->entry.cFileName); + + char filename[PATH_MAX]; + if (WideCharToMultiByte(CP_UTF8, 0, m->entry.cFileName, -1, filename, PATH_MAX, NULL, NULL) == 0) + { + lua_pushstring(L, "unable to decode filename"); + return lua_error(L); + } + + lua_pushstring(L, filename); return 1; } diff --git a/src/host/os_rmdir.c b/src/host/os_rmdir.c index 01e6d1cce5..46e65d9daa 100644 --- a/src/host/os_rmdir.c +++ b/src/host/os_rmdir.c @@ -14,8 +14,14 @@ int os_rmdir(lua_State* L) const char* path = luaL_checkstring(L, 1); #if PLATFORM_WINDOWS - z = RemoveDirectoryW(utf8_towide(L, path)); - lua_pop(L, 1); + wchar_t wide_path[PATH_MAX]; + if (MultiByteToWideChar(CP_UTF8, 0, path, -1, wide_path, PATH_MAX) == 0) + { + lua_pushstring(L, "unable to encode path"); + return lua_error(L); + } + + z = RemoveDirectoryW(wide_path); #else z = (0 == rmdir(path)); #endif diff --git a/src/host/premake.h b/src/host/premake.h index 4c3b982688..c8bb3f236a 100644 --- a/src/host/premake.h +++ b/src/host/premake.h @@ -87,12 +87,6 @@ void do_normalize(lua_State* L, char* buffer, const char* path); int do_pathsearch(lua_State* L, const char* filename, const char* path); void do_translate(char* value, const char sep); -/* Unicode conversion helper functions (required for Windows systems) */ -#ifdef PLATFORM_WINDOWS -const char* utf8_fromwide(lua_State* L, const wchar_t* wstr); -const wchar_t* utf8_towide(lua_State* L, const char* str); -#endif - /* Built-in functions */ int criteria_compile(lua_State* L); int criteria_delete(lua_State* L); diff --git a/src/host/utf8handling.c b/src/host/utf8handling.c deleted file mode 100644 index e9bb9374a5..0000000000 --- a/src/host/utf8handling.c +++ /dev/null @@ -1,33 +0,0 @@ -/** - * \file utf8handking.c - * \brief Handles conversions between Unicode (UTF-8) and system native encoding (wide chars on Windows) - * \author Copyright (c) 2017 Jérôme Leclercq and the Premake project - */ - -#include "premake.h" -#include "stdlib.h" - -#ifdef PLATFORM_WINDOWS -const char* utf8_fromwide(lua_State* L, const wchar_t* wstr) -{ - int size_required = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL); - - char* unicode_str = (char*) malloc(size_required * sizeof(char)); - WideCharToMultiByte(CP_UTF8, 0, wstr, -1, unicode_str, size_required, NULL, NULL); - - lua_pushstring(L, unicode_str); - free(unicode_str); - - return unicode_str; -} - -const wchar_t* utf8_towide(lua_State* L, const char* str) -{ - int size_required = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); - - wchar_t* wide_string = (wchar_t*) lua_newuserdata(L, size_required * sizeof(wchar_t)); - MultiByteToWideChar(CP_UTF8, 0, str, -1, wide_string, size_required); - - return wide_string; -} -#endif