Annotate

util/ztact.lua @ 552:6db1f41e475a

util.multitable - A table with multiple key for each entry
author Waqas Hussain <waqas20@gmail.com>
date Fri, 05 Dec 2008 04:56:04 +0500
parent 519:cccd610a0ef9
child 615:4ae3e81513f3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
1 -- Prosody IM v0.1
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
2 -- Copyright (C) 2008 Matthew Wild
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
3 -- Copyright (C) 2008 Waqas Hussain
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
4 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
5 -- This program is free software; you can redistribute it and/or
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
6 -- modify it under the terms of the GNU General Public License
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
7 -- as published by the Free Software Foundation; either version 2
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
8 -- of the License, or (at your option) any later version.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
9 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
10 -- This program is distributed in the hope that it will be useful,
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
13 -- GNU General Public License for more details.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
14 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
15 -- You should have received a copy of the GNU General Public License
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
16 -- along with this program; if not, write to the Free Software
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
18 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
19
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
20
337
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
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 -- public domain 20080410 lua@ztact.com
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
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 pcall (require, 'lfs') -- lfs may not be installed/necessary.
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 pcall (require, 'pozix') -- pozix may not be installed/necessary.
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 local getfenv, ipairs, next, pairs, pcall, require, select, tostring, type =
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 getfenv, ipairs, next, pairs, pcall, require, select, tostring, type
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 local unpack, xpcall =
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 unpack, xpcall
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 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
36
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 local assert, print = assert, print
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 local error = error
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 module ((...) or 'ztact') ------------------------------------- module ztact
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
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 -- dir -------------------------------------------------------------------- dir
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
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 function dir (path) -- - - - - - - - - - - - - - - - - - - - - - - - - - dir
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 local it = lfs.dir (path)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 return function ()
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 repeat
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 local dir = it ()
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 if dir ~= '.' and dir ~= '..' then return dir end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 until not dir
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 end end
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 function is_file (path) -- - - - - - - - - - - - - - - - - - is_file (path)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 local mode = lfs.attributes (path, 'mode')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 return mode == 'file' and path
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 end
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
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 -- network byte ordering -------------------------------- network byte ordering
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65
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 function htons (word) -- - - - - - - - - - - - - - - - - - - - - - - - htons
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 return (word-word%0x100)/0x100, word%0x100
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70
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 -- pcall2 -------------------------------------------------------------- pcall2
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 getfenv ().pcall = pcall -- store the original pcall as ztact.pcall
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
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 local argc, argv, errorhandler, pcall2_f
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79
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 local function _pcall2 () -- - - - - - - - - - - - - - - - - - - - - _pcall2
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 local tmpv = argv
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 argv = nil
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 return pcall2_f (unpack (tmpv, 1, argc))
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 seterrorhandler (func) -- - - - - - - - - - - - - - seterrorhandler
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 errorhandler = func
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 function pcall2 (f, ...) -- - - - - - - - - - - - - - - - - - - - - - pcall2
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 pcall2_f = f
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 argc = select ('#', ...)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 argv = { ... }
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 if not errorhandler then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 local debug = require ('debug')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 errorhandler = debug.traceback
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 return xpcall (_pcall2, errorhandler)
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 append (t, ...) -- - - - - - - - - - - - - - - - - - - - - - append
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 local insert = table.insert
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 for i,v in ipairs {...} do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 insert (t, v)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 end 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 print_r (d, indent) -- - - - - - - - - - - - - - - - - - - print_r
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 io.write (rep, k, '\n')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 print_r (v, (indent or 0) + 1)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 else io.write (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 io.write (d, '\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
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 function tohex (s) -- - - - - - - - - - - - - - - - - - - - - - - - - tohex
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 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
130 end
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 function tostring_r (d, indent, tab0) -- - - - - - - - - - - - - tostring_r
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 tab1 = tab0 or {}
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 local rep = string.rep (' ', indent or 0)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 if type (d) == 'table' then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 for k,v in pairs (d) do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 if type (v) == 'table' then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 append (tab1, rep, k, '\n')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 tostring_r (v, (indent or 0) + 1, tab1)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 else append (tab1, rep, k, ' = ', tostring (v), '\n') end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 else append (tab1, d, '\n') end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 if not tab0 then return table.concat (tab1) end
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 -- queue manipulation -------------------------------------- queue manipulation
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
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 -- 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
154 --
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 -- 1..2
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 -- 3..4 1..2
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 -- 3..4 1..2 5..6
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 -- 1..2 5..6
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 -- 1..2
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160
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 local function print_queue (queue, ...) -- - - - - - - - - - - - print_queue
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 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
164 io.write ('\t')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 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
166 print (...)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168
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 function dequeue (queue) -- - - - - - - - - - - - - - - - - - - - - dequeue
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 local p = queue.p
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 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
174
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 if not p[1] then return nil end
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 element = queue[p[1]]
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 queue[p[1]] = nil
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[1] < p[2] then p[1] = p[1] + 1
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[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
183
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 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
185
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 else p[1], p[2] = nil, nil end
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 print_queue (queue, ' de '..element)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 return element
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191
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 function enqueue (queue, element) -- - - - - - - - - - - - - - - - - enqueue
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 local p = queue.p
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 if not p then queue.p = {} p = queue.p end
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 if p[5] then -- p3..p4 p1..p2 p5..p6
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 p[6] = p[6]+1
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 queue[p[6]] = element
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 elseif p[3] then -- p3..p4 p1..p2
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 if p[4]+1 < p[1] then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 p[4] = p[4] + 1
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 queue[p[4]] = element
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 else
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 p[5] = p[2]+1
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 p[6], queue[p[5]] = p[5], element
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 elseif p[1] then -- p1..p2
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 if p[1] == 1 then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215 p[2] = p[2] + 1
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 queue[p[2]] = element
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 else
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 p[3], p[4], queue[1] = 1, 1, element
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 else -- empty queue
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223 p[1], p[2], queue[1] = 1, 1, element
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226 print_queue (queue, ' '..element)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230 local function test_queue ()
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
231 t = {}
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
232 enqueue (t, 1)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233 enqueue (t, 2)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234 enqueue (t, 3)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235 enqueue (t, 4)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236 enqueue (t, 5)
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 enqueue (t, 6)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
240 enqueue (t, 7)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241 enqueue (t, 8)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242 enqueue (t, 9)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247 enqueue (t, 'a')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 enqueue (t, 'b')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250 enqueue (t, 'c')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
255 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
256 enqueue (t, 'd')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
259 dequeue (t)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
260 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
261
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 -- test_queue ()
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
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
266 function queue_len (queue)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
267 end
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
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
270 function queue_peek (queue)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
273
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
274 -- tree manipulation ---------------------------------------- tree manipulation
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
275
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 function set (parent, ...) --- - - - - - - - - - - - - - - - - - - - - - set
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 -- print ('set', ...)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
280
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281 local len = select ('#', ...)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 local key, value = select (len-1, ...)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283 local cutpoint, cutkey
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 for i=1,len-2 do
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 local key = select (i, ...)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
288 local child = parent[key]
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
289
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
290 if value == nil then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
291 if child == nil then return
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
292 elseif next (child, next (child)) then cutpoint = nil cutkey = nil
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
293 elseif cutpoint == nil then cutpoint = parent cutkey = key end
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 elseif child == nil then child = {} parent[key] = child end
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 parent = child
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
298 end
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 if value == nil and cutpoint then cutpoint[cutkey] = nil
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
301 else parent[key] = value return value end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
302 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
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
305 function get (parent, ...) --- - - - - - - - - - - - - - - - - - - - - - get
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
306 local len = select ('#', ...)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
307 for i=1,len do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
308 parent = parent[select (i, ...)]
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
309 if parent == nil then break end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
310 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
311 return parent
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
312 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 -- misc ------------------------------------------------------------------ misc
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
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
318 function find (path, ...) --------------------------------------------- find
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 local dirs, operators = { path }, {...}
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
321 for operator in ivalues (operators) do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
322 if not operator (path) then break end end
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 while next (dirs) do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
325 local parent = table.remove (dirs)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
326 for child in assert (pozix.opendir (parent)) do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
327 if child and child ~= '.' and child ~= '..' then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
328 local path = parent..'/'..child
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
329 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
330 for operator in ivalues (operators) do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
331 if not operator (path) then break end end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
332 end end end end
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
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
335 function ivalues (t) ----------------------------------------------- ivalues
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
336 local i = 0
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
337 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
338 end
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
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
341 function lson_encode (mixed, f, indent, indents) --------------- lson_encode
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
342
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
343
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
344 local capture
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
345 if not f then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
346 capture = {}
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
347 f = function (s) append (capture, s) end
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
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
350 indent = indent or 0
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
351 indents = indents or {}
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
352 indents[indent] = indents[indent] or string.rep (' ', 2*indent)
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 local type = type (mixed)
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 if type == 'number' then f (mixed)
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 else if type == 'string' then f (string.format ('%q', mixed))
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 else if type == 'table' then
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
361 f ('{')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
362 for k,v in pairs (mixed) do
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
363 f ('\n')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
364 f (indents[indent])
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
365 f ('[') f (lson_encode (k)) f ('] = ')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
366 lson_encode (v, f, indent+1, indents)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
367 f (',')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
368 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
369 f (' }')
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
370 end end end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
371
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
372 if capture then return table.concat (capture) end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
373 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
374
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
375
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
376 function timestamp (time) ---------------------------------------- timestamp
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
377 return os.date ('%Y%m%d.%H%M%S', time)
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
378 end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
379
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
380
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
381 function values (t) ------------------------------------------------- values
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
382 local k, v
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
383 return function () k, v = next (t, k) return v end
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
384 end