C++ libconfig::Config::lookupValue( )














































C++ libconfig::Config::lookupValue( )



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!!

More Articles of Shailly Jain:

Name Views Likes
C++ cv::subtract 3263 0
C++ libconfig::Setting::setFormat( ) 716 0
Cast operators for Setting provided by config library 477 0
C++ cv::bitwise_or 1812 0
C++ libconfig::Setting::getType( ) 811 0
C++ tinyxml2::XMLNode::LastChild( ) 560 0
C++ libconfig::Setting::isAggregate( ) 416 0
C++ cv::bitwise_not 2434 0
Basics of configuration files supported by libconfig in C++ 1467 0
C++ tinyxml2::XMLNode::ToDeclaration( ) 742 0
C++ libconfig::Config.exists( ) 959 0
C++ cv::trace( ) 693 0
C++ libconfig::Setting::getName( ) 699 0
C++ tinyxml2::XMLNode::GetLineNum( ) 518 0
C++ libconfig::Config.readFile( ) 1925 0
C++ tinxyxml2 Introduction 799 0
C++ libconfig::Setting::add( ) 659 0
C++ tinyxml2::XMLNode::FirstChild( ) 572 0
C++ libconfig::Config::setAutoConvert( ) 579 0
Subscripting Setting objects in C++ - libconfig::Setting 398 0
C++ tinyxml2::XMLNode::ToElement( ) 679 0
C++ libconfig::Setting::isScalar( ) 365 0
C++ tinyxml2::XMLNode::FirstChildElement( ) 1041 0
C++ libconfig::Config::writeFile( ) 845 0
C++ libconfig::Config::getAutoConvert( ) 661 0
C++ libconfig::Setting::exists( ) 563 0
C++ cv::bitwise_and 7175 0
C++ libconfig::Setting::remove( ) 653 0
C++ libconfig::Setting::isNumber( ) 453 0
C++ tinyxml2::XMLNode::ToComment( ) 562 0
C++ libconfig::Setting::getIndex( ) 529 0
C++ libconfig::Setting::isRoot( ) 423 0
C++ libconfig::Setting::getParent( ) 512 0
C++ tinyxml2::XMLNode::ToText( ) 552 0
Exceptions defined by libconfig library 724 0
Assignment operators for Setting provided by libconfig library 358 0
C++ tinyxml2::XMLNode::ToDocument( ) 405 0
C++ libconfig::ParseException - Methods 951 0
C++ libconfig Introduction 1413 0
C++ tinyxml2::XMLNode::ToUnknown( ) 448 0
C++ libconfig::Config.lookup( ) 3439 0
C++ libconfig::Setting::getPath( ) 620 0
C++ libconfig::Setting::getFormat( ) 415 0
C++ cv::bitwise_xor 1235 0
C++ libconfig::Config.lookupValue( ) 3393 0
C++ tinyxml2::XMLNode 420 0
C++ tinyxml2::XMLNode::Parent( ) 612 0
C++ libconfig::Config.getRoot( ) 955 0
C++ tinyxml2::XMLNode::LastChildElement( ) 458 0
C++ libconfig::Setting::isGroup( ) 547 0
C++ tinyxml2::XMLDocument::LoadFile 1713 0
C++ libconfig::Setting::isList( ) 286 0
C++ libconfig::Setting::isArray( ) 587 0
C++ tinyxml2 installation 1369 0

Comments