diff options
author | Jeongik Cha <jeongik@google.com> | 2021-07-21 08:10:55 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2021-07-21 08:10:55 +0000 |
commit | a37f71eb9874f9a159ef7f0f55b94ccb5f4e9e2f (patch) | |
tree | 40206be5d73a97c3b4838b071eae7e98445c6c4e /contrib/chained/examples/example1.cpp | |
parent | 3fe6886360b935adc1408c19866a9f2b7fc0d275 (diff) | |
parent | 2426ed1f1a365e8c39ab32ffbe31b6be494f6c1e (diff) | |
download | libconfig-a37f71eb9874f9a159ef7f0f55b94ccb5f4e9e2f.tar.gz |
Import libconfig am: 1199caa2e8 am: 604433215f am: 9effa07f5b am: 2426ed1f1a
Original change: https://android-review.googlesource.com/c/platform/external/libconfig/+/1772393
Change-Id: Ibd4ac853f0351e8b9273b185c0f3994dc66f3207
Diffstat (limited to 'contrib/chained/examples/example1.cpp')
-rw-r--r-- | contrib/chained/examples/example1.cpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/contrib/chained/examples/example1.cpp b/contrib/chained/examples/example1.cpp new file mode 100644 index 0000000..593bbd6 --- /dev/null +++ b/contrib/chained/examples/example1.cpp @@ -0,0 +1,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 +} |