summaryrefslogtreecommitdiff
path: root/tex/context/base/l-dir.lua
diff options
context:
space:
mode:
authorHans Hagen <pragma@wxs.nl>2008-03-24 23:24:00 +0100
committerHans Hagen <pragma@wxs.nl>2008-03-24 23:24:00 +0100
commit55bcf0e607bb8af553581e74293687ed635bf877 (patch)
tree328adc5c0c28d46fd49369999d0b160c3d7582f8 /tex/context/base/l-dir.lua
parent3bcf8de3c62348cb5526ddb37b3f4ea0f3d1df4f (diff)
downloadcontext-55bcf0e607bb8af553581e74293687ed635bf877.tar.gz
stable 2008.03.24 23:24
Diffstat (limited to 'tex/context/base/l-dir.lua')
-rw-r--r--tex/context/base/l-dir.lua108
1 files changed, 74 insertions, 34 deletions
diff --git a/tex/context/base/l-dir.lua b/tex/context/base/l-dir.lua
index 864e207b6..0cc913d13 100644
--- a/tex/context/base/l-dir.lua
+++ b/tex/context/base/l-dir.lua
@@ -12,18 +12,68 @@ dir = { }
if lfs then do
+--~ local attributes = lfs.attributes
+--~ local walkdir = lfs.dir
+--~
+--~ local function glob_pattern(path,patt,recurse,action)
+--~ local ok, scanner = xpcall(function() return walkdir(path) end, function() end) -- kepler safe
+--~ if ok and type(scanner) == "function" then
+--~ if not path:find("/$") then path = path .. '/' end
+--~ for name in scanner do
+--~ local full = path .. name
+--~ local mode = attributes(full,'mode')
+--~ if mode == 'file' then
+--~ if name:find(patt) then
+--~ action(full)
+--~ end
+--~ elseif recurse and (mode == "directory") and (name ~= '.') and (name ~= "..") then
+--~ glob_pattern(full,patt,recurse,action)
+--~ end
+--~ end
+--~ end
+--~ end
+--~
+--~ dir.glob_pattern = glob_pattern
+--~
+--~ local function glob(pattern, action)
+--~ local t = { }
+--~ local action = action or function(name) t[#t+1] = name end
+--~ local path, patt = pattern:match("^(.*)/*%*%*/*(.-)$")
+--~ local recurse = path and patt
+--~ if not recurse then
+--~ path, patt = pattern:match("^(.*)/(.-)$")
+--~ if not (path and patt) then
+--~ path, patt = '.', pattern
+--~ end
+--~ end
+--~ patt = patt:gsub("([%.%-%+])", "%%%1")
+--~ patt = patt:gsub("%*", ".*")
+--~ patt = patt:gsub("%?", ".")
+--~ patt = "^" .. patt .. "$"
+--~ -- print('path: ' .. path .. ' | pattern: ' .. patt .. ' | recurse: ' .. tostring(recurse))
+--~ glob_pattern(path,patt,recurse,action)
+--~ return t
+--~ end
+--~
+--~ dir.glob = glob
+
local attributes = lfs.attributes
local walkdir = lfs.dir
local function glob_pattern(path,patt,recurse,action)
- local ok, scanner = xpcall(function() return walkdir(path) end, function() end) -- kepler safe
+ local ok, scanner
+ if path == "/" then
+ ok, scanner = xpcall(function() return walkdir(path..".") end, function() end) -- kepler safe
+ else
+ ok, scanner = xpcall(function() return walkdir(path) end, function() end) -- kepler safe
+ end
if ok and type(scanner) == "function" then
if not path:find("/$") then path = path .. '/' end
for name in scanner do
local full = path .. name
local mode = attributes(full,'mode')
if mode == 'file' then
- if name:find(patt) then
+ if full:find(patt) then
action(full)
end
elseif recurse and (mode == "directory") and (name ~= '.') and (name ~= "..") then
@@ -37,29 +87,36 @@ if lfs then do
local function glob(pattern, action)
local t = { }
- local action = action or function(name) table.insert(t,name) end
- local path, patt = pattern:match("^(.*)/*%*%*/*(.-)$")
- local recurse = path and patt
- if not recurse then
- path, patt = pattern:match("^(.*)/(.-)$")
- if not (path and patt) then
- path, patt = '.', pattern
- end
+ local path, rest, patt, recurse
+ local action = action or function(name) t[#t+1] = name end
+ local pattern = pattern:gsub("^%*%*","./**")
+ local pattern = pattern:gsub("/%*/","/**/")
+ path, rest = pattern:match("^(/)(.-)$")
+ if path then
+ path = path
+ else
+ path, rest = pattern:match("^([^/]*)/(.-)$")
end
- patt = patt:gsub("([%.%-%+])", "%%%1")
- patt = patt:gsub("%*", ".*")
- patt = patt:gsub("%?", ".")
- patt = "^" .. patt .. "$"
- -- print('path: ' .. path .. ' | pattern: ' .. patt .. ' | recurse: ' .. tostring(recurse))
+ patt = rest:gsub("([%.%-%+])", "%%%1")
+ patt = patt:gsub("%*", "[^/]*")
+ patt = patt:gsub("%?", "[^/]")
+ patt = patt:gsub("%[%^/%]%*%[%^/%]%*", ".*")
+ if path == "" then path = "." end
+ -- print(pattern, path, patt)
+ recurse = patt:find("%.%*/")
glob_pattern(path,patt,recurse,action)
return t
end
dir.glob = glob
- -- todo: speedup
+ --~ list = dir.glob("**/*.tif")
+ --~ list = dir.glob("/**/*.tif")
+ --~ list = dir.glob("./**/*.tif")
+ --~ list = dir.glob("oeps/**/*.tif")
+ --~ list = dir.glob("/oeps/**/*.tif")
- local function globfiles(path,recurse,func,files)
+ local function globfiles(path,recurse,func,files) -- func == pattern or function
if type(func) == "string" then
local s = func -- alas, we need this indirect way
func = function(name) return name:find(s) end
@@ -101,23 +158,6 @@ if lfs then do
--~ mkdirs(".","/a/b/c")
--~ mkdirs("a","b","c")
---~ function dir.mkdirs(...)
---~ local pth, err, lst = "", false, table.concat({...},"/")
---~ for _, s in ipairs(lst:split("/")) do
---~ if pth == "" then
---~ pth = (s == "" and "/") or s
---~ else
---~ pth = pth .. "/" .. s
---~ end
---~ if s == "" then
---~ -- can be network path
---~ elseif not lfs.isdir(pth) then
---~ lfs.mkdir(pth)
---~ end
---~ end
---~ return pth, not err
---~ end
-
local make_indeed = true -- false
if string.find(os.getenv("PATH"),";") then