scan.lua

Sun, 07 May 2017 20:02:53 +0100

author
Tuomo Valkonen <tuomov@iki.fi>
date
Sun, 07 May 2017 20:02:53 +0100
changeset 6
219d7a7304f8
parent 3
b2df1b3f2c83
child 7
038275cd92ed
permissions
-rw-r--r--

5.3 updates

2
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
1
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
2 module("scan", package.seeall)
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
3
1
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
4 require('lfs')
2
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
5 require('path')
1
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
6
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
7 local function filtered_dir(d)
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
8 local f, s_, v_ = lfs.dir(d)
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
9 local function g(s, v)
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
10 while true do
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11 local vn=f(s, v)
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
12 if vn~='.' and vn~='..' then
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
13 return vn
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14 end
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15 end
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
16 end
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
17 return g, s_, v_
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18 end
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
19
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20 function scan(d)
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21 local h={}
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22 for f in filtered_dir(d) do
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23 local n=d..'/'..f
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24 local a=lfs.attributes(n)
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25 if a.mode=='directory' then
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26 local nh=scan(n)
3
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 2
diff changeset
27 h[f]=nh
1
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28 elseif a.mode=='file' then
2
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
29 h[f]=true
1
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
30 end
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
31 end
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
32 return h
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33 end
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
34
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35 function hfold(h, a, f, g)
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36 for n, c in pairs(h) do
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37 if type(c)=='table' then
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
38 hfold(c, f(a, n), f, g)
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
39 else
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
40 g(a, n)
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
41 end
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42 end
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43 end
2
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
44
3
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 2
diff changeset
45 function map(h, g, d)
2
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
46 local function dir(prefix, name)
3
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 2
diff changeset
47 local p=path.join(prefix, name)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 2
diff changeset
48 if d then
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 2
diff changeset
49 d(p)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 2
diff changeset
50 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 2
diff changeset
51 return p
2
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
52 end
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
53
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
54 local function file(prefix, name)
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
55 local f=path.join(prefix, name)
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
56 g(f)
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
57 end
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
58
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
59 hfold(h, "", dir, file)
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
60 end

mercurial