Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Program: Config API
- Version: 1.0
- Author: ben657
- Support:
- Description: This API allows you to easily make configs for
- your users to edit, rather than them having to
- read code and edit variables. These files can
- then be read by this API and the values can be
- used in your programs.
- Note: Currently, tables cannot be written to configs, but in
- the future this functionality may be implemented.
- --]]
- loaded = false
- dir = ""
- file = ""
- path = ""
- internal = {}
- function create()
- local file = fs.open(path,"a")
- file.close()
- end
- --[[
- Attempts to load the config using the specified directory
- and file name, and load the contents into internal memory.
- If the path is not found, it will be created.
- --]]
- function load(directory,fileName)
- dir = directory
- file = fileName
- path = dir.."/"..file
- fs.makeDir(dir)
- create(path)
- local file = fs.open(path, "r")
- repeat
- line = file.readLine()
- if(line ~= nil) then
- local asWords = line:gsub(":","")
- local parts = {}
- for word in asWords:gmatch("%w+") do table.insert(parts,word) end
- internal[parts[1]] = parts[2]
- end
- until line == nil
- loaded = true
- file.close()
- end
- --[[
- Attempts to write the specified value to the specified key in the
- internal config.
- Returns true if successful.
- Returns false if no config is loaded.
- This only writes to the config in memory, not the file. To save
- to file, call config.save().
- --]]
- function writeVal(key,value)
- if(loaded == false) then
- return false
- else
- local toWrite = value
- if(value == true) then toWrite = "true"
- elseif(value == false) then toWrite = "false" end
- internal[key] = toWrite
- return true
- end
- end
- --[[
- Attempts to read a value from the internal config and return it.
- Returns the value assigned to the specified key if successful.
- Returns nil if no config is loaded or no value was found at the key.
- --]]
- function readVal(key)
- if(loaded == false) then
- return nil
- end
- toReturn = internal[key]
- if(toReturn == "true") then return true
- elseif(toReturn == "false") then return false
- else return internal[key] end
- end
- --[[
- Attempts to save the internal config to the loaded file path.
- Returns true if successful.
- Returns false if no config is loaded.
- The internal config is unloaded after saving, so config.load()
- must be called again to use the config.
- --]]
- function save()
- if(loaded == true) then
- local file = fs.open(path,"w")
- for i,v in pairs(internal) do
- file.writeLine(i.." = "..v)
- end
- file.close()
- internal = {}
- loaded = false
- return true
- else
- return false
- end
- end
- --[[
- Returns true if the config has no data.
- Returns false if the config contains any data.
- Returns nil if no config is loaded.
- --]]
- function isEmpty()
- if(loaded == false) then
- return nil
- end
- if(fs.getSize(path) == 0)then
- return true
- else return false end
- end
Add Comment
Please, Sign In to add comment