Read INI

ReadINI parses ini formatted text files into a table.

ReadIni(inputfile)

The returned table is in the format Table[Section][Parameter] = Value

function ReadIni(inputfile)
local file = assert(io.open(inputfile, 'r'), 'Unable to open ' .. inputfile)
local tbl, section = {}
local num = 0
for line in file:lines() do
num = num + 1
if not line:match('^%s-;') then
local key, command = line:match('^([^=]+)=(.+)')
if line:match('^%s-%[.+') then
section = line:match('^%s-%[([^%]]+)'):lower()
if not tbl[section] then tbl[section] = {} end
elseif key and command and section then
tbl[section][key:lower():match('^s*(%S*)%s*$')] = command:match('^s*(.-)%s*$')
elseif #line > 0 and section and not key or command then
print(num .. ': Invalid property or value.')
end
end
end
if not section then print('No sections found in ' .. inputfile) end
file:close()
return tbl
end