Aegisub/vendor/luajit/unicode-io.patch
2018-05-12 03:06:01 -04:00

64 lines
1.9 KiB
Diff

diff --git a/vendor/luajit/src/lib_io.c b/vendor/luajit/src/lib_io.c
index 9cdcfcfcb..6858e6455 100644
--- a/vendor/luajit/src/lib_io.c
+++ b/vendor/luajit/src/lib_io.c
@@ -24,6 +24,16 @@
#include "lj_ff.h"
#include "lj_lib.h"
+#if LJ_TARGET_WINDOWS
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+
+static int widen(const char *in, wchar_t *out)
+{
+ return MultiByteToWideChar(CP_UTF8, 0, in, -1, out, MAX_PATH);
+}
+#endif
+
/* Userdata payload for I/O file. */
typedef struct IOFileUD {
FILE *fp; /* File handle. */
@@ -82,7 +92,15 @@ static IOFileUD *io_file_open(lua_State *L, const char *mode)
{
const char *fname = strdata(lj_lib_checkstr(L, 1));
IOFileUD *iof = io_file_new(L);
+#if LJ_TARGET_WINDOWS
+ wchar_t wfname[MAX_PATH];
+ wchar_t wmode[MAX_PATH];
+ if (!widen(fname, wfname) || !widen(mode, wmode))
+ luaL_argerror(L, 1, lj_str_pushf(L, "%s: failed to convert path to utf-16", fname));
+ iof->fp = _wfopen(wfname, wmode);
+#else
iof->fp = fopen(fname, mode);
+#endif
if (iof->fp == NULL)
luaL_argerror(L, 1, lj_str_pushf(L, "%s: %s", fname, strerror(errno)));
return iof;
@@ -407,7 +425,14 @@ LJLIB_CF(io_open)
GCstr *s = lj_lib_optstr(L, 2);
const char *mode = s ? strdata(s) : "r";
IOFileUD *iof = io_file_new(L);
+#if LJ_TARGET_WINDOWS
+ wchar_t wfname[MAX_PATH];
+ wchar_t wmode[MAX_PATH];
+ if (widen(fname, wfname) && widen(mode, wmode))
+ iof->fp = _wfopen(wfname, wmode);
+#else
iof->fp = fopen(fname, mode);
+#endif
return iof->fp != NULL ? 1 : luaL_fileresult(L, 0, fname);
}
@@ -423,7 +448,10 @@ LJLIB_CF(io_popen)
fflush(NULL);
iof->fp = popen(fname, mode);
#else
- iof->fp = _popen(fname, mode);
+ wchar_t wfname[MAX_PATH];
+ wchar_t wmode[MAX_PATH];
+ if (widen(fname, wfname) && widen(mode, wmode))
+ iof->fp = _wpopen(wfname, wmode);
#endif
return iof->fp != NULL ? 1 : luaL_fileresult(L, 0, fname);
#else