AsyncConfig_demo.cpp

An example of how to use the Config class

00001 #include <iostream>
00002 #include <string>
00003 #include <cstdlib>
00004 
00005 #include <AsyncConfig.h>
00006 
00007 using namespace std;
00008 using namespace Async;
00009 
00010 int main(int argc, char **argv)
00011 {
00012   Config cfg;
00013   if (!cfg.open("test.cfg"))
00014   {
00015     cerr << "*** Error: Could not open config file test.cfg\n";
00016     exit(1);
00017   }
00018 
00019     // Read the string value without checking if it exists or not.
00020   cout << "value=" << cfg.getValue("SECTION1", "VALUE1") << endl;
00021   
00022     // Read the string value, returning it in a variable.
00023     // The return value will indicate if the variable was found or not.
00024   string str_val;
00025   if (cfg.getValue("SECTION1", "VALUE2", str_val))
00026   {
00027     cout << "str_val=" << str_val << endl;
00028   }
00029   else
00030   {
00031     cerr << "*** ERROR: Config variable SECTION1/VALUE2 not found.\n";
00032   }
00033   
00034     // Read an integer value.
00035   int int_val = 0;
00036   if (cfg.getValue("SECTION2", "MY_INT", int_val))
00037   {
00038     cout << "int_val=" << int_val << endl;
00039   }
00040   else
00041   {
00042     cerr << "*** ERROR: Config variable SECTION2/MY_INT malformed or "
00043             "not found.\n";
00044   }
00045   
00046     // Read a char value. Missing value is OK.
00047   char char_val = 'Q';
00048   if (cfg.getValue("SECTION1", "NO_VALUE", char_val, true))
00049   {
00050     cout << "char_val=" << char_val << endl;
00051   }
00052   else
00053   {
00054     cerr << "*** ERROR: Config variable SECTION1/NO_VALUE malformed.\n";
00055   }
00056   
00057     // Read a float with min and max limits.
00058   float float_val = 0.0;
00059   if (cfg.getValue("SECTION2", "MY_FLOAT", 3.0f, 4.0f, float_val))
00060   {
00061     cout << "float_val=" << float_val << endl;
00062   }
00063   else
00064   {
00065     cerr << "*** ERROR: Config variable SECTION2/MY_FLOAT malformed, "
00066             "not found or out of range.\n";
00067   }
00068 }

Generated on Mon Mar 25 18:14:42 2013 for Async by  doxygen 1.4.7