summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/l-dir.lua
diff options
context:
space:
mode:
authorHans Hagen <pragma@wxs.nl>2018-06-02 23:42:05 +0200
committerContext Git Mirror Bot <phg42.2a@gmail.com>2018-06-02 23:42:05 +0200
commitac0e3262fb027e4ab586204bf2d5a05e9a831933 (patch)
treeaa43c752abe60ee65a9a9b09b32fa19ee10bd787 /tex/context/base/mkiv/l-dir.lua
parente52e58ff4a7007ca774905727391e5f78135f98d (diff)
downloadcontext-ac0e3262fb027e4ab586204bf2d5a05e9a831933.tar.gz
2018-06-02 22:41:00
Diffstat (limited to 'tex/context/base/mkiv/l-dir.lua')
-rw-r--r--tex/context/base/mkiv/l-dir.lua83
1 files changed, 46 insertions, 37 deletions
diff --git a/tex/context/base/mkiv/l-dir.lua b/tex/context/base/mkiv/l-dir.lua
index bc691d536..d13550643 100644
--- a/tex/context/base/mkiv/l-dir.lua
+++ b/tex/context/base/mkiv/l-dir.lua
@@ -75,7 +75,8 @@ function dir.current()
return (gsub(currentdir(),"\\","/"))
end
--- somewhat optimized
+-- The next one is somewhat optimized but still slow but it's a pitty that the iterator
+-- doesn't return a mode too.
local function glob_pattern_function(path,patt,recurse,action)
if isdir(path) then
@@ -89,6 +90,8 @@ local function glob_pattern_function(path,patt,recurse,action)
usedpath = path
end
local dirs
+ local nofdirs = 0
+ local noffiles = #result
for name in walkdir(usedpath) do
if name ~= "." and name ~= ".." then
local full = path .. name
@@ -98,16 +101,18 @@ local function glob_pattern_function(path,patt,recurse,action)
action(full)
end
elseif recurse and mode == "directory" then
- if not dirs then
- dirs = { full }
+ if dirs then
+ nofdirs = nofdirs + 1
+ dirs[nofdirs] = full
else
- dirs[#dirs+1] = full
+ nofdirs = 1
+ dirs = { full }
end
end
end
end
if dirs then
- for i=1,#dirs do
+ for i=1,nofdirs do
glob_pattern_function(dirs[i],patt,recurse,action)
end
end
@@ -118,38 +123,41 @@ local function glob_pattern_table(path,patt,recurse,result)
if not result then
result = { }
end
- if isdir(path) then
- local usedpath
- if path == "/" then
- usedpath = "/."
- elseif not find(path,"/$") then
- usedpath = path .. "/."
- path = path .. "/"
- else
- usedpath = path
- end
- local dirs
- for name in walkdir(usedpath) do
- if name ~= "." and name ~= ".." then
- local full = path .. name
- local mode = attributes(full,'mode')
- if mode == 'file' then
- if not patt or find(full,patt) then
- result[#result+1] = full
- end
- elseif recurse and mode == "directory" then
- if not dirs then
- dirs = { full }
- else
- dirs[#dirs+1] = full
- end
+ local usedpath
+ if path == "/" then
+ usedpath = "/."
+ elseif not find(path,"/$") then
+ usedpath = path .. "/."
+ path = path .. "/"
+ else
+ usedpath = path
+ end
+ local dirs
+ local nofdirs = 0
+ local noffiles = #result
+ for name, a in walkdir(usedpath) do
+ if name ~= "." and name ~= ".." then
+ local full = path .. name
+ local mode = attributes(full,'mode')
+ if mode == 'file' then
+ if not patt or find(full,patt) then
+ noffiles = noffiles + 1
+ result[noffiles] = full
+ end
+ elseif recurse and mode == "directory" then
+ if dirs then
+ nofdirs = nofdirs + 1
+ dirs[nofdirs] = full
+ else
+ nofdirs = 1
+ dirs = { full }
end
end
end
- if dirs then
- for i=1,#dirs do
- glob_pattern_table(dirs[i],patt,recurse,result)
- end
+ end
+ if dirs then
+ for i=1,nofdirs do
+ glob_pattern_table(dirs[i],patt,recurse,result)
end
end
return result
@@ -160,12 +168,13 @@ local function globpattern(path,patt,recurse,method)
if patt and sub(patt,1,-3) == path then
patt = false
end
+ local okay = isdir(path)
if kind == "function" then
- return glob_pattern_function(path,patt,recurse,method)
+ return okay and glob_pattern_function(path,patt,recurse,method) or { }
elseif kind == "table" then
- return glob_pattern_table(path,patt,recurse,method)
+ return okay and glob_pattern_table(path,patt,recurse,method) or method
else
- return glob_pattern_table(path,patt,recurse,{ })
+ return okay and glob_pattern_table(path,patt,recurse,{ }) or { }
end
end