Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- newLibCFG 1.0.0 by Admicos.
- --
- -- table keys, [table comments, number line count] if not simple readConfigRaw(string configStr, bool simple)
- -- the heart of the API. Will parse the configStr.
- --
- -- table keys, [table comments, number line count] if not simple readConfig(string configFile, bool simple)
- -- file wrapper for readConfigRaw
- --
- -- nil writeConfig(file, keys, comments, len)
- -- writes keys/comments to config, must be used without simple
- --
- -- --
- --
- -- if you are NOT writing to a file, it's recommended to run the readConfig() or
- -- readConfigRaw() functions with the second argument "true", meaning "simple" as
- -- it will just return the table similar to the original libCFG, shown below
- --
- -- "simple" or original libCFG:
- -- table["key"] = value
- --
- -- newer approach, recommended for file modification
- -- table[linenumber][1] = key
- -- table[linenumber][2] = value
- local function _split(str, splitter)
- local t = {}
- local function helper(line) table.insert(t, line) return "" end
- helper((str:gsub("(.-)" .. splitter, helper)))
- return t
- end
- local function _trim(s)
- return (s:gsub("^%s*(.-)%s*$", "%1"))
- end
- function readConfigRaw(string, simple)
- local comments = {}
- local keys = {}
- local stringAsLines = _split(string, "\n")
- local line = ""
- for i = 1, #stringAsLines do
- line = _trim(stringAsLines[i])
- if line:sub(1, 1) == "#" then --Line is a comment
- if not simple then comments[i] = line:sub(2, #line) end
- elseif line ~= "" then --Line is not a comment nor empty.
- local l = _split(line, ":")
- if #l <= 1 then error("File format is incorrect") end
- local k = table.remove(l, 1)
- local v = _trim(table.concat(l, ":"))
- if simple then keys[_trim(k)] = v
- else keys[i] = {_trim(k), v}
- end
- end
- end
- line = nil
- if simple then return keys
- else return keys, comments, #stringAsLines
- end
- end
- function readConfig(file, simple)
- local file = fs.open(file, "r")
- if file == nil then error("Couldn't open file") end
- local parsed = {readConfigRaw(file.readAll(), simple or false)}
- file.close()
- return table.unpack(parsed)
- end
- function writeConfig(file, keys, comments, len)
- local w_file = fs.open(file, "w")
- if w_file == nil then error("Couldn't open file") end
- for i = 1, #file + #comments do
- if comments[i] then w_file.writeLine("#" .. comments[i])
- elseif keys[i] then w_file.writeLine(keys[i][1] .. ": " .. keys[i][2])
- else w_file.writeLine()
- end
- end
- w_file.close()
- end
- local function simple_test()
- local keys = readConfigRaw([[
- #a
- #a:b
- hello: world
- ]], true)
- for key, value in pairs(keys) do
- stringStuff.printWithFormat("&8" .. key .. "&5 = &0" .. value)
- end
- end
- local function test()
- local keys, comments, len = readConfigRaw([[
- #newlibCFG Example config
- #lines starting with hashes are comments
- #Here is an example config entry
- key: value
- #Here is an example config that could be used in a program
- saveDirectory: /program/saves
- saveInterval: 10
- #Check the forum post for usage.
- ]])
- for i = 1, len do
- if comments[i] then stringStuff.printWithFormat("&d#" .. comments[i])
- elseif keys[i] then stringStuff.printWithFormat("&8" .. keys[i][1] .. "&5 = &0" .. keys[i][2])
- else print()
- end
- end
- print("Trying to write to 'test.cfg'")
- writeConfig(shell.resolve("test.cfg"), keys, comments, len)
- end
- if shell ~= nil then --not loaded as API, so run test.
- local a = { ... }
- if #a == 1 and a[1] == "simple" then simple_test()
- else test()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement