fix doc example typo
[boost.git] / boost / property_tree / info_parser.hpp
bloba25a95eb47bfc8964af1353c514b67eb229969a0
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 #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>
18 #include <istream>
20 namespace boost { namespace property_tree { namespace info_parser
23 /**
24 * Read INFO from a the given stream and translate it to a property tree.
25 * @note Clears existing contents of property tree. In case of error the property tree unmodified.
26 * @throw info_parser_error On error translating the INFO stream to a property tree.
27 * @param stream Stream from which to read in the property tree.
28 * @param[out] pt The property tree to populate.
30 template<class Ptree, class Ch>
31 void read_info(std::basic_istream<Ch> &stream,
32 Ptree &pt)
34 Ptree local;
35 read_info_internal(stream, local, std::string(), 0);
36 pt.swap(local);
39 /**
40 * Read INFO from a the given stream and translate it to a property tree.
41 * @note Clears existing contents of property tree. In case of error the property tree unmodified.
42 * @param stream Stream from which to read in the property tree.
43 * @param[out] pt The property tree to populate.
44 * @param default_ptree The property tree to which to set @c pt on error reading the INFO stream.
46 template<class Ptree, class Ch>
47 void read_info(std::basic_istream<Ch> &stream,
48 Ptree &pt,
49 const Ptree &default_ptree)
51 try
53 read_info(stream, pt);
55 catch (file_parser_error &)
57 pt = default_ptree;
61 /**
62 * Read INFO from a the given file and translate it to a property tree.
63 * @note Clears existing contents of property tree. In case of error the property tree unmodified.
64 * @throw info_parser_error On error translating the INFO stream to a property tree.
65 * @param filename Name of file from which to read in the property tree.
66 * @param[out] pt The property tree to populate.
67 * @param loc The locale to use when reading in the file contents.
69 template<class Ptree>
70 void read_info(const std::string &filename,
71 Ptree &pt,
72 const std::locale &loc = std::locale())
74 std::basic_ifstream<typename Ptree::key_type::value_type> stream(filename.c_str());
75 if (!stream)
76 BOOST_PROPERTY_TREE_THROW(info_parser_error("cannot open file for reading", filename, 0));
77 stream.imbue(loc);
78 Ptree local;
79 read_info_internal(stream, local, filename, 0);
80 pt.swap(local);
83 /**
84 * Read INFO from a the given file and translate it to a property tree.
85 * @note Clears existing contents of property tree. In case of error the property tree unmodified.
86 * @param filename Name of file from which to read in the property tree.
87 * @param[out] pt The property tree to populate.
88 * @param loc The locale to use when reading in the file contents.
89 * @param default_ptree The property tree to which to set @c pt on error reading the INFO stream.
91 template<class Ptree>
92 void read_info(const std::string &filename,
93 Ptree &pt,
94 const Ptree &default_ptree,
95 const std::locale &loc = std::locale())
97 try
99 read_info(filename, pt, loc);
101 catch (file_parser_error &)
103 pt = default_ptree;
108 * Translates the property tree to INFO and writes it the given output stream.
109 * @throw info_parser_error In case of error translating the property tree to INFO
110 * or writing to the output stream.
111 * @param stream The stream to which to write the INFO representation of the
112 * property tree.
113 * @param pt The property tree to tranlsate to INFO and output.
114 * @param settings The settings to use when writing the INFO data.
116 template<class Ptree, class Ch>
117 void write_info(std::basic_ostream<Ch> &stream,
118 const Ptree &pt,
119 const info_writer_settings<Ch> &settings=info_writer_settings<Ch>())
121 write_info_internal(stream, pt, std::string(), settings);
125 * Translates the property tree to INFO and writes it the given file.
126 * @throw info_parser_error In case of error translating the property tree to INFO
127 * or writing to the file.
128 * @param filename The name of the file to which to write the INFO representation
129 * of the property tree.
130 * @param pt The property tree to tranlsate to INFO and output.
131 * @param settings The settings to use when writing the INFO data.
132 * @param loc The locale to use when writing the file.
134 template<class Ptree>
135 void write_info(const std::string &filename,
136 const Ptree &pt,
137 const std::locale &loc = std::locale(),
138 const info_writer_settings<typename Ptree::key_type::value_type> &settings = info_writer_make_settings<typename Ptree::key_type::value_type>())
140 std::basic_ofstream<typename Ptree::key_type::value_type> stream(filename.c_str());
141 if (!stream)
142 BOOST_PROPERTY_TREE_THROW(info_parser_error("cannot open file for writing", filename, 0));
143 stream.imbue(loc);
144 write_info_internal(stream, pt, filename, settings);
147 } } }
149 namespace boost { namespace property_tree
151 using info_parser::info_parser_error;
152 using info_parser::read_info;
153 using info_parser::write_info;
154 using info_parser::info_writer_settings;
155 using info_parser::info_writer_make_settings;
158 #endif