Sat, 12 Sep 2009 21:27:57 +0300
(don't know)
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) | |
27 | h[n]=nh | |
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 | |
45 | function map(h, g) | |
46 | local function dir(prefix, name) | |
47 | return path.join(prefix, name) | |
48 | end | |
49 | ||
50 | local function file(prefix, name) | |
51 | local f=path.join(prefix, name) | |
52 | g(f) | |
53 | end | |
54 | ||
55 | hfold(h, "", dir, file) | |
56 | end |