This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ben657's profile picture

Config API - Easily make user editable configs! [V1.0]

Started by ben657, 05 September 2012 - 01:35 PM
ben657 #1
Posted 05 September 2012 - 03:35 PM
Config API

Program: Config API
Version: 1.0
Author: ben657

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.

Installing

To use this API, copy and paste the code into a blank document, and follow the steps below for how you want it to work.
  • World-wide usage (any computer in the world/server can use the API)
    • Copy the document into "modsComputerCraftluaromapis" naming it as "config".
  • Per-computer usage (only the computer installed on can use the API)
    • Use cc-get or copy the document into the computer that needs to use its file system.
Get it

Code: Pastie
cc-get: cc-get install config-api (info)

Usage

config.load(directory,fileName)
This method allows you to load a config to memory,
so you can read and write variables to it.
If no file is found, one will be created.
directory - The folder to look in for the file.
fileName - The file to look for in the directory.

config.load("foo","bar") --[[ Finds the file at "/foo/bar" ]]

config.writeVal(key,value)
This method allows you to store avalue with a given name
into the internal memory.
It will not be saved to file until config.save() is called.
key - The name of the value in the config.
value - The value to store in the config.

config.writeVal("foo","bar") --[[ Writes the value "bar" with the name "foo" in the config, it will look like "foo = bar" in the file. ]]
[/CODE]
[i]config.readVal(key)[/i]
This method allows you to read a value from the loaded config.
[i]key [/i]- The name of the value to be read.
[CODE]
var = config.readVal("foo") --[[ Going off the last example, this will return the value "bar" into the variable. ]]

config.save()
This method commits all changes to the config to file, in the currently
loaded path.

config.save() --[[ Going off the last example, the file "/foo/bar" will have "foo = bar" written to it on the top line. ]]
[/CODE]

[b][size=5]Notes[/size][/b]

Please feel free to use the API, but if you need people to download it, please link to this page, or at least credit
me in your post, a little recognition is all I ask!

Also, any bugs or errors, give me a shout in this topic and i'll do my best to help you out!
Finally, i'd love for anyone to give me suggestions on anything you'd like added to the API.
ben657 #2
Posted 05 September 2012 - 07:42 PM
C'mon guys, even just a "it works!" would be nice :D/>/>
Teraminer #3
Posted 05 September 2012 - 09:15 PM
Intresting but I am not going to make a program that needs configs cause I fail advanced stuff :D/>/>

PS Nice job! diamon… or maybe wireless mining turtle(diamond pickaxe) to you?
Leonardoas111 #4
Posted 08 September 2012 - 05:28 AM
Interesting idea… I might use this on TeamEcon’s iPayPal. I’ll credit you of course.
Kilandor #5
Posted 10 September 2012 - 09:04 PM
I am working on a users api, also a auth server/client to go with it. So I started to handle things myself then remember I had seen this so I would give it a go.
It works great.

I would like to see a unLoad() this way you can unload a file without having to save() it
Example for users api there are various bits of data i want to read without any need for saving.
ben657 #6
Posted 13 September 2012 - 01:11 PM
I am working on a users api, also a auth server/client to go with it. So I started to handle things myself then remember I had seen this so I would give it a go.
It works great.

I would like to see a unLoad() this way you can unload a file without having to save() it
Example for users api there are various bits of data i want to read without any need for saving.

I can put an UnLoad function in, but it isn't really needed, if you want a different set of data loaded, just use load again, no actual files are stored in memory, they are opened and closed as needed. Just the keys and values are stored in memory.
Kilandor #7
Posted 14 September 2012 - 08:47 PM
Yea I realize that, but I don't know what someone else using the api my want to do. They might try modifying my loaded data. So the idea of unload is just for simply when you don't need to save() to save a write operation
ben657 #8
Posted 14 September 2012 - 11:11 PM
Yea I realize that, but I don't know what someone else using the api my want to do. They might try modifying my loaded data. So the idea of unload is just for simply when you don't need to save() to save a write operation

Ah, I see your point now, never considered that others might be using it :)/>/> I'll put an UnLoad function in in the morning to clear memory, thanks for the suggestion :P/>/>
viluon #9
Posted 20 September 2012 - 12:34 PM
cool! Great idea but im having this problem:
API config is already beign loaded
parrallel:22: login.als:35:  attempt to index ? (a nil value)
my code:
Spoiler

