1 /* ----------------------------------------------------------------------------
2 libconfig - A library for processing structured configuration files
3 Copyright (C) 2005-2010 Mark A Lindner
4
5 This file is part of libconfig.
6
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.
11
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.
16
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 ----------------------------------------------------------------------------
21 */
22
23 #include <iostream>
24 #include <iomanip>
25 #include <cstdlib>
26 #include <libconfig.h++>
27
28 using namespace std;
29 using namespace libconfig;
30
31 // This example reads the configuration file 'example.cfg' and displays
32 // some of its contents.
33
main(int argc,char ** argv)34 int main(int argc, char **argv)
35 {
36 Config cfg;
37
38 // Read the file. If there is an error, report it and exit.
39 try
40 {
41 cfg.readFile("example.cfg");
42 }
43 catch(const FileIOException &fioex)
44 {
45 std::cerr << "I/O error while reading file." << std::endl;
46 return(EXIT_FAILURE);
47 }
48 catch(const ParseException &pex)
49 {
50 std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
51 << " - " << pex.getError() << std::endl;
52 return(EXIT_FAILURE);
53 }
54
55 // Get the store name.
56 try
57 {
58 string name = cfg.lookup("name");
59 cout << "Store name: " << name << endl << endl;
60 }
61 catch(const SettingNotFoundException &nfex)
62 {
63 cerr << "No 'name' setting in configuration file." << endl;
64 }
65
66 const Setting& root = cfg.getRoot();
67
68 // Output a list of all books in the inventory.
69 try
70 {
71 const Setting &books = root["inventory"]["books"];
72 int count = books.getLength();
73
74 cout << setw(30) << left << "TITLE" << " "
75 << setw(30) << left << "AUTHOR" << " "
76 << setw(6) << left << "PRICE" << " "
77 << "QTY"
78 << endl;
79
80 for(int i = 0; i < count; ++i)
81 {
82 const Setting &book = books[i];
83
84 // Only output the record if all of the expected fields are present.
85 string title, author;
86 double price;
87 int qty;
88
89 if(!(book.lookupValue("title", title)
90 && book.lookupValue("author", author)
91 && book.lookupValue("price", price)
92 && book.lookupValue("qty", qty)))
93 continue;
94
95 cout << setw(30) << left << title << " "
96 << setw(30) << left << author << " "
97 << '$' << setw(6) << right << price << " "
98 << qty
99 << endl;
100 }
101 cout << endl;
102 }
103 catch(const SettingNotFoundException &nfex)
104 {
105 // Ignore.
106 }
107
108 // Output a list of all books in the inventory.
109 try
110 {
111 const Setting &movies = root["inventory"]["movies"];
112 int count = movies.getLength();
113
114 cout << setw(30) << left << "TITLE" << " "
115 << setw(10) << left << "MEDIA" << " "
116 << setw(6) << left << "PRICE" << " "
117 << "QTY"
118 << endl;
119
120 for(int i = 0; i < count; ++i)
121 {
122 const Setting &movie = movies[i];
123
124 // Only output the record if all of the expected fields are present.
125 string title, media;
126 double price;
127 int qty;
128
129 if(!(movie.lookupValue("title", title)
130 && movie.lookupValue("media", media)
131 && movie.lookupValue("price", price)
132 && movie.lookupValue("qty", qty)))
133 continue;
134
135 cout << setw(30) << left << title << " "
136 << setw(10) << left << media << " "
137 << '$' << setw(6) << right << price << " "
138 << qty
139 << endl;
140 }
141 cout << endl;
142 }
143 catch(const SettingNotFoundException &nfex)
144 {
145 // Ignore.
146 }
147
148 return(EXIT_SUCCESS);
149 }
150
151 // eof
152