blob: f3713af1c1518327d4dd3f6a64c01b73d9f024c8 (
plain)
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
|
local INDENT_STR = " "
local function str_to_source(str)
return "\"" .. str .. "\""
end
local function table_to_str(tbl, indent)
indent = indent or 1
-- to prevent people doing stuff that this function doesn't expect
assert(not getmetatable(tbl), "cannot export a table with a metatable")
local src = "{"
for k, v in pairs(tbl) do
print(k, v)
if type(k) == "function" or type(v) == "function" then
error("Cannot export function " .. tostring(k) .. " = " .. tostring(v))
end
if type(k) ~= "number" then
src = src .. "["
if type(k) == "string" then
src = src .. str_to_source(k)
elseif type(k) == "table" then
error("Tables as keys aren't supported for exporting")
else
src = src .. tostring(k)
end
src = src .. "]="
end
if type(v) == "table" then
src = src .. table_to_str(v, indent + 1)
elseif type(v) == "string" then
src = src .. str_to_source(v)
else
src = src .. tostring(v)
end
src = src .. ","
end
src = src .. "}"
return src
end
function export_to_source(tbl, file_name)
local src = table_to_str(tbl)
src = "return " .. src
local file = io.open(file_name, "w")
if file == nil then
error("could not open file '" .. file_name "'")
end
file:write(src)
file:close()
end
|