aboutsummaryrefslogtreecommitdiff
path: root/contrib/chained/examples/example1.cpp
blob: 593bbd6e90084382109dedffce141b2d9e1ec457 (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
/* ----------------------------------------------------------------------------
   libconfig - A library for processing structured configuration files
   libconfig chained - Extension for reading the configuration and defining 
                       the configuration specification at once.
   Copyright (C) 2016 Richard Schubert

   This file is part of libconfig contributions.

   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_chained.h"

using namespace std;
using namespace libconfig;

// This example reads basic information from config file 
// and reacts on missing mandatory values.

void example1(Config& cfg)
{
	ChainedSetting cs(cfg.getRoot());

	string name = cs["name"].defaultValue("<name>").isMandatory();
	string abstract = cs["abstract"].defaultValue("<unknown>");
	double longitude = cs["longitude"].min(-180.0).max(180.0).isMandatory();
	double latitude = cs["latitude"].min(-90.0).max(90.0).isMandatory();

	if (cs.isAnyMandatorySettingMissing())
	{
		cerr << "Cannot proceed until all mandatory settings are set." << endl;
		return;
	}

	// from here on all read config values are valid
}