2011-12-05 21:31:40 +00:00
|
|
|
DocSettings = {}
|
|
|
|
|
|
|
|
function DocSettings:open(docfile)
|
2012-03-04 16:29:19 +00:00
|
|
|
local new = { file = docfile..".kpdfview.lua", data = {} }
|
|
|
|
local ok, stored = pcall(dofile,new.file)
|
|
|
|
if ok then
|
|
|
|
new.data = stored
|
2011-12-05 21:31:40 +00:00
|
|
|
end
|
2011-12-07 23:45:39 +00:00
|
|
|
return setmetatable(new, { __index = DocSettings})
|
2011-12-05 21:31:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function DocSettings:readsetting(key)
|
2012-03-04 16:29:19 +00:00
|
|
|
return self.data[key]
|
|
|
|
end
|
|
|
|
|
|
|
|
function DocSettings:savesetting(key, value)
|
|
|
|
self.data[key] = value
|
|
|
|
end
|
|
|
|
|
2012-03-06 22:17:37 +00:00
|
|
|
function dump(data)
|
|
|
|
local out = {}
|
|
|
|
DocSettings:_serialize(data, out, 0)
|
|
|
|
return table.concat(out)
|
|
|
|
end
|
|
|
|
|
2012-03-04 16:29:19 +00:00
|
|
|
-- simple serialization function, won't do uservalues, functions, loops
|
|
|
|
function DocSettings:_serialize(what, outt, indent)
|
|
|
|
if type(what) == "table" then
|
|
|
|
local didrun = false
|
|
|
|
table.insert(outt, "{")
|
|
|
|
for k, v in pairs(what) do
|
|
|
|
if didrun then
|
|
|
|
table.insert(outt, ",")
|
|
|
|
end
|
|
|
|
table.insert(outt, "\n")
|
|
|
|
table.insert(outt, string.rep("\t", indent+1))
|
|
|
|
table.insert(outt, "[")
|
|
|
|
self:_serialize(k, outt, indent+1)
|
|
|
|
table.insert(outt, "] = ")
|
|
|
|
self:_serialize(v, outt, indent+1)
|
|
|
|
didrun = true
|
2011-12-05 21:31:40 +00:00
|
|
|
end
|
2012-03-04 16:29:19 +00:00
|
|
|
if didrun then
|
|
|
|
table.insert(outt, "\n")
|
|
|
|
table.insert(outt, string.rep("\t", indent))
|
|
|
|
end
|
|
|
|
table.insert(outt, "}")
|
|
|
|
elseif type(what) == "string" then
|
|
|
|
table.insert(outt, string.format("%q", what))
|
|
|
|
elseif type(what) == "number" or type(what) == "boolean" then
|
|
|
|
table.insert(outt, tostring(what))
|
2011-12-05 21:31:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-04 16:29:19 +00:00
|
|
|
function DocSettings:flush()
|
|
|
|
-- write a serialized version of the data table
|
|
|
|
if not self.file then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local f_out = io.open(self.file, "w")
|
|
|
|
if f_out ~= nil then
|
|
|
|
local out = {"-- we can read Lua syntax here!\nreturn "}
|
|
|
|
self:_serialize(self.data, out, 0)
|
|
|
|
table.insert(out, "\n")
|
|
|
|
f_out:write(table.concat(out))
|
|
|
|
f_out:close()
|
2011-12-05 21:31:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function DocSettings:close()
|
2012-03-04 16:29:19 +00:00
|
|
|
self:flush()
|
2011-12-05 21:31:40 +00:00
|
|
|
end
|