aboutsummaryrefslogtreecommitdiffstats
path: root/examples/c++/example1.cpp
blob: 1fb5ff634e4dfe19ba8e347e9b0f1b096a71f598 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* ----------------------------------------------------------------------------
   libconfig - A library for processing structured configuration files
   Copyright (C) 2005-2010  Mark A Lindner

   This file is part of libconfig.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public License
   as published by the Free Software Foundation; either version 2.1 of
   the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; if not, see
   <http://www.gnu.org/licenses/>.
   ----------------------------------------------------------------------------
*/

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <libconfig.h++>

using namespace std;
using namespace libconfig;

// This example reads the configuration file 'example.cfg' and displays
// some of its contents.

int main(int argc, char **argv)
{
  Config cfg;

  // Read the file. If there is an error, report it and exit.
  try
  {
    cfg.readFile("example.cfg");
  }
  catch(const FileIOException &fioex)
  {
    std::cerr << "I/O error while reading file." << std::endl;
    return(EXIT_FAILURE);
  }
  catch(const ParseException &pex)
  {
    std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
              << " - " << pex.getError() << std::endl;
    return(EXIT_FAILURE);
  }

  // Get the store name.
  try
  {
    string name = cfg.lookup("name");
    cout << "Store name: " << name << endl << endl;
  }
  catch(const SettingNotFoundException &nfex)
  {
    cerr << "No 'name' setting in configuration file." << endl;
  }

  const Setting& root = cfg.getRoot();

  // Output a list of all books in the inventory.
  try
  {
    const Setting &books = root["inventory"]["books"];
    int count = books.getLength();

    cout << setw(30) << left << "TITLE" << "  "
         << setw(30) << left << "AUTHOR" << "   "
         << setw(6) << left << "PRICE" << "  "
         << "QTY"
         << endl;

    for(int i = 0; i < count; ++i)
    {
      const Setting &book = books[i];

      // Only output the record if all of the expected fields are present.
      string title, author;
      double price;
      int qty;

      if(!(book.lookupValue("title", title)
           && book.lookupValue("author", author)
           && book.lookupValue("price", price)
           && book.lookupValue("qty", qty)))
        continue;

      cout << setw(30) << left << title << "  "
           << setw(30) << left << author << "  "
           << '$' << setw(6) << right << price << "  "
           << qty
           << endl;
    }
    cout << endl;
  }
  catch(const SettingNotFoundException &nfex)
  {
    // Ignore.
  }

  // Output a list of all books in the inventory.
  try
  {
    const Setting &movies = root["inventory"]["movies"];
    int count = movies.getLength();

    cout << setw(30) << left << "TITLE" << "  "
         << setw(10) << left << "MEDIA" << "   "
         << setw(6) << left << "PRICE" << "  "
         << "QTY"
         << endl;

    for(int i = 0; i < count; ++i)
    {
      const Setting &movie = movies[i];

      // Only output the record if all of the expected fields are present.
      string title, media;
      double price;
      int qty;

      if(!(movie.lookupValue("title", title)
           && movie.lookupValue("media", media)
           && movie.lookupValue("price", price)
           && movie.lookupValue("qty", qty)))
        continue;

      cout << setw(30) << left << title << "  "
           << setw(10) << left << media << "  "
           << '$' << setw(6) << right << price << "  "
           << qty
           << endl;
    }
    cout << endl;
  }
  catch(const SettingNotFoundException &nfex)
  {
    // Ignore.
  }

  return(EXIT_SUCCESS);
}

