Annotate

util/ztact.lua @ 417:d60e63379284

Removed commented code
author Waqas Hussain <waqas20@gmail.com>
date Wed, 26 Nov 2008 01:55:07 +0500
parent 337:4a1dd1c2c219
child 519:cccd610a0ef9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
337
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- public domain 20080410 lua@ztact.com
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 pcall (require, 'lfs') -- lfs may not be installed/necessary.
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 pcall (require, 'pozix') -- pozix may not be installed/necessary.
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local getfenv, ipairs, next, pairs, pcall, require, select, tostring, type =
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 getfenv, ipairs, next, pairs, pcall, require, select, tostring, type
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local unpack, xpcall =
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 unpack, xpcall
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local io, lfs, os, string, table, pozix = io, lfs, os, string, table, pozix
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local assert, print = assert, print
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local error = error
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 module ((...) or 'ztact') ------------------------------------- module ztact
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 -- dir -------------------------------------------------------------------- dir
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 function dir (path) -- - - - - - - - - - - - - - - - - - - - - - - - - - dir
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 local it = lfs.dir (path)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 return function ()
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 repeat
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 local dir = it ()
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 if dir ~= '.' and dir ~= '..' then return dir end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 until not dir
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 end end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 function is_file (path) -- - - - - - - - - - - - - - - - - - is_file (path)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 local mode = lfs.attributes (path, 'mode')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 return mode == 'file' and path
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 -- network byte ordering -------------------------------- network byte ordering
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 function htons (word) -- - - - - - - - - - - - - - - - - - - - - - - - htons
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 return (word-word%0x100)/0x100, word%0x100
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 -- pcall2 -------------------------------------------------------------- pcall2
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 getfenv ().pcall = pcall -- store the original pcall as ztact.pcall
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 local argc, argv, errorhandler, pcall2_f
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 local function _pcall2 () -- - - - - - - - - - - - - - - - - - - - - _pcall2
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 local tmpv = argv
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 argv = nil
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 return pcall2_f (unpack (tmpv, 1, argc))
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 function seterrorhandler (func) -- - - - - - - - - - - - - - seterrorhandler
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 errorhandler = func
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 function pcall2 (f, ...) -- - - - - - - - - - - - - - - - - - - - - - pcall2
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 pcall2_f = f
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 argc = select ('#', ...)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 argv = { ... }
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 if not errorhandler then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 local debug = require ('debug')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 errorhandler = debug.traceback
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 return xpcall (_pcall2, errorhandler)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 function append (t, ...) -- - - - - - - - - - - - - - - - - - - - - - append
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 local insert = table.insert
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 for i,v in ipairs {...} do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 insert (t, v)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 end end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 function print_r (d, indent) -- - - - - - - - - - - - - - - - - - - print_r
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 local rep = string.rep (' ', indent or 0)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 if type (d) == 'table' then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 for k,v in pairs (d) do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 if type (v) == 'table' then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 io.write (rep, k, '\n')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 print_r (v, (indent or 0) + 1)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 else io.write (rep, k, ' = ', tostring (v), '\n') end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 else io.write (d, '\n') end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 function tohex (s) -- - - - - - - - - - - - - - - - - - - - - - - - - tohex
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 return string.format (string.rep ('%02x ', #s), string.byte (s, 1, #s))
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 function tostring_r (d, indent, tab0) -- - - - - - - - - - - - - tostring_r
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 tab1 = tab0 or {}
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 local rep = string.rep (' ', indent or 0)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 if type (d) == 'table' then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 for k,v in pairs (d) do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 if type (v) == 'table' then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 append (tab1, rep, k, '\n')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 tostring_r (v, (indent or 0) + 1, tab1)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 else append (tab1, rep, k, ' = ', tostring (v), '\n') end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 else append (tab1, d, '\n') end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 if not tab0 then return table.concat (tab1) end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 -- queue manipulation -------------------------------------- queue manipulation
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 -- Possible queue states. 1 (i.e. queue.p[1]) is head of queue.
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 --
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 -- 1..2
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 -- 3..4 1..2
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 -- 3..4 1..2 5..6
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 -- 1..2 5..6
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 -- 1..2
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 local function print_queue (queue, ...) -- - - - - - - - - - - - print_queue
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 for i=1,10 do io.write ((queue[i] or '.')..' ') end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 io.write ('\t')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 for i=1,6 do io.write ((queue.p[i] or '.')..' ') end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 print (...)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 function dequeue (queue) -- - - - - - - - - - - - - - - - - - - - - dequeue
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 local p = queue.p
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 if not p and queue[1] then queue.p = { 1, #queue } p = queue.p end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 if not p[1] then return nil end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 local element = queue[p[1]]
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 queue[p[1]] = nil
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 if p[1] < p[2] then p[1] = p[1] + 1
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 elseif p[4] then p[1], p[2], p[3], p[4] = p[3], p[4], nil, nil
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 elseif p[5] then p[1], p[2], p[5], p[6] = p[5], p[6], nil, nil
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 else p[1], p[2] = nil, nil end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 print_queue (queue, ' de '..element)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 return element
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 function enqueue (queue, element) -- - - - - - - - - - - - - - - - - enqueue
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 local p = queue.p
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 if not p then queue.p = {} p = queue.p end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 if p[5] then -- p3..p4 p1..p2 p5..p6
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 p[6] = p[6]+1
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 queue[p[6]] = element
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 elseif p[3] then -- p3..p4 p1..p2
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 if p[4]+1 < p[1] then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 p[4] = p[4] + 1
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 queue[p[4]] = element
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 else
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 p[5] = p[2]+1
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 p[6], queue[p[5]] = p[5], element
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 elseif p[1] then -- p1..p2
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 if p[1] == 1 then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 p[2] = p[2] + 1
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 queue[p[2]] = element
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 else
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 p[3], p[4], queue[1] = 1, 1, element
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 else -- empty queue
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 p[1], p[2], queue[1] = 1, 1, element
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 print_queue (queue, ' '..element)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 local function test_queue ()
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 t = {}
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 enqueue (t, 1)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 enqueue (t, 2)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 enqueue (t, 3)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215 enqueue (t, 4)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 enqueue (t, 5)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 enqueue (t, 6)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 enqueue (t, 7)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221 enqueue (t, 8)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 enqueue (t, 9)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227 enqueue (t, 'a')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229 enqueue (t, 'b')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230 enqueue (t, 'c')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
231 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
232 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236 enqueue (t, 'd')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
240 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243 -- test_queue ()
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 function queue_len (queue)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250 function queue_peek (queue)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254 -- tree manipulation ---------------------------------------- tree manipulation
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
255
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
256
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257 function set (parent, ...) --- - - - - - - - - - - - - - - - - - - - - - set
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
259 -- print ('set', ...)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
260
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
261 local len = select ('#', ...)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262 local key, value = select (len-1, ...)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263 local cutpoint, cutkey
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
264
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
265 for i=1,len-2 do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
266
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
267 local key = select (i, ...)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
268 local child = parent[key]
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
269
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
270 if value == nil then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271 if child == nil then return
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 elseif next (child, next (child)) then cutpoint = nil cutkey = nil
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
273 elseif cutpoint == nil then cutpoint = parent cutkey = key end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
274
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
275 elseif child == nil then child = {} parent[key] = child end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
277 parent = child
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
280 if value == nil and cutpoint then cutpoint[cutkey] = nil
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281 else parent[key] = value return value end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
284
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
285 function get (parent, ...) --- - - - - - - - - - - - - - - - - - - - - - get
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
286 local len = select ('#', ...)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
287 for i=1,len do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
288 parent = parent[select (i, ...)]
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
289 if parent == nil then break end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
290 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
291 return parent
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
292 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
293
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
294
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
295 -- misc ------------------------------------------------------------------ misc
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
296
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
297
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
298 function find (path, ...) --------------------------------------------- find
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
299
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
300 local dirs, operators = { path }, {...}
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
301 for operator in ivalues (operators) do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
302 if not operator (path) then break end end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
303
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
304 while next (dirs) do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
305 local parent = table.remove (dirs)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
306 for child in assert (pozix.opendir (parent)) do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
307 if child and child ~= '.' and child ~= '..' then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
308 local path = parent..'/'..child
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
309 if pozix.stat (path, 'is_dir') then table.insert (dirs, path) end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
310 for operator in ivalues (operators) do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
311 if not operator (path) then break end end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
312 end end end end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
313
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
314
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
315 function ivalues (t) ----------------------------------------------- ivalues
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
316 local i = 0
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
317 return function () if t[i+1] then i = i + 1 return t[i] end end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
318 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
319
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
320
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
321 function lson_encode (mixed, f, indent, indents) --------------- lson_encode
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
322
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
323
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
324 local capture
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
325 if not f then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
326 capture = {}
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
327 f = function (s) append (capture, s) end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
328 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
329
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
330 indent = indent or 0
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
331 indents = indents or {}
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
332 indents[indent] = indents[indent] or string.rep (' ', 2*indent)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
333
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
334 local type = type (mixed)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
335
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
336 if type == 'number' then f (mixed)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
337
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
338 else if type == 'string' then f (string.format ('%q', mixed))
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
339
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
340 else if type == 'table' then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
341 f ('{')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
342 for k,v in pairs (mixed) do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
343 f ('\n')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
344 f (indents[indent])
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
345 f ('[') f (lson_encode (k)) f ('] = ')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
346 lson_encode (v, f, indent+1, indents)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
347 f (',')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
348 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
349 f (' }')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
350 end end end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
351
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
352 if capture then return table.concat (capture) end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
353 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
354
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
355
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
356 function timestamp (time) ---------------------------------------- timestamp
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
357 return os.date ('%Y%m%d.%H%M%S', time)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
358 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
359
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
360
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
361 function values (t) ------------------------------------------------- values
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
362 local k, v
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
363 return function () k, v = next (t, k) return v end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
364 end