Sign in to follow this  
Kris2456

Loading config file

Recommended Posts

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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It all depends, what format the file is in.

Share this post


Link to post
Share on other sites
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 "

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 do

cfg.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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I don't know these classes, but I always use the GetPrivateProfileInt, GetPrivateProfileString, WritePrivateProfileString functions to read ini-like files.
Very simple, just one line to get a setting..

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this