修复 lfs.dir

This commit is contained in:
baidwwy 2021-05-04 15:12:44 +08:00
parent 845dcbadfc
commit 8eaba63f42
3 changed files with 23 additions and 26 deletions

View File

@ -1,7 +1,7 @@
--[[
@Author : baidwwy
@Date : 2021-02-11 11:49:09
@LastEditTime : 2021-04-29 08:07:46
@LastEditTime : 2021-05-04 14:53:50
--]]
local gge = gge
local _ENV = setmetatable({}, {__index=_G})
@ -225,9 +225,11 @@ function 遍历目录(path,...)
return function ()
repeat
local file = dir(u)
if file then
local f = path..'/'..file
local f = path..'\\'..file
local attr = lfs.attributes (f)
if attr and attr.mode == "directory" then
if file ~= "." and file ~= ".." then
table.insert(pt, f)

View File

@ -641,6 +641,7 @@ static int dir_iter(lua_State * L)
{
#ifdef _WIN32
//struct _finddata_t c_file;
struct utf8_finddata c_file;
#else
struct dirent *entry;
#endif
@ -1185,28 +1186,28 @@ int luaopen_lfs(lua_State * L)
#if defined(_WIN32)
void UTF8ToUnicode(const char* UTF8, WCHAR* UNI)
{
UNI[0] = 0;
int len = MultiByteToWideChar(CP_UTF8, 0, UTF8, -1, NULL, 0);
MultiByteToWideChar(CP_UTF8, 0, UTF8, -1, UNI, len);
}
void UnicodeToUTF8(const WCHAR* UNI, char* UTF8)
{
UTF8[0] = 0;
int len = WideCharToMultiByte(CP_UTF8, 0, UNI, -1, NULL, 0, NULL, NULL);
WideCharToMultiByte(CP_UTF8, 0, UNI, -1, UTF8, len, NULL, NULL);
}
int utf8_stat(const char* _FileName, struct _stat64* _Stat)
int utf8_stat(const char* UTF8Name, struct _stat64* _Stat)
{
WCHAR FileName[MAX_PATH];
memset(FileName, 0, sizeof(FileName));
UTF8ToUnicode(_FileName, FileName);
UTF8ToUnicode(UTF8Name, FileName);
return _wstat64(FileName, _Stat);
}
int utf8_chdir(const char* _Path)
{
WCHAR Path[MAX_PATH];
memset(Path, 0, sizeof(Path));
UTF8ToUnicode(_Path, Path);
return _wchdir(Path);
}
@ -1214,7 +1215,6 @@ int utf8_chdir(const char* _Path)
int utf8_mkdir(const char* _Path)
{
WCHAR Path[MAX_PATH];
memset(Path, 0, sizeof(Path));
UTF8ToUnicode(_Path, Path);
return _wmkdir(Path);
}
@ -1222,7 +1222,6 @@ int utf8_mkdir(const char* _Path)
int utf8_rmdir(const char* _Path)
{
WCHAR Path[MAX_PATH];
memset(Path, 0, sizeof(Path));
UTF8ToUnicode(_Path, Path);
return _wrmdir(Path);
}
@ -1230,7 +1229,6 @@ int utf8_rmdir(const char* _Path)
char* utf8_getcwd(char* _DstBuf, size_t _SizeInBytes)
{
WCHAR Buf[MAX_PATH];
memset(Buf, 0, sizeof(Buf));
WCHAR* uni = _wgetcwd(Buf, MAX_PATH);
if (uni && _SizeInBytes>=wcslen(uni)) {
UnicodeToUTF8(uni, _DstBuf);
@ -1239,12 +1237,13 @@ char* utf8_getcwd(char* _DstBuf, size_t _SizeInBytes)
return NULL;
}
intptr_t utf8_findfirst(const char* _FileName, struct utf8_finddata* _FindData)
intptr_t utf8_findfirst(const char* UTF8Name, struct utf8_finddata* _FindData)
{
WCHAR FileName[MAX_PATH];
memset(FileName, 0, sizeof(FileName));
UTF8ToUnicode(_FileName, FileName);
UTF8ToUnicode(UTF8Name, FileName);
intptr_t r = _wfindfirst(FileName, &_FindData->data);
_FindData->name[0] = 0;
if (r != -1)
UnicodeToUTF8(_FindData->data.name, _FindData->name);
return r;
}
@ -1252,6 +1251,8 @@ intptr_t utf8_findfirst(const char* _FileName, struct utf8_finddata* _FindData)
int utf8_findnext(intptr_t _FindHandle, struct utf8_finddata* _FindData)
{
int r = _wfindnext(_FindHandle, &_FindData->data);
_FindData->name[0] = 0;
if (r != -1)
UnicodeToUTF8(_FindData->data.name, _FindData->name);
return r;
}
@ -1259,10 +1260,9 @@ int utf8_findnext(intptr_t _FindHandle, struct utf8_finddata* _FindData)
BOOLEAN utf8_CreateSymbolicLink(LPCSTR lpSymlinkFileName, LPCSTR lpTargetFileName, DWORD dwFlags)
{
WCHAR FileName[MAX_PATH];
memset(FileName, 0, sizeof(FileName));
UTF8ToUnicode(lpSymlinkFileName, FileName);
WCHAR FileName2[MAX_PATH];
memset(FileName2, 0, sizeof(FileName2));
UTF8ToUnicode(lpTargetFileName, FileName2);
return CreateSymbolicLinkW(FileName, FileName2, dwFlags);
}
@ -1270,10 +1270,9 @@ BOOLEAN utf8_CreateSymbolicLink(LPCSTR lpSymlinkFileName, LPCSTR lpTargetFileNam
BOOLEAN utf8_CreateHardLink(LPCSTR lpFileName, LPCSTR lpExistingFileName, LPSECURITY_ATTRIBUTES lpSecurityAttributes)
{
WCHAR FileName[MAX_PATH];
memset(FileName, 0, sizeof(FileName));
UTF8ToUnicode(lpFileName, FileName);
WCHAR FileName2[MAX_PATH];
memset(FileName2, 0, sizeof(FileName2));
UTF8ToUnicode(lpExistingFileName, FileName2);
return CreateHardLinkW(FileName, FileName2, lpSecurityAttributes);
}
@ -1281,7 +1280,6 @@ BOOLEAN utf8_CreateHardLink(LPCSTR lpFileName, LPCSTR lpExistingFileName, LPSECU
HANDLE utf8_CreateFile(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
{
WCHAR FileName[MAX_PATH];
memset(FileName, 0, sizeof(FileName));
UTF8ToUnicode(lpFileName, FileName);
return CreateFileW(FileName, dwDesiredAccess, dwShareMode,lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
}
@ -1289,7 +1287,6 @@ HANDLE utf8_CreateFile(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMo
DWORD utf8_GetFinalPathNameByHandle(HANDLE hFile, LPSTR lpszFilePath, DWORD cchFilePath, DWORD dwFlags)
{
WCHAR Buf[MAX_PATH];
memset(Buf, 0, sizeof(Buf));
DWORD r = GetFinalPathNameByHandleW(hFile, Buf, MAX_PATH, dwFlags);
if (cchFilePath>=wcslen(Buf)){
UnicodeToUTF8(Buf, lpszFilePath);
@ -1300,16 +1297,14 @@ DWORD utf8_GetFinalPathNameByHandle(HANDLE hFile, LPSTR lpszFilePath, DWORD cchF
BOOL utf8_GetFileAttributesExW(LPCSTR lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId, LPVOID lpFileInformation)
{
WCHAR FileName[MAX_PATH];
memset(FileName, 0, sizeof(FileName));
UTF8ToUnicode(lpFileName, FileName);
BOOL r = GetFileAttributesExW(FileName, fInfoLevelId , lpFileInformation);
return r;
}
int utf8_utime(const char* _FileName, const struct utimbuf* _Time) {
int utf8_utime(const char* UTF8Name, const struct utimbuf* _Time) {
WCHAR Path[MAX_PATH];
memset(Path, 0, sizeof(Path));
UTF8ToUnicode(_FileName, Path);
UTF8ToUnicode(UTF8Name, Path);
return _wutime(Path, (struct _utimbuf*)_Time);
}
#endif

View File

@ -43,7 +43,7 @@ struct utf8_finddata
{
struct _wfinddata_t data;
char name[260];
}c_file;
};
int utf8_stat(const char* _FileName, struct _stat64* _Stat);
int utf8_chdir(const char* _Path);