1*2e9d4914SAndroid Build Coastguard Worker /* ----------------------------------------------------------------------------
2*2e9d4914SAndroid Build Coastguard Worker libconfig - A library for processing structured configuration files
3*2e9d4914SAndroid Build Coastguard Worker Copyright (C) 2005-2010 Mark A Lindner
4*2e9d4914SAndroid Build Coastguard Worker
5*2e9d4914SAndroid Build Coastguard Worker This file is part of libconfig.
6*2e9d4914SAndroid Build Coastguard Worker
7*2e9d4914SAndroid Build Coastguard Worker This library is free software; you can redistribute it and/or
8*2e9d4914SAndroid Build Coastguard Worker modify it under the terms of the GNU Lesser General Public License
9*2e9d4914SAndroid Build Coastguard Worker as published by the Free Software Foundation; either version 2.1 of
10*2e9d4914SAndroid Build Coastguard Worker the License, or (at your option) any later version.
11*2e9d4914SAndroid Build Coastguard Worker
12*2e9d4914SAndroid Build Coastguard Worker This library is distributed in the hope that it will be useful, but
13*2e9d4914SAndroid Build Coastguard Worker WITHOUT ANY WARRANTY; without even the implied warranty of
14*2e9d4914SAndroid Build Coastguard Worker MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15*2e9d4914SAndroid Build Coastguard Worker Lesser General Public License for more details.
16*2e9d4914SAndroid Build Coastguard Worker
17*2e9d4914SAndroid Build Coastguard Worker You should have received a copy of the GNU Library General Public
18*2e9d4914SAndroid Build Coastguard Worker License along with this library; if not, see
19*2e9d4914SAndroid Build Coastguard Worker <http://www.gnu.org/licenses/>.
20*2e9d4914SAndroid Build Coastguard Worker ----------------------------------------------------------------------------
21*2e9d4914SAndroid Build Coastguard Worker */
22*2e9d4914SAndroid Build Coastguard Worker
23*2e9d4914SAndroid Build Coastguard Worker #include <iostream>
24*2e9d4914SAndroid Build Coastguard Worker #include <iomanip>
25*2e9d4914SAndroid Build Coastguard Worker #include <cstdlib>
26*2e9d4914SAndroid Build Coastguard Worker #include <libconfig.h++>
27*2e9d4914SAndroid Build Coastguard Worker
28*2e9d4914SAndroid Build Coastguard Worker using namespace std;
29*2e9d4914SAndroid Build Coastguard Worker using namespace libconfig;
30*2e9d4914SAndroid Build Coastguard Worker
31*2e9d4914SAndroid Build Coastguard Worker // This example reads the configuration file 'example.cfg', adds a new
32*2e9d4914SAndroid Build Coastguard Worker // movie record to the movies list, and writes the updated configuration to
33*2e9d4914SAndroid Build Coastguard Worker // 'updated.cfg'.
34*2e9d4914SAndroid Build Coastguard Worker
main(int argc,char ** argv)35*2e9d4914SAndroid Build Coastguard Worker int main(int argc, char **argv)
36*2e9d4914SAndroid Build Coastguard Worker {
37*2e9d4914SAndroid Build Coastguard Worker static const char *output_file = "updated.cfg";
38*2e9d4914SAndroid Build Coastguard Worker
39*2e9d4914SAndroid Build Coastguard Worker Config cfg;
40*2e9d4914SAndroid Build Coastguard Worker
41*2e9d4914SAndroid Build Coastguard Worker cfg.setOptions(Config::OptionFsync
42*2e9d4914SAndroid Build Coastguard Worker | Config::OptionSemicolonSeparators
43*2e9d4914SAndroid Build Coastguard Worker | Config::OptionColonAssignmentForGroups
44*2e9d4914SAndroid Build Coastguard Worker | Config::OptionOpenBraceOnSeparateLine);
45*2e9d4914SAndroid Build Coastguard Worker
46*2e9d4914SAndroid Build Coastguard Worker // Read the file. If there is an error, report it and exit.
47*2e9d4914SAndroid Build Coastguard Worker try
48*2e9d4914SAndroid Build Coastguard Worker {
49*2e9d4914SAndroid Build Coastguard Worker cfg.readFile("example.cfg");
50*2e9d4914SAndroid Build Coastguard Worker }
51*2e9d4914SAndroid Build Coastguard Worker catch(const FileIOException &fioex)
52*2e9d4914SAndroid Build Coastguard Worker {
53*2e9d4914SAndroid Build Coastguard Worker std::cerr << "I/O error while reading file." << std::endl;
54*2e9d4914SAndroid Build Coastguard Worker return(EXIT_FAILURE);
55*2e9d4914SAndroid Build Coastguard Worker }
56*2e9d4914SAndroid Build Coastguard Worker catch(const ParseException &pex)
57*2e9d4914SAndroid Build Coastguard Worker {
58*2e9d4914SAndroid Build Coastguard Worker std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
59*2e9d4914SAndroid Build Coastguard Worker << " - " << pex.getError() << std::endl;
60*2e9d4914SAndroid Build Coastguard Worker return(EXIT_FAILURE);
61*2e9d4914SAndroid Build Coastguard Worker }
62*2e9d4914SAndroid Build Coastguard Worker
63*2e9d4914SAndroid Build Coastguard Worker // Find the 'movies' setting. Add intermediate settings if they don't yet
64*2e9d4914SAndroid Build Coastguard Worker // exist.
65*2e9d4914SAndroid Build Coastguard Worker Setting &root = cfg.getRoot();
66*2e9d4914SAndroid Build Coastguard Worker
67*2e9d4914SAndroid Build Coastguard Worker if(! root.exists("inventory"))
68*2e9d4914SAndroid Build Coastguard Worker root.add("inventory", Setting::TypeGroup);
69*2e9d4914SAndroid Build Coastguard Worker
70*2e9d4914SAndroid Build Coastguard Worker Setting &inventory = root["inventory"];
71*2e9d4914SAndroid Build Coastguard Worker
72*2e9d4914SAndroid Build Coastguard Worker if(! inventory.exists("movies"))
73*2e9d4914SAndroid Build Coastguard Worker inventory.add("movies", Setting::TypeList);
74*2e9d4914SAndroid Build Coastguard Worker
75*2e9d4914SAndroid Build Coastguard Worker Setting &movies = inventory["movies"];
76*2e9d4914SAndroid Build Coastguard Worker
77*2e9d4914SAndroid Build Coastguard Worker // Create the new movie entry.
78*2e9d4914SAndroid Build Coastguard Worker Setting &movie = movies.add(Setting::TypeGroup);
79*2e9d4914SAndroid Build Coastguard Worker
80*2e9d4914SAndroid Build Coastguard Worker movie.add("title", Setting::TypeString) = "Buckaroo Banzai";
81*2e9d4914SAndroid Build Coastguard Worker movie.add("media", Setting::TypeString) = "DVD";
82*2e9d4914SAndroid Build Coastguard Worker movie.add("price", Setting::TypeFloat) = 12.99;
83*2e9d4914SAndroid Build Coastguard Worker movie.add("qty", Setting::TypeInt) = 20;
84*2e9d4914SAndroid Build Coastguard Worker
85*2e9d4914SAndroid Build Coastguard Worker // Write out the updated configuration.
86*2e9d4914SAndroid Build Coastguard Worker try
87*2e9d4914SAndroid Build Coastguard Worker {
88*2e9d4914SAndroid Build Coastguard Worker cfg.writeFile(output_file);
89*2e9d4914SAndroid Build Coastguard Worker cerr << "Updated configuration successfully written to: " << output_file
90*2e9d4914SAndroid Build Coastguard Worker << endl;
91*2e9d4914SAndroid Build Coastguard Worker
92*2e9d4914SAndroid Build Coastguard Worker }
93*2e9d4914SAndroid Build Coastguard Worker catch(const FileIOException &fioex)
94*2e9d4914SAndroid Build Coastguard Worker {
95*2e9d4914SAndroid Build Coastguard Worker cerr << "I/O error while writing file: " << output_file << endl;
96*2e9d4914SAndroid Build Coastguard Worker return(EXIT_FAILURE);
97*2e9d4914SAndroid Build Coastguard Worker }
98*2e9d4914SAndroid Build Coastguard Worker
99*2e9d4914SAndroid Build Coastguard Worker return(EXIT_SUCCESS);
100*2e9d4914SAndroid Build Coastguard Worker }
101*2e9d4914SAndroid Build Coastguard Worker
102*2e9d4914SAndroid Build Coastguard Worker // eof
103