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

Preferences, Settings, Configuration -- Store Them All! Settings File Api!

Started by bwhodle, 24 July 2013 - 05:01 PM
bwhodle #1
Posted 24 July 2013 - 07:01 PM
Apparently the mods didn't like my epically legit rednet hacking concept. D:…
I've decided that they won't like anything similar so I decided to make something else.

Settings File API
Don't you have a program but you can't store any important settings or preferences or other necessary storage stuffies? This API is here to help!!! Please read the below stuff for information about how to set up a settings file and how to use the API. Please post any issues you find with the API and any suggestions for features I should add. I would also love if someone made an editor for settings files. Thanks for checking it out!
Pastebin: FTmDPGRF
On your computer, type: "pastebin get FtmDPGRF settings" or whatever you are supposed to type and it will appear for you.

This API is designed to handle a new type of file I created called a settings file. (Inspired by .ini files)
These are very user friendly and also computer-friendly with this new API. Here's how one would look:

; #Text editor settings file
printer_side: top
debug_mode: 0
last find = "  coroutine.r"

[Colors]
text_background: 19
text_foreground: 18
window_border_background: 15
window_border foreground: 10
color_1: 8

[CustomShortcuts]
save = CtrlShiftS
print = CtrlShiftP
help = CtrlH

A settings file is supposed to look utterly simple. You probably understand all of the above already, right??
Here are the features of a settings file:
Name-Value Data storage: Your information can be stored as a value, and can be referenced by a name.
For example:

printer_side = top
Simple, right?
This simple method contains several features that will make you happier.
Automatic white space removal: names and values are trimmed of leading and ending whitespace. For example:

			some key	   =   hehehehe
is the exact same thing as:

some key=hehehehe
Your choice of symbol: You can also use a colon or equals sign as the separator! So:

printer side: top
is the same thing as

printer side = top
Types: Values can be of two types: strings or numbers. A number is anything that works as a number, and a string is anything that doesn't work as a number! When you read them with the API, it will return a value of the correct type. No specification is needed, the API will automatically detect types.
Explicit String Declaration: You can even declare a string manually in a value and force it to be treated as a string using quotation marks. Anything within quotation marks won't be changed by the parser, and will be given to you directly. For example:

number_of_cows = "10"
Isn't the same thing as

number_of_cows = 10
Just as

text = "	stuff"
Is not the same thing as

text =	stuff

Comments: Anything after a semicolon is considered a comment. Pretty simple, huh?
Sections: You can declare a section by placing it's name in square brackets like this:

[section name]
name = value
othername = somevalue
Anything underneath a section title, all the way up to the next section title/file end, is part of that section. Any values you place at the top of the file without declaring a section are considered as unsectioned values, values that don't have a section.

That's how a settings file looks. Here's how to use the simple API:



settings.openSettingsFile(filepath)
description: Opens a settings file and returns a handle to it
filepath: The file path of the settings file
returns: a settings file handle from the opened file



settingsfilehandle.addSection(name)
description: Adds a section
name: Name of the section to add.



settingsfilehandle.getValue(key)
description: Gets a global/unsectioned value from it's name
key: Name of value to retrieve
returns: Value if exists, else nil



settingsfilehandle.getSectionedValue(section, key)
description: Gets a sectioned value from it's name and section
section: Section containing the sectioned value
key: Name of the value
returns: Value if exists, else nil



settingsfilehandle.setValue(key, value)
description: Sets a value according to it's name. If it doesn't exist, it gets created.
key: Name of the value
value: Value to set it to



settingsfilehandle.setSectionedValue(section, key, value)
description: Sets a sectioned value based on it's name. If it doesn't exist, it gets created.
section: Section containing the sectioned value
key: Name of the value
value: Value to set it to



settingsfilehandle.save(path)
description: Saves the settings file to the given path.
path: Path to save the settings file to.


Here is an example of how to open the example file at the very beginning of this document, get the value of "printer_side", modify that value, insert a section, insert a sectioned value, and save the file.

os.loadAPI("settings")
local settingsfile = settings.openSettingsFile("examplefile")
print(settingsfile.getValue("printer_side"))
settingsfile.setValue("printer_side", "left")
settingsfile.addSection("Personal Information")
settingsfile.setSectionedValue("Personal Information", "Name", "Joe Shmoe")
settingsfile.save("examplefile")
os.unloadAPI("settings")
UMayBleed #2
Posted 25 July 2013 - 01:22 PM
This is extremely useful.
bwhodle #3
Posted 26 July 2013 - 08:14 PM
This is extremely useful.
Thanks! Any suggestions??
LDShadowLord #4
Posted 28 July 2013 - 12:44 PM
Now all it needs is some way to have a secure version of this to store passwords and usernames!
jesusthekiller #5
Posted 28 July 2013 - 06:57 PM
Very nice API!
ardera #6
Posted 04 August 2013 - 05:24 AM
This is exactly what I was searching for. Great API :)/>
xorax #7
Posted 01 January 2014 - 04:08 PM
I have a little problem using this API

os.loadAPI("settings")
local props = settings.openSettingsFile("propeties")
os.unloadAPI("settings")

But there is an Error:
settings:43: attempt to index ? (a nil value)

