Annotate

util/ztact.lua @ 1452:0ae8787e4e7d

Merged with trunk
author Waqas Hussain <waqas20@gmail.com>
date Thu, 02 Jul 2009 00:47:21 +0500
parent 896:2c0b9e3c11c3
child 1523:841d61be198f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
896
2c0b9e3c11c3 0.3->0.4
Matthew Wild <mwild1@gmail.com>
parents: 759
diff changeset
1 -- Prosody IM v0.4
615
4ae3e81513f3 0.1 -> 0.2
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
2 -- This file is included with Prosody IM. It has modifications,
4ae3e81513f3 0.1 -> 0.2
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
3 -- which are hereby placed in the public domain.
337
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 -- public domain 20080410 lua@ztact.com
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 pcall (require, 'lfs') -- lfs may not be installed/necessary.
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 pcall (require, 'pozix') -- pozix may not be installed/necessary.
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local getfenv, ipairs, next, pairs, pcall, require, select, tostring, type =
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 getfenv, ipairs, next, pairs, pcall, require, select, tostring, type
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local unpack, xpcall =
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 unpack, xpcall
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 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
18
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local assert, print = assert, print
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 local error = error
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22
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 module ((...) or 'ztact') ------------------------------------- module ztact
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
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 -- dir -------------------------------------------------------------------- dir
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 function dir (path) -- - - - - - - - - - - - - - - - - - - - - - - - - - dir
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 local it = lfs.dir (path)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 return function ()
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 repeat
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 local dir = it ()
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 if dir ~= '.' and dir ~= '..' then return dir end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 until not dir
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 function is_file (path) -- - - - - - - - - - - - - - - - - - is_file (path)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local mode = lfs.attributes (path, 'mode')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 return mode == 'file' and path
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44
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 -- network byte ordering -------------------------------- network byte ordering
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 function htons (word) -- - - - - - - - - - - - - - - - - - - - - - - - htons
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 return (word-word%0x100)/0x100, word%0x100
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52
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 -- pcall2 -------------------------------------------------------------- pcall2
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55
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 getfenv ().pcall = pcall -- store the original pcall as ztact.pcall
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58
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 local argc, argv, errorhandler, pcall2_f
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 local function _pcall2 () -- - - - - - - - - - - - - - - - - - - - - _pcall2
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 local tmpv = argv
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 argv = nil
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 return pcall2_f (unpack (tmpv, 1, argc))
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 function seterrorhandler (func) -- - - - - - - - - - - - - - seterrorhandler
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 errorhandler = func
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73
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 function pcall2 (f, ...) -- - - - - - - - - - - - - - - - - - - - - - pcall2
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 pcall2_f = f
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 argc = select ('#', ...)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 argv = { ... }
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 if not errorhandler then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 local debug = require ('debug')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 errorhandler = debug.traceback
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 return xpcall (_pcall2, errorhandler)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 function append (t, ...) -- - - - - - - - - - - - - - - - - - - - - - append
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 local insert = table.insert
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 for i,v in ipairs {...} do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 insert (t, v)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 end end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 function print_r (d, indent) -- - - - - - - - - - - - - - - - - - - print_r
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 local rep = string.rep (' ', indent or 0)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 if type (d) == 'table' then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 for k,v in pairs (d) do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 if type (v) == 'table' then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 io.write (rep, k, '\n')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 print_r (v, (indent or 0) + 1)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 else io.write (rep, k, ' = ', tostring (v), '\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 else io.write (d, '\n') end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 function tohex (s) -- - - - - - - - - - - - - - - - - - - - - - - - - tohex
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 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
112 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113
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 function tostring_r (d, indent, tab0) -- - - - - - - - - - - - - tostring_r
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 tab1 = tab0 or {}
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 local rep = string.rep (' ', indent or 0)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 if type (d) == 'table' then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 for k,v in pairs (d) do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 if type (v) == 'table' then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 append (tab1, rep, k, '\n')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 tostring_r (v, (indent or 0) + 1, tab1)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 else append (tab1, rep, k, ' = ', tostring (v), '\n') end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 else append (tab1, d, '\n') end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 if not tab0 then return table.concat (tab1) end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130
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 -- queue manipulation -------------------------------------- queue manipulation
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133
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 -- 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
136 --
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 -- 1..2
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 -- 3..4 1..2
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 -- 3..4 1..2 5..6
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 -- 1..2 5..6
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 -- 1..2
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 local function print_queue (queue, ...) -- - - - - - - - - - - - print_queue
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 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
146 io.write ('\t')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 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
148 print (...)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150
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 function dequeue (queue) -- - - - - - - - - - - - - - - - - - - - - dequeue
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 local p = queue.p
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 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
156
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 if not p[1] then return nil end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 local element = queue[p[1]]
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 queue[p[1]] = nil
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 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
163
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 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
165
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 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
167
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 else p[1], p[2] = nil, nil end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 print_queue (queue, ' de '..element)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 return element
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173
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 function enqueue (queue, element) -- - - - - - - - - - - - - - - - - enqueue
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 local p = queue.p
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 if not p then queue.p = {} p = queue.p end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 if p[5] then -- p3..p4 p1..p2 p5..p6
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 p[6] = p[6]+1
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 queue[p[6]] = element
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 elseif p[3] then -- p3..p4 p1..p2
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 if p[4]+1 < p[1] then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 p[4] = p[4] + 1
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 queue[p[4]] = element
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 else
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 p[5] = p[2]+1
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 p[6], queue[p[5]] = p[5], element
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 elseif p[1] then -- p1..p2
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 if p[1] == 1 then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 p[2] = p[2] + 1
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 queue[p[2]] = element
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 else
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 p[3], p[4], queue[1] = 1, 1, element
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 else -- empty queue
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 p[1], p[2], queue[1] = 1, 1, element
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 print_queue (queue, ' '..element)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 local function test_queue ()
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 t = {}
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 enqueue (t, 1)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215 enqueue (t, 2)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 enqueue (t, 3)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 enqueue (t, 4)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 enqueue (t, 5)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221 enqueue (t, 6)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 enqueue (t, 7)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223 enqueue (t, 8)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 enqueue (t, 9)
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 dequeue (t)
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, 'a')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
231 enqueue (t, 'b')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
232 enqueue (t, 'c')
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 dequeue (t)
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 enqueue (t, 'd')
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 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243
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 -- test_queue ()
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 function queue_len (queue)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252 function queue_peek (queue)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254
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 -- tree manipulation ---------------------------------------- tree manipulation
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257
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 function set (parent, ...) --- - - - - - - - - - - - - - - - - - - - - - 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 -- print ('set', ...)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263 local len = select ('#', ...)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
264 local key, value = select (len-1, ...)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
265 local cutpoint, cutkey
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 for i=1,len-2 do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
268
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
269 local key = select (i, ...)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
270 local child = parent[key]
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 if value == nil then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
273 if child == nil then return
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
274 elseif next (child, next (child)) then cutpoint = nil cutkey = nil
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
275 elseif cutpoint == nil then cutpoint = parent cutkey = key 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 elseif child == nil then child = {} parent[key] = child end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 parent = child
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
280 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 if value == nil and cutpoint then cutpoint[cutkey] = nil
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283 else parent[key] = value return value end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
284 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
285
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
286
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
287 function get (parent, ...) --- - - - - - - - - - - - - - - - - - - - - - get
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
288 local len = select ('#', ...)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
289 for i=1,len do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
290 parent = parent[select (i, ...)]
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
291 if parent == nil then break end
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 return parent
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
294 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
295
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 -- misc ------------------------------------------------------------------ misc
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
298
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 function find (path, ...) --------------------------------------------- find
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
301
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
302 local dirs, operators = { path }, {...}
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
303 for operator in ivalues (operators) do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
304 if not operator (path) then break end end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
305
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
306 while next (dirs) do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
307 local parent = table.remove (dirs)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
308 for child in assert (pozix.opendir (parent)) do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
309 if child and child ~= '.' and child ~= '..' then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
310 local path = parent..'/'..child
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
311 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
312 for operator in ivalues (operators) do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
313 if not operator (path) then break end end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
314 end end end end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
315
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
316
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
317 function ivalues (t) ----------------------------------------------- ivalues
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
318 local i = 0
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
319 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
320 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
321
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 function lson_encode (mixed, f, indent, indents) --------------- lson_encode
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
324
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
325
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
326 local capture
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
327 if not f then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
328 capture = {}
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
329 f = function (s) append (capture, s) end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
330 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
331
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
332 indent = indent or 0
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
333 indents = indents or {}
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
334 indents[indent] = indents[indent] or string.rep (' ', 2*indent)
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 local type = type (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 if type == 'number' then f (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 == 'string' then f (string.format ('%q', mixed))
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
341
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
342 else if type == 'table' then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
343 f ('{')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
344 for k,v in pairs (mixed) do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
345 f ('\n')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
346 f (indents[indent])
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
347 f ('[') f (lson_encode (k)) f ('] = ')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
348 lson_encode (v, f, indent+1, indents)
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
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
351 f (' }')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
352 end end end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
353
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
354 if capture then return table.concat (capture) end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
355 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
356
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
357
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
358 function timestamp (time) ---------------------------------------- timestamp
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
359 return os.date ('%Y%m%d.%H%M%S', time)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
360 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
361
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
362
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
363 function values (t) ------------------------------------------------- values
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
364 local k, v
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
365 return function () k, v = next (t, k) return v end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
366 end