C++ libconfig::Config.getRoot( )














































C++ libconfig::Config.getRoot( )



C++ libconfig::Config.getRoot( )

The getRoot( ) method has the following prototype:
Setting& getRoot ( )
This method returns the root setting of the configuration, which is a group.

The following example shows how to use getRoot( ) method:
/*include necessary header file*/
#include <iostream>
#include <stdlib.h>
#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);
}

/*get the root setting of the configuration file*/
libconfig::Setting& root = config.getRoot();

try {
/*get books settings under inventory settings under root setting*/
libconfig::Setting& books = root["inventory"]["books"];

/*iterate though all books found in configuration*/
for (auto i = books.begin(); i != books.end(); ++i){
/*get all details of particular book*/
std::string title = i->lookup("title");
std::string author = i->lookup("author");
double price = i->lookup("price");
int quantity = i->lookup("qty");

/*display details to console*/
std::cout << "{title: " << title << ", author: " << author
<< ", price " << price << ", quantity: " << quantity
<< "}" << std::endl;
}
} catch(libconfig::SettingNotFoundException &e) {
/*in case if setting does not exist*/
std::cerr << e.what() << std::endl;
} catch (libconfig::SettingTypeException &e){
/*in case if type of setting do not match with type*/
/*declared by the programmer*/
std::cerr << e.what();
}

/*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:
{title: Treasure Island, author: Robert Louis Stevenson, price 29.99, quantity: 5}
{title: Snow Crash, author: Neal Stephenson, price 9.99, quantity: 8}

More Articles of Shailly Jain:

Name Views Likes
C++ cv::subtract 3260 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( ) 559 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( ) 3392 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