Thank you
logsys #8
Posted 09 April 2014 - 10:26 PM
Looking forward to use it… Can I use it in my programs?
Edit: And publish it
Edited on 09 April 2014 - 08:26 PM
apemanzilla #9
Posted 09 April 2014 - 10:31 PM
Looking forward to use it… Can I use it in my programs?
Edit: And publish it
Do you think he'd post it here if you weren't allowed to? ;)/>
logsys #10
Posted 14 April 2014 - 09:20 PM
Looking forward to use it… Can I use it in my programs?
Edit: And publish it
Do you think he'd post it here if you weren't allowed to? ;)/>

True, doesn't work anyway
InDieTasten #11
Posted 18 April 2014 - 12:16 PM
Now all it needs is some way to have a secure version of this to store passwords and usernames!
i don't think you want to store authentication details in a config file ;)/>
there is no need in that. you could just ramble them through some encryption/hashing(there's plenty ported to lua)-algorithms and save them as serialized table or whatever ;)/>
Parzivail #12
Posted 13 May 2014 - 10:53 PM
I have a little problem using this API

os.loadAPI("settings")
local props = settings.openSettingsFile("properties")
os.unloadAPI("settings")

But there is an Error:
settings:43: attempt to index ? (a nil value)

Thank you

Haha, I just had that happen. Create the "properties" file in the computer's directory (the program doesn't make it for you). That should fix it! :D/>
Edited on 13 May 2014 - 08:54 PM
dcbartlett #13
Posted 19 July 2015 - 11:00 AM
Does this programs/API still work with 1.7.x I'm currently trying to load a file and its giving me an error.


-- Load All API's thats needed
os.loadAPI("iniApi")
local settingsFile = settings.openSettingsFile("settings.ini")
settingsFile.addSection("Monitor")
settingsFile.save("settings.ini")


attempt to index ? (a nil value)
TheChance #14
Posted 20 July 2015 - 09:20 AM
Does this programs/API still work with 1.7.x I'm currently trying to load a file and its giving me an error.


-- Load All API's thats needed
os.loadAPI("iniApi")
local settingsFile = settings.openSettingsFile("settings.ini")
settingsFile.addSection("Monitor")
settingsFile.save("settings.ini")


attempt to index ? (a nil value)

It's working for me, though I've run into a couple of quirks.

Which line throws that error? If iniApi is this API, you either need to load it as "settings", or call "iniApi.openSettingsFile()" and so forth.

If you're loading the API as settings somewhere above, does the file exist? This API won't create it for you.
Yarillo #15
Posted 22 July 2015 - 06:46 PM
Hey !
I love your API.
But I used it quite a lot and I had to fork it for it to suit my needs. Obviously, i'm not publishing my version. Most of that code is yours.
But if you want to see the changes I made, here it is:

pastebin get U5ix0UGp ini

I'll let you judge by yourself if you like my changes or not !
But I'd be very happy if you implemented some of the features of my fork.

SpoilerFirstly, I added aliases.
settings.open = settings.openSettingsFile
settings.close = settingsfilehandle.save
settings.read = settings.getValue
settings.write = settings.setValue
Because when using it I hated typing those long names and I had trouble remembering them.
I felt like it was simpler.

Then, I also made it more flexible.
No need for "setSectionedValue" and "getSectionedValue" anymore.
You can still use them on my version, but it's pointless.

You can use settings.read with two arguments like so:
> settings.read(section, key),
or with one:
> settings.read(key)
If you give it two args, it's obvious that you want to read a key contained in a section. I didn't see any need for two separate functions.

Same for settings.write(section, key, value)
Edited on 22 July 2015 - 04:49 PM
MKlegoman357 #16
Posted 22 July 2015 - 06:57 PM
-snip-

The author of this API hasn't been online for about 2 years now, I doubt he will respond anytime soon, not even talking about merging your changes to his API.
Yarillo #17
Posted 22 July 2015 - 10:06 PM
Hahaha woops. So that's why the code is so different from today's standards
I thought it was recent but that's just because dcbartlett actually responded to it recently :>

Well nevermind then.
Edited on 22 July 2015 - 08:07 PM
TheChance #18
Posted 27 July 2015 - 12:29 AM
Hey !
I love your API.
But I used it quite a lot and I had to fork it for it to suit my needs. Obviously, i'm not publishing my version. Most of that code is yours.

Being as the author of this API hasn't been around for two years, and at least two other people seem to be using the API, maybe it wouldn't be so bad to publish your fork under its own thread =P
Yarillo #19
Posted 27 July 2015 - 03:54 PM
I wouldn't feel confortable doing that :0
MatthewC529 #20
Posted 27 July 2015 - 09:40 PM
I wouldn't feel confortable doing that :0

It's fine. Of course if it was a recent, active, thread you wouldn't want to try and post very similar code, but if you have code that works and improves upon the original as a derivation and there is no license barring said activity as against the intent of the author it is perfectly reasonable. It was the same reason I re-wrote CC Autotyper, it was unlicensed and although it was niche, people used it. Since I had sufficient Java experience I originally released a nearly identical but fixed version 1.0.0 and now it is version 3.0.1 with a GUI and all, vastly different and vastly improved from the original.

Of course, not seeing this, I released my CC Config API but you should release your improvements to this API if you feel that the code is up to it. Surely some people will appreciate it. I license all of mine under the same license, though it is generally public domain. You could do the same.
moomoomoo3O9 #21
Posted 01 January 2016 - 03:19 AM
Funny enough, I actually forked this API as well, to add the ability to add comments to the file.
Here's my code. I gave credit to the author and linked this post, of course.