#include <ConfigFile.h>
Public Methods | |
ConfigFile (const std::string &fileName) | |
Constructs a ConfigFile using the given filename as a config source. | |
bool | varExists (const std::string §ion, const std::string &varName) |
Returns true if the variable in the given section was defined. | |
template<class T> void | getVar (const std::string §ion, const std::string &varName, T &retVal, const T &defVal) |
Templated extraction function. | |
template<> void | getVar (const std::string §ion, 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 §ion, 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. |
The format of the file is line-based, and is as follows:
[section2_header] variable=[section_header] # comment line. Any line starting with # variable=value variable2=value2
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.
|
Constructs a ConfigFile using the given filename as a config source.
|
|
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.
|
|
Specialization of getVar for std::string, so that it returns the whole variable value string, including spaces.
|
|
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=.
|
|
Returns true if the variable in the given section was defined.
|