This article only looks at the bold text, too many catch statements. The two examples are from the example folder of the libconfig package ,.
Example 1:
# Include <iostream>
# Include <iomanip>
# Include <cstdlib>
# Include <libconfig. h ++>
Using namespace STD;
Using namespace libconfig;
// This example reads the configuration file 'example. cfg 'and display some of its contents.
Int main (INT argc, char ** argv)
{
Config CFG;
Try
{
Cfg. readfile ("example. cfg"); // read the configuration file
}
Catch (const fileioexception & fioex)
{
STD: cerr <"I/O error while reading file." <STD: Endl;
Return (exit_failure );
}
Catch (const parseexception & PEX)
{
STD: cerr <"Parse error at" <Pex. GetFile () <":" <Pex. Getline ()
<"-" <Pex. geterror () <STD: Endl;
Return (exit_failure );
}
// Get the store name.
Try
{
String name = cfg. Lookup ("name"); // query a path named "name", get setting, and store it to name
Cout <"store name:" <name <Endl;
}
Catch (const settingnotfoundexception & nfex)
{
Cerr <"No 'name' setting in configuration file." <Endl;
}
Const setting & root = cfg. getroot (); // get the root setting
// Output a list of all books in the inventory.
Try
{
Const setting & books = root ["inventory"] ["books"];
Int COUNT = books. getlength (); // get the length of a setting to read it cyclically.
For (INT I = 0; I <count; ++ I)
{
Const setting & book = books [I];
// Only output the record if all of the expected fields are present.
String title, author;
Double price;
Int qty;
If (! (Book. lookupvalue ("title", title) // query the setting file named title stored in the title.
& Book. lookupvalue ("author", author)
& Book. lookupvalue ("price", price)
& Book. lookupvalue ("QTY", qty )))
Continue;
Cout <SETW (30) <left <title <""
<SETW (30) <left <author <""
<'$' <SETW (6) <right <price <""
<Qty
<Endl;
}
Cout <Endl;
}
Catch (const settingnotfoundexception & nfex)
{
// Ignore.
}
Return (exit_success );
}
// EOF
Example 2 header file omitted
// This example reads the configuration file 'example. cfg ', adds a new
// Movie record to the movies list, and writes the updated configuration
// 'Updated. cfg '.
Int main (INT argc, char ** argv)
{
Static const char * output_file = "updated. cfg ";
Config CFG;
// Read the file. If there is an error, report it and exit.
Try
{
Cfg. readfile ("example. cfg"); // read the configuration file
}
Catch (const fileioexception & fioex)
{
STD: cerr <"I/O error while reading file." <STD: Endl;
Return (exit_failure );
}
Catch (const parseexception & PEX)
{
STD: cerr <"Parse error at" <Pex. GetFile () <":" <Pex. Getline ()
<"-" <Pex. geterror () <STD: Endl;
Return (exit_failure );
}
// Get the store name.
Try
{
String name = cfg. Lookup ("name"); // you can find a setting (Name: Name) in the path "name" and store it in the string name.
Cout <"store name:" <name <Endl;
}
Catch (const settingnotfoundexception & nfex)
{
Cerr <"No 'name' setting in configuration file." <Endl;
}
// Find the 'movies' setting. add Intermediate settings if they don't yet
// Exist.
Setting & root = cfg. getroot (); // return root setting
If (! Root. exists ("inventory "))
Root. Add ("inventory", setting: typegroup );
Setting & Inventory = root ["inventory"];
If (! Inventory. exists ("Movies "))
Inventory. Add ("Movies", setting: typelist );
Setting & movies = inventory ["Movies"];
// Create the new movie entry.
Setting & movie = movies. Add (setting: typegroup); // Add a setting: typegroup type sub setting.
Movie. Add ("title", setting: typestring) = "buckaroo banzai ";//Add a sub-setting
Movie. Add ("Media", setting: typestring) = "DVD ";
Movie. Add ("price", setting: typefloat) = 12.99;
Movie. Add ("QTY", setting: typeint) = 20;
// Write out the updated configuration.
Try
{
Cfg. writefile (output_file); // write the configuration to a file.
Cerr <"updated configuration successfully written to:" <output_file
<Endl;
}
Catch (const fileioexception & fioex)
{
Cerr <"I/O error while writing file:" <output_file <Endl;
Return (exit_failure );
}
Return (exit_success );
}
// EOF
Configuration file:
Example. cfg:
// An example configuration file that stores information about a store.
// Basic store information:
Name = "books, movies & more ";
// Store inventory:
Inventory =
{
Books = ({Title = "Treasure Island ";
Author = "Robert Louis Steven son ";
Price = 29.99;
Qty = 5 ;},
{Title = "Snow Crash ";
Author = "Neal Stephen enson ";
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 ;};
};