xref: /aosp_15_r20/external/libconfig/contrib/chained/examples/example1.cpp (revision 2e9d491483b805f09ea864149eadd5680efcc72a)
1 /* ----------------------------------------------------------------------------
2    libconfig - A library for processing structured configuration files
3    libconfig chained - Extension for reading the configuration and defining
4                        the configuration specification at once.
5    Copyright (C) 2016 Richard Schubert
6 
7    This file is part of libconfig contributions.
8 
9    This library is free software; you can redistribute it and/or
10    modify it under the terms of the GNU Lesser General Public License
11    as published by the Free Software Foundation; either version 2.1 of
12    the License, or (at your option) any later version.
13 
14    This library is distributed in the hope that it will be useful, but
15    WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17    Lesser General Public License for more details.
18 
19    You should have received a copy of the GNU Library General Public
20    License along with this library; if not, see
21    <http://www.gnu.org/licenses/>.
22    ----------------------------------------------------------------------------
23 */
24 
25 #include <iostream>
26 #include <iomanip>
27 #include <cstdlib>
28 #include "../libconfig_chained.h"
29 
30 using namespace std;
31 using namespace libconfig;
32 
33 // This example reads basic information from config file
34 // and reacts on missing mandatory values.
35 
example1(Config & cfg)36 void example1(Config& cfg)
37 {
38 	ChainedSetting cs(cfg.getRoot());
39 
40 	string name = cs["name"].defaultValue("<name>").isMandatory();
41 	string abstract = cs["abstract"].defaultValue("<unknown>");
42 	double longitude = cs["longitude"].min(-180.0).max(180.0).isMandatory();
43 	double latitude = cs["latitude"].min(-90.0).max(90.0).isMandatory();
44 
45 	if (cs.isAnyMandatorySettingMissing())
46 	{
47 		cerr << "Cannot proceed until all mandatory settings are set." << endl;
48 		return;
49 	}
50 
51 	// from here on all read config values are valid
52 }
53