Posted by member 12025 on 2006-12-13 09:53:13
Another really simple one. It saves out a table, and all subtables, to evars.
Example:
Becomes:
from calling tabletorc (_G.sometable.this,"this","C:\\test.rc")
Example:
_G.sometable.this = _G.sometable.this or {};
this.x = 50
this.y = 100
this.w = 150
this.h = 75
this.x = 50
this.y = 100
this.w = 150
this.h = 75
Becomes:
this.y "100"
this.h "75"
this.w "150"
this.x "50"
this.h "75"
this.w "150"
this.x "50"
from calling tabletorc (_G.sometable.this,"this","C:\\test.rc")
tabletorc = function (tbl,name,path)
local lines = {}
local list = { [tostring (tbl)] = true }
-- list of traversed tables to keep from infinite loops
-- Ex: _G._G = _G
for k,v in pairs (tbl) do
tabletorctext (name,k,v,lines,list)
end
local f = io.open (path,"w")
f:write (table.concat (lines,"\\n"))
f:close()
end
tabletorctext = function (tbl,k,v,lines,list)
local t = type (v)
if t == "string" or t == "number" or t == "boolean" then
v = tostring (v)
if string.len (v) ~= 0 then
table.insert (lines, tbl.."."..k.." \\""..string.gsub (v,"\\n","").."\\"")
end
elseif t == "table" and not list [tostring (v)] then
list[tostring(v)] = true
for kk,vv in pairs (v) do
tabletorctext (tbl.."."..k,kk,vv,lines,list)
end
end
end
local lines = {}
local list = { [tostring (tbl)] = true }
-- list of traversed tables to keep from infinite loops
-- Ex: _G._G = _G
for k,v in pairs (tbl) do
tabletorctext (name,k,v,lines,list)
end
local f = io.open (path,"w")
f:write (table.concat (lines,"\\n"))
f:close()
end
tabletorctext = function (tbl,k,v,lines,list)
local t = type (v)
if t == "string" or t == "number" or t == "boolean" then
v = tostring (v)
if string.len (v) ~= 0 then
table.insert (lines, tbl.."."..k.." \\""..string.gsub (v,"\\n","").."\\"")
end
elseif t == "table" and not list [tostring (v)] then
list[tostring(v)] = true
for kk,vv in pairs (v) do
tabletorctext (tbl.."."..k,kk,vv,lines,list)
end
end
end