mirror of https://github.com/koreader/koreader
added simple serializer, use lua instead of SQLite
parent
357da594d4
commit
788eb90455
@ -1,38 +1,66 @@
|
||||
DocSettings = {}
|
||||
|
||||
function DocSettings:open(docfile)
|
||||
local new = {}
|
||||
new.docdb, errno, errstr = sqlite3.open(docfile..".kpdfview")
|
||||
if new.docdb ~= nil then
|
||||
new.docdb:exec("CREATE TABLE IF NOT EXISTS settings (key TEXT PRIMARY KEY, value TEXT);")
|
||||
new.stmt_readsetting = new.docdb:prepare("SELECT value FROM settings WHERE key = ?;")
|
||||
new.stmt_savesetting = new.docdb:prepare("INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?);")
|
||||
local new = { file = docfile..".kpdfview.lua", data = {} }
|
||||
local ok, stored = pcall(dofile,new.file)
|
||||
if ok then
|
||||
new.data = stored
|
||||
end
|
||||
return setmetatable(new, { __index = DocSettings})
|
||||
end
|
||||
|
||||
function DocSettings:readsetting(key)
|
||||
if self.docdb ~= nil then
|
||||
self.stmt_readsetting:reset()
|
||||
self.stmt_readsetting:bind_values(key)
|
||||
local result = self.stmt_readsetting:step()
|
||||
if result == sqlite3.ROW then
|
||||
return self.stmt_readsetting:get_value(0)
|
||||
return self.data[key]
|
||||
end
|
||||
|
||||
function DocSettings:savesetting(key, value)
|
||||
self.data[key] = value
|
||||
end
|
||||
|
||||
-- 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
|
||||
end
|
||||
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))
|
||||
end
|
||||
end
|
||||
|
||||
function DocSettings:savesetting(key, value)
|
||||
if self.docdb ~= nil then
|
||||
self.stmt_savesetting:reset()
|
||||
self.stmt_savesetting:bind_values(key, value)
|
||||
self.stmt_savesetting:step()
|
||||
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()
|
||||
end
|
||||
end
|
||||
|
||||
function DocSettings:close()
|
||||
if self.docdb ~= nil then
|
||||
self.docdb:close()
|
||||
self.docdb = nil
|
||||
end
|
||||
self:flush()
|
||||
end
|
||||
|
Loading…
Reference in New Issue