scan.lua

Sat, 12 Sep 2009 21:27:57 +0300

author
Tuomo Valkonen <tuomov@iki.fi>
date
Sat, 12 Sep 2009 21:27:57 +0300
changeset 2
3975fa5ed630
parent 1
e2face1be50e
child 3
b2df1b3f2c83
permissions
-rw-r--r--

(don't know)

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)
e2face1be50e Basic directory scanner
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27 h[n]=nh
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
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
45 function map(h, g)
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
46 local function dir(prefix, name)
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
47 return path.join(prefix, name)
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
48 end
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
49
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
50 local function file(prefix, name)
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
51 local f=path.join(prefix, name)
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
52 g(f)
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
53 end
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
54
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
55 hfold(h, "", dir, file)
3975fa5ed630 (don't know)
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
56 end

mercurial