#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <iterator>
using namespace std;
int main(int argc, char **argv)
{
if (!cfg.
open(
"test.cfg"))
{
cerr << "*** Error: Could not open config file test.cfg\n";
exit(1);
}
cout <<
"value=" << cfg.
getValue(
"SECTION1",
"VALUE1") << endl;
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";
}
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";
}
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";
}
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";
}
[](const std::string& val)
{
cout << "SECTION1/VALUE1=" << val << endl;
});
cfg.
setValue(
"SECTION1",
"VALUE1",
"A subscribed string value");
[](int val)
{
cout << "SECTION2/MY_INT=" << val << endl;
});
cfg.
setValue(
"SECTION2",
"MY_INT", 4711);
[=](const std::vector<int>& vec)
{
std::copy(vec.begin(), vec.end(),
std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
});
cfg.
setValue(
"SECTION1",
"VEC", std::vector<int>{42,43,44});
return 0;
}
A class for reading "INI-foramtted" configuration files.
A class for reading INI-formatted configuration files.
bool open(const std::string &name)
Open the given config file.
const std::string & getValue(const std::string §ion, const std::string &tag) const
Return the string value of the given configuration variable.
void setValue(const std::string §ion, const std::string &tag, const std::string &value)
Set the value of a configuration variable.
void subscribeValue(const std::string §ion, const std::string &tag, const char *def, F func)
Subscribe to the given configuration variable (char*)
Namespace for the asynchronous programming classes.