root/library/bdm/base/libconfig/examples/c++/example2.cpp @ 706

Revision 706, 2.8 kB (checked in by smidl, 15 years ago)

eol-native

  • Property svn:eol-style set to native
Line 
1/* ----------------------------------------------------------------------------
2   libconfig - A library for processing structured configuration files
3   Copyright (C) 2005-2009  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
28using namespace std;
29using 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
35int main(int argc, char **argv)
36{
37  static const char *output_file = "updated.cfg";
38
39  Config cfg;
40
41  // Read the file. If there is an error, report it and exit.
42  try
43  {
44    cfg.readFile("example.cfg");
45  }
46  catch(const FileIOException &fioex)
47  {
48    std::cerr << "I/O error while reading file." << std::endl;
49    return(EXIT_FAILURE);
50  }
51  catch(const ParseException &pex)
52  {
53    std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
54              << " - " << pex.getError() << std::endl;
55    return(EXIT_FAILURE);
56  }
57
58  // Find the 'movies' setting. Add intermediate settings if they don't yet
59  // exist.
60  Setting &root = cfg.getRoot();
61
62  if(! root.exists("inventory"))
63    root.add("inventory", Setting::TypeGroup);
64
65  Setting &inventory = root["inventory"];
66
67  if(! inventory.exists("movies"))
68    inventory.add("movies", Setting::TypeList);
69
70  Setting &movies = inventory["movies"];
71
72  // Create the new movie entry.
73  Setting &movie = movies.add(Setting::TypeGroup);
74
75  movie.add("title", Setting::TypeString) = "Buckaroo Banzai";
76  movie.add("media", Setting::TypeString) = "DVD";
77  movie.add("price", Setting::TypeFloat) = 12.99;
78  movie.add("qty", Setting::TypeInt) = 20;
79
80  // Write out the updated configuration.
81  try
82  {
83    cfg.writeFile(output_file);
84    cerr << "Updated configuration successfully written to: " << output_file
85         << endl;
86
87  }
88  catch(const FileIOException &fioex)
89  {
90    cerr << "I/O error while writing file: " << output_file << endl;
91    return(EXIT_FAILURE);
92  }
93
94  return(EXIT_SUCCESS);
95}
96
97// eof
Note: See TracBrowser for help on using the browser.