Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

ConfigFile Class Reference

The ConfigFile class provides a clean interface to pulling information out of a "ini"-style configuration file, split into sections, variables and values. More...

#include <ConfigFile.h>

List of all members.

Public Methods

 ConfigFile (const std::string &fileName)
 Constructs a ConfigFile using the given filename as a config source.

bool varExists (const std::string &section, const std::string &varName)
 Returns true if the variable in the given section was defined.

template<class T> void getVar (const std::string &section, const std::string &varName, T &retVal, const T &defVal)
 Templated extraction function.

template<> void getVar (const std::string &section, const std::string &varName, std::string &retVal, const std::string &defVal)
 Specialization of getVar for std::string, so that it returns the whole variable value string, including spaces.

void getVar (const std::string &section, const std::string &varName, std::string &retVal, const char *defVal)
 An overload of getVar strictly for convience when trying to pull out std::string objects with a constant default value -- it eliminates a temporary construction.


Detailed Description

The ConfigFile class provides a clean interface to pulling information out of a "ini"-style configuration file, split into sections, variables and values.

The format of the file is line-based, and is as follows:

[section_header] # comment line. Any line starting with # variable=value variable2=value2

[section2_header] variable=

The file is not very white-space sensitive. Spaces between the brackets and the section header name will not be counted -- for example: the line

[ Section Name ]

will be interepreted as "Section Name".

Same thing applies for variable names. Spaces on the start of the line and before the = do not count. However, spaces AFTER the = DO count as part of the variable's value -- this is needed so you can have string values with leading spaces. However, note that with istream extraction of numeric types, spaces before the number are ignored. For example, the line

varName = 15.0

Will be interepreted as defining a variable "varName" (case-sensitive, but without the spaces), with the value of " 15.0" (with 2 leading spaces). If the value were to be extracted as a float, it would properly extract as the number 15.0, but if extracted as a string it will have the leading spaces.

The variable and section names are both case-sensitive.

Comment lines (lines whose first character starts with #), blank lines, and invalid lines are completely ignored, so as many blank lines or comments may be added.

All variables defined after a section header are under that section, until another section header occurs. Variables defined before the first section header are in the section "" (empty string). A section may contain 0 or more variables. The variable namespace for each section is different, so a variable called "x" can exist in multiple sections. It is invalid for the same variable name to be defined in the same section, if it is, one of the values will be the final value -- which one is undefined.

ConfigFile is not default constructable.


Constructor & Destructor Documentation

ConfigFile::ConfigFile const std::string &    fileName [explicit]
 

Constructs a ConfigFile using the given filename as a config source.


Member Function Documentation

void ConfigFile::getVar const std::string &    section,
const std::string &    varName,
std::string &    retVal,
const char *    defVal
[inline]
 

An overload of getVar strictly for convience when trying to pull out std::string objects with a constant default value -- it eliminates a temporary construction.

template<>
void ConfigFile::getVar const std::string &    section,
const std::string &    varName,
std::string &    retVal,
const std::string &    defVal
[inline]
 

Specialization of getVar for std::string, so that it returns the whole variable value string, including spaces.

template<class T>
void ConfigFile::getVar const std::string &    section,
const std::string &    varName,
T &    retVal,
const T &    defVal
[inline]
 

Templated extraction function.

Extracts the value of the specified variable into retVal. The extraction operator>>(istream &, T &) must be defined for the type T. If the given section or variable is not defined, defVal is assigned to retVal using operator=.

Parameters:
section  the name of the section
varName  the name of the variable in that section
retVal  [OUT] result is copied into retVal
defVal  default value if section or variable not defined.

bool ConfigFile::varExists const std::string &    section,
const std::string &    varName
 

Returns true if the variable in the given section was defined.


The documentation for this class was generated from the following files:
Generated on Tue Mar 4 02:29:26 2003 for SuperIsoBomb by doxygen1.2.18