1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2002-2006 Marcin Kalicinski
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)
8 // For more information, see www.boost.org
9 // ----------------------------------------------------------------------------
10 #ifndef BOOST_PROPERTY_TREE_INFO_PARSER_HPP_INCLUDED
11 #define BOOST_PROPERTY_TREE_INFO_PARSER_HPP_INCLUDED
13 #include <boost/property_tree/ptree.hpp>
14 #include <boost/property_tree/detail/info_parser_error.hpp>
15 #include <boost/property_tree/detail/info_parser_writer_settings.hpp>
16 #include <boost/property_tree/detail/info_parser_read.hpp>
17 #include <boost/property_tree/detail/info_parser_write.hpp>
20 namespace boost { namespace property_tree { namespace info_parser
24 * Read INFO from a the given stream and translate it to a property tree.
25 * @note Replaces the existing contents. Strong exception guarantee.
26 * @throw info_parser_error If the stream cannot be read, doesn't contain
27 * valid INFO, or a conversion fails.
29 template<class Ptree, class Ch>
30 void read_info(std::basic_istream<Ch> &stream, Ptree &pt)
33 read_info_internal(stream, local, std::string(), 0);
38 * Read INFO from a the given stream and translate it to a property tree.
39 * @note Replaces the existing contents. Strong exception guarantee.
40 * @param default_ptree If parsing fails, pt is set to a copy of this tree.
42 template<class Ptree, class Ch>
43 void read_info(std::basic_istream<Ch> &stream, Ptree &pt,
44 const Ptree &default_ptree)
47 read_info(stream, pt);
48 } catch(file_parser_error &) {
54 * Read INFO from a the given file and translate it to a property tree. The
55 * tree's key type must be a string type, i.e. it must have a nested
56 * value_type typedef that is a valid parameter for basic_ifstream.
57 * @note Replaces the existing contents. Strong exception guarantee.
58 * @throw info_parser_error If the file cannot be read, doesn't contain
59 * valid INFO, or a conversion fails.
62 void read_info(const std::string &filename, Ptree &pt,
63 const std::locale &loc = std::locale())
65 std::basic_ifstream<typename Ptree::key_type::value_type>
66 stream(filename.c_str());
68 BOOST_PROPERTY_TREE_THROW(info_parser_error(
69 "cannot open file for reading", filename, 0));
73 read_info_internal(stream, local, filename, 0);
78 * Read INFO from a the given file and translate it to a property tree. The
79 * tree's key type must be a string type, i.e. it must have a nested
80 * value_type typedef that is a valid parameter for basic_ifstream.
81 * @note Replaces the existing contents. Strong exception guarantee.
82 * @param default_ptree If parsing fails, pt is set to a copy of this tree.
85 void read_info(const std::string &filename,
87 const Ptree &default_ptree,
88 const std::locale &loc = std::locale())
91 read_info(filename, pt, loc);
92 } catch(file_parser_error &) {
98 * Writes a tree to the stream in INFO format.
99 * @throw info_parser_error If the stream cannot be written to, or a
101 * @param settings The settings to use when writing the INFO data.
103 template<class Ptree, class Ch>
104 void write_info(std::basic_ostream<Ch> &stream,
106 const info_writer_settings<Ch> &settings =
107 info_writer_settings<Ch>())
109 write_info_internal(stream, pt, std::string(), settings);
113 * Writes a tree to the file in INFO format. The tree's key type must be a
114 * string type, i.e. it must have a nested value_type typedef that is a
115 * valid parameter for basic_ofstream.
116 * @throw info_parser_error If the file cannot be written to, or a
118 * @param settings The settings to use when writing the INFO data.
120 template<class Ptree>
121 void write_info(const std::string &filename,
123 const std::locale &loc = std::locale(),
124 const info_writer_settings<
125 typename Ptree::key_type::value_type
127 info_writer_make_settings<
128 typename Ptree::key_type::value_type>())
130 std::basic_ofstream<typename Ptree::key_type::value_type>
131 stream(filename.c_str());
133 BOOST_PROPERTY_TREE_THROW(info_parser_error(
134 "cannot open file for writing", filename, 0));
137 write_info_internal(stream, pt, filename, settings);
142 namespace boost { namespace property_tree
144 using info_parser::info_parser_error;
145 using info_parser::read_info;
146 using info_parser::write_info;
147 using info_parser::info_writer_settings;
148 using info_parser::info_writer_make_settings;