From 77b453ae2a132853da3e64286d612af49147b30f Mon Sep 17 00:00:00 2001
From: Hans Hagen <pragma@wxs.nl>
Date: Mon, 17 Dec 2007 14:54:00 +0100
Subject: stable 2007.12.17 14:54

---
 scripts/context/lua/luatools.lua | 51 +++++++++++++++++++++++++++++-----------
 scripts/context/lua/mtxrun.lua   | 51 +++++++++++++++++++++++++++++-----------
 2 files changed, 74 insertions(+), 28 deletions(-)

(limited to 'scripts')

diff --git a/scripts/context/lua/luatools.lua b/scripts/context/lua/luatools.lua
index 26902a4bc..abfc345e4 100644
--- a/scripts/context/lua/luatools.lua
+++ b/scripts/context/lua/luatools.lua
@@ -1330,6 +1330,17 @@ function number.toset(n)
     return (tostring(n)):match("(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)")
 end
 
+local format = string.format
+
+function number.toevenhex(n)
+    local s = format("%X",n)
+    if #s % 2 == 0 then
+        return s
+    else
+        return "0" .. s
+    end
+end
+
 -- the lpeg way is slower on 8 digits, but faster on 4 digits, some 7.5%
 -- on
 --
@@ -1568,26 +1579,32 @@ dir = { }
 
 -- optimizing for no string.find (*) does not save time
 
-if lfs then
+if lfs then do
+
+    local attributes = lfs.attributes
+    local walkdir    = lfs.dir
 
-    function dir.glob_pattern(path,patt,recurse,action)
-        local ok, scanner = xpcall(function() return lfs.dir(path) end, function() end) -- kepler safe
+    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 = lfs.attributes(full,'mode')
+                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
-                    dir.glob_pattern(full,patt,recurse,action)
+                    glob_pattern(full,patt,recurse,action)
                 end
             end
         end
     end
 
-    function dir.glob(pattern, action)
+    dir.glob_pattern = glob_pattern
+
+    local function glob(pattern, action)
         local t = { }
         local action = action or function(name) table.insert(t,name) end
         local path, patt = pattern:match("^(.*)/*%*%*/*(.-)$")
@@ -1603,22 +1620,26 @@ if lfs then
         patt = patt:gsub("%?", ".")
         patt = "^" .. patt .. "$"
      -- print('path: ' .. path .. ' | pattern: ' .. patt .. ' | recurse: ' .. tostring(recurse))
-        dir.glob_pattern(path,patt,recurse,action)
+        glob_pattern(path,patt,recurse,action)
         return t
     end
 
-    function dir.globfiles(path,recurse,func,files)
+    dir.glob = glob
+
+    -- todo: speedup
+
+    local function globfiles(path,recurse,func,files)
         if type(func) == "string" then
             local s = func -- alas, we need this indirect way
             func = function(name) return name:find(s) end
         end
         files = files or { }
-        for name in lfs.dir(path) do
+        for name in walkdir(path) do
             if name:find("^%.") then
                 --- skip
-            elseif lfs.attributes(name,'mode') == "directory" then
+            elseif attributes(name,'mode') == "directory" then
                 if recurse then
-                    dir.globfiles(path .. "/" .. name,recurse,func,files)
+                    globfiles(path .. "/" .. name,recurse,func,files)
                 end
             elseif func then
                 if func(name) then
@@ -1631,6 +1652,8 @@ if lfs then
         return files
     end
 
+    dir.globfiles = globfiles
+
     -- t = dir.glob("c:/data/develop/context/sources/**/????-*.tex")
     -- t = dir.glob("c:/data/develop/tex/texmf/**/*.tex")
     -- t = dir.glob("c:/data/develop/context/texmf/**/*.tex")
@@ -1639,7 +1662,7 @@ if lfs then
     -- print(dir.ls("*.tex"))
 
     function dir.ls(pattern)
-        return table.concat(dir.glob(pattern),"\n")
+        return table.concat(glob(pattern),"\n")
     end
 
     --~ mkdirs("temp")
