aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk
diff options
context:
space:
mode:
authorPavel Labath <pavelo@centrum.sk>2011-06-11 17:08:39 (GMT)
committerPavel Labath <pavelo@centrum.sk>2013-02-16 23:49:23 (GMT)
commitb752c9844890fbd0df1628e8ac5679a50c064389 (patch)
treee02b37fba15723281465a946a1b17e5ea7bb3eed /src/FbTk
parenteb0eef14134ee667c60f75f53de4e24950f3c117 (diff)
downloadfluxbox_pavel-b752c9844890fbd0df1628e8ac5679a50c064389.zip
fluxbox_pavel-b752c9844890fbd0df1628e8ac5679a50c064389.tar.bz2
Add support for array lua resources
Diffstat (limited to 'src/FbTk')
-rw-r--r--src/FbTk/LResourceHelper.lua77
-rw-r--r--src/FbTk/ResTraits.hh4
2 files changed, 48 insertions, 33 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)
7end; 7end;
8 8
9local function check_arg(table, key) 9local function check_arg(table, key)
10 if type(key) ~= 'string' then 10 if type(key) ~= 'string' and type(key) ~= 'number' then
11 myerror(table, 'expecting strings as keys.'); 11 myerror(table, 'expecting strings as keys.');
12 end; 12 end;
13 if string.match(key, "^_") then 13 if string.match(key, "^_") then
@@ -53,11 +53,21 @@ local function index(table, key)
53 return t; 53 return t;
54end; 54end;
55 55
56local function append_name(str, name)
57 if type(name) == 'string' and string.match(name, '^%a+$') then
58 return str .. '.' .. name;
59 elseif type(name) == 'number' then
60 return str .. '[' .. string.format('%g', name) .. ']';
61 else
62 return nil;
63 end;
64end;
65
56new_cat = function(table, key) 66new_cat = function(table, key)
57 local meta = getmetatable(table); 67 local meta = getmetatable(table);
58 local mt = { 68 local mt = {
59 __newindex = newindex, __index = index, 69 __newindex = newindex, __index = index,
60 _magic = cat_magic, _fullname = meta._fullname .. '.' .. key, _state = 0 70 _magic = cat_magic, _fullname = append_name(meta._fullname, key), _state = 0
61 }; 71 };
62 meta[key] = setmetatable({}, mt); 72 meta[key] = setmetatable({}, mt);
63 return meta[key]; 73 return meta[key];
@@ -69,10 +79,18 @@ local function register_resource(root, name, object)
69 79
70 local head, tail = string.match(name, '^(%a+)%.?(.*)'); 80 local head, tail = string.match(name, '^(%a+)%.?(.*)');
71 local t = meta[head]; 81 local t = meta[head];
82 local mt = getmetatable(t);
83 if mt ~= nil and mt._magic == cat_magic then
84 t = mt;
85 end;
86
72 if tail == '' then 87 if tail == '' then
73 meta[head] = object;
74 if getmetatable(object) == res_magic then 88 if getmetatable(object) == res_magic then
89 meta[head] = object;
75 write_resource(object, t); 90 write_resource(object, t);
91 else
92 meta[head] = nil;
93 root[head] = object;
76 end; 94 end;
77 return t; 95 return t;
78 end; 96 end;
@@ -83,44 +101,41 @@ local function register_resource(root, name, object)
83 return register_resource(t, tail, object); 101 return register_resource(t, tail, object);
84end; 102end;
85 103
86local function dump_value(val) 104local function dump_(key, value, fd)
87 if type(val) == "string" then 105 if type(value) == 'string' then
88 return string.format('%q', val); 106 fd:write(key, ' = ', string.format('%q', value), '\n');
89 elseif type(val) == "number" then 107 elseif type(value) == 'number' then
90 return string.format('%g', val); 108 fd:write(key, ' = ', string.format('%g', value), '\n');
91 else 109 elseif type(value) == 'table' then
92 error('Unsupported value type: ' .. type(val)); 110 fd:write(key, ' = {}\n');
93 end; 111 for k, v in pairs(value) do
94end; 112 k = append_name(key, k);
95
96local function dump_(root, fd)
97 local meta = getmetatable(root);
98 if not string.match(meta._fullname, "^[%a.]+$") then
99 error("Someone has been messing with metatables.");
100 end;
101 113
102 for k, v in pairs(meta) do
103 if type(k) == "string" and string.match(k, "^%a+$") then
104 local mt = getmetatable(v); 114 local mt = getmetatable(v);
105
106 fd:write(meta._fullname, '.', k, ' = ');
107 if mt ~= nil and mt._magic == cat_magic then 115 if mt ~= nil and mt._magic == cat_magic then
108 fd:write('{}\n'); 116 v = mt;
109 dump(v); 117 elseif mt == res_magic then
110 fd:write('\n'); 118 v = read_resource(v);
111 else 119 end;
112 if mt == res_magic then 120
113 v = read_resource(v); 121 if k ~= nil then
114 end; 122 dump_(k, v, fd);
115 fd:write(dump_value(v), '\n');
116 end; 123 end;
117 end; 124 end;
125 fd:write('\n');
126 else
127 error('Unsupported value type: ' .. type(val));
118 end; 128 end;
119end; 129end;
120 130
121local function dump(root, file) 131local function dump(root, file)
122 local fd = io.open(file, 'w'); 132 local fd = io.open(file, 'w');
123 dump_(root, fd); 133 local meta = getmetatable(root);
134 if not string.match(meta._fullname, "^[%a]+$") then
135 error("Someone has been messing with metatables.");
136 end;
137
138 dump_(meta._fullname, meta, fd);
124 fd:close(); 139 fd:close();
125end; 140end;
126 141
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 {
163 163
164 for(size_t i = 0; i < x.size(); ++i) { 164 for(size_t i = 0; i < x.size(); ++i) {
165 Traits::toLua(x[i], l); 165 Traits::toLua(x[i], l);
166 l.rawseti(-2, i); 166 l.rawseti(-2, i+1);
167 } 167 }
168 } 168 }
169 169
@@ -189,7 +189,7 @@ struct VectorTraits {
189 Type retval; 189 Type retval;
190 190
191 if(l.type(-1) == lua::TTABLE) { 191 if(l.type(-1) == lua::TTABLE) {
192 for(size_t i = 0; l.rawgeti(-1, i), !l.isnil(-1); ++i) { 192 for(size_t i = 1; l.rawgeti(-1, i), !l.isnil(-1); ++i) {
193 try { 193 try {
194 retval.push_back(Traits::fromLua(l)); 194 retval.push_back(Traits::fromLua(l));
195 } 195 }