Last active
August 18, 2018 15:43
-
-
Save DaeZak/787cefcdec28a9f505a29aca3aefec45 to your computer and use it in GitHub Desktop.
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
--[==[ | |
This lib provides two functions: | |
* load_file(file) -- parses an ini file and returns a table | |
* parse(data) -- parses string data in ini format and returns a table | |
A few notes: iniparse will allow ; and # style comments, it does not allow duplicate sections and it will overwrite | |
duplicate keys/values with the last given key/value pair. | |
--]==] | |
local c={}local d=function(f)return string.gsub(f,"^%s*(.-)%s*$","%1")end local e=function(f)return string.gsub(f,"^(.-)%s*([;#].*)$","%1")end c.config=function(f)c.opts={trim=f.trim==nil and true or f.trim,lc=f.lc==nil and true or f.lc}end c.load_file=function(f)assert(type(f)=='string','load_file given non-string arg')local g=assert(io.open(f,'r'),'Error opening'..f)c.parse(g:read('*all'))end c.parse=function(f)assert(type(f)=='string','parse given non-string arg')local g={}local h=nil for i in f:gmatch("([^\n]*)\r?\n")do local j=c.opts.trim and d(i)or i j=c.opts.lc and j:lower()or j j=e(j)if j:len()>0 then local k=string.match(j,"^%[([%w%s%p]*)%]")if k~=nil then if g[k]~=nil then error('Invalid ini - duplicate section '..k)end g[k]={}h=k else a,b=string.match(j,"^([%w%s%p]*)=([%w%s%p]*)$")a=c.opts.trim and d(a)or a b=c.opts.trim and d(b)or b if h==nil then g[a]=b else g[h][a]=b end end end end return g end c.config{}return c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment