awesome-wm-nice/table.lua

109 lines
3.4 KiB
Lua
Raw Normal View History

2020-09-01 04:48:32 +02:00
--[[
Courtesy of: http://lua-users.org/wiki/SaveTableToFile
2021-12-02 23:34:53 +01:00
]]
local function exportstring(s)
return string.format("%q", s)
end
2020-09-01 04:48:32 +02:00
-- The Save Function
2020-09-01 04:48:32 +02:00
local function save(tbl, filename)
local charS, charE = " ", "\n"
local file, err = io.open(filename, "wb")
2021-12-02 23:34:53 +01:00
if err then
return err
end
2020-09-01 04:48:32 +02:00
-- Initialize variables for save procedure
2021-12-02 23:34:53 +01:00
local tables, lookup = { tbl }, { [tbl] = 1 }
2020-09-01 04:48:32 +02:00
file:write("return {" .. charE)
for idx, t in ipairs(tables) do
file:write("-- Table: {" .. idx .. "}" .. charE)
file:write("{" .. charE)
local thandled = {}
for i, v in ipairs(t) do
thandled[i] = true
local stype = type(v)
-- only handle value
if stype == "table" then
if not lookup[v] then
table.insert(tables, v)
lookup[v] = #tables
end
file:write(charS .. "{" .. lookup[v] .. "}," .. charE)
elseif stype == "string" then
file:write(charS .. exportstring(v) .. "," .. charE)
elseif stype == "number" then
file:write(charS .. tostring(v) .. "," .. charE)
end
end
for i, v in pairs(t) do
-- escape handled values
2021-12-02 23:34:53 +01:00
if not thandled[i] then
2020-09-01 04:48:32 +02:00
local str = ""
local stype = type(i)
-- handle index
if stype == "table" then
if not lookup[i] then
table.insert(tables, i)
lookup[i] = #tables
end
str = charS .. "[{" .. lookup[i] .. "}]="
elseif stype == "string" then
str = charS .. "[" .. exportstring(i) .. "]="
elseif stype == "number" then
str = charS .. "[" .. tostring(i) .. "]="
end
if str ~= "" then
stype = type(v)
-- handle value
if stype == "table" then
if not lookup[v] then
table.insert(tables, v)
lookup[v] = #tables
end
file:write(str .. "{" .. lookup[v] .. "}," .. charE)
elseif stype == "string" then
file:write(str .. exportstring(v) .. "," .. charE)
elseif stype == "number" then
file:write(str .. tostring(v) .. "," .. charE)
end
end
end
end
file:write("}," .. charE)
end
2021-12-02 23:34:53 +01:00
file:write "}"
2020-09-01 04:48:32 +02:00
file:close()
end
-- The Load Function
local function load(sfile)
local ftables, err = loadfile(sfile)
2021-12-02 23:34:53 +01:00
if err then
return nil, err
end
2020-09-01 04:48:32 +02:00
local tables = ftables()
for idx = 1, #tables do
local tolinki = {}
for i, v in pairs(tables[idx]) do
2021-12-02 23:34:53 +01:00
if type(v) == "table" then
tables[idx][i] = tables[v[1]]
end
2020-09-01 04:48:32 +02:00
if type(i) == "table" and tables[i[1]] then
2021-12-02 23:34:53 +01:00
table.insert(tolinki, { i, tables[i[1]] })
2020-09-01 04:48:32 +02:00
end
end
-- link indices
for _, v in ipairs(tolinki) do
tables[idx][v[2]], tables[idx][v[1]] = tables[idx][v[1]], nil
end
end
return tables[1]
end
2021-12-02 23:34:53 +01:00
return { save = save, load = load }