Jump to content




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

api utility

  • You cannot reply to this topic
17 replies to this topic

#1 ben657

  • Members
  • 65 posts

Posted 05 September 2012 - 02: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. ]]
config.readVal(key)
This method allows you to read a value from the loaded config.
key - The name of the value to be read.
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. ]]

Notes

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.

#2 ben657

  • Members
  • 65 posts

Posted 05 September 2012 - 06:42 PM

C'mon guys, even just a "it works!" would be nice :D/>

#3 Teraminer

  • New Members
  • 175 posts
  • LocationDon't look behind you..

Posted 05 September 2012 - 08: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[s](diamond pickaxe) to you?

#4 TundraFish

  • New Members
  • 15 posts

Posted 08 September 2012 - 04:28 AM

Interesting idea... I might use this on TeamEcon’s iPayPal. I’ll credit you of course.

#5 Kilandor

  • Members
  • 3 posts

Posted 10 September 2012 - 08: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.

#6 ben657

  • Members
  • 65 posts

Posted 13 September 2012 - 12:11 PM

View PostKilandor, on 10 September 2012 - 08:04 PM, said:

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.

#7 Kilandor

  • Members
  • 3 posts

Posted 14 September 2012 - 07: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

#8 ben657

  • Members
  • 65 posts

Posted 14 September 2012 - 10:11 PM

View PostKilandor, on 14 September 2012 - 07:47 PM, said:

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/>

#9 viluon

  • Members
  • 183 posts
  • LocationCzech Republic

Posted 20 September 2012 - 11:34 AM

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


#10 ben657

  • Members
  • 65 posts

Posted 20 September 2012 - 05:57 PM

View Postviluon, on 20 September 2012 - 11:34 AM, said:

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

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 :)/>

#11 Erwin

  • New Members
  • 1 posts

Posted 21 October 2012 - 08: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!!!

#12 Marikc0

  • Members
  • 10 posts

Posted 24 November 2013 - 12:02 PM

Does this still work or are there any alternatives APIs available?

#13 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 24 November 2013 - 05:12 PM

View PostMarikc0, on 24 November 2013 - 12:02 PM, said:

Does this still work or are there any alternatives APIs available?
toBIT made one, but I don't have the link to it...

#14 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 24 November 2013 - 09:15 PM

View PostFreack100, on 24 November 2013 - 05:12 PM, said:

View PostMarikc0, on 24 November 2013 - 12:02 PM, said:

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 by theoriginalbit, 24 November 2013 - 09:15 PM.


#15 LDDestroier

  • Members
  • 1,095 posts
  • LocationACDC Town

Posted 03 April 2015 - 04: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

#16 RootUser

  • Members
  • 11 posts

Posted 08 September 2015 - 02:42 PM

Thanks) Credits at startup (added some lines at start to print credits to console and identify in OS)

#17 RootUser

  • Members
  • 11 posts

Posted 09 September 2015 - 08: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.

#18 RootUser

  • Members
  • 11 posts

Posted 10 September 2015 - 06: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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users