Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement