Advertisement
gknova61

config API

Nov 26th, 2012
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.30 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.     if dir ~= nil then
  37.         path = dir.."/"..file
  38.         fs.makeDir(dir)
  39.         create(path)
  40.         local file = fs.open(path, "r")
  41.         repeat
  42.             line = file.readLine()
  43.             if(line ~= nil) then
  44.                 local asWords = line:gsub(":","")
  45.                 local parts = {}
  46.                 for word in asWords:gmatch("%w+") do table.insert(parts,word) end
  47.                 internal[parts[1]] = parts[2]
  48.             end
  49.         until line == nil
  50.         loaded = true
  51.         file.close()
  52.     else
  53.         path = file
  54.         create(path)
  55.         local file = fs.open(path, "r")
  56.         repeat
  57.             line = file.readLine()
  58.             if(line ~= nil) then
  59.                 local asWords = line:gsub(":","")
  60.                 local parts = {}
  61.                 for word in asWords:gmatch("%w+") do table.insert(parts,word) end
  62.                 internal[parts[1]] = parts[2]
  63.             end
  64.         until line == nil
  65.         loaded = true
  66.         file.close()
  67.     end
  68. end
  69.  
  70. --[[
  71. Attempts to write the specified value to the specified key in the
  72. internal config.
  73. Returns true if successful.
  74. Returns false if no config is loaded.
  75. This only writes to the config in memory, not the file. To save
  76. to file, call config.save().
  77. --]]
  78. function writeVal(key,value)
  79.     if(loaded == false) then
  80.         return false
  81.     else
  82.         local toWrite = value
  83.         if(value == true) then toWrite = "true"
  84.         elseif(value == false) then toWrite = "false" end
  85.         internal[key] = toWrite
  86.         return true
  87.     end
  88. end
  89.  
  90. --[[
  91. Attempts to read a value from the internal config and return it.
  92. Returns the value assigned to the specified key if successful.
  93. Returns nil if no config is loaded or no value was found at the key.
  94. --]]
  95. function readVal(key)
  96.     if(loaded == false) then
  97.         return nil
  98.     end
  99.     toReturn = internal[key]
  100.     if(toReturn == "true") then return true
  101.     elseif(toReturn == "false") then return false
  102.     else return internal[key] end
  103. end
  104.  
  105. --[[
  106. Attempts to save the internal config to the loaded file path.
  107. Returns true if successful.
  108. Returns false if no config is loaded.
  109. The internal config is unloaded after saving, so config.load()
  110. must be called again to use the config.
  111. --]]
  112. function save()
  113.     if(loaded == true) then
  114.         local file = fs.open(path,"w")
  115.         for i,v in pairs(internal) do
  116.             file.writeLine(i.." = "..v)
  117.         end
  118.         file.close()
  119.         internal = {}
  120.         loaded = false
  121.         return true
  122.     else
  123.         return false
  124.     end
  125. end
  126.  
  127. --[[
  128. Returns true if the config has no data.
  129. Returns false if the config contains any data.
  130. Returns nil if no config is loaded.
  131. --]]
  132. function isEmpty()
  133.     if(loaded == false) then
  134.         return nil
  135.     end
  136.     if(fs.getSize(path) == 0)then
  137.         return true
  138.     else return false end
  139. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement