Fix io.open unicode override

This commit is contained in:
Lynix 2017-05-18 20:30:42 +02:00
parent b2a6f1e09c
commit 97282c31b5
5 changed files with 33 additions and 81 deletions

View File

@ -18,6 +18,9 @@
#include "lauxlib.h"
#include "lualib.h"
#if defined(LUA_WIN)
#include <windows.h>
#endif
#define IO_INPUT 1
@ -177,8 +180,29 @@ static int io_tostring (lua_State *L) {
static int io_open (lua_State *L) {
const char *filename = luaL_checkstring(L, 1);
const char *mode = luaL_optstring(L, 2, "r");
#if defined(LUA_WIN)
wchar_t wide_path[4096];
if (MultiByteToWideChar(CP_UTF8, 0, filename, -1, wide_path, 4096) == 0)
{
lua_pushstring(L, "unable to encode path");
return lua_error(L);
}
wchar_t wide_mode[64];
if (MultiByteToWideChar(CP_UTF8, 0, mode, -1, wide_mode, 64) == 0)
{
lua_pushstring(L, "unable to encode open mode");
return lua_error(L);
}
#endif
FILE **pf = newfile(L);
#if defined(LUA_WIN)
*pf = _wfopen(wide_path, wide_mode);
#else
*pf = fopen(filename, mode);
#endif
return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
}

View File

@ -1,63 +0,0 @@
/**
* \file os_rmdir.c
* \brief Remove a subdirectory.
* \author Copyright (c) 2002-2013 Jason Perkins and the Premake project
*/
#include "premake.h"
#if PLATFORM_WINDOWS
/* from Lua source code */
static FILE** newfile(lua_State *L)
{
FILE **pf = (FILE **) lua_newuserdata(L, sizeof(FILE *));
*pf = NULL; /* file handle is currently `closed' */
luaL_getmetatable(L, LUA_FILEHANDLE);
lua_setmetatable(L, -2);
return pf;
}
static int pushresult(lua_State *L, int i, const char *filename) {
int en = errno; /* calls to Lua API may change this value */
if (i) {
lua_pushboolean(L, 1);
return 1;
}
else {
lua_pushnil(L);
if (filename)
lua_pushfstring(L, "%s: %s", filename, strerror(en));
else
lua_pushfstring(L, "%s", strerror(en));
lua_pushinteger(L, en);
return 3;
}
}
int io_open(lua_State *L)
{
const char* filename = luaL_checkstring(L, 1);
const char* mode = luaL_optstring(L, 2, "r");
wchar_t wide_path[PATH_MAX];
if (MultiByteToWideChar(CP_UTF8, 0, filename, -1, wide_path, PATH_MAX) == 0)
{
lua_pushstring(L, "unable to encode path");
return lua_error(L);
}
wchar_t wide_mode[64];
if (MultiByteToWideChar(CP_UTF8, 0, mode, -1, wide_mode, 64) == 0)
{
lua_pushstring(L, "unable to encode open mode");
return lua_error(L);
}
FILE **pf = newfile(L);
*pf = _wfopen(wide_path, wide_mode);
return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
}
#endif

View File

@ -8,6 +8,11 @@
#include <ctype.h>
#include <string.h>
// isspace custom version (visual c++'s one doesn't like utf8 characters)
static int is_space(char c)
{
return (c >= 9 && c <= 13) || c == 32;
}
static void* normalize_substring(const char* str, const char* endPtr, char* writePtr) {
const char* const source = str;
@ -93,7 +98,7 @@ int path_normalize(lua_State* L)
const char* endPtr;
// skip leading white spaces
while (*readPtr && isspace(*readPtr)) {
while (*readPtr && is_space(*readPtr)) {
++readPtr;
}
@ -106,14 +111,14 @@ int path_normalize(lua_State* L)
}
// find the end of sub path
while (*endPtr && !isspace(*endPtr)) {
while (*endPtr && !is_space(*endPtr)) {
++endPtr;
}
writePtr = normalize_substring(readPtr, endPtr, writePtr);
// skip any white spaces between sub paths
while (*endPtr && isspace(*endPtr)) {
while (*endPtr && is_space(*endPtr)) {
*(writePtr++) = *(endPtr++);
}
@ -121,7 +126,7 @@ int path_normalize(lua_State* L)
}
// skip any trailing white spaces
while (isspace(*(--endPtr))) {
while (is_space(*(--endPtr))) {
--writePtr;
}

View File

@ -43,13 +43,6 @@ static const luaL_Reg debug_functions[] = {
{ NULL, NULL }
};
#if PLATFORM_WINDOWS
static const luaL_Reg io_functions[] = {
{"open", io_open},
{NULL, NULL}
};
#endif
static const luaL_Reg path_functions[] = {
{ "getabsolute", path_getabsolute },
{ "getrelative", path_getrelative },
@ -145,9 +138,6 @@ int premake_init(lua_State* L)
luaL_register(L, "criteria", criteria_functions);
luaL_register(L, "debug", debug_functions);
luaL_register(L, "path", path_functions);
#if PLATFORM_WINDOWS
luaL_register(L, "io", io_functions);
#endif
luaL_register(L, "os", os_functions);
luaL_register(L, "string", string_functions);
luaL_register(L, "buffered", buffered_functions);

View File

@ -99,10 +99,6 @@ int path_join(lua_State* L);
int path_normalize(lua_State* L);
int path_translate(lua_State* L);
int path_wildcards(lua_State* L);
#if PLATFORM_WINDOWS
// utf8 version
int io_open(lua_State* L);
#endif
int os_chdir(lua_State* L);
int os_chmod(lua_State* L);
int os_copyfile(lua_State* L);