1 /* ----------------------------------------------------------------------------
2 libconfig - A library for processing structured configuration files
3 Copyright (C) 2005-2010 Mark A Lindner
5 This file is part of libconfig.
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public License
9 as published by the Free Software Foundation; either version 2.1 of
10 the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with this library; if not, see
19 <http://www.gnu.org/licenses/>.
20 ----------------------------------------------------------------------------
26 #include <libconfig.h++>
29 using namespace libconfig;
31 // This example reads the configuration file 'example.cfg' and displays
32 // some of its contents.
34 int main(int argc, char **argv)
38 // Read the file. If there is an error, report it and exit.
41 cfg.readFile("example.cfg");
43 catch(const FileIOException &fioex)
45 std::cerr << "I/O error while reading file." << std::endl;
48 catch(const ParseException &pex)
50 std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
51 << " - " << pex.getError() << std::endl;
55 // Get the store name.
58 string name = cfg.lookup("name");
59 cout << "Store name: " << name << endl << endl;
61 catch(const SettingNotFoundException &nfex)
63 cerr << "No 'name' setting in configuration file." << endl;
66 const Setting& root = cfg.getRoot();
68 // Output a list of all books in the inventory.
71 const Setting &books = root["inventory"]["books"];
72 int count = books.getLength();
74 cout << setw(30) << left << "TITLE" << " "
75 << setw(30) << left << "AUTHOR" << " "
76 << setw(6) << left << "PRICE" << " "
80 for(int i = 0; i < count; ++i)
82 const Setting &book = books[i];
84 // Only output the record if all of the expected fields are present.
89 if(!(book.lookupValue("title", title)
90 && book.lookupValue("author", author)
91 && book.lookupValue("price", price)
92 && book.lookupValue("qty", qty)))
95 cout << setw(30) << left << title << " "
96 << setw(30) << left << author << " "
97 << '$' << setw(6) << right << price << " "
103 catch(const SettingNotFoundException &nfex)
108 // Output a list of all books in the inventory.
111 const Setting &movies = root["inventory"]["movies"];
112 int count = movies.getLength();
114 cout << setw(30) << left << "TITLE" << " "
115 << setw(10) << left << "MEDIA" << " "
116 << setw(6) << left << "PRICE" << " "
120 for(int i = 0; i < count; ++i)
122 const Setting &movie = movies[i];
124 // Only output the record if all of the expected fields are present.
129 if(!(movie.lookupValue("title", title)
130 && movie.lookupValue("media", media)
131 && movie.lookupValue("price", price)
132 && movie.lookupValue("qty", qty)))
135 cout << setw(30) << left << title << " "
136 << setw(10) << left << media << " "
137 << '$' << setw(6) << right << price << " "
143 catch(const SettingNotFoundException &nfex)
148 return(EXIT_SUCCESS);