• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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', adds a new
32 // movie record to the movies list, and writes the updated configuration to
33 // 'updated.cfg'.
34 
main(int argc,char ** argv)35 int main(int argc, char **argv)
36 {
37   static const char *output_file = "updated.cfg";
38 
39   Config cfg;
40 
41   cfg.setOptions(Config::OptionFsync
42                  | Config::OptionSemicolonSeparators
43                  | Config::OptionColonAssignmentForGroups
44                  | Config::OptionOpenBraceOnSeparateLine);
45 
46   // Read the file. If there is an error, report it and exit.
47   try
48   {
49     cfg.readFile("example.cfg");
50   }
51   catch(const FileIOException &fioex)
52   {
53     std::cerr << "I/O error while reading file." << std::endl;
54     return(EXIT_FAILURE);
55   }
56   catch(const ParseException &pex)
57   {
58     std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
59               << " - " << pex.getError() << std::endl;
60     return(EXIT_FAILURE);
61   }
62 
63   // Find the 'movies' setting. Add intermediate settings if they don't yet
64   // exist.
65   Setting &root = cfg.getRoot();
66 
67   if(! root.exists("inventory"))
68     root.add("inventory", Setting::TypeGroup);
69 
70   Setting &inventory = root["inventory"];
71 
72   if(! inventory.exists("movies"))
73     inventory.add("movies", Setting::TypeList);
74 
75   Setting &movies = inventory["movies"];
76 
77   // Create the new movie entry.
78   Setting &movie = movies.add(Setting::TypeGroup);
79 
80   movie.add("title", Setting::TypeString) = "Buckaroo Banzai";
81   movie.add("media", Setting::TypeString) = "DVD";
82   movie.add("price", Setting::TypeFloat) = 12.99;
83   movie.add("qty", Setting::TypeInt) = 20;
84 
85   // Write out the updated configuration.
86   try
87   {
88     cfg.writeFile(output_file);
89     cerr << "Updated configuration successfully written to: " << output_file
90          << endl;
91 
92   }
93   catch(const FileIOException &fioex)
94   {
95     cerr << "I/O error while writing file: " << output_file << endl;
96     return(EXIT_FAILURE);
97   }
98 
99   return(EXIT_SUCCESS);
100 }
101 
102 // eof
103