Skip to content

Commit

Permalink
new dir iterator for win32 returning filename and object to be querie…
Browse files Browse the repository at this point in the history
…d by o:attr(name) (names = mode or size)
  • Loading branch information
sonoro1234 committed Feb 24, 2021
1 parent e4dfc72 commit ba39e41
Showing 1 changed file with 49 additions and 15 deletions.
64 changes: 49 additions & 15 deletions lfs_ffi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -442,45 +442,79 @@ if OS == "Windows" then
findclose(dir._dentry)
dir.closed = true
end


local dir_attrs = {
_A_ARCH = 0x20,
_A_HIDDEN = 0x02,
_A_NORMAL = 0x00,
_A_RDONLY = 0x01,
_A_SUBDIR = 0x10,
_A_SYSTEM = 0x04
}

local function dir_attr(t,attr)
if not type(attr)=="string" then error("dir_attr must have a string") end
if attr=="mode" then
if bit.band(t.attrib,dir_attrs._A_SUBDIR)~=0 then
return "directory"
else
return "file"
end
elseif attr=="size" then
return t.size
end
end

local dentry_type = ffi.metatype("_finddata_t",
{__index = {
attr = dir_attr
}
})

local function iterator(dir)
if dir.closed ~= false then error("closed directory") end
local entry = ffi.new("_finddata_t")
if not dir._dentry then
dir.entry = ffi.new(dentry_type)
dir._dentry = ffi.new(dir_type)
dir._dentry.handle = findfirst(dir._pattern, entry)
dir._dentry.handle = findfirst(dir._pattern, dir.entry)
if dir._dentry.handle == -1 then
dir.closed = true
return nil, errno()
return false, errno()
end
return ffi_str(entry.name)
return ffi_str(dir.entry.name), dir.entry
end

if findnext(dir._dentry.handle, entry) == 0 then
return ffi_str(entry.name)
if findnext(dir._dentry.handle, dir.entry) == 0 then
return ffi_str(dir.entry.name), dir.entry
end
close(dir)
return nil
end

local wdentry_type = ffi.metatype("_wfinddata_t",
{__index = {
attr = dir_attr
},
})

local function witerator(dir)
if dir.closed ~= false then error("closed directory") end
local entry = ffi.new("_wfinddata_t")
if not dir._dentry then
dir.entry = ffi.new(wdentry_type)
dir._dentry = ffi.new(dir_type)
local szPattern = win_utf8_to_unicode(dir._pattern);
dir._dentry.handle = wfindfirst(szPattern, entry)
dir._dentry.handle = wfindfirst(szPattern, dir.entry)
if dir._dentry.handle == -1 then
dir.closed = true
return nil, errno()
return false, errno()
end
local szName = win_unicode_to_utf8(entry.name)--, -1, szName, 512);
return ffi_str(szName)
local szName = win_unicode_to_utf8(dir.entry.name)--, -1, szName, 512);
return ffi_str(szName),dir.entry
end

if wfindnext(dir._dentry.handle, entry) == 0 then
local szName = win_unicode_to_utf8(entry.name)--, -1, szName, 512);
return ffi_str(szName)
if wfindnext(dir._dentry.handle, dir.entry) == 0 then
local szName = win_unicode_to_utf8(dir.entry.name)--, -1, szName, 512);
return ffi_str(szName),dir.entry
end
close(dir)
return nil
Expand Down

0 comments on commit ba39e41

Please sign in to comment.