2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 -- public domain 20080410 lua@ztact.com
26 pcall (require, 'lfs') -- lfs may not be installed/necessary.
27 pcall (require, 'pozix') -- pozix may not be installed/necessary.
30 local getfenv, ipairs, next, pairs, pcall, require, select, tostring, type =
31 getfenv, ipairs, next, pairs, pcall, require, select, tostring, type
32 local unpack, xpcall =
35 local io, lfs, os, string, table, pozix = io, lfs, os, string, table, pozix
37 local assert, print = assert, print
42 module ((...) or 'ztact') ------------------------------------- module ztact
45 -- dir -------------------------------------------------------------------- dir
48 function dir (path) -- - - - - - - - - - - - - - - - - - - - - - - - - - dir
49 local it = lfs.dir (path)
53 if dir ~= '.' and dir ~= '..' then return dir end
58 function is_file (path) -- - - - - - - - - - - - - - - - - - is_file (path)
59 local mode = lfs.attributes (path, 'mode')
60 return mode == 'file' and path
64 -- network byte ordering -------------------------------- network byte ordering
67 function htons (word) -- - - - - - - - - - - - - - - - - - - - - - - - htons
68 return (word-word%0x100)/0x100, word%0x100
72 -- pcall2 -------------------------------------------------------------- pcall2
75 getfenv ().pcall = pcall -- store the original pcall as ztact.pcall
78 local argc, argv, errorhandler, pcall2_f
81 local function _pcall2 () -- - - - - - - - - - - - - - - - - - - - - _pcall2
84 return pcall2_f (unpack (tmpv, 1, argc))
88 function seterrorhandler (func) -- - - - - - - - - - - - - - seterrorhandler
93 function pcall2 (f, ...) -- - - - - - - - - - - - - - - - - - - - - - pcall2
96 argc = select ('#', ...)
99 if not errorhandler then
100 local debug = require ('debug')
101 errorhandler = debug.traceback
104 return xpcall (_pcall2, errorhandler)
108 function append (t, ...) -- - - - - - - - - - - - - - - - - - - - - - append
109 local insert = table.insert
110 for i,v in ipairs {...} do
115 function print_r (d, indent) -- - - - - - - - - - - - - - - - - - - print_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 io.write (rep, k, '\n')
121 print_r (v, (indent or 0) + 1)
122 else io.write (rep, k, ' = ', tostring (v), '\n') end
124 else io.write (d, '\n') end
128 function tohex (s) -- - - - - - - - - - - - - - - - - - - - - - - - - tohex
129 return string.format (string.rep ('%02x ', #s), string.byte (s, 1, #s))
133 function tostring_r (d, indent, tab0) -- - - - - - - - - - - - - tostring_r
136 local rep = string.rep (' ', indent or 0)
137 if type (d) == 'table' then
138 for k,v in pairs (d) do
139 if type (v) == 'table' then
140 append (tab1, rep, k, '\n')
141 tostring_r (v, (indent or 0) + 1, tab1)
142 else append (tab1, rep, k, ' = ', tostring (v), '\n') end
144 else append (tab1, d, '\n') end
146 if not tab0 then return table.concat (tab1) end
150 -- queue manipulation -------------------------------------- queue manipulation
153 -- Possible queue states. 1 (i.e. queue.p[1]) is head of queue.
162 local function print_queue (queue, ...) -- - - - - - - - - - - - print_queue
163 for i=1,10 do io.write ((queue[i] or '.')..' ') end
165 for i=1,6 do io.write ((queue.p[i] or '.')..' ') end
170 function dequeue (queue) -- - - - - - - - - - - - - - - - - - - - - dequeue
173 if not p and queue[1] then queue.p = { 1, #queue } p = queue.p end
175 if not p[1] then return nil end
177 local element = queue[p[1]]
180 if p[1] < p[2] then p[1] = p[1] + 1
182 elseif p[4] then p[1], p[2], p[3], p[4] = p[3], p[4], nil, nil
184 elseif p[5] then p[1], p[2], p[5], p[6] = p[5], p[6], nil, nil
186 else p[1], p[2] = nil, nil end
188 print_queue (queue, ' de '..element)
193 function enqueue (queue, element) -- - - - - - - - - - - - - - - - - enqueue
196 if not p then queue.p = {} p = queue.p end
198 if p[5] then -- p3..p4 p1..p2 p5..p6
200 queue[p[6]] = element
202 elseif p[3] then -- p3..p4 p1..p2
204 if p[4]+1 < p[1] then
206 queue[p[4]] = element
210 p[6], queue[p[5]] = p[5], element
213 elseif p[1] then -- p1..p2
216 queue[p[2]] = element
219 p[3], p[4], queue[1] = 1, 1, element
223 p[1], p[2], queue[1] = 1, 1, element
226 print_queue (queue, ' '..element)
230 local function test_queue ()
266 function queue_len (queue)
270 function queue_peek (queue)
274 -- tree manipulation ---------------------------------------- tree manipulation
277 function set (parent, ...) --- - - - - - - - - - - - - - - - - - - - - - set
279 -- print ('set', ...)
281 local len = select ('#', ...)
282 local key, value = select (len-1, ...)
283 local cutpoint, cutkey
287 local key = select (i, ...)
288 local child = parent[key]
291 if child == nil then return
292 elseif next (child, next (child)) then cutpoint = nil cutkey = nil
293 elseif cutpoint == nil then cutpoint = parent cutkey = key end
295 elseif child == nil then child = {} parent[key] = child end
300 if value == nil and cutpoint then cutpoint[cutkey] = nil
301 else parent[key] = value return value end
305 function get (parent, ...) --- - - - - - - - - - - - - - - - - - - - - - get
306 local len = select ('#', ...)
308 parent = parent[select (i, ...)]
309 if parent == nil then break end
315 -- misc ------------------------------------------------------------------ misc
318 function find (path, ...) --------------------------------------------- find
320 local dirs, operators = { path }, {...}
321 for operator in ivalues (operators) do
322 if not operator (path) then break end end
325 local parent = table.remove (dirs)
326 for child in assert (pozix.opendir (parent)) do
327 if child and child ~= '.' and child ~= '..' then
328 local path = parent..'/'..child
329 if pozix.stat (path, 'is_dir') then table.insert (dirs, path) end
330 for operator in ivalues (operators) do
331 if not operator (path) then break end end
335 function ivalues (t) ----------------------------------------------- ivalues
337 return function () if t[i+1] then i = i + 1 return t[i] end end
341 function lson_encode (mixed, f, indent, indents) --------------- lson_encode
347 f = function (s) append (capture, s) end
351 indents = indents or {}
352 indents[indent] = indents[indent] or string.rep (' ', 2*indent)
354 local type = type (mixed)
356 if type == 'number' then f (mixed)
358 else if type == 'string' then f (string.format ('%q', mixed))
360 else if type == 'table' then
362 for k,v in pairs (mixed) do
365 f ('[') f (lson_encode (k)) f ('] = ')
366 lson_encode (v, f, indent+1, indents)
372 if capture then return table.concat (capture) end
376 function timestamp (time) ---------------------------------------- timestamp
377 return os.date ('%Y%m%d.%H%M%S', time)
381 function values (t) ------------------------------------------------- values
383 return function () k, v = next (t, k) return v end