Basic directory scanner

Tue, 17 Mar 2009 21:39:41 +0200

author
Tuomo Valkonen <tuomov@iki.fi>
date
Tue, 17 Mar 2009 21:39:41 +0200
changeset 1
e2face1be50e
parent 0
30d76ecdd725
child 2
3975fa5ed630

Basic directory scanner

scan.lua file | annotate | diff | comparison | revisions
test.lua file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scan.lua	Tue Mar 17 21:39:41 2009 +0200
@@ -0,0 +1,40 @@
+require('lfs')
+
+local function filtered_dir(d) 
+    local f, s_, v_ = lfs.dir(d)
+    local function g(s, v)
+        while true do
+            local vn=f(s, v)
+            if vn~='.' and vn~='..' then
+                return vn
+            end
+        end
+    end
+    return g, s_, v_
+end
+
+
+function scan(d)
+    local h={}
+    for f in filtered_dir(d) do
+        local n=d..'/'..f
+        local a=lfs.attributes(n)
+        if a.mode=='directory' then
+            local nh=scan(n)
+            h[n]=nh
+        elseif a.mode=='file' then
+            h[n]=true
+        end
+    end
+    return h
+end
+
+function hfold(h, a, f, g)
+    for n, c in pairs(h) do
+        if type(c)=='table' then
+            hfold(c, f(a, n), f, g)
+        else
+            g(a, n)
+        end
+    end
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test.lua	Tue Mar 17 21:39:41 2009 +0200
@@ -0,0 +1,9 @@
+
+require('scan')
+
+h=scan('/home/tuomov/work/ion-homepage/src')
+
+hfold(h, '',
+      function(a, n) return a.."/"..n end, 
+      function(a, n) print(a.."/"..n) end)
+      
\ No newline at end of file

mercurial