// eof
__ _ _ __ __| | ___ _ __ ___ | (_) | _____ | '_ \| | | / __| __/ _` | '_ \ / _` |/ _ \ '__/ __| | | | |/ / _ \ | |_) | |_| \__ \ || (_| | | | | (_| | __/ | \__ \_ | | | < __/ |_.__/ \__, |___/\__\__,_|_| |_|\__,_|\___|_| |___( ) |_|_|_|\_\___| |___/ |/ _ _ _ _ _ _ _ | |_| |__ ___ ___| |__ ___ _ __ ___ | (_) __ _| |__ | |_ ___ | __| '_ \ / _ \ / __| '_ \ / _ \| '__/ _ \ | | |/ _` | '_ \| __/ __| | |_| | | | __/ \__ \ | | | (_) | | | __/ | | | (_| | | | | |_\__ \ \__|_| |_|\___| |___/_| |_|\___/|_| \___| |_|_|\__, |_| |_|\__|___/ |___/ __ _ _ _ _ ___ / _| | |_ _____ ___ __ ___ | |_| |__ __ _| |_ __ _ / _ \| |_ | __/ _ \ \ /\ / / '_ \/ __| | __| '_ \ / _` | __| / _` | | (_) | _| | || (_) \ V V /| | | \__ \ | |_| | | | (_| | |_ | (_| | \___/|_| \__\___/ \_/\_/ |_| |_|___/ \__|_| |_|\__,_|\__| \__,_| _ _ ___ __ _(_) | ___ _ __ / __|/ _` | | |/ _ \| '__| \__ \ (_| | | | (_) | | |___/\__,_|_|_|\___/|_| _ _ _ _ _ __ __ _ ___ ___ ___ ___ __ _| |_ ___| |_ __ _| |_ ___ __| | | '_ \ / _` / __/ __|/ _ \/ __| / _` | __| / __| __/ _` | __/ _ \/ _` | | |_) | (_| \__ \__ \ __/\__ \ | (_| | |_ \__ \ || (_| | || __/ (_| | | .__/ \__,_|___/___/\___||___/ \__,_|\__| |___/\__\__,_|\__\___|\__,_| |_| _ _ _ _ | |_(_)_ __ ___ ___ ___ | |__ _ _| |_ _ __ _____ _____ _ __ | __| | '_ ` _ \ / _ \/ __| | '_ \| | | | __| | '_ \ / _ \ \ / / _ \ '__| | |_| | | | | | | __/\__ \ | |_) | |_| | |_ | | | | __/\ V / __/ | \__|_|_| |_| |_|\___||___/ |_.__/ \__,_|\__| |_| |_|\___| \_/ \___|_| _ _ _ _ _ ___ __ _| | |___ __ _| |_ ___ _ __ | |_| |__ ___ / __/ _` | | / __| / _` | __| / _ \| '_ \ | __| '_ \ / _ \ | (_| (_| | | \__ \ | (_| | |_ | (_) | | | | | |_| | | | __/ \___\__,_|_|_|___/ \__,_|\__| \___/|_| |_| \__|_| |_|\___| _ _ __ ___ __ _ _ _| | __ _ _ __ _ __ _ _ _ __ | '__/ _ \/ _` | | | | |/ _` | '__| | '__| | | | '_ \ | | | __/ (_| | |_| | | (_| | | | | | |_| | | | |_ |_| \___|\__, |\__,_|_|\__,_|_| |_| \__,_|_| |_(_) |___/ ____ _ / ___|___ _ ____ _____ _ __ ___ ___| |_ _ | | / _ \| '_ \ \ / / _ \ '__/ __|/ _ \ | | | | | |__| (_) | | | \ V / __/ | \__ \ __/ | |_| |_ \____\___/|_| |_|\_/ \___|_| |___/\___|_|\__, ( ) |___/|/ _ _ _ _ _ _ __ _| | | ___ ___ _ __ ___(_) __| | ___ _ __ __ _| |_(_) ___ _ __ ___ / _` | | | / __/ _ \| '_ \/ __| |/ _` |/ _ \ '__/ _` | __| |/ _ \| '_ \/ __| | (_| | | | | (_| (_) | | | \__ \ | (_| | __/ | | (_| | |_| | (_) | | | \__ \ \__,_|_|_| \___\___/|_| |_|___/_|\__,_|\___|_| \__,_|\__|_|\___/|_| |_|___/ __ _ _ ___ / _| ___ _____ __ __ _ ___(_) __| | ___ _ _ ___ _ _ / _ \| |_ / __|/ _ \ \/ / / _` / __| |/ _` |/ _ \ | | | |/ _ \| | | | | (_) | _| \__ \ __/> < | (_| \__ \ | (_| | __/_ | |_| | (_) | |_| | \___/|_| |___/\___/_/\_\ \__,_|___/_|\__,_|\___( ) \__, |\___/ \__,_| |/ |___/ _ ___ __ _ _ __ _ __ ___ ___ ___| |_ ___ ___ _ __ ___ ___ / __/ _` | '_ \ | '_ ` _ \ / _ \/ _ \ __| / __|/ _ \| '_ ` _ \ / _ \ | (_| (_| | | | | | | | | | | __/ __/ |_ \__ \ (_) | | | | | | __/ \___\__,_|_| |_| |_| |_| |_|\___|\___|\__| |___/\___/|_| |_| |_|\___| _ _ _ ___ | |_| |__ ___ _ __ _ __ ___ ___ _ __ | | ___ / _ \| __| '_ \ / _ \ '__| | '_ \ / _ \/ _ \| '_ \| |/ _ \ | (_) | |_| | | | __/ | | |_) | __/ (_) | |_) | | __/ \___/ \__|_| |_|\___|_| | .__/ \___|\___/| .__/|_|\___| |_| |_| _ _ ___ _ __ ___ ___ ___ _ __ | |___ _(_) ___ ___ / _ \| '_ \ / __/ _ \ / _ \| '__| | __\ \ /\ / / |/ __/ _ \ | (_) | | | | (_| __/ | (_) | | | |_ \ V V /| | (_| __/ \___/|_| |_|\___\___| \___/|_| \__| \_/\_/ |_|\___\___| _ _ _ _ __ _ _ __ __| | | |_| |__ ___ _ _ _ __ ___ _ __ ___ __ _(_)_ __ / _` | '_ \ / _` | | __| '_ \ / _ \ | | | | '__/ _ \ '_ ` _ \ / _` | | '_ \ | (_| | | | | (_| | | |_| | | | __/ |_| | | | | __/ | | | | | (_| | | | | | \__,_|_| |_|\__,_| \__|_| |_|\___|\__, | |_| \___|_| |_| |_|\__,_|_|_| |_| |___/ _ _ __ ___ _ __ _ __ ___ __ _ _ __ ___ _ __ | |_ | '_ \ / _ \ '__| '_ ` _ \ / _` | '_ \ / _ \ '_ \| __| | |_) | __/ | | | | | | | (_| | | | | __/ | | | |_ | .__/ \___|_| |_| |_| |_|\__,_|_| |_|\___|_| |_|\__| |_| _ __ _ (_)_ __ / _| |_ _ ___ _ __ ___ ___ ___ ___ _ __ | | '_ \| |_| | | | |/ _ \ '_ \ / __/ _ \/ __| / _ \| '_ \ | | | | | _| | |_| | __/ | | | (_| __/\__ \ | (_) | | | | |_|_| |_|_| |_|\__,_|\___|_| |_|\___\___||___/ \___/|_| |_| _ _ __ _ _ ___ _ _ _ __ | (_)/ _| ___ | | | |/ _ \| | | | '__| | | | |_ / _ \ | |_| | (_) | |_| | | | | | _| __/_ \__, |\___/ \__,_|_| |_|_|_| \___(_) |___/ _____ _ | ____|_ _____ _ __ _ _ ___ _ __ ___ (_)___ | _| \ \ / / _ \ '__| | | |/ _ \| '_ \ / _ \ | / __| | |___ \ V / __/ | | |_| | (_) | | | | __/ | \__ \ |_____| \_/ \___|_| \__, |\___/|_| |_|\___| |_|___/ |___/ __ _ _ _ __ ___ ____ _ _ __ ___ ___ / _| | |_| |__ (_)___ / _` \ \ /\ / / _` | '__/ _ \ / _ \| |_ | __| '_ \| / __| | (_| |\ V V / (_| | | | __/ | (_) | _| | |_| | | | \__ \ \__,_| \_/\_/ \__,_|_| \___| \___/|_| \__|_| |_|_|___/ _ _ __| (_)___ ___ _ __ ___ _ __ __ _ _ __ ___ _ _ / _` | / __|/ __| '__/ _ \ '_ \ / _` | '_ \ / __| | | | | (_| | \__ \ (__| | | __/ |_) | (_| | | | | (__| |_| | \__,_|_|___/\___|_| \___| .__/ \__,_|_| |_|\___|\__, | |_| |___/ _ _ _ _ | |__ ___| |___ _____ ___ _ __ | |_| |__ ___ | '_ \ / _ \ __\ \ /\ / / _ \/ _ \ '_ \ | __| '_ \ / _ \ | |_) | __/ |_ \ V V / __/ __/ | | | | |_| | | | __/ |_.__/ \___|\__| \_/\_/ \___|\___|_| |_| \__|_| |_|\___| _ _ __ _ ___ __ _ _ _ __ _(_)_ __ | |_ __ _ _ __ ___ ___ / _` |/ __/ _` | | | |/ _` | | '_ \| __/ _` | '_ \ / __/ _ \ | (_| | (_| (_| | |_| | (_| | | | | | || (_| | | | | (_| __/ \__,_|\___\__, |\__,_|\__,_|_|_| |_|\__\__,_|_| |_|\___\___| |_| ___ ___ ___ _ __ / __|/ _ \/ _ \ '_ \ \__ \ __/ __/ | | | |___/\___|\___|_| |_| __ _ _ _ __ _ ___ / _| __ _ _ __ ___ (_) (_) __ _ _ __ / _` / __| | |_ / _` | '_ ` _ \| | | |/ _` | '__| | (_| \__ \ | _| (_| | | | | | | | | | (_| | | \__,_|___/ |_| \__,_|_| |_| |_|_|_|_|\__,_|_| _ _ __ ____ _| | |_ __ __ _ _ __ ___ _ __ ___ _ __ \ \ /\ / / _` | | | '_ \ / _` | '_ \ / _ \ '__| / _ \| '__| \ V V / (_| | | | |_) | (_| | |_) | __/ | | (_) | | \_/\_/ \__,_|_|_| .__/ \__,_| .__/ \___|_| \___/|_| |_| |_| _ _ _ __ _ _ (_)_ __ ___| |_ __ _ _ __ | |_ / _|_ __(_) ___ _ __ __| | | | '_ \/ __| __/ _` | '_ \| __| | |_| '__| |/ _ \ '_ \ / _` | | | | | \__ \ || (_| | | | | |_ | _| | | | __/ | | | (_| |_ |_|_| |_|___/\__\__,_|_| |_|\__| |_| |_| |_|\___|_| |_|\__,_(_) _____ _ _ _ _ |_ _| |__ ___ ___| |__ ___ _ __ ___ (_) ___ __ _| | | | | '_ \ / _ \ / __| '_ \ / _ \ '_ ` _ \| |/ __/ _` | | | | | | | | __/ | (__| | | | __/ | | | | | | (_| (_| | | |_| |_| |_|\___| \___|_| |_|\___|_| |_| |_|_|\___\__,_|_| _ _ _ _ _ _ _ _ __ _ ___| |_(_) ___ _ __ (_) |_ ___ _ __ | |_ __ _(_) |___ (_)___ / _` |/ __| __| |/ _ \| '_ \ | | __| / _ \ '_ \| __/ _` | | / __| | / __| | (_| | (__| |_| | (_) | | | | | | |_ | __/ | | | || (_| | | \__ \ | \__ \ \__,_|\___|\__|_|\___/|_| |_| |_|\__| \___|_| |_|\__\__,_|_|_|___/ |_|___/ _ _ _ | | ___ ___ ___ __ _____ _ __| |_| |__ | |/ _ \/ __/ __| \ \ /\ / / _ \| '__| __| '_ \ | | __/\__ \__ \ \ V V / (_) | | | |_| | | | |_|\___||___/___/ \_/\_/ \___/|_| \__|_| |_| _ _ _ _ __ _ _ __ __ _| |_ _ ___(_)_ __ __ _ | |_| |__ __ _ _ __ / _` | '_ \ / _` | | | | |_ / | '_ \ / _` | | __| '_ \ / _` | '_ \ | (_| | | | | (_| | | |_| |/ /| | | | | (_| | | |_| | | | (_| | | | | \__,_|_| |_|\__,_|_|\__, /___|_|_| |_|\__, | \__|_| |_|\__,_|_| |_| |___/ |___/ _ _ _ _ ___ _ __ (_) ___ _ _(_)_ __ __ _ / \ | |_ __ _ _ __ _ _ / _ \ '_ \ | |/ _ \| | | | | '_ \ / _` | / _ \| __| / _` | '_ \| | | | | __/ | | || | (_) | |_| | | | | | (_| |_ / ___ \ |_ | (_| | | | | |_| | \___|_| |_|/ |\___/ \__, |_|_| |_|\__, (_) /_/ \_\__| \__,_|_| |_|\__, | |__/ |___/ |___/ |___/ _ _ _ _ _ __ __ _| |_ ___ | |_| |__ ___ ___ ___ ___(_)_ __ | '__/ _` | __/ _ \ | __| '_ \ / _ \/ __|/ _ \ / __| \ \/ / | | | (_| | || __/_ | |_| | | | __/\__ \ __/ \__ \ |> < |_| \__,_|\__\___( ) \__|_| |_|\___||___/\___| |___/_/_/\_\ |/ _ _ _ _ __ (_) ___ ___ ___ ___ __ _ _ __ ___ __ _| |__ ___ _ _| |_ | '_ \| |/ _ \/ __/ _ \/ __| / _` | '__/ _ \ / _` | '_ \ / _ \| | | | __| | |_) | | __/ (_| __/\__ \ | (_| | | | __/ | (_| | |_) | (_) | |_| | |_ | .__/|_|\___|\___\___||___/ \__,_|_| \___| \__,_|_.__/ \___/ \__,_|\__| |_| _ _ _ _ _ __ ___ ___ _ __ __ _(_) |_| |__ __ _| |__ ___ _ __ ___ | '_ ` _ \ / _ \ '_ \ \ \ /\ / / | __| '_ \ \ \ /\ / / '_ \ / _ \| '_ ` _ \ | | | | | | __/ | | | \ V V /| | |_| | | | \ V V /| | | | (_) | | | | | | |_| |_| |_|\___|_| |_| \_/\_/ |_|\__|_| |_| \_/\_/ |_| |_|\___/|_| |_| |_| ___ __ _ _ |_ _| / _| ___| | |_ __ _ _ __ | | | |_ / _ \ | __| / _` | '_ \ | | | _| __/ | |_ | (_| | | | | |___| |_| \___|_|\__| \__,_|_| |_| _ _ _ _ (_)_ __ ___ _ __ ___ ___ __| (_) __ _| |_ ___ | | '_ ` _ \| '_ ` _ \ / _ \/ _` | |/ _` | __/ _ \ | | | | | | | | | | | | __/ (_| | | (_| | || __/ |_|_| |_| |_|_| |_| |_|\___|\__,_|_|\__,_|\__\___| _ _ ___ _ _ _ __ ___ _ __ __ _| |_ | |_ ___ _ _ ___ ___ / __| | | | '_ ` _ \| '_ \ / _` | __| _____ | __/ _ \ | | | / __|/ _ \ \__ \ |_| | | | | | | |_) | (_| | |_ |_____| | || (_) | | |_| \__ \ __/ |___/\__, |_| |_| |_| .__/ \__,_|\__| \__\___/ \__,_|___/\___| |___/ |_| _ _ __ __ __ __ _ ___ ___ (_)_ __ (_)_ __ __ _ ___ / _| | \/ | __ ___ __ / _` | / __/ _ \| | '_ \| | '_ \ / _` | / _ \| |_ | |\/| |/ _` \ \/ / | (_| | | (_| (_) | | | | | | | | | (_| | | (_) | _| | | | | (_| |> < \__,_| \___\___/|_|_| |_|_|_| |_|\__, | \___/|_| |_| |_|\__,_/_/\_\ |___/ ____ _ _ _ | __ ) ___ ___ _ __| |__ ___ | |__ _ __ ___ ( )___ | _ \ / _ \/ _ \ '__| '_ \ / _ \| '_ \| '_ ` _ \|// __| | |_) | __/ __/ | | |_) | (_) | | | | | | | | | \__ \ |____/ \___|\___|_| |_.__/ \___/|_| |_|_| |_| |_| |___/ _ __ ___ ___ _ __ ___ | '_ ` _ \ / _ \| '__/ _ \ | | | | | | (_) | | | __/ |_| |_| |_|\___/|_| \___| _ _ __ _ _ ___ __ _| |_(_)___ / _| __ _ ___| |_ ___ _ __ _ _ | |_ ___ / __|/ _` | __| / __| |_ / _` |/ __| __/ _ \| '__| | | | | __/ _ \ \__ \ (_| | |_| \__ \ _| (_| | (__| || (_) | | | |_| | | || (_) | |___/\__,_|\__|_|___/_| \__,_|\___|\__\___/|_| \__, | \__\___/ |___/ _ _ _ _ _ __ ___ ___ | |_| |__ __ _ _ __ | |_| |__ ___ | '_ ` _ \ / _ \ | __| '_ \ / _` | '_ \ | __| '_ \ / _ \ | | | | | | __/ | |_| | | | (_| | | | | | |_| | | | __/ |_| |_| |_|\___| \__|_| |_|\__,_|_| |_| \__|_| |_|\___| ___ _ __ __ _ __ _ _ _ ___ __ _____ __ _ _ _ ___ / _ \| '_ \ / _` |/ _` | | | |/ _ \ \ \ / / _ \ / _` | | | |/ _ \ | (_) | |_) | (_| | (_| | |_| | __/ \ V / (_) | (_| | |_| | __/ \___/| .__/ \__,_|\__, |\__,_|\___| \_/ \___/ \__, |\__,_|\___| |_| |_| |___/ _ _ _ _ _ _ _ __ _____ _ __ __| | ( | )___ _ __ ___ _ __ __ _| |_| |__ _ _( | ) \ \ /\ / / _ \| '__/ _` | V V/ _ \ '_ ` _ \| '_ \ / _` | __| '_ \| | | |V V \ V V / (_) | | | (_| | | __/ | | | | | |_) | (_| | |_| | | | |_| | _ \_/\_/ \___/|_| \__,_| \___|_| |_| |_| .__/ \__,_|\__|_| |_|\__, | (_) |_| |___/ _ _ _ _ _ / \ | (_)___| |_ __ _(_)_ __ _____ _____ / _ \ | | / __| __/ _` | | '__| |_____|_____| / ___ \| | \__ \ || (_| | | | /_/ \_\_|_|___/\__\__,_|_|_| ____ _ _ _ ____ _ __ __ _ _ / ___|___ ___ | | _____ ( | ) ___|(_)_ __ | \/ | ___ _ __( | ) | | / _ \ / _ \| |/ / _ \ V V\___ \| \ \/ / | |\/| |/ _ \ '_ \V V | |__| (_) | (_) | < __/_ ___) | |> < | | | | __/ | | | \____\___/ \___/|_|\_\___( ) |____/|_/_/\_\ |_| |_|\___|_| |_| |/