Fixed ANSI-C compatibility in lj_fopen.c

This commit is contained in:
Wohlstand 2016-03-25 00:53:13 +03:00
parent 7115753e47
commit e876ae06a2

View File

@ -7,16 +7,18 @@
FILE *_lua_fopen(const char *filename, const char *mode) FILE *_lua_fopen(const char *filename, const char *mode)
{ {
int new_Len1 = 0;
int new_Len2 = 0;
int fn_len_s = strlen(filename); int fn_len_s = strlen(filename);
if(fn_len_s==0) return NULL;
int m_len_s = strlen(mode); int m_len_s = strlen(mode);
if(fn_len_s==0) return NULL;
if(m_len_s==0) return NULL; if(m_len_s==0) return NULL;
wchar_t path[MAX_PATH]; wchar_t path[MAX_PATH];
wchar_t wmode[MAX_PATH]; wchar_t wmode[MAX_PATH];
int new_Len1 = MultiByteToWideChar(CP_UTF8, 0, filename, fn_len_s, path, fn_len_s); new_Len1 = MultiByteToWideChar(CP_UTF8, 0, filename, fn_len_s, path, fn_len_s);
if(new_Len1>=MAX_PATH) return NULL; if(new_Len1>=MAX_PATH) return NULL;
path[new_Len1] = L'\0'; path[new_Len1] = L'\0';
int new_Len2 = MultiByteToWideChar(CP_UTF8, 0, mode, m_len_s, wmode, m_len_s); new_Len2 = MultiByteToWideChar(CP_UTF8, 0, mode, m_len_s, wmode, m_len_s);
if(new_Len2>=MAX_PATH) return NULL; if(new_Len2>=MAX_PATH) return NULL;
wmode[new_Len2] = L'\0'; wmode[new_Len2] = L'\0';
FILE *f = _wfopen(path, wmode); FILE *f = _wfopen(path, wmode);
@ -25,21 +27,21 @@ FILE *_lua_fopen(const char *filename, const char *mode)
FILE *_lua_freopen(const char *filename, const char *mode, FILE * oldfile) FILE *_lua_freopen(const char *filename, const char *mode, FILE * oldfile)
{ {
int new_Len1 = 0;
int new_Len2 = 0;
int fn_len_s = strlen(filename); int fn_len_s = strlen(filename);
int m_len_s = strlen(mode);
if(fn_len_s==0) return NULL; if(fn_len_s==0) return NULL;
int m_len_s=strlen(filename);
if(m_len_s==0) return NULL; if(m_len_s==0) return NULL;
wchar_t path[MAX_PATH]; wchar_t path[MAX_PATH];
wchar_t wmode[MAX_PATH]; wchar_t wmode[MAX_PATH];
int new_Len1 = MultiByteToWideChar(CP_UTF8, 0, filename, fn_len_s, path, fn_len_s); new_Len1 = MultiByteToWideChar(CP_UTF8, 0, filename, fn_len_s, path, fn_len_s);
if(new_Len1>=MAX_PATH) return NULL; if(new_Len1>=MAX_PATH) return NULL;
path[new_Len1] = L'\0'; path[new_Len1] = L'\0';
int new_Len2 = MultiByteToWideChar(CP_UTF8, 0, mode, m_len_s, wmode, m_len_s); new_Len2 = MultiByteToWideChar(CP_UTF8, 0, mode, m_len_s, wmode, m_len_s);
if(new_Len2>=MAX_PATH) return NULL; if(new_Len2>=MAX_PATH) return NULL;
wmode[new_Len2] = L'\0'; wmode[new_Len2] = L'\0';
FILE *f = _wfreopen(path, wmode, oldfile); FILE *f = _wfreopen(path, wmode, oldfile);
LocalFree(path);
LocalFree(wmode);
return f; return f;
} }