Forums » Community Projects

Reading the Config file

May 02, 2008 PsyRa link
Greetings:

I was wondering if anyone knows of a way to grab the entire contents of a configuration section from the config.ini file.

I looked here http://www.vo-wiki.com/racecar/index.php?title=Gkini

but nothing came up that would allow the following.

gkini.ReadString("TargetControl", "OpenTargetControl", "")
gkini.ReadString("TargetControl", "TargetLock", "")

What I want is something that allows just the entry of "TargetControl" that returns OpenTargetControl and TargetLock, and any other keys contained inside the "TargetControl" section.

Is this possible?

Thanks
May 02, 2008 maq link
Don't think so. (really helpful eh?)
Why'd you want that anyway?
May 02, 2008 blacknet link
spickle

Definition: spickle(table intab) -> string

Description:
Converts a table into a string roughly of the format "value","value",{"tablevalue","tablevalue";tablekey="value"};key="value",key="value"

Arguments:
intab input table

Returns:
The spickled string

=================
unspickle

Definition: unspickle(string in) -> table outtab

Description:
Converts a spickled string into a table

Arguments:
in input string

Returns:
outtab The unspickled table
May 02, 2008 maq link
or there's that, if that's what you want.. >.>
May 03, 2008 PsyRa link
I am afraid not. There is no way I know how to get all the values stored in the config file into a table so I can spickle it.

The same problem remains.

The reason I want to be able to do this, is I would like to add configurations in based on user selection.

For example
TargetKeyX="Joe" where the "X" is the key assigned by the player. Now it is easy enough to set all these values, what I am looking for is a way to find everything I have set, without running through each and every possibility for "X".
I guess that I will have to figure out a way to index these without just grabbing the entire contents of

gkini.ReadString("TargetControl")

because that just plain does not work.
May 03, 2008 blacknet link
PsyRa try this.

myapp.config = {} where myapp.config is all the config settings. then do a gkini.WriteString on spickle(myapp.config) that will dump everything.
May 03, 2008 PsyRa link
Any idea when this will bump into the string length limit in the config file?

I know drazed had some issues with trying to track more than 50 records in targetls with something similar.

http://vendetta-online.com/x/msgboard/9/17440?page=4
"-limit roidlist to 50 rocks per sector, sorry but this is necessary as saving too many per sector can break your config.ini file =("

I guess I could store them in numerically increasing values, and just roll them if the string length got to long, and set a stable config record to indicate the number of these records.

Man that feels like a horrible Kludge.
May 03, 2008 mr_spuck link
I'd do something like this:

get all values

function read_entries()
 local entries = {}
 local num = ReadInt("TC", "TK_num", 0)
 for i=1,num do
  local entry = unspickle(ReadString("TC", "TK"..i, ""))
  if entry and entry.key and entry.value then
   entries[entry.key] = entry.value
  end
 end
 return entries
end

write all values:

function write_entries(entries)
 local num = 0
 for i,v in pairs(entries)
  num = num + 1
  WriteString("TC", "TK"..num, spickle{key=i, value=v})
 end
 WriteInt("TC", "TK_num", num)
end

get the value of key

function read_entry(key)
 local entries = read_entries()
 return entries[key]
end

write value to key

function write_entry(key, value)
 local entries = read_entries()
 entries[key] = value
 write_entries(entries)
end

you'd end up with a section looking like this in your config file (unless I got something wrong):
[TC]
TK_num = 2
TK1 = {key="X", value="joe"}
TK2 = {key="Y", value="frank"}

It has pretty much the same functionality as spickle except that it spreads out the size limitation over all entries.
It's horribly inefficient though cause it's reading the whole section on each operation.
May 04, 2008 PsyRa link
You know, that was just were I was going to head with it mr_spuck, and it worked well enough.

Then only problem now is that I have all these deleted records I can't get rid of. I limited my exposure to them by bunching up the data at the top and reusing the spaces. That said, once made, I can't figure out how to kill a config record without manually deleting it.

Oh well, Kludge, Kludge, Kludge.
May 05, 2008 mr_spuck link
yah that's a problem .. I don't think there's a way to remove entries. I wouldn't store thousands of entries that way. But if the number of entries has a known upper limit it should be ok.

You could also store everything in one entry without using spickle, like a tab separated list or something.. but that may get messy if you try to store anything complex and I don't know if WriteString has its own limitations..

edit:
Or store the keys in one entry and values in another ..that could work well.

example:
TK_keys = X\tY ..
TK_values = joe\tfrank ..

but again I don't know if WriteString will have issues with it .. could be that it will strip out special characters or chop off the end if it gets too long

edit: whoops strike that .. WriteString is actually what limits what you can write into an entry not spickle .. I always thought spickle was the problem.
May 05, 2008 PsyRa link
Yea. I was storing things in Record1="Value1:Value2:Value3" format and splitting at the ":", however I did a full conversion to spickle/unspickle, and it is much easier to work with. Records I am keeping only hold three "Values", and none of them I suspect will go over the WriteString limit, and as I said, I am keeping the Record1 count down by clearing out deprecated settings as often as possible.

Thanks for the help.
May 05, 2008 mr_spuck link
alrighty! .. whatever works for you