Advertisement
ndfjay

confAPI

Dec 3rd, 2015
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.80 KB | None | 0 0
  1. --[[ USAGE:
  2. confAPI.makeConfig( path ) - Makes an empty config file at the specified path
  3.  
  4. confAPI.loadConfig( path ) - returns the config table from the path
  5.  
  6. confAPI.addConfigLine( path, suffix, value) - Add a config value to the end of the file
  7.  
  8. confAPI.modifyConfigLine( path, line, suffix, value) - Re-writes a config entry at a specific line
  9.  
  10. 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)
  11.  
  12. confAPI.changeValue( path, line, value ) - Changes the value on a specific line but doesnt change the identifier
  13.  
  14. ]]--
  15.  
  16. local function exists(path)
  17.         local file = assert(io.open(path, "r"))
  18.         if file ~= nil then
  19.                 file:close()
  20.                 return true
  21.         end
  22.        
  23.         return false
  24. end
  25.  
  26. local function getTable(path)
  27.         if exists(path) then
  28.                 local file = io.open(path, "r")
  29.                 local lines = {}
  30.                 local i = 1
  31.                 local line = file:read("*l")
  32.                 while line ~= nil do
  33.                         lines[i] = line
  34.                         line = file:read("*l")
  35.                         i = i + 1
  36.                 end
  37.                 file:close()
  38.                 return lines
  39.         end
  40.         return {}
  41. end
  42.  
  43. local function getLine(path, n)
  44.         if exists(path) then
  45.                 local lines = getTable(path)
  46.                 return lines[n]
  47.         end
  48.         return ""
  49. end
  50.  
  51.  
  52. local function fwrite(path, text)
  53.         local file = assert(io.open(path, "w"))
  54.         file:write(text)
  55.         file:close()
  56. end
  57.  
  58.  
  59. local function fwriteFromTable(path, t)
  60.         local text = ""
  61.         for _, line in pairs(t) do
  62.                 text = text..line.."\n"
  63.         end
  64.         fwrite(path, text)
  65. end
  66.  
  67. -- BEGINNING OF CODE
  68.  
  69. function makeConfig( path )
  70.     fwrite(path, "")
  71.     return true
  72. end
  73.  
  74. function loadConfig( path )
  75.     _conf = getTable(path)
  76.     return _conf
  77. end
  78.  
  79. function addConfigLine( path, suffix, value)
  80.     _conf = getTable( path )
  81.     _conf[#_conf+1] = suffix..": "..value
  82.     fwriteFromTable(path, _conf)
  83.     return _conf
  84. end
  85.  
  86. function modifyConfigLine( path, line, suffix, value)
  87.     _conf = getTable( path )
  88.     _conf[line] = suffix..": "..value
  89.     fwriteFromTable(path, _conf)
  90.     return _conf
  91. end
  92.  
  93. function getValue( t, line, isNumber)
  94.     if isNumber then
  95.         _val = tonumber(string.sub(t[line], string.find(t[line], ":")+2))
  96.     else
  97.         _val = string.sub(t[line], string.find(t[line], ":")+2)
  98.     end
  99.     return _val
  100. end
  101.  
  102. function changeValue( path, line, value )
  103.     t = getTable( path )
  104.     _str = string.sub(t[line],1, string.find(t[line], ":"))
  105.     t[line] = _str.." "..value
  106.     fwriteFromTable(path, t)
  107.  
  108.  
  109.     return t
  110. end
  111.  
  112. -- Usage CODE
  113. local tArgs = { ... }
  114.  
  115. if #tArgs == 1 then
  116.     print ("USAGE: help 1, help 2, help 3")
  117.     elseif #tArgs == 2 then
  118.     if tArgs[1] == "help" or "?" or "HELP" or "Help" then
  119.         term.clear()
  120.         term.setCursorPos(1,1)
  121.         if tArgs[2] == "1" then
  122.         print ("USAGE: makeConfig( path )")
  123.         print (" ")
  124.         print ("       loadConfig( path ) -- returns table from path")
  125.         print (" ")
  126.         print ("       addConfigLine( path, suffix, value) -- Add a config value to the end of the file")
  127.         elseif tArgs[2] == "2" then
  128.         print ("       modifyConfigLine( path, line, suffix, value) -- Re-writes a config entry at a specific line")
  129.         print (" ")
  130.         print ("       getValue( t, line, isNumber) -- Returns the config value from a specific line without it's identifier | returns string if false")
  131.         print ("       eg: test = getValue(loadConfig('config'), 2, false")
  132.         elseif tArgs[2] == "3" then
  133.         print ("       changeValue( path, line, value ) -- Changes the value on a specific line but doesnt change the identifier")
  134.     end
  135.     end
  136. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement