Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of UTF-8 for Windows #728

Merged
merged 6 commits into from
Apr 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/host/os_chdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ int do_chdir(lua_State* L, const char* path)
(void)(L); /* warning: unused parameter */

#if PLATFORM_WINDOWS
z = SetCurrentDirectoryA(path);
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
Expand Down
17 changes: 16 additions & 1 deletion src/host/os_copyfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,22 @@ int os_copyfile(lua_State* L)
const char* dst = luaL_checkstring(L, 2);

#if PLATFORM_WINDOWS
z = CopyFileA(src, dst, FALSE);
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);
Expand Down
6 changes: 5 additions & 1 deletion src/host/os_getcwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ int do_getcwd(char* buffer, size_t size)
int result;

#if PLATFORM_WINDOWS
result = (GetCurrentDirectoryA((DWORD)size, buffer) != 0);
wchar_t wbuffer[PATH_MAX];

result = (GetCurrentDirectoryW(PATH_MAX, wbuffer) != 0);
if (result) {
WideCharToMultiByte(CP_UTF8, 0, wbuffer, -1, buffer, size, NULL, NULL);

do_translate(buffer, '/');
}
#else
Expand Down
9 changes: 8 additions & 1 deletion src/host/os_isdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ int os_isdir(lua_State* L)
const char* path = luaL_checkstring(L, 1);
#ifdef _WIN32
int attr;

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 */
Expand All @@ -27,7 +34,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);
Expand Down
16 changes: 13 additions & 3 deletions src/host/os_isfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,25 @@
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);
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;
Expand Down
9 changes: 8 additions & 1 deletion src/host/os_islink.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ int os_islink(lua_State* L)

#if PLATFORM_WINDOWS
{
DWORD attr = GetFileAttributesA(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);
}

DWORD attr = GetFileAttributesW(wide_path);
if (attr != INVALID_FILE_ATTRIBUTES) {
lua_pushboolean(L, (attr & FILE_ATTRIBUTE_REPARSE_POINT) != 0);
return 1;
Expand Down
2 changes: 1 addition & 1 deletion src/host/os_locate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
29 changes: 23 additions & 6 deletions src/host/os_match.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,23 @@ 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);

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 = FindFirstFileA(mask, &m->entry);

m->handle = FindFirstFileW(wide_mask, &m->entry);
m->is_first = 1;
lua_pushlightuserdata(L, m);
return 1;
Expand All @@ -40,7 +49,15 @@ 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);

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;
}

Expand All @@ -64,11 +81,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;
Expand Down Expand Up @@ -159,7 +176,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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/host/os_pathsearch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 8 additions & 1 deletion src/host/os_rmdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ int os_rmdir(lua_State* L)
const char* path = luaL_checkstring(L, 1);

#if PLATFORM_WINDOWS
z = RemoveDirectoryA(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);
}

z = RemoveDirectoryW(wide_path);
#else
z = (0 == rmdir(path));
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/host/os_writefile_ifnotequal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions src/host/premake.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,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;
}
Expand Down Expand Up @@ -321,7 +325,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);
Expand Down
3 changes: 1 addition & 2 deletions src/host/premake.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,12 @@ 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);


/* Built-in functions */
int criteria_compile(lua_State* L);
int criteria_delete(lua_State* L);
Expand Down