Update config.lua

This commit is contained in:
Ale32bit 2016-11-02 21:02:27 +01:00 committed by GitHub
parent 3f821c3c7b
commit 6bbcf55c39

View file

@ -1,141 +1,58 @@
--Config API from LMNetOS function write(file,config,value)
local data = {}
if fs.isDir(file) then
return false
end
if fs.exists(file) then
local f = fs.open(file,"r")
local con = f.readAll()
f.close()
con = textutils.unserialize(con)
if not con then
return false
end
data = con
end
function read(cfgfile, cfg) data[config] = value
local rtn local f = fs.open(file,"w")
local ok, err = pcall(function() f.write(textutils.serialize(data))
local path f.close()
if not cfgfile then return true
path = "/.lmnet/sys.conf"
else
path = cfgfile
end end
local file = fs.open(path, "r")
if not file then function read(file,config)
local data = {}
if fs.isDir(file) then
return nil return nil
end end
local lines = {} if fs.exists(file) then
local line = "" local f = fs.open(file,"r")
while line ~= nil do local con = f.readAll()
line = file.readLine() f.close()
if line then con = textutils.unserialize(con)
table.insert(lines, line) if not con then
end
end
file.close()
local config = {}
for _, v in pairs(lines) do
local tmp
local tmp2 = ""
for match in string.gmatch(v, "[^\=]+") do
if tmp then
tmp2 = tmp2..match
else
tmp = match
end
end
config[tmp] = textutils.unserialize(tostring(tmp2))
end
for i, v in pairs(config) do
if i == cfg then
rtn = v
return
end
end
end)
if not ok then
return false, err
end
return rtn
end
function write(cfgfile, cfg, value)
local rtn = false
local ok, err = pcall(function()
local path
if not cfgfile then
path = "/.lmnet/sys.conf"
else
path = cfgfile
end
if fs.exists(path) and fs.isDir(path) then
error(path.." is a directory", 0)
end
local readfile = fs.open(path, "r")
local readlines = {}
if readfile then
local readline = ""
while readline ~= nil do
readline = readfile.readLine()
if readline then
table.insert(readlines, readline)
end
end
readfile.close()
end
local config = {}
if fs.exists(path) and not fs.isDir(path) then
for _, v in pairs(readlines) do
local tmp
local tmp2 = ""
for match in string.gmatch(v, "[^\=]+") do
if tmp then
tmp2 = tmp2..match
else
tmp = match
end
end
config[tmp] = textutils.unserialize(tostring(tmp2))
end
end
local writefile = fs.open(path, "w")
config[cfg] = value
table.sort(config)
for i, v in pairs(config) do
writefile.writeLine(i.."="..textutils.serialize(v))
end
writefile.close()
rtn = true
end)
return rtn
end
function list(cfgfile)
local rtn
local ok, err = pcall(function()
local path
if not cfgfile then
path = "/.lmnet/sys.conf"
else
path = cfgfile
end
local file = fs.open(path, "r")
if not file then
return nil return nil
end end
local lines = {} data = con
local line = ""
while line ~= nil do
line = file.readLine()
if line then
table.insert(lines, line)
end end
return data[config]
end end
file.close()
local config = {} function list(file)
for _, v in pairs(lines) do local data = {}
local tmp if fs.isDir(file) then
local tmp2 = "" return nil
for match in string.gmatch(v, "[^\=]+") do
if tmp then
tmp2 = tmp2..match
else
tmp = match
end end
if fs.exists(file) then
local f = fs.open(file,"r")
local con = f.readAll()
f.close()
con = textutils.unserialize(con)
if not con then
return nil
end end
config[tmp] = textutils.unserialize(tostring(tmp2)) data = con
end end
rtn = config return data
end)
if not ok then
return false, err
end
return rtn
end end