-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Bart van Strien
committed
Dec 6, 2011
1 parent
d8154f1
commit 3151950
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
inifile = {} | ||
|
||
local lines | ||
local write | ||
|
||
if love then | ||
lines = love.filesystem.lines | ||
write = love.filesystem.write | ||
else | ||
lines = function(name) return assert(io.open(name)):lines() end | ||
write = function(name, contents) return assert(io.open(name, "w")):write(contents) end | ||
end | ||
|
||
function inifile.parse(name) | ||
local t = {} | ||
local section | ||
for line in lines(name) do | ||
local s = line:match("^%[([^%]]+)%]$") | ||
if s then | ||
section = s | ||
t[section] = t[section] or {} | ||
end | ||
local key, value = line:match("^(%w+)%s-=%s-(.+)$") | ||
if tonumber(value) then value = tonumber(value) end | ||
if value == "true" then value = true end | ||
if value == "false" then value = false end | ||
if key and value then | ||
t[section][key] = value | ||
end | ||
end | ||
return t | ||
end | ||
|
||
function inifile.save(name, t) | ||
local contents = "" | ||
for section, s in pairs(t) do | ||
local sec = ("[%s]\n"):format(section) | ||
for key, value in pairs(s) do | ||
sec = sec .. ("%s=%s\n"):format(key, tostring(value)) | ||
end | ||
contents = contents .. sec .. "\n" | ||
end | ||
write(name, contents) | ||
end | ||
|
||
return inifile |