scan.lua

Sat, 12 Sep 2009 21:27:57 +0300

author
Tuomo Valkonen <tuomov@iki.fi>
date
Sat, 12 Sep 2009 21:27:57 +0300
changeset 2
3975fa5ed630
parent 1
e2face1be50e
child 3
b2df1b3f2c83
permissions
-rw-r--r--

(don't know)


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[n]=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)
    local function dir(prefix, name)
        return path.join(prefix, name)
    end
    
    local function file(prefix, name)
        local f=path.join(prefix, name)
        g(f)
    end
    
    hfold(h, "", dir, file)
end

mercurial