2 -- This file is included with Prosody IM. It has modifications,
3 -- which are hereby placed in the public domain.
5 -- public domain 20080410 lua@ztact.com
8 pcall (require, 'lfs') -- lfs may not be installed/necessary.
9 pcall (require, 'pozix') -- pozix may not be installed/necessary.
12 local getfenv, ipairs, next, pairs, pcall, require, select, tostring, type =
13 getfenv, ipairs, next, pairs, pcall, require, select, tostring, type
14 local unpack, xpcall =
17 local io, lfs, os, string, table, pozix = io, lfs, os, string, table, pozix
19 local assert, print = assert, print
24 module ((...) or 'ztact') ------------------------------------- module ztact
27 -- dir -------------------------------------------------------------------- dir
30 function dir (path) -- - - - - - - - - - - - - - - - - - - - - - - - - - dir
31 local it = lfs.dir (path)
35 if dir ~= '.' and dir ~= '..' then return dir end
40 function is_file (path) -- - - - - - - - - - - - - - - - - - is_file (path)
41 local mode = lfs.attributes (path, 'mode')
42 return mode == 'file' and path
46 -- network byte ordering -------------------------------- network byte ordering
49 function htons (word) -- - - - - - - - - - - - - - - - - - - - - - - - htons
50 return (word-word%0x100)/0x100, word%0x100
54 -- pcall2 -------------------------------------------------------------- pcall2
57 getfenv ().pcall = pcall -- store the original pcall as ztact.pcall
60 local argc, argv, errorhandler, pcall2_f
63 local function _pcall2 () -- - - - - - - - - - - - - - - - - - - - - _pcall2
66 return pcall2_f (unpack (tmpv, 1, argc))
70 function seterrorhandler (func) -- - - - - - - - - - - - - - seterrorhandler
75 function pcall2 (f, ...) -- - - - - - - - - - - - - - - - - - - - - - pcall2
78 argc = select ('#', ...)
81 if not errorhandler then
82 local debug = require ('debug')
83 errorhandler = debug.traceback
86 return xpcall (_pcall2, errorhandler)
90 function append (t, ...) -- - - - - - - - - - - - - - - - - - - - - - append
91 local insert = table.insert
92 for i,v in ipairs {...} do
97 function print_r (d, indent) -- - - - - - - - - - - - - - - - - - - print_r
98 local rep = string.rep (' ', indent or 0)
99 if type (d) == 'table' then
100 for k,v in pairs (d) do
101 if type (v) == 'table' then
102 io.write (rep, k, '\n')
103 print_r (v, (indent or 0) + 1)
104 else io.write (rep, k, ' = ', tostring (v), '\n') end
106 else io.write (d, '\n') end
110 function tohex (s) -- - - - - - - - - - - - - - - - - - - - - - - - - tohex
111 return string.format (string.rep ('%02x ', #s), string.byte (s, 1, #s))
115 function tostring_r (d, indent, tab0) -- - - - - - - - - - - - - tostring_r
117 local tab1 = tab0 or {}
118 local rep = string.rep (' ', indent or 0)
119 if type (d) == 'table' then
120 for k,v in pairs (d) do
121 if type (v) == 'table' then
122 append (tab1, rep, k, '\n')
123 tostring_r (v, (indent or 0) + 1, tab1)
124 else append (tab1, rep, k, ' = ', tostring (v), '\n') end
126 else append (tab1, d, '\n') end
128 if not tab0 then return table.concat (tab1) end
132 -- queue manipulation -------------------------------------- queue manipulation
135 -- Possible queue states. 1 (i.e. queue.p[1]) is head of queue.
144 local function print_queue (queue, ...) -- - - - - - - - - - - - print_queue
145 for i=1,10 do io.write ((queue[i] or '.')..' ') end
147 for i=1,6 do io.write ((queue.p[i] or '.')..' ') end
152 function dequeue (queue) -- - - - - - - - - - - - - - - - - - - - - dequeue
155 if not p and queue[1] then queue.p = { 1, #queue } p = queue.p end
157 if not p[1] then return nil end
159 local element = queue[p[1]]
162 if p[1] < p[2] then p[1] = p[1] + 1
164 elseif p[4] then p[1], p[2], p[3], p[4] = p[3], p[4], nil, nil
166 elseif p[5] then p[1], p[2], p[5], p[6] = p[5], p[6], nil, nil
168 else p[1], p[2] = nil, nil end
170 print_queue (queue, ' de '..element)
175 function enqueue (queue, element) -- - - - - - - - - - - - - - - - - enqueue
178 if not p then queue.p = {} p = queue.p end
180 if p[5] then -- p3..p4 p1..p2 p5..p6
182 queue[p[6]] = element
184 elseif p[3] then -- p3..p4 p1..p2
186 if p[4]+1 < p[1] then
188 queue[p[4]] = element
192 p[6], queue[p[5]] = p[5], element
195 elseif p[1] then -- p1..p2
198 queue[p[2]] = element
201 p[3], p[4], queue[1] = 1, 1, element
205 p[1], p[2], queue[1] = 1, 1, element
208 print_queue (queue, ' '..element)
212 local function test_queue ()
248 function queue_len (queue)
252 function queue_peek (queue)
256 -- tree manipulation ---------------------------------------- tree manipulation
259 function set (parent, ...) --- - - - - - - - - - - - - - - - - - - - - - set
261 -- print ('set', ...)
263 local len = select ('#', ...)
264 local key, value = select (len-1, ...)
265 local cutpoint, cutkey
269 local key = select (i, ...)
270 local child = parent[key]
273 if child == nil then return
274 elseif next (child, next (child)) then cutpoint = nil cutkey = nil
275 elseif cutpoint == nil then cutpoint = parent cutkey = key end
277 elseif child == nil then child = {} parent[key] = child end
282 if value == nil and cutpoint then cutpoint[cutkey] = nil
283 else parent[key] = value return value end
287 function get (parent, ...) --- - - - - - - - - - - - - - - - - - - - - - get
288 local len = select ('#', ...)
290 parent = parent[select (i, ...)]
291 if parent == nil then break end
297 -- misc ------------------------------------------------------------------ misc
300 function find (path, ...) --------------------------------------------- find
302 local dirs, operators = { path }, {...}
303 for operator in ivalues (operators) do
304 if not operator (path) then break end end
307 local parent = table.remove (dirs)
308 for child in assert (pozix.opendir (parent)) do
309 if child and child ~= '.' and child ~= '..' then
310 local path = parent..'/'..child
311 if pozix.stat (path, 'is_dir') then table.insert (dirs, path) end
312 for operator in ivalues (operators) do
313 if not operator (path) then break end end
317 function ivalues (t) ----------------------------------------------- ivalues
319 return function () if t[i+1] then i = i + 1 return t[i] end end
323 function lson_encode (mixed, f, indent, indents) --------------- lson_encode
329 f = function (s) append (capture, s) end
333 indents = indents or {}
334 indents[indent] = indents[indent] or string.rep (' ', 2*indent)
336 local type = type (mixed)
338 if type == 'number' then f (mixed)
340 else if type == 'string' then f (string.format ('%q', mixed))
342 else if type == 'table' then
344 for k,v in pairs (mixed) do
347 f ('[') f (lson_encode (k)) f ('] = ')
348 lson_encode (v, f, indent+1, indents)
354 if capture then return table.concat (capture) end
358 function timestamp (time) ---------------------------------------- timestamp
359 return os.date ('%Y%m%d.%H%M%S', time)
363 function values (t) ------------------------------------------------- values
365 return function () k, v = next (t, k) return v end