Created
November 1, 2010 13:42
-
-
Save zeen/658188 to your computer and use it in GitHub Desktop.
INI parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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