Setting & add (const std::string &name, Setting::Type type)
Setting & add (const char *name, Setting::Type type)
Setting & add (Setting::Type type)
/*read setting application.misc*/
/*returns a group as*/
/* misc {
pi = 3.141592654;
bigint = 9223372036854775807L;
columns = [ "Last Name", "First Name", "MI" ];
bitmask = 0x1FC3;
};*/
libconfig::Setting& setting = config.lookup("application.misc" );
/*now to add setting e = 2.718 we will write*/
setting.add("e", libconfig::Setting::TypeFloat);
/*by default the setting application.misc["e"] will have 0.0*/
/* misc {
pi = 3.141592654;
bigint = 9223372036854775807L;
columns = [ "Last Name", "First Name", "MI" ];
bitmask = 0x1FC3;
e : 0;
};*/
/*following line prints e : 0*/
std::cout << "e : " << (double)config.lookup("application.misc" )["e"] << std::endl;
/*to assign the value 2.718 we need to use assignment operator as*/
setting["e"] = 2.718;
/* misc {
pi = 3.141592654;
bigint = 9223372036854775807L;
columns = [ "Last Name", "First Name", "MI" ];
bitmask = 0x1FC3;
e : 2.718;
};*/
/*following line prints e : 2.718*/
std::cout << "e : " << (double)config.lookup("application.misc" )["e"] << std::endl;
/*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 ("sample.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);
}
try {
/*read setting application.misc*/
/*returns a group as*/
/* misc {
pi = 3.141592654;
bigint = 9223372036854775807L;
columns = [ "Last Name", "First Name", "MI" ];
bitmask = 0x1FC3;
};*/
libconfig::Setting& setting = config.lookup("application.misc" );
/*now to add setting e = 2.718 we will write*/
setting.add("e", libconfig::Setting::TypeFloat);
/*by default the setting application.misc["e"] will have 0.0*/
/*following line prints e : 0*/
std::cout << "e : " << (double)config.lookup("application.misc" )["e"] << std::endl;
/*to assign the value 2.718 we need to use assignment operator as*/
setting["e"] = 2.718;
/*following line prints e : 2.718*/
std::cout << "e : " << (double)config.lookup("application.misc" )["e"] << std::endl;
} catch(libconfig::SettingNotFoundException &e) {
/*in case if setting does not exist*/
std::cerr << R"(No "name" or "version" setting(s) in configuration file.)" << std::endl;
} catch (libconfig::SettingTypeException &e){
/*in case if type of setting do not match with type*/
/*declared by the programmer*/
std::cerr << "Type mismatch exception occurred!!\n";
}
/*return 0 to caller to indicate normal termination of program*/
return 0;
}
Comments