C++ libconfig::Config.lookupValue( )
The lookupValue( ) method is the convenience method for looking up the values of a setting with the given path. The lookupValue( ) method has the following prototypes:
bool lookupValue (const char *path, bool &value)
bool lookupValue (const std::string &path, bool &value)
bool lookupValue (const std::string &path, int &value)
bool lookupValue (const char *path, int &value)
bool lookupValue (const char *path, unsigned int &value)
bool lookupValue (const std::string &path, unsigned int &value)
bool lookupValue (const char *path, long long &value)
bool lookupValue (const std::string &path, long long &value)
bool lookupValue (const char *path, float &value)
bool lookupValue (const std::string &path, float &value)
bool lookupValue (const char *path, double &value)
bool lookupValue (const std::string &path, double &value)
bool lookupValue (const char *path, const char *&value)
bool lookupValue (const std::string &path, const char *&value)
bool lookupValue (const char *path, std::string &value)
bool lookupValue (const std::string &path, std::string &value)
These are convenience methods for looking up the value of a setting with the given path. If the setting is found and is of an appropriate type, the value is stored in value and the method returns true. Otherwise, value is left unmodified and the method returns false. These methods do not throw exceptions.
Storage for const char * values is managed by the library and released automatically when the setting is destroyed or when its value is changed; the string must not be freed by the caller. For safety and convenience, always assigning string values to a std::string is suggested.
The following example shows how to use lookupValue( ) method:
/*include necessary header file*/
#include <iostream>
#include <cstdlib>
#include <libconfig.h++>
/*driver function: program execution begins from here*/
int main(){
/*create an instance of config*/
libconfig::Config config;
/*read a configuration file*/
try{
config.readFile ("example.cfg");
} catch (libconfig::FileIOException &e){
/*inform user about IOException*/
std::cerr << "FileIOException occurred. Could not read \"example.cfg\"!!\n";
/*terminate program*/
exit (EXIT_FAILURE);
} catch (libconfig::ParseException &e){
/*inform user about the parse exception*/
std::cerr << "Parse error at " << e.getFile() << ":" << e.getLine()
<< " - " << e.getError() << std::endl;
/*terminate program*/
return(EXIT_FAILURE);
}
/*settings to be read from configuration file*/
std::string name, version;
/*lookup two values at once and perform */
/*error handling if any one of them not found*/
/*or are of the wrong type*/
/*Both settings exists and are of correct type*/
if (config.lookupValue("name", name) &&
config.lookupValue("version", version)){
/*display details to console*/
std::cout << "Name: " << name << std::endl;
std::cout << "Version: " << version << std::endl;
} else {
std::cout << "Setting \"name\" or \"version\" do not exist!!\n";
}
/*1st setting does not exist and hence lead to short-circuiting*/
if (config.lookupValue("my_name", name) &&
config.lookupValue("version", version)){
/*display details to console*/
std::cout << "My_Name: " << name << std::endl;
std::cout << "Version: " << version << std::endl;
} else {
std::cout << "Setting \"my_name\" or \"version\" do not exist!!\n";
}
int int_name;
/*1st setting exist but is of incorrect type
* and hence lead to short-circuiting*/
if (config.lookupValue("name", int_name) &&
config.lookupValue("version", version)){
/*display details to console*/
std::cout << "Name: " << int_name << std::endl;
std::cout << "Version: " << version << std::endl;
} else {
std::cout << "Setting \"name\" or \"version\" do not exist!!\n";
}
/*return 0 to caller to indicate normal termination of program*/
return 0;
}
The configuration file used as an example here has the following content:
/*example.cfg: An example configuration file that stores information about a store. */
// Basic store information:
name = "Books, Movies & More";
// version
version = "1.0";
// Store inventory:
inventory =
{
books = ( { title = "Treasure Island";
author = "Robert Louis Stevenson";
price = 29.99;
qty = 5; },
{ title = "Snow Crash";
author = "Neal Stephenson";
price = 9.99;
qty = 8; }
);
movies = ( { title = "Brazil";
media = "DVD";
price = 19.99;
qty = 11; },
{ title = "The City of Lost Children";
media = "DVD";
price = 18.99;
qty = 5; },
{ title = "Memento";
media = "Blu-Ray";
price = 24.99;
qty = 20;
},
{ title = "Howard the Duck"; }
);
};
// Store hours:
hours =
{
mon = { open = 9; close = 18; };
tue = { open = 9; close = 18; };
wed = { open = 9; close = 18; };
thu = { open = 9; close = 18; };
fri = { open = 9; close = 20; };
sat = { open = 9; close = 20; };
sun = { open = 11; close = 16; };
};
Assuming that you are on Linux platform:
Use the following command to compile the above source code:
g++ `pkg-config --cflags libconfig++` main.cpp -o main `pkg-config --libs libconfig++`
The above command after successful compilation will generate an object/binary file.
Use the following command to execute the object/binary file:
./main
Here is the output generated by above program:
Name: Books, Movies & More
Version: 1.0
Setting "my_name" or "version" do not exist!!
Setting "name" or "version" do not exist!!
Comments