Load config files like Apache

A forum to store posts deemed exceptionally wise and useful
Post Reply
Katsankat
Posts: 178
Joined: Sun Mar 12, 2006 4:15 am
Contact:

Load config files like Apache

Post by Katsankat »

Hi all,

Applications sometimes need to store variables between executions.
For example screen resolution chosen by client, or his favourite 3D acceleration driver, etc...
Apache, Samba, PHP, Half-Life... They do load their config with the method i am going to show here.

You all know this format:
- a text file
- a serie of pairs name/value separated by a '='
- comments are marked with '#'
- no sections

This method is older than XML, and used by 90% of linux professional apps.
Although XML benefits of several open-source librairies to parse it, I still find the ".conf" way :

- more flexible than XML (possibility to add comments with #)
- Smaller files than XML
- clear display (no tags)
- easy to edit manually
- portable, unlike INI files.

But don't get me wrong, it's mainly a question of personnal taste.Everyone does the way he wants, Free as in Freedom. For bigger structures, the binary in/out is better (as used by the bsp format for example).
In all cases, the parser has to read the file from beginning to the End Of File.

---------------------

Let's see directly how to use it.

Add the 5 files in your project directory:
config.cpp
config.h
exception.h
file.cpp
file.h
In your project, add

Code: Select all

#include <iostream>
#include "config.h"
Declare the object which will hold the pairs name/values:

Code: Select all

Config myconf;
Then load your config files into this space:

Code: Select all

myconf.addConfigFile("config/cfg.conf");
myconf.addConfigFile("../config/server.cfg");
Now you can access a value by its name:

Code: Select all

cout << "nick is: " << c->getEntry("nick") << endl;
Everything is string, you have to convert it to boolean, long, double, whatever.

See this Dev-CPP example project, although it shouldn't be hard to use it somewhere else!

You can implement this easily in your Irrlicht projects.
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

Not bad i like it :D
But it's realy only for config stuff like driver resolution and maybe key setup....
i actually did something simmilar myself but not as nice as u did cause i didn't thought about it like that.....hmmm...whatever good work.
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
Post Reply