Loading config file

Started by
10 comments, last by Toolmaker 19 years, 6 months ago
Ver simple question (I hope): How do you load a config file. i thought about it, but im not sure how you get those tags to work. Thx
------------ "Here lies a toppled God,His fall was not a small one,We but built his pedastle,A narrow, and a tall one" Frank Herbert (Dune:Messiah)
Advertisement
It all depends, what format the file is in.
assume .cfg
------------ "Here lies a toppled God,His fall was not a small one,We but built his pedastle,A narrow, and a tall one" Frank Herbert (Dune:Messiah)
Probably not what you want, but here's my (rather rubbish) config file loader: Link

Example usage:
int main(){CConfigFile theFile;   if(!theFile.Load("Bla.ini"))      return 1;   printf("%s\n",theFile.GetValue("Settings","Option").c_str());   return 0;}


EDIT: As the AP below said, .cfg isn't a format. My loader assumes format in the following format:
# Any line starting with a # is a comment
[This is a section title]
This is a key = This is a value
This is another key = "This is a value. The quotes will be stripped "
Quote:Original post by Kris2456
assume .cfg

'.cfg' is a file extension rather than a file format. You have to b e more specific. Is it another program's configuration or do you want to write (and parse) your own?

Ok what i mean is:

files which look like this:
[ammo]100[health]50[name]Guy


That kind of file. So after a tag, the value is read in.
------------ "Here lies a toppled God,His fall was not a small one,We but built his pedastle,A narrow, and a tall one" Frank Herbert (Dune:Messiah)
That doesn't look like any kind of settings file I've seen. So there probably won't be any pre-written classes or functions for loading it. You could use my class (or one similar), and change your data layout to:

[Entity]
ammo = 100
health = 50
name = Guy
The details of the format don't really matter - your loading function just has to match it.

In any case, you'll need a parser of some sort. You can write your own, or I'm sure there are free ones available. One of the GPG books has an article on a parser in it (more complicated than what you need here, though, I think).

In your case, you want to parse by line (\n delimited), with each line having two tokens. [] should be treated like quotes. Your first token is your key, and your second token is your value. How you process the tokens depends on how your system is set up internally.

Most likely, you'll find the key using a map or hash table of some sort, and each key will be associated with a variable. Then you'll use atoi(), atof(), stringstream, or whatever to convert the second token to an appropriate value (or leave it as-is if it's a string).

Does that help at all?
I'm creating certain application for Win32 environment using Delphi/Object Pascal and I needed to create kind of config file library (yep, I know there is registry and TIniFile aso., but I somehow liked the idea of storing config in XML format (but this is not what I wanted)...
I've implemented simple library, which does it's job well...
The thing, what it is doing is simply binding some variables from source code to parameters in config file (which allowed me to simply load and save config file (when, of course I'm sure, that I don't free that variables prior to saving that cfg)...

This is a snippet from my testing code (rewritten to C++ while there is no Delphi syntax hlt capability in GDN forums :)

ConfigFile cfg("testfiles/config1.xml");cfg.Bind('myintval1', myint);cfg.Bind('mystrval1', mystr);cfg.Bind('mytestfloatval1', myfloat);cfg.Load(); //this will fill the variables by values from cfg// some stuff to docfg.Save(); // this will save the variables to cfg


ps.: as i've said, it's rewritten to C++, but this principle should work fine anyway
Evil Steeve your class will save me a lot of time, considering i'm learning C++ as i do write my game (well i'm a very good game scripter (UnrealScript, home made engine (not by me) Scripts) but i do only know from C++ the basics i need (Script interface) and i want to know a lot more - the other side of the script :).
Thanks.

This topic is closed to new replies.

Advertisement