Templatized C++ Configuration File Parser Library
This simple web-page explains how to get hold of, and use, the TConf (Templatized C++ Configuration File Parser) library.
The software eases the use of configuration files in programs.
I have used the TCLAP library for most of my programs and it is great.
However, sometimes I found myself having more parameters than I wanted to type, so I developed a small configuration
parser library, inspired by the TCLAP project. I recommend using TCONF in combination with TCLAP so you still have all the nice
help display for your users and also to pass the TCONF config file to your program in a nice manner.
FAQs:
[1] How to download the source?
You can go to the sourceforge project page.
[2] How to extract the files?
Just run the following command:
tar -zxvf <filename>
[3] What would a configuration file look like?
This codeblock shows a configuration file with various data types:
#Configuration file for MyGame
#This will set the number of players in the game
players : 16
#This is the gravity constant
gravity : 9.8
#Name of default user
username : Free Waterfall Jr.
[4] What is the layout of the configuration file?
Empty lines and lines beginning with the '#' symbol are ignored by the parser. You can prefix a line with
the '#' character for comments to parameters.
Please note that the style must be identifier : value
It is important that there is just 1 blank space between the identifier and the ':' colon, and just 1 blank space between the ':' colon and the value.
It is not possible to have a value spanning multiple lines.
[5] How do I use it? Can I see some example code?
This is a small example that displays how to extract various types from a configuration file.
#include <string>
#include "tconf.h"
#include "ConfigValueItem.h"
using std::string;
int main()
{
ConfigValueItem<int>* cvi = new ConfigValueItem<int>("players", 4, false);
ConfigValueItem<double>* cvi2 = new ConfigValueItem<double>("gravity", 10.0, false);
ConfigValueItem<string>* cvi3 = new ConfigValueItem<string>("username", "default", false);
TConf parser("game.cfg");
parser.Add(cvi);
parser.Add(cvi2);
parser.Add(cvi3);
parser.ParseList();
int number_of_players = cvi->GetValue();
double earth_gravity = cvi2->GetValue();
string default_user = cvi3->GetValue();
}
[6] Please explain to me what the parameters of ConfigValueItem mean
ConfigValueItem<int>* cvi = new ConfigValueItem<int>("players", 4, false);
- The first paramter "players" is the identifier that the parser should look for in the config file.
- The second parameter is the default parameter. In case the identifier or file was not found, the default value is assigned.
- The third parameter denotes whether the parameter is required or not. If the parameter is required but not found, it will thrown an exception
Revised Aug 08, 2007