1:e2face1be50e | 2:3975fa5ed630 |
---|---|
1 | |
2 module("scan", package.seeall) | |
3 | |
1 require('lfs') | 4 require('lfs') |
5 require('path') | |
2 | 6 |
3 local function filtered_dir(d) | 7 local function filtered_dir(d) |
4 local f, s_, v_ = lfs.dir(d) | 8 local f, s_, v_ = lfs.dir(d) |
5 local function g(s, v) | 9 local function g(s, v) |
6 while true do | 10 while true do |
11 end | 15 end |
12 end | 16 end |
13 return g, s_, v_ | 17 return g, s_, v_ |
14 end | 18 end |
15 | 19 |
16 | |
17 function scan(d) | 20 function scan(d) |
18 local h={} | 21 local h={} |
19 for f in filtered_dir(d) do | 22 for f in filtered_dir(d) do |
20 local n=d..'/'..f | 23 local n=d..'/'..f |
21 local a=lfs.attributes(n) | 24 local a=lfs.attributes(n) |
22 if a.mode=='directory' then | 25 if a.mode=='directory' then |
23 local nh=scan(n) | 26 local nh=scan(n) |
24 h[n]=nh | 27 h[n]=nh |
25 elseif a.mode=='file' then | 28 elseif a.mode=='file' then |
26 h[n]=true | 29 h[f]=true |
27 end | 30 end |
28 end | 31 end |
29 return h | 32 return h |
30 end | 33 end |
31 | 34 |
36 else | 39 else |
37 g(a, n) | 40 g(a, n) |
38 end | 41 end |
39 end | 42 end |
40 end | 43 end |
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 |