1 local format = string.format;
2 local setmetatable, type = setmetatable, type;
3 local pairs, ipairs = pairs, ipairs;
4 local char = string.char;
5 local loadfile, setfenv, pcall = loadfile, setfenv, pcall;
7 local io_open = io.open;
8 local os_remove = os.remove;
9 local tostring, tonumber = tostring, tonumber;
12 local t_insert = table.insert;
14 local indent = function(f, i)
20 local data_path = "data";
28 local log = function (type, msg) return log(type, "datamanager", msg); end
31 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
34 return s and (s:gsub("+", " "):gsub("%%([a-fA-F0-9][a-fA-F0-9])", urlcodes));
38 return s and (s:gsub("%W", function (c) return format("%%%x", c:byte()); end));
42 local function basicSerialize (o)
43 if type(o) == "number" or type(o) == "boolean" then
45 else -- assume it is a string -- FIXME make sure it's a string. throw an error otherwise.
46 return (format("%q", tostring(o)):gsub("\\\n", "\\n"));
51 local function simplesave (f, o, ind)
52 if type(o) == "number" then
54 elseif type(o) == "string" then
55 f:write((format("%q", o):gsub("\\\n", "\\n")))
56 elseif type(o) == "table" then
58 for k,v in pairs(o) do
60 f:write("[", basicSerialize(k), "] = ")
61 simplesave(f, v, ind+1)
66 elseif type(o) == "boolean" then
67 f:write(o and "true" or "false");
69 error("cannot serialize a " .. type(o))
73 ------- API -------------
75 function set_data_path(path)
79 function getpath(username, host, datastore, ext)
82 return format("%s/%s/%s/%s.%s", data_path, encode(host), datastore, encode(username), ext);
84 return format("%s/%s/%s.%s", data_path, encode(host), datastore, ext);
86 return format("%s/%s.%s", data_path, datastore, ext);
90 function load(username, host, datastore)
91 local data, ret = loadfile(getpath(username, host, datastore));
93 log("warn", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
97 local success, ret = pcall(data);
99 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
105 function store(username, host, datastore, data)
109 -- save the datastore
110 local f, msg = io_open(getpath(username, host, datastore), "w+");
112 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
116 simplesave(f, data, 1);
118 if not next(data) then -- try to delete empty datastore
119 os_remove(getpath(username, host, datastore));
121 -- we write data even when we are deleting because lua doesn't have a
122 -- platform independent way of checking for non-exisitng files
126 function list_append(username, host, datastore, data)
127 if not data then return; end
128 -- save the datastore
129 local f, msg = io_open(getpath(username, host, datastore, "list"), "a+");
131 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
135 simplesave(f, data, 1);
141 function list_store(username, host, datastore, data)
145 -- save the datastore
146 local f, msg = io_open(getpath(username, host, datastore, "list"), "w+");
148 log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
151 for _, d in ipairs(data) do
157 if not next(data) then -- try to delete empty datastore
158 os_remove(getpath(username, host, datastore, "list"));
160 -- we write data even when we are deleting because lua doesn't have a
161 -- platform independent way of checking for non-exisitng files
165 function list_load(username, host, datastore)
166 local data, ret = loadfile(getpath(username, host, datastore, "list"));
168 log("warn", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
172 setfenv(data, {item = function(i) t_insert(items, i); end});
173 local success, ret = pcall(data);
175 log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));