Skip to content

Instantly share code, notes, and snippets.

@zeen
Created November 1, 2010 13:42
Show Gist options
  • Save zeen/658188 to your computer and use it in GitHub Desktop.
Save zeen/658188 to your computer and use it in GitHub Desktop.
INI parser
function ini_parse(text)
text = "[]\n"..text:gsub(";[^\n]*\n", ""); -- remove comments and add dummy section
local result = {};
local section = "";
local lineno = 0;
for line in text:gmatch("([^\n]+)") do
lineno = lineno + 1;
local block = line:match("^%[([^%]*)%]$");
if block then
section = block;
result[section] = result[section] or {};
else
local k,v = line:match("^([^=]*)=(.*)$");
if not k then return nil, "parse error on line "..lineno; end
result[section][k] = v;
end
end
return result;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment