From e963304b5c274c0ea5973f505e2a6f3f7f151054 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Sat, 11 Jun 2011 19:08:39 +0200 Subject: Add support for array lua resources --- src/FbTk/LResourceHelper.lua | 77 ++++++++++++++++++++++++++------------------ src/FbTk/ResTraits.hh | 4 +-- src/main.cc | 9 +++--- 3 files changed, 53 insertions(+), 37 deletions(-) diff --git a/src/FbTk/LResourceHelper.lua b/src/FbTk/LResourceHelper.lua index f336e05..dd4ac2e 100644 --- a/src/FbTk/LResourceHelper.lua +++ b/src/FbTk/LResourceHelper.lua @@ -7,7 +7,7 @@ local function myerror(table, msg) end; local function check_arg(table, key) - if type(key) ~= 'string' then + if type(key) ~= 'string' and type(key) ~= 'number' then myerror(table, 'expecting strings as keys.'); end; if string.match(key, "^_") then @@ -53,11 +53,21 @@ local function index(table, key) return t; end; +local function append_name(str, name) + if type(name) == 'string' and string.match(name, '^%a+$') then + return str .. '.' .. name; + elseif type(name) == 'number' then + return str .. '[' .. string.format('%g', name) .. ']'; + else + return nil; + end; +end; + new_cat = function(table, key) local meta = getmetatable(table); local mt = { __newindex = newindex, __index = index, - _magic = cat_magic, _fullname = meta._fullname .. '.' .. key, _state = 0 + _magic = cat_magic, _fullname = append_name(meta._fullname, key), _state = 0 }; meta[key] = setmetatable({}, mt); return meta[key]; @@ -69,10 +79,18 @@ local function register_resource(root, name, object) local head, tail = string.match(name, '^(%a+)%.?(.*)'); local t = meta[head]; + local mt = getmetatable(t); + if mt ~= nil and mt._magic == cat_magic then + t = mt; + end; + if tail == '' then - meta[head] = object; if getmetatable(object) == res_magic then + meta[head] = object; write_resource(object, t); + else + meta[head] = nil; + root[head] = object; end; return t; end; @@ -83,44 +101,41 @@ local function register_resource(root, name, object) return register_resource(t, tail, object); end; -local function dump_value(val) - if type(val) == "string" then - return string.format('%q', val); - elseif type(val) == "number" then - return string.format('%g', val); - else - error('Unsupported value type: ' .. type(val)); - end; -end; - -local function dump_(root, fd) - local meta = getmetatable(root); - if not string.match(meta._fullname, "^[%a.]+$") then - error("Someone has been messing with metatables."); - end; +local function dump_(key, value, fd) + if type(value) == 'string' then + fd:write(key, ' = ', string.format('%q', value), '\n'); + elseif type(value) == 'number' then + fd:write(key, ' = ', string.format('%g', value), '\n'); + elseif type(value) == 'table' then + fd:write(key, ' = {}\n'); + for k, v in pairs(value) do + k = append_name(key, k); - for k, v in pairs(meta) do - if type(k) == "string" and string.match(k, "^%a+$") then local mt = getmetatable(v); - - fd:write(meta._fullname, '.', k, ' = '); if mt ~= nil and mt._magic == cat_magic then - fd:write('{}\n'); - dump(v); - fd:write('\n'); - else - if mt == res_magic then - v = read_resource(v); - end; - fd:write(dump_value(v), '\n'); + v = mt; + elseif mt == res_magic then + v = read_resource(v); + end; + + if k ~= nil then + dump_(k, v, fd); end; end; + fd:write('\n'); + else + error('Unsupported value type: ' .. type(val)); end; end; local function dump(root, file) local fd = io.open(file, 'w'); - dump_(root, fd); + local meta = getmetatable(root); + if not string.match(meta._fullname, "^[%a]+$") then + error("Someone has been messing with metatables."); + end; + + dump_(meta._fullname, meta, fd); fd:close(); end; diff --git a/src/FbTk/ResTraits.hh b/src/FbTk/ResTraits.hh index 32be48d..6bcd0f4 100644 --- a/src/FbTk/ResTraits.hh +++ b/src/FbTk/ResTraits.hh @@ -163,7 +163,7 @@ struct VectorTraits { for(size_t i = 0; i < x.size(); ++i) { Traits::toLua(x[i], l); - l.rawseti(-2, i); + l.rawseti(-2, i+1); } } @@ -189,7 +189,7 @@ struct VectorTraits { Type retval; if(l.type(-1) == lua::TTABLE) { - for(size_t i = 0; l.rawgeti(-1, i), !l.isnil(-1); ++i) { + for(size_t i = 1; l.rawgeti(-1, i), !l.isnil(-1); ++i) { try { retval.push_back(Traits::fromLua(l)); } diff --git a/src/main.cc b/src/main.cc index f340ada..a18ae56 100644 --- a/src/main.cc +++ b/src/main.cc @@ -413,16 +413,17 @@ void updateConfigFilesIfNeeded(const std::string& rc_file) { } } - +#include "WinButton.hh" int main(int argc, char **argv) { lua::state l; FbTk::LResourceManager::initState(l); FbTk::LResourceManager man(l, "session"); - l.loadstring("session.asdf = 321; session.q=456;"); + l.loadstring("session.asdf = {'close', 'close'}; session.q={{1,2,{3,4}},{{z={{5}}}}};"); l.call(0,0); - FbTk::Resource asdf(man, 123, "asdf", "ASDF"); - cout << "asdf: " << asdf << endl; + man.save("/dev/stdout", NULL); + FbTk::Resource, FbTk::VectorTraits > > asdf(man, std::vector(), "asdf", "ASDF"); + cout << "asdf: " << asdf.getString() << endl; man.save("/dev/stdout", NULL); return 0; -- cgit v0.11.2