3 -- public domain 20080410 lua@ztact.com
6 pcall (require, 'lfs') -- lfs may not be installed/necessary.
7 pcall (require, 'pozix') -- pozix may not be installed/necessary.
10 local getfenv, ipairs, next, pairs, pcall, require, select, tostring, type =
11 getfenv, ipairs, next, pairs, pcall, require, select, tostring, type
12 local unpack, xpcall =
15 local io, lfs, os, string, table, pozix = io, lfs, os, string, table, pozix
17 local assert, print = assert, print
22 module ((...) or 'ztact') ------------------------------------- module ztact
25 -- dir -------------------------------------------------------------------- dir
28 function dir (path) -- - - - - - - - - - - - - - - - - - - - - - - - - - dir
29 local it = lfs.dir (path)
33 if dir ~= '.' and dir ~= '..' then return dir end
38 function is_file (path) -- - - - - - - - - - - - - - - - - - is_file (path)
39 local mode = lfs.attributes (path, 'mode')
40 return mode == 'file' and path
44 -- network byte ordering -------------------------------- network byte ordering
47 function htons (word) -- - - - - - - - - - - - - - - - - - - - - - - - htons
48 return (word-word%0x100)/0x100, word%0x100
52 -- pcall2 -------------------------------------------------------------- pcall2
55 getfenv ().pcall = pcall -- store the original pcall as ztact.pcall
58 local argc, argv, errorhandler, pcall2_f
61 local function _pcall2 () -- - - - - - - - - - - - - - - - - - - - - _pcall2
64 return pcall2_f (unpack (tmpv, 1, argc))
68 function seterrorhandler (func) -- - - - - - - - - - - - - - seterrorhandler
73 function pcall2 (f, ...) -- - - - - - - - - - - - - - - - - - - - - - pcall2
76 argc = select ('#', ...)
79 if not errorhandler then
80 local debug = require ('debug')
81 errorhandler = debug.traceback
84 return xpcall (_pcall2, errorhandler)
88 function append (t, ...) -- - - - - - - - - - - - - - - - - - - - - - append
89 local insert = table.insert
90 for i,v in ipairs {...} do
95 function print_r (d, indent) -- - - - - - - - - - - - - - - - - - - print_r
96 local rep = string.rep (' ', indent or 0)
97 if type (d) == 'table' then
98 for k,v in pairs (d) do
99 if type (v) == 'table' then
100 io.write (rep, k, '\n')
101 print_r (v, (indent or 0) + 1)
102 else io.write (rep, k, ' = ', tostring (v), '\n') end
104 else io.write (d, '\n') end
108 function tohex (s) -- - - - - - - - - - - - - - - - - - - - - - - - - tohex
109 return string.format (string.rep ('%02x ', #s), string.byte (s, 1, #s))
113 function tostring_r (d, indent, tab0) -- - - - - - - - - - - - - tostring_r
116 local rep = string.rep (' ', indent or 0)
117 if type (d) == 'table' then
118 for k,v in pairs (d) do
119 if type (v) == 'table' then
120 append (tab1, rep, k, '\n')
121 tostring_r (v, (indent or 0) + 1, tab1)
122 else append (tab1, rep, k, ' = ', tostring (v), '\n') end
124 else append (tab1, d, '\n') end
126 if not tab0 then return table.concat (tab1) end
130 -- queue manipulation -------------------------------------- queue manipulation
133 -- Possible queue states. 1 (i.e. queue.p[1]) is head of queue.
142 local function print_queue (queue, ...) -- - - - - - - - - - - - print_queue
143 for i=1,10 do io.write ((queue[i] or '.')..' ') end
145 for i=1,6 do io.write ((queue.p[i] or '.')..' ') end
150 function dequeue (queue) -- - - - - - - - - - - - - - - - - - - - - dequeue
153 if not p and queue[1] then queue.p = { 1, #queue } p = queue.p end
155 if not p[1] then return nil end
157 local element = queue[p[1]]
160 if p[1] < p[2] then p[1] = p[1] + 1
162 elseif p[4] then p[1], p[2], p[3], p[4] = p[3], p[4], nil, nil
164 elseif p[5] then p[1], p[2], p[5], p[6] = p[5], p[6], nil, nil
166 else p[1], p[2] = nil, nil end
168 print_queue (queue, ' de '..element)
173 function enqueue (queue, element) -- - - - - - - - - - - - - - - - - enqueue
176 if not p then queue.p = {} p = queue.p end
178 if p[5] then -- p3..p4 p1..p2 p5..p6
180 queue[p[6]] = element
182 elseif p[3] then -- p3..p4 p1..p2
184 if p[4]+1 < p[1] then
186 queue[p[4]] = element
190 p[6], queue[p[5]] = p[5], element
193 elseif p[1] then -- p1..p2
196 queue[p[2]] = element
199 p[3], p[4], queue[1] = 1, 1, element
203 p[1], p[2], queue[1] = 1, 1, element
206 print_queue (queue, ' '..element)
210 local function test_queue ()
246 function queue_len (queue)
250 function queue_peek (queue)
254 -- tree manipulation ---------------------------------------- tree manipulation
257 function set (parent, ...) --- - - - - - - - - - - - - - - - - - - - - - set
259 -- print ('set', ...)
261 local len = select ('#', ...)
262 local key, value = select (len-1, ...)
263 local cutpoint, cutkey
267 local key = select (i, ...)
268 local child = parent[key]
271 if child == nil then return
272 elseif next (child, next (child)) then cutpoint = nil cutkey = nil
273 elseif cutpoint == nil then cutpoint = parent cutkey = key end
275 elseif child == nil then child = {} parent[key] = child end
280 if value == nil and cutpoint then cutpoint[cutkey] = nil
281 else parent[key] = value return value end
285 function get (parent, ...) --- - - - - - - - - - - - - - - - - - - - - - get
286 local len = select ('#', ...)
288 parent = parent[select (i, ...)]
289 if parent == nil then break end
295 -- misc ------------------------------------------------------------------ misc
298 function find (path, ...) --------------------------------------------- find
300 local dirs, operators = { path }, {...}
301 for operator in ivalues (operators) do
302 if not operator (path) then break end end
305 local parent = table.remove (dirs)
306 for child in assert (pozix.opendir (parent)) do
307 if child and child ~= '.' and child ~= '..' then
308 local path = parent..'/'..child
309 if pozix.stat (path, 'is_dir') then table.insert (dirs, path) end
310 for operator in ivalues (operators) do
311 if not operator (path) then break end end
315 function ivalues (t) ----------------------------------------------- ivalues
317 return function () if t[i+1] then i = i + 1 return t[i] end end
321 function lson_encode (mixed, f, indent, indents) --------------- lson_encode
327 f = function (s) append (capture, s) end
331 indents = indents or {}
332 indents[indent] = indents[indent] or string.rep (' ', 2*indent)
334 local type = type (mixed)
336 if type == 'number' then f (mixed)
338 else if type == 'string' then f (string.format ('%q', mixed))
340 else if type == 'table' then
342 for k,v in pairs (mixed) do
345 f ('[') f (lson_encode (k)) f ('] = ')
346 lson_encode (v, f, indent+1, indents)
352 if capture then return table.concat (capture) end
356 function timestamp (time) ---------------------------------------- timestamp
357 return os.date ('%Y%m%d.%H%M%S', time)
361 function values (t) ------------------------------------------------- values
363 return function () k, v = next (t, k) return v end