scan.lua

Sat, 01 Nov 2014 23:34:21 +0000

author
Tuomo Valkonen <tuomov@iki.fi>
date
Sat, 01 Nov 2014 23:34:21 +0000
changeset 5
7667b101cb1e
parent 3
b2df1b3f2c83
child 7
038275cd92ed
permissions
-rwxr-xr-x

Lua 5.2 compatibility hack


module("scan", package.seeall)

require('lfs')
require('path')

local function filtered_dir(d) 
    local f, s_, v_ = lfs.dir(d)
    local function g(s, v)
        while true do
            local vn=f(s, v)
            if vn~='.' and vn~='..' then
                return vn
            end
        end
    end
    return g, s_, v_
end

function scan(d)
    local h={}
    for f in filtered_dir(d) do
        local n=d..'/'..f
        local a=lfs.attributes(n)
        if a.mode=='directory' then
            local nh=scan(n)
            h[f]=nh
        elseif a.mode=='file' then
            h[f]=true
        end
    end
    return h
end

function hfold(h, a, f, g)
    for n, c in pairs(h) do
        if type(c)=='table' then
            hfold(c, f(a, n), f, g)
        else
            g(a, n)
        end
    end
end

function map(h, g, d)
    local function dir(prefix, name)
        local p=path.join(prefix, name)
        if d then
            d(p)
        end
        return p
    end
    
    local function file(prefix, name)
        local f=path.join(prefix, name)
        g(f)
    end
    
    hfold(h, "", dir, file)
end

mercurial