Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Add a recursive version of 'dir' #133

Open
Pharap opened this issue Dec 20, 2019 · 0 comments
Open

Proposal: Add a recursive version of 'dir' #133

Pharap opened this issue Dec 20, 2019 · 0 comments

Comments

@Pharap
Copy link

Pharap commented Dec 20, 2019

While it's completely possible to implement this in plain Lua,
it's a fair bit of effort on the user's part.
Having this functionality as part of the library would be quite useful.

A Lua implementation might look something like this:

local function recursedir(top)
	local dir = lfs.dir
	local insert = table.insert
	local remove = table.remove

	local directories = {}
	local directory = top
	local iterator, directoryObject = dir(directory)

	return function()
		local filename = iterator(directoryObject)

		while filename == nil do
			if #directories == 0 then
				return nil
			end

			directory = remove(directories)
			iterator, directoryObject = dir(directory)
			filename = iterator(directoryObject)
		end

		local path = directory..pathSeparator..filename

		if filename ~= '..' and filename ~= '.' then
			local attributes = getAttributes(path)					
			if attributes ~= nil and attributes.mode == 'directory' then
				insert(directories, path)
			end
		end

		return path
	end
end

I expect a C implementation could be made more efficient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant