forked from Spring-Chobby/Chobby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
savetable.lua
240 lines (202 loc) · 5.15 KB
/
savetable.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
-- $Id: savetable.lua 3171 2008-11-06 09:06:29Z det $
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
-- file: savetable.lua
-- brief: a human friendly table writer
-- author: Dave Rodgers
--
-- Copyright (C) 2007.
-- Licensed under the terms of the GNU GPL, v2 or later.
--
-- 2 Sept 2008, quantum: added table.show and table.print
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
if (table.echo) then
return
end
local indentString = ' '
local savedTables = {}
-- setup a lua keyword map
local keyWords = {
"and", "break", "do", "else", "elseif", "end", "false", "for", "function",
"if", "in", "local", "nil", "not", "or", "repeat", "return", "then", "true",
"until", "while"
}
local keyWordSet = {}
for _,w in ipairs(keyWords) do
keyWordSet[w] = true
end
keyWords = nil -- don't need the array anymore
--------------------------------------------------------------------------------
local function encloseStr(s)
return string.format('%q', s)
end
local function encloseKey(s)
local wrap = not (string.find(s, '^%a[_%a%d]*$'))
if (not wrap) then
if (string.len(s) <= 0) then wrap = true end
end
if (not wrap) then
if (keyWordSet[s]) then wrap = true end
end
if (wrap) then
return string.format('[%q]', s)
else
return s
end
end
local keyTypes = {
['string'] = true,
['number'] = true,
['boolean'] = true,
}
local valueTypes = {
['string'] = true,
['number'] = true,
['boolean'] = true,
['table'] = true,
}
local function CompareKeys(kv1, kv2)
local k1, v1 = kv1[1], kv1[2]
local k2, v2 = kv2[1], kv2[2]
local ktype1 = type(k1)
local ktype2 = type(k2)
if (ktype1 ~= ktype2) then
return (ktype1 > ktype2)
end
local vtype1 = type(v1)
local vtype2 = type(v2)
if ((vtype1 == 'table') and (vtype2 ~= 'table')) then
return false
end
if ((vtype1 ~= 'table') and (vtype2 == 'table')) then
return true
end
return (k1 < k2)
end
local function MakeSortedTable(t)
local st = {}
for k,v in pairs(t) do
if (keyTypes[type(k)] and valueTypes[type(v)]) then
table.insert(st, { k, v })
end
end
table.sort(st, CompareKeys)
return st
end
local function SaveTable(t, file, indent)
file:write('{\n')
local indent = indent .. indentString
local st = MakeSortedTable(t)
for _,kv in ipairs(st) do
local k, v = kv[1], kv[2]
local ktype = type(k)
local vtype = type(v)
-- output the key
if (ktype == 'string') then
file:write(indent..encloseKey(k)..' = ')
else
file:write(indent..'['..tostring(k)..'] = ')
end
-- output the value
if (vtype == 'string') then
file:write(encloseStr(v)..',\n')
elseif (vtype == 'number') then
file:write(tostring(v)..',\n')
elseif (vtype == 'boolean') then
file:write(tostring(v)..',\n')
elseif (vtype == 'table') then
if (savedTables[v]) then
error("table.save() does not support recursive tables")
end
if (next(v)) then
savedTables[t] = true
SaveTable(v, file, indent)
file:write(indent..'},\n')
savedTables[t] = nil
else
file:write('{},\n') -- empty table
end
end
end
end
function ShowTable(t, indent)
local strings = {}
strings[#strings+1] = '{\n'
local indent = indent .. indentString
local st = MakeSortedTable(t)
for _,kv in ipairs(st) do
local k, v = kv[1], kv[2]
local ktype = type(k)
local vtype = type(v)
-- output the key
if (ktype == 'string') then
strings[#strings+1] = (indent..encloseKey(k)..' = ')
else
strings[#strings+1] = (indent..'['..tostring(k)..'] = ')
end
-- output the value
if (vtype == 'string') then
strings[#strings+1] = (encloseStr(v)..',\n')
elseif (vtype == 'number') then
strings[#strings+1] = (tostring(v)..',\n')
elseif (vtype == 'boolean') then
strings[#strings+1] = (tostring(v)..',\n')
elseif (vtype == 'table') then
if (savedTables[v]) then
error("table.save() does not support recursive tables")
end
if (next(v)) then
savedTables[t] = true
strings[#strings+1] = ShowTable(v, indent)
strings[#strings+1] = (indent..'},\n')
savedTables[t] = nil
else
strings[#strings+1] = ('{},\n') -- empty table
end
end
end
return table.concat(strings)
end
function table.show(t, header)
local strings = {}
if (header) then
strings[#strings+1] = (header..'\n')
end
strings[#strings+1] = 'return '
strings[#strings+1] = ShowTable(t, '')
strings[#strings+1] = '}\n'
for k,v in pairs(savedTables) do
savedTables[k] = nil
end
return table.concat(strings)
end
function table.print(t, header)
print(table.show(t, header))
end
function table.echo(t, header)
Spring.Echo(table.show(t, header))
end
function table.save(t, filename, header)
if (not filename) then
error("Error while saving.")
return
end
local file, errMsg = io.open(filename, 'w')
if (not file) then
error(errMsg .. "\nFile not saved.")
return
end
if (header) then
file:write(header..'\n')
end
file:write('return ')
SaveTable(t, file, '')
file:write('}\n')
file:close()
for k,v in pairs(savedTables) do
savedTables[k] = nil
end
end