@@ -1666,7 +1689,7 @@ if lfs then
 
     dir.makedirs = dir.mkdirs
 
-end
+end end
 
 
 -- filename : l-boolean.lua
diff --git a/scripts/context/lua/mtxrun.lua b/scripts/context/lua/mtxrun.lua
index 5a0e3aaeb..011ee8bb2 100644
--- a/scripts/context/lua/mtxrun.lua
+++ b/scripts/context/lua/mtxrun.lua
@@ -1365,6 +1365,17 @@ function number.toset(n)
     return (tostring(n)):match("(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)")
 end
 
+local format = string.format
+
+function number.toevenhex(n)
+    local s = format("%X",n)
+    if #s % 2 == 0 then
+        return s
+    else
+        return "0" .. s
+    end
+end
+
 -- the lpeg way is slower on 8 digits, but faster on 4 digits, some 7.5%
 -- on
 --
@@ -1583,26 +1594,32 @@ dir = { }
 
 -- optimizing for no string.find (*) does not save time
 
-if lfs then
+if lfs then do
+
+    local attributes = lfs.attributes
+    local walkdir    = lfs.dir
 
-    function dir.glob_pattern(path,patt,recurse,action)
-        local ok, scanner = xpcall(function() return lfs.dir(path) end, function() end) -- kepler safe
+    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 = lfs.attributes(full,'mode')
+                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
-                    dir.glob_pattern(full,patt,recurse,action)
+                    glob_pattern(full,patt,recurse,action)
                 end
             end
         end
     end
 
-    function dir.glob(pattern, action)
+    dir.glob_pattern = glob_pattern
+
+    local function glob(pattern, action)
         local t = { }
         local action = action or function(name) table.insert(t,name) end
         local path, patt = pattern:match("^(.*)/*%*%*/*(.-)$")
@@ -1618,22 +1635,26 @@ if lfs then
         patt = patt:gsub("%?", ".")
         patt = "^" .. patt .. "$"
      -- print('path: ' .. path .. ' | pattern: ' .. patt .. ' | recurse: ' .. tostring(recurse))
-        dir.glob_pattern(path,patt,recurse,action)
+        glob_pattern(path,patt,recurse,action)
         return t
     end
 
-    function dir.globfiles(path,recurse,func,files)
+    dir.glob = glob
+
+    -- todo: speedup
+
+    local function globfiles(path,recurse,func,files)
         if type(func) == "string" then
             local s = func -- alas, we need this indirect way
             func = function(name) return name:find(s) end
         end
         files = files or { }
-        for name in lfs.dir(path) do
+        for name in walkdir(path) do
             if name:find("^%.") then
                 --- skip
-            elseif lfs.attributes(name,'mode') == "directory" then
+            elseif attributes(name,'mode') == "directory" then
                 if recurse then
-                    dir.globfiles(path .. "/" .. name,recurse,func,files)
+                    globfiles(path .. "/" .. name,recurse,func,files)
                 end
             elseif func then
                 if func(name) then
@@ -1646,6 +1667,8 @@ if lfs then
         return files
     end
 
+    dir.globfiles = globfiles
+
     -- t = dir.glob("c:/data/develop/context/sources/**/????-*.tex")
     -- t = dir.glob("c:/data/develop/tex/texmf/**/*.tex")
     -- t = dir.glob("c:/data/develop/context/texmf/**/*.tex")
@@ -1654,7 +1677,7 @@ if lfs then
     -- print(dir.ls("*.tex"))
 
     function dir.ls(pattern)
-        return table.concat(dir.glob(pattern),"\n")
+        return table.concat(glob(pattern),"\n")
     end
 
     --~ mkdirs("temp")
@@ -1681,7 +1704,7 @@ if lfs then
 
     dir.makedirs = dir.mkdirs
 
-end
+end end
 
 
 -- filename : l-boolean.lua
-- 
cgit v1.2.3