-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathini.lua
45 lines (39 loc) · 1.07 KB
/
ini.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module(..., package.seeall)
function get(filename)
local f, err = io.open(filename, 'r')
if not f then return nil, err end
local initable = {}
local current_section
for l in f:lines() do
local i = l:find("#")
if i then l = l:sub(1, i - 1) end
l = l:match("^%s*(.-)%s*$")
if l ~= "" then
local section = l:match("^%[([_%w%s]*)%]$")
if section then
current_section = section
if not initable[current_section] then initable[current_section] = {} end
else
local key, value = l:match("([^=]*)%=(.*)")
key = key:match("^%s*(.-)%s*$")
value = value:match("^%s*(.-)%s*$")
if key and value and current_section then
initable[current_section][key] = value
end
end
end
end
f:close()
return initable
end
function set(filename, initable)
local f, err = io.open(filename, 'w')
if not f then return nil, err end
for section, section_table in pairs(initable) do
f:write("[" .. tostring(section) .. "]\n")
for key, value in pairs(section_table) do
f:write(tostring(key) .. "=" .. tostring(value) .. "\n")
end
end
f:close()
end