Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ USAGE:
- confAPI.makeConfig( path ) - Makes an empty config file at the specified path
- confAPI.loadConfig( path ) - returns the config table from the path
- confAPI.addConfigLine( path, suffix, value) - Add a config value to the end of the file
- confAPI.modifyConfigLine( path, line, suffix, value) - Re-writes a config entry at a specific line
- confAPI.getValue( t, line, isNumber) - Returns the config value from a specific line without it's identifier | returns string if false eg: test = confAPI.getValue(loadConfig('/config'), 2, false)
- confAPI.changeValue( path, line, value ) - Changes the value on a specific line but doesnt change the identifier
- ]]--
- local function exists(path)
- local file = assert(io.open(path, "r"))
- if file ~= nil then
- file:close()
- return true
- end
- return false
- end
- local function getTable(path)
- if exists(path) then
- local file = io.open(path, "r")
- local lines = {}
- local i = 1
- local line = file:read("*l")
- while line ~= nil do
- lines[i] = line
- line = file:read("*l")
- i = i + 1
- end
- file:close()
- return lines
- end
- return {}
- end
- local function getLine(path, n)
- if exists(path) then
- local lines = getTable(path)
- return lines[n]
- end
- return ""
- end
- local function fwrite(path, text)
- local file = assert(io.open(path, "w"))
- file:write(text)
- file:close()
- end
- local function fwriteFromTable(path, t)
- local text = ""
- for _, line in pairs(t) do
- text = text..line.."\n"
- end
- fwrite(path, text)
- end
- -- BEGINNING OF CODE
- function makeConfig( path )
- fwrite(path, "")
- return true
- end
- function loadConfig( path )
- _conf = getTable(path)
- return _conf
- end
- function addConfigLine( path, suffix, value)
- _conf = getTable( path )
- _conf[#_conf+1] = suffix..": "..value
- fwriteFromTable(path, _conf)
- return _conf
- end
- function modifyConfigLine( path, line, suffix, value)
- _conf = getTable( path )
- _conf[line] = suffix..": "..value
- fwriteFromTable(path, _conf)
- return _conf
- end
- function getValue( t, line, isNumber)
- if isNumber then
- _val = tonumber(string.sub(t[line], string.find(t[line], ":")+2))
- else
- _val = string.sub(t[line], string.find(t[line], ":")+2)
- end
- return _val
- end
- function changeValue( path, line, value )
- t = getTable( path )
- _str = string.sub(t[line],1, string.find(t[line], ":"))
- t[line] = _str.." "..value
- fwriteFromTable(path, t)
- return t
- end
- -- Usage CODE
- local tArgs = { ... }
- if #tArgs == 1 then
- print ("USAGE: help 1, help 2, help 3")
- elseif #tArgs == 2 then
- if tArgs[1] == "help" or "?" or "HELP" or "Help" then
- term.clear()
- term.setCursorPos(1,1)
- if tArgs[2] == "1" then
- print ("USAGE: makeConfig( path )")
- print (" ")
- print (" loadConfig( path ) -- returns table from path")
- print (" ")
- print (" addConfigLine( path, suffix, value) -- Add a config value to the end of the file")
- elseif tArgs[2] == "2" then
- print (" modifyConfigLine( path, line, suffix, value) -- Re-writes a config entry at a specific line")
- print (" ")
- print (" getValue( t, line, isNumber) -- Returns the config value from a specific line without it's identifier | returns string if false")
- print (" eg: test = getValue(loadConfig('config'), 2, false")
- elseif tArgs[2] == "3" then
- print (" changeValue( path, line, value ) -- Changes the value on a specific line but doesnt change the identifier")
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement