ConfigFile

ConfigFile class, for reading/writing INI-style config files.

File format is fairly flexible, whitespace and comments beginning with # or ; are ignored & left intact.

Use []’s to denote sections.

Variables are defined in var=val format.

Summary
ConfigFile class, for reading/writing INI-style config files.
Default constructor for ConfigFile.
Constructor for ConfigFile, calls open.
Calls close upon the ConfigFile.
open a file, processing it as an INI-like config file.
Flushes the data written to the config file to disk, generally needs not be called.
Flushes the data and closes, open must be called again before using same ConfigFile.
Template function for setting variables in the config file.
Template function for getting values from the config file.

(Con/ De)structors

ConfigFile

ConfigFile()

Default constructor for ConfigFile.

ConfigFile

ConfigFile(const std::string &filename)

Constructor for ConfigFile, calls open.

Parameters

filenameName of ConfigFile to open.

~ConfigFile

virtual ~ConfigFile()

Calls close upon the ConfigFile.

File Access

open

void open(const std::string &filename)

open a file, processing it as an INI-like config file.

Parameters

filenameName of ConfigFile to open.

flush

void flush()

Flushes the data written to the config file to disk, generally needs not be called.

close

void close()

Flushes the data and closes, open must be called again before using same ConfigFile.

setVariable

template<class varType> void setVariable(const std::string &sec,
const std::string &var,
varType value)

Template function for setting variables in the config file.

WARNING: Do not try to use this with user-defined types, numeric types and strings work fine, and this is all that should be contained in an INI.

Parameters

secsection name within config file
varvariable name within section
valuevalue to set variable equal to

getVariable

template<class varType> varType getVariable(const std::string &sec,
const std::string &var,
varType defVal) const

Template function for getting values from the config file.  Supports returning a default value if the desired variable was not found.

WARNING: Do not try to use this with user-defined types, numeric types and strings work fine, and this is all that should be contained in an INI.

Parameters

secsection name within config file
varvariable name within section
defValvalue to return if variable does not exist

Returns

Value of variable within config file or defVal if value was not found.

ConfigFile()
Default constructor for ConfigFile.
void open(const std::string &filename)
open a file, processing it as an INI-like config file.
virtual ~ConfigFile()
Calls close upon the ConfigFile.
void flush()
Flushes the data written to the config file to disk, generally needs not be called.
void close()
Flushes the data and closes, open must be called again before using same ConfigFile.
template<class varType> void setVariable(const std::string &sec,
const std::string &var,
varType value)
Template function for setting variables in the config file.
template<class varType> varType getVariable(const std::string &sec,
const std::string &var,
varType defVal) const
Template function for getting values from the config file.