I wrote some simple iterator to fetch all keys and values from et config like files: (key “value” etc)
I don’t know if this is what you wanted tho, but here are sample usage and code:
Usage (should be used in for loop):
-- use it in for loop to fetch all keys and values
for [B]key[/B], [B]value [/B]in [I]readconfig [/I][B]"sample.cfg"[/B] do
-- you can fill your table here as well as do some value check
configTable [[B]key[/B]] = [B]value[/B]
end
function readconfig (fileName)
fd, len = et.trap_FS_FOpenFile(fileName, et.FS_READ)
if len ~= -1 then
filedata = et.trap_FS_Read(fd, len)
end
et.trap_FS_FCloseFile(fd)
if not filedata then return nil end
local lines = string.gmatch(filedata, "[^
]+")
local i = 0
-- basic iterator + line iterator
return function ()
while true do
local key, value = parseConfig ( lines (i + 1) )
-- skipping empty strings ...
if key then
if key ~= "" then
return key, value
end
else
return nil
end
end
end
end
function parseConfig (line)
if not line then return nil end
-- lets clean double slash comment, "set" and "exec" keywords
line = line :gsub ("//.+", "")
:gsub ("^%s*set%s+", "")
:gsub ("^%s*exec%s+.+", "")
:gsub ("^%s*(.-)%s*$", "%1") -- trim string
-- skip
if line == "" then return "" end
local key, value = line:match "([%w_]+)%s+(.+)"
if key and value then
local temp = tonumber (value)
if temp then
return key, temp
else
temp = value:match "^\"(.+)\"$"
-- all string values should be enclosed with quotes
if temp then
-- trying to conver "16" string numbers into numbers
value, temp = temp, tonumber (temp)
if temp then
value = temp
end
return key, value
elseif value == '""' then
-- empty values
return key, ""
end
end
end
return ""
end