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 prints out the expected configuration specification. 35 example3(Config & cfg)36void example3(Config& cfg) 37 { 38 ChainedSetting cs(cfg.getRoot()); 39 40 Config tmpCfgSpec; 41 tmpCfgSpec.setOptions(Config::OptionOpenBraceOnSeparateLine | Config::OptionSemicolonSeparators); 42 tmpCfgSpec.setTabWidth(3); 43 cs.captureExpectedSpecification(&tmpCfgSpec); 44 45 string name = cs["name"].defaultValue("<name>").isMandatory(); 46 string abstract = cs["abstract"].defaultValue("<unknown>"); 47 double longitude = cs["longitude"].min(-180.0).max(180.0).isMandatory(); 48 double latitude = cs["latitude"].min(-90.0).max(90.0).isMandatory(); 49 50 ChainedSetting movie0 = cs["inventory"]["movies"][0]; 51 string book0tile = cs["inventory"]["books"][0]["title"].defaultValue("bookXYZ"); 52 53 if (cs.isAnyMandatorySettingMissing()) 54 { 55 cerr << "Cannot proceed until all mandatory settings are set." << endl << endl; 56 57 auto cfgSpec = cs.getCapturedSpecification("tmpCfgSpec.cfg"); 58 cerr << "Expected Config Layout: " << endl << endl; 59 cerr << "// -- begin --" << endl; 60 cerr << cfgSpec; 61 cerr << "// --- end ---" << endl << endl; 62 return; 63 } 64 65 // from here on all read config values are valid 66 } 67