Brian Silverman | 836e90c | 2018-08-04 16:19:46 -0700 | [diff] [blame^] | 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 | //[debug_settings_includes |
| 12 | #include <boost/property_tree/ptree.hpp> |
| 13 | #include <boost/property_tree/xml_parser.hpp> |
| 14 | #include <boost/foreach.hpp> |
| 15 | #include <string> |
| 16 | #include <set> |
| 17 | #include <exception> |
| 18 | #include <iostream> |
| 19 | namespace pt = boost::property_tree; |
| 20 | //] |
| 21 | //[debug_settings_data |
| 22 | struct debug_settings |
| 23 | { |
| 24 | std::string m_file; // log filename |
| 25 | int m_level; // debug level |
| 26 | std::set<std::string> m_modules; // modules where logging is enabled |
| 27 | void load(const std::string &filename); |
| 28 | void save(const std::string &filename); |
| 29 | }; |
| 30 | //] |
| 31 | //[debug_settings_load |
| 32 | void debug_settings::load(const std::string &filename) |
| 33 | { |
| 34 | // Create empty property tree object |
| 35 | pt::ptree tree; |
| 36 | |
| 37 | // Parse the XML into the property tree. |
| 38 | pt::read_xml(filename, tree); |
| 39 | |
| 40 | // Use the throwing version of get to find the debug filename. |
| 41 | // If the path cannot be resolved, an exception is thrown. |
| 42 | m_file = tree.get<std::string>("debug.filename"); |
| 43 | |
| 44 | // Use the default-value version of get to find the debug level. |
| 45 | // Note that the default value is used to deduce the target type. |
| 46 | m_level = tree.get("debug.level", 0); |
| 47 | |
| 48 | // Use get_child to find the node containing the modules, and iterate over |
| 49 | // its children. If the path cannot be resolved, get_child throws. |
| 50 | // A C++11 for-range loop would also work. |
| 51 | BOOST_FOREACH(pt::ptree::value_type &v, tree.get_child("debug.modules")) { |
| 52 | // The data function is used to access the data stored in a node. |
| 53 | m_modules.insert(v.second.data()); |
| 54 | } |
| 55 | |
| 56 | } |
| 57 | //] |
| 58 | //[debug_settings_save |
| 59 | void debug_settings::save(const std::string &filename) |
| 60 | { |
| 61 | // Create an empty property tree object. |
| 62 | pt::ptree tree; |
| 63 | |
| 64 | // Put the simple values into the tree. The integer is automatically |
| 65 | // converted to a string. Note that the "debug" node is automatically |
| 66 | // created if it doesn't exist. |
| 67 | tree.put("debug.filename", m_file); |
| 68 | tree.put("debug.level", m_level); |
| 69 | |
| 70 | // Add all the modules. Unlike put, which overwrites existing nodes, add |
| 71 | // adds a new node at the lowest level, so the "modules" node will have |
| 72 | // multiple "module" children. |
| 73 | BOOST_FOREACH(const std::string &name, m_modules) |
| 74 | tree.add("debug.modules.module", name); |
| 75 | |
| 76 | // Write property tree to XML file |
| 77 | pt::write_xml(filename, tree); |
| 78 | } |
| 79 | //] |
| 80 | |
| 81 | int main() |
| 82 | { |
| 83 | try |
| 84 | { |
| 85 | debug_settings ds; |
| 86 | ds.load("debug_settings.xml"); |
| 87 | ds.save("debug_settings_out.xml"); |
| 88 | std::cout << "Success\n"; |
| 89 | } |
| 90 | catch (std::exception &e) |
| 91 | { |
| 92 | std::cout << "Error: " << e.what() << "\n"; |
| 93 | } |
| 94 | return 0; |
| 95 | } |