Async  0.18.0
Public Member Functions | List of all members
Async::Config Class Reference

A class for reading INI-formatted configuration files. More...

#include <AsyncConfig.h>

Public Member Functions

 Config (void)
 Default constuctor. More...
 
 ~Config (void)
 Destructor. More...
 
bool open (const std::string &name)
 Open the given config file. More...
 
const std::string & getValue (const std::string &section, const std::string &tag) const
 Return the string value of the given configuration variable. More...
 
bool getValue (const std::string &section, const std::string &tag, std::string &value) const
 Get the string value of the given configuration variable. More...
 
template<typename Rsp >
bool getValue (const std::string &section, const std::string &tag, Rsp &rsp, bool missing_ok=false) const
 Get the value of the given configuration variable. More...
 
template<typename Rsp >
bool getValue (const std::string &section, const std::string &tag, const Rsp &min, const Rsp &max, Rsp &rsp, bool missing_ok=false) const
 Get a range checked variable value. More...
 
std::list< std::string > listSection (const std::string &section)
 Return the name of all the tags in the given section. More...
 

Detailed Description

A class for reading INI-formatted configuration files.

Author
Tobias Blomberg
Date
2004-03-17

This class is used to read configuration files that is in the famous MS Windows INI file format. An example of a configuration file and how to use the class is shown below.

[SECTION1]
VALUE1=The value
VALUE2="Hello, "
"multi line "
"value!"
[SECTION2]
VALUE1="Whatever you want"
MY_INT=42
MY_FLOAT=3.14159
#include <iostream>
#include <string>
#include <cstdlib>
#include <AsyncConfig.h>
using namespace std;
using namespace Async;
int main(int argc, char **argv)
{
Config cfg;
if (!cfg.open("test.cfg"))
{
cerr << "*** Error: Could not open config file test.cfg\n";
exit(1);
}
// Read the string value without checking if it exists or not.
cout << "value=" << cfg.getValue("SECTION1", "VALUE1") << endl;
// Read the string value, returning it in a variable.
// The return value will indicate if the variable was found or not.
string str_val;
if (cfg.getValue("SECTION1", "VALUE2", str_val))
{
cout << "str_val=" << str_val << endl;
}
else
{
cerr << "*** ERROR: Config variable SECTION1/VALUE2 not found.\n";
}
// Read an integer value.
int int_val = 0;
if (cfg.getValue("SECTION2", "MY_INT", int_val))
{
cout << "int_val=" << int_val << endl;
}
else
{
cerr << "*** ERROR: Config variable SECTION2/MY_INT malformed or "
"not found.\n";
}
// Read a char value. Missing value is OK.
char char_val = 'Q';
if (cfg.getValue("SECTION1", "NO_VALUE", char_val, true))
{
cout << "char_val=" << char_val << endl;
}
else
{
cerr << "*** ERROR: Config variable SECTION1/NO_VALUE malformed.\n";
}
// Read a float with min and max limits.
float float_val = 0.0;
if (cfg.getValue("SECTION2", "MY_FLOAT", 3.0f, 4.0f, float_val))
{
cout << "float_val=" << float_val << endl;
}
else
{
cerr << "*** ERROR: Config variable SECTION2/MY_FLOAT malformed, "
"not found or out of range.\n";
}
}
Examples:
AsyncConfig_demo.cpp.

Definition at line 133 of file AsyncConfig.h.

Constructor & Destructor Documentation

Async::Config::Config ( void  )
inline

Default constuctor.

Definition at line 139 of file AsyncConfig.h.

Async::Config::~Config ( void  )

Destructor.

Member Function Documentation

const std::string& Async::Config::getValue ( const std::string &  section,
const std::string &  tag 
) const

Return the string value of the given configuration variable.

Parameters
sectionThe name of the section where the configuration variable is located
tagThe name of the configuration variable to get
Returns
Returns String with content of the configuration variable. If no variable is found an empty string is returned

This function will return the string value corresponding to the given configuration variable. If the configuration variable is unset, an empty sting is returned.

Examples:
AsyncConfig_demo.cpp.

Referenced by getValue().

bool Async::Config::getValue ( const std::string &  section,
const std::string &  tag,
std::string &  value 
) const

Get the string value of the given configuration variable.

Parameters
sectionThe name of the section where the configuration variable is located
tagThe name of the configuration variable to get
valueThe value is returned in this argument. Any previous contents is wiped
Returns
Returns true on success or else false on failure

This function is used to get the value for a configuration variable of type "string".

template<typename Rsp >
bool Async::Config::getValue ( const std::string &  section,
const std::string &  tag,
Rsp &  rsp,
bool  missing_ok = false 
) const
inline

Get the value of the given configuration variable.

Parameters
sectionThe name of the section where the configuration variable is located
tagThe name of the configuration variable to get
rspThe value is returned in this argument. Successful completion overwrites previous contents
missing_okIf set to true, return true if the configuration variable is missing
Returns
Returns true on success or else false on failure.

This function is used to get the value of a configuraiton variable. It's a template function meaning that it can take any value type that supports the operator>> function. Note that when the value is of type string, the overloaded getValue is used rather than this function. Normally a missing configuration variable is seen as an error and the function returns false. If the missing_ok parameter is set to true, this function returns true for a missing variable but till returns false if an illegal value is specified.

Definition at line 204 of file AsyncConfig.h.

References getValue().

template<typename Rsp >
bool Async::Config::getValue ( const std::string &  section,
const std::string &  tag,
const Rsp &  min,
const Rsp &  max,
Rsp &  rsp,
bool  missing_ok = false 
) const
inline

Get a range checked variable value.

Parameters
sectionThe name of the section where the configuration variable is located
tagThe name of the configuration variable to get.
minSmallest valid value.
maxLargest valid value.
rspThe value is returned in this argument. Successful completion overwites prevoius contents.
missing_okIf set to true, return true if the configuration variable is missing
Returns
Returns true if value is within range otherwise false.

This function is used to get the value of the given configuration variable, checking if it is within the given range (min <= value <= max). Requires operators >>, < and > to be defined in the value object. Normally a missing configuration variable is seen as an error and the function returns false. If the missing_ok parameter is set to true, this function returns true for a missing variable but till returns false if an illegal value is specified.

Definition at line 245 of file AsyncConfig.h.

References getValue().

std::list<std::string> Async::Config::listSection ( const std::string &  section)

Return the name of all the tags in the given section.

Parameters
sectionThe name of the section where the configuration variables are located
Returns
Returns the list of tags in the given section
bool Async::Config::open ( const std::string &  name)

Open the given config file.

Parameters
nameThe name of the configuration file to open
Returns
Returns true on success or else false.
Examples:
AsyncConfig_demo.cpp.

The documentation for this class was generated from the following file: