| 1 | // ---------------------------------------------------------------------------- |
| 2 | // Copyright (C) 2002-2006 Marcin Kalicinski |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0. |
| 5 | // (See accompanying file LICENSE_1_0.txt or copy at |
| 6 | // http://www.boost.org/LICENSE_1_0.txt) |
| 7 | // |
| 8 | // For more information, see www.boost.org |
| 9 | // ---------------------------------------------------------------------------- |
| 10 | |
| 11 | #include <boost/property_tree/ptree.hpp> |
| 12 | #include <boost/property_tree/info_parser.hpp> |
| 13 | #include <iostream> |
| 14 | #include <iomanip> |
| 15 | #include <string> |
| 16 | |
| 17 | using namespace boost::property_tree; |
| 18 | |
| 19 | static ptree dflt; |
| 20 | |
| 21 | // Process settings using empty ptree trick. Note that it is considerably simpler |
| 22 | // than version which does not use the "trick" |
| 23 | void process_settings(const std::string &filename) |
| 24 | { |
| 25 | ptree pt; |
| 26 | read_info(filename, pt); |
| 27 | const ptree &settings = pt.get_child(path: "settings" , default_value&: dflt); |
| 28 | std::cout << "\n Processing " << filename << std::endl; |
| 29 | std::cout << " Setting 1 is " << settings.get(path: "setting1" , default_value: 0) << std::endl; |
| 30 | std::cout << " Setting 2 is " << settings.get(path: "setting2" , default_value: 0.0) << std::endl; |
| 31 | std::cout << " Setting 3 is " << settings.get(path: "setting3" , default_value: "default" ) << std::endl; |
| 32 | } |
| 33 | |
| 34 | // Process settings not using empty ptree trick. This one must duplicate much of the code. |
| 35 | void process_settings_without_trick(const std::string &filename) |
| 36 | { |
| 37 | ptree pt; |
| 38 | read_info(filename, pt); |
| 39 | if (boost::optional<ptree &> settings = pt.get_child_optional(path: "settings" )) |
| 40 | { |
| 41 | std::cout << "\n Processing " << filename << std::endl; |
| 42 | std::cout << " Setting 1 is " << settings.get().get(path: "setting1" , default_value: 0) << std::endl; |
| 43 | std::cout << " Setting 2 is " << settings.get().get(path: "setting2" , default_value: 0.0) << std::endl; |
| 44 | std::cout << " Setting 3 is " << settings.get().get(path: "setting3" , default_value: "default" ) << std::endl; |
| 45 | } |
| 46 | else |
| 47 | { |
| 48 | std::cout << "\n Processing " << filename << std::endl; |
| 49 | std::cout << " Setting 1 is " << 0 << std::endl; |
| 50 | std::cout << " Setting 2 is " << 0.0 << std::endl; |
| 51 | std::cout << " Setting 3 is " << "default" << std::endl; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | int main() |
| 56 | { |
| 57 | try |
| 58 | { |
| 59 | std::cout << "Processing settings with empty-ptree-trick:\n" ; |
| 60 | process_settings(filename: "settings_fully-existent.info" ); |
| 61 | process_settings(filename: "settings_partially-existent.info" ); |
| 62 | process_settings(filename: "settings_non-existent.info" ); |
| 63 | std::cout << "\nProcessing settings without empty-ptree-trick:\n" ; |
| 64 | process_settings_without_trick(filename: "settings_fully-existent.info" ); |
| 65 | process_settings_without_trick(filename: "settings_partially-existent.info" ); |
| 66 | process_settings_without_trick(filename: "settings_non-existent.info" ); |
| 67 | } |
| 68 | catch (std::exception &e) |
| 69 | { |
| 70 | std::cout << "Error: " << e.what() << "\n" ; |
| 71 | } |
| 72 | return 0; |
| 73 | } |
| 74 | |