Sat, 01 Nov 2014 23:34:21 +0000
Lua 5.2 compatibility hack
2 | 1 | |
2 | module("scan", package.seeall) | |
3 | ||
1 | 4 | require('lfs') |
2 | 5 | require('path') |
1 | 6 | |
7 | local function filtered_dir(d) | |
8 | local f, s_, v_ = lfs.dir(d) | |
9 | local function g(s, v) | |
10 | while true do | |
11 | local vn=f(s, v) | |
12 | if vn~='.' and vn~='..' then | |
13 | return vn | |
14 | end | |
15 | end | |
16 | end | |
17 | return g, s_, v_ | |
18 | end | |
19 | ||
20 | function scan(d) | |
21 | local h={} | |
22 | for f in filtered_dir(d) do | |
23 | local n=d..'/'..f | |
24 | local a=lfs.attributes(n) | |
25 | if a.mode=='directory' then | |
26 | local nh=scan(n) | |
3 | 27 | h[f]=nh |
1 | 28 | elseif a.mode=='file' then |
2 | 29 | h[f]=true |
1 | 30 | end |
31 | end | |
32 | return h | |
33 | end | |
34 | ||
35 | function hfold(h, a, f, g) | |
36 | for n, c in pairs(h) do | |
37 | if type(c)=='table' then | |
38 | hfold(c, f(a, n), f, g) | |
39 | else | |
40 | g(a, n) | |
41 | end | |
42 | end | |
43 | end | |
2 | 44 | |
3 | 45 | function map(h, g, d) |
2 | 46 | local function dir(prefix, name) |
3 | 47 | local p=path.join(prefix, name) |
48 | if d then | |
49 | d(p) | |
50 | end | |
51 | return p | |
2 | 52 | end |
53 | ||
54 | local function file(prefix, name) | |
55 | local f=path.join(prefix, name) | |
56 | g(f) | |
57 | end | |
58 | ||
59 | hfold(h, "", dir, file) | |
60 | end |