jonnathonchristopher

Config API

Dec 3rd, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.90 KB | None | 0 0
  1. --[[
  2. Program: Config API
  3. Version: 1.0
  4. Author:  ben657
  5. Support:
  6.  
  7. Description: This API allows you to easily make configs for
  8.          your users to edit, rather than them having to
  9.          read code and edit variables. These files can
  10.              then be read by this API and the values can be
  11.          used in your programs.
  12.                          
  13. Note: Currently, tables cannot be written to configs, but in
  14.       the future this functionality may be implemented.
  15. --]]
  16.  
  17. loaded = false
  18. dir = ""
  19. file = ""
  20. path = ""
  21. internal = {}
  22.  
  23.  function create()
  24.     local file = fs.open(path,"a")
  25.     file.close()
  26.  end
  27.  
  28.  --[[
  29.  Attempts to load the config using the specified directory
  30.  and file name, and load the contents into internal memory.
  31.  If the path is not found, it will be created.
  32.  --]]
  33. function load(directory,fileName)
  34.     dir = directory
  35.     file = fileName
  36.     path = dir.."/"..file
  37.     fs.makeDir(dir)
  38.     create(path)
  39.     local file = fs.open(path, "r")
  40.     repeat
  41.         line = file.readLine()
  42.         if(line ~= nil) then
  43.             local asWords = line:gsub(":","")
  44.             local parts = {}
  45.             for word in asWords:gmatch("%w+") do table.insert(parts,word) end
  46.             internal[parts[1]] = parts[2]
  47.         end
  48.     until line == nil
  49.     loaded = true
  50.     file.close()
  51. end
  52.  
  53. --[[
  54. Attempts to write the specified value to the specified key in the
  55. internal config.
  56. Returns true if successful.
  57. Returns false if no config is loaded.
  58. This only writes to the config in memory, not the file. To save
  59. to file, call config.save().
  60. --]]
  61. function writeVal(key,value)
  62.     if(loaded == false) then
  63.         return false
  64.     else
  65.         local toWrite = value
  66.         if(value == true) then toWrite = "true"
  67.         elseif(value == false) then toWrite = "false" end
  68.         internal[key] = toWrite
  69.         return true
  70.     end
  71. end
  72.  
  73. --[[
  74. Attempts to read a value from the internal config and return it.
  75. Returns the value assigned to the specified key if successful.
  76. Returns nil if no config is loaded or no value was found at the key.
  77. --]]
  78. function readVal(key)
  79.     if(loaded == false) then
  80.         return nil
  81.     end
  82.     toReturn = internal[key]
  83.     if(toReturn == "true") then return true
  84.     elseif(toReturn == "false") then return false
  85.     else return internal[key] end
  86. end
  87.  
  88. --[[
  89. Attempts to save the internal config to the loaded file path.
  90. Returns true if successful.
  91. Returns false if no config is loaded.
  92. The internal config is unloaded after saving, so config.load()
  93. must be called again to use the config.
  94. --]]
  95. function save()
  96.     if(loaded == true) then
  97.         local file = fs.open(path,"w")
  98.         for i,v in pairs(internal) do
  99.             file.writeLine(i.." = "..v)
  100.         end
  101.         file.close()
  102.         internal = {}
  103.         loaded = false
  104.         return true
  105.     else
  106.         return false
  107.     end
  108. end
  109.  
  110. --[[
  111. Returns true if the config has no data.
  112. Returns false if the config contains any data.
  113. Returns nil if no config is loaded.
  114. --]]
  115. function isEmpty()
  116.     if(loaded == false) then
  117.         return nil
  118.     end
  119.     if(fs.getSize(path) == 0)then
  120.         return true
  121.     else return false end
  122. end
Add Comment
Please, Sign In to add comment