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

newLibCFG -- basic configuration API

Started by Admicos, 23 December 2015 - 07:27 PM
Admicos #1
Posted 23 December 2015 - 08:27 PM
This is old. Get the newer versions here.

libCFG is the configuration API of AdmiOS RW.

Installation: execute
pastebin get icmFaCNc libCFG
Demo:
pastebin get q13TtRKt demo
The demo assumes you have libCFG in the same folder as the demo
External Modifier:

pastebin get YMVBPquM aconf
The external modifier is a program that can edit/delete and create keys in libCFG files.
Like the demo, it also assumes you have libCFG in the same folder. But it will use the already-loaded libCFG if it's running in AdmiOS (New version with the variable is coming very soon)

How to use:
1. load libCFG like so:
os.loadAPI("libCFG")
2. Use the methods below to read/write config files.
3. Make a cool program
4. Profit

Methods:
- readConfig(file)
libCFG.readConfig(file)
Reads the contents of the file and returns a table of the configuration keys/values.

- readConfigRaw(string)
libCFG.readConfig(string)
Reads the contents of the string and returns a table of the configuration keys/values.

The returned tables can be used like:
value = table["key"]

- writeConfig(file, table)
libCFG.writeConfig(file, table)
Writes the contents of the table to file.

Table should be formatted like this:
table = {
key = "value"
}

Config style:
An example config is shown below:

#libCFG Example config

#lines starting with hashtags are comments
#they will be ignored and will NOT be rewritten when you use writeConfig()

#comments CANNOT use colons
#this comment: will not work

#Here is an example config entry
key: value


#Here is an example config that could be used in a program
saveDirectory: /program/saves
saveInterval: 10

#these entries will result as
# table["saveDirectory"] = "/program/saves"
# table["saveInterval"] = 10

Note: Writing to a config will remove all the comments. If you know how to preserve them, just comment it here and i will probably use it.
Edited on 26 September 2016 - 06:42 PM
Admicos #2
Posted 24 December 2015 - 10:49 PM
I just edited in a External Modifier program that can modify libCFG files.
It's available in the original post.
Admicos #3
Posted 31 August 2016 - 04:04 PM
I currently finished a newer version of libCFG, which can replicate the original file 100%, meaning the comments, whitespace etc will stay!

Released.
Edited on 31 August 2016 - 03:48 PM
Admicos #4
Posted 31 August 2016 - 05:42 PM
newLibCFG
newLibCFG is a basic configuration reader/writer for your ComputerCraft programs.

Configuration file syntax is compatible/exactly the same as libCFG meaning if you actually used it in anything you did, you can modify few lines and make your program use newLibCFG.

Documentation:
[indent=1]
table keys, [table comments, number line count] if not simple readConfigRaw(string configStr, bool simple)
[/indent]
[indent=1]This is what actually does most of the stuff. the configStr is a string (seperated by \n) that contains the configuration keys, values and comments.[/indent]

[indent=1]the "simple" boolean, will probably be set to "true" in most use cases, since it simplifies the output to the exact same level as libCFG does.[/indent]

[indent=1]if simple is false, the function will return keys, comments and the line count in 3 different variables.[/indent]
[indent=1]The keys and the comments are tables of line numbers which contain the comment string, or a table of key, value. If you are not modifying a file, trying to iterate over this is pointless and you should use simple instead.[/indent]

[indent=1]If simple is true, the function will return a simple table "keys" which contain values (just like libCFG)[/indent]

[indent=1]
table keys, [table comments, number line count] if not simple readConfig(string configFile, bool simple)
[/indent]
[indent=1]This is just a wrapper for the function above, and will behave the exact same way. Except it will take a (absolute) file path instead of a string[/indent]

[indent=1]
nil writeConfig(file, keys, comments, len)
[/indent]
[indent=1]This will write the keys and comments to the file.[/indent]

[indent=1]Please note that the keys and comments need to have "simple" disabled in order to be understood by this function.[/indent]
[indent=1]Also, file path is absolute and len is required.[/indent]



[indent=1]There are simple_test() and test() functions near the end of the file that will get executed if you try to run this as a program (argument 1 might be "simple") rather than a API.[/indent]
[indent=1]Those will contain examples of how the API works, but they require "stringStuff" to be installed, which you can get from a copy of AdmiOS.[/indent]


Downloads:
[indent=1]pastebin: e4hJ32cS[/indent]
[indent=1]packman: admicos/newLibCFG[/indent]
Bomb Bloke #5
Posted 01 September 2016 - 03:08 AM
Threads merged; keep all versions of the one project together, please!