From 6017c4b385b7107faf2feb1f3a77293bc15a2efd Mon Sep 17 00:00:00 2001 From: Ale32bit Date: Fri, 25 Sep 2015 20:18:02 +0200 Subject: [PATCH] Create config.lua --- src/apis/config.lua | 141 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 src/apis/config.lua diff --git a/src/apis/config.lua b/src/apis/config.lua new file mode 100644 index 0000000..39706ea --- /dev/null +++ b/src/apis/config.lua @@ -0,0 +1,141 @@ +--Config API from LMNetOS + +function read(cfgfile, cfg) + 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 + end + local lines = {} + local line = "" + while line ~= nil do + line = file.readLine() + if line then + table.insert(lines, line) + 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 + end + local lines = {} + local line = "" + while line ~= nil do + line = file.readLine() + if line then + table.insert(lines, line) + 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 + rtn = config + end) + if not ok then + return false, err + end + return rtn +end