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