--START>Load files--
startInterface=function()
local function reprintLoadAnim(anim)
  term.clear()
  term.setCursorPos(1,1)
  print("Loading local user account database...")
  write(anim)
  sleep(0.1)
end
term.clear()
term.setCursorPos(1,1)
textutils.slowPrint("Loading local user account database...")
write("-")
sleep(0.2)
local counter=0
while true do
  reprintLoadAnim("\\")
  reprintLoadAnim("|")
  reprintLoadAnim("/")
  reprintLoadAnim("-")
  if counter==20 then
   break
  end
  counter=counter+1
end
term.clear()
term.setCursorPos(1,1)
end
--START>End of first part--
--START>Second part>load files--
load=function()
os.loadAPI("/apis/config")
config.load("cfg","test.cfg")
config.writeVal("loaded",true)
config.writeVal("Guest","1234")
config.save()

end
--START>End of script--

--TODO>MULTITASK>Run startInterface() & load() functions at the same time, wait for completion--
parallel.waitForAny(startInterface, load)


--Set variables--
local w,h=term.getSize()
ben657 #10
Posted 20 September 2012 - 06:57 PM
cool! Great idea but im having this problem:
API config is already beign loaded
parrallel:22: login.als:35:  attempt to index ? (a nil value)
my code:
Spoiler

--START>Load files--
startInterface=function()
local function reprintLoadAnim(anim)
  term.clear()
  term.setCursorPos(1,1)
  print("Loading local user account database...")
  write(anim)
  sleep(0.1)
end
term.clear()
term.setCursorPos(1,1)
textutils.slowPrint("Loading local user account database...")
write("-")
sleep(0.2)
local counter=0
while true do
  reprintLoadAnim("")
  reprintLoadAnim("|")
  reprintLoadAnim("/")
  reprintLoadAnim("-")
  if counter==20 then
   break
  end
  counter=counter+1
end
term.clear()
term.setCursorPos(1,1)
end
--START>End of first part--
--START>Second part>load files--
load=function()
os.loadAPI("/apis/config")
config.load("cfg","test.cfg")
config.writeVal("loaded",true)
config.writeVal("Guest","1234")
config.save()

end
--START>End of script--

--TODO>MULTITASK>Run startInterface() & load() functions at the same time, wait for completion--
parallel.waitForAny(startInterface, load)


--Set variables--
local w,h=term.getSize()

By the looks of it, you put the API in the rom/Apis folder right? If so, you don't need to call os.LoadApi(), it auto loads anything in that folder so it's trying to load something already in memory. Hope this helps :)/>/>
Erwin #11
Posted 21 October 2012 - 09:50 AM
Hey, I modified your code by adding the following function.



function read(directory,fileName)
dir = directory
file = fileName
path = dir.."/"..file
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


Its the same as load EXCEPT it is missing the make Directory part. This stops the programs from getting an access denied when accessing a floppy disk and some other areas of the computer. So all you do is create a config file, then move it where you want it and can still edit it. Hooray!!!
Marikc0 #12
Posted 24 November 2013 - 12:02 PM
Does this still work or are there any alternatives APIs available?
H4X0RZ #13
Posted 24 November 2013 - 05:12 PM
Does this still work or are there any alternatives APIs available?
toBIT made one, but I don't have the link to it…
theoriginalbit #14
Posted 24 November 2013 - 09:15 PM
Does this still work or are there any alternatives APIs available?
BIT made one, but I don't have the link to it…
Link to my programs/APIs is in my signature.
Edited on 24 November 2013 - 08:15 PM
LDDestroier #15
Posted 03 April 2015 - 05:41 AM
I got a program on pastebin to help easily download this api, seeing as cc-get is very hard to get. It uses the pastie link:

pastebin get D0VrN6nM pastie
pastie get 4668374 config
RootUser #16
Posted 08 September 2015 - 03:42 PM
Thanks) Credits at startup (added some lines at start to print credits to console and identify in OS)
RootUser #17
Posted 09 September 2015 - 09:39 AM
Bugreport: this API can't work with dots. So, when i am putting variable colors.green (or other dotted var), API gets only "colors". After this ANY window render API (like my winUtils (builtin, uses native window api to draw full windows, not boxes)) will crash.
RootUser #18
Posted 10 September 2015 - 07:18 AM
I can't connect 2 parts of varname like ## in C (preprocessor). So, my code will be larger or you will upgrade config API.