Chris@16
|
1 // ----------------------------------------------------------------------------
|
Chris@16
|
2 // Copyright (C) 2002-2006 Marcin Kalicinski
|
Chris@16
|
3 //
|
Chris@16
|
4 // Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
5 // (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
6 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7 //
|
Chris@16
|
8 // For more information, see www.boost.org
|
Chris@16
|
9 // ----------------------------------------------------------------------------
|
Chris@16
|
10 #ifndef BOOST_PROPERTY_TREE_INFO_PARSER_HPP_INCLUDED
|
Chris@16
|
11 #define BOOST_PROPERTY_TREE_INFO_PARSER_HPP_INCLUDED
|
Chris@16
|
12
|
Chris@16
|
13 #include <boost/property_tree/ptree.hpp>
|
Chris@16
|
14 #include <boost/property_tree/detail/info_parser_error.hpp>
|
Chris@16
|
15 #include <boost/property_tree/detail/info_parser_writer_settings.hpp>
|
Chris@16
|
16 #include <boost/property_tree/detail/info_parser_read.hpp>
|
Chris@16
|
17 #include <boost/property_tree/detail/info_parser_write.hpp>
|
Chris@16
|
18 #include <istream>
|
Chris@16
|
19
|
Chris@16
|
20 namespace boost { namespace property_tree { namespace info_parser
|
Chris@16
|
21 {
|
Chris@16
|
22
|
Chris@16
|
23 /**
|
Chris@16
|
24 * Read INFO from a the given stream and translate it to a property tree.
|
Chris@16
|
25 * @note Replaces the existing contents. Strong exception guarantee.
|
Chris@16
|
26 * @throw info_parser_error If the stream cannot be read, doesn't contain
|
Chris@16
|
27 * valid INFO, or a conversion fails.
|
Chris@16
|
28 */
|
Chris@16
|
29 template<class Ptree, class Ch>
|
Chris@16
|
30 void read_info(std::basic_istream<Ch> &stream, Ptree &pt)
|
Chris@16
|
31 {
|
Chris@16
|
32 Ptree local;
|
Chris@16
|
33 read_info_internal(stream, local, std::string(), 0);
|
Chris@16
|
34 pt.swap(local);
|
Chris@16
|
35 }
|
Chris@16
|
36
|
Chris@16
|
37 /**
|
Chris@16
|
38 * Read INFO from a the given stream and translate it to a property tree.
|
Chris@16
|
39 * @note Replaces the existing contents. Strong exception guarantee.
|
Chris@16
|
40 * @param default_ptree If parsing fails, pt is set to a copy of this tree.
|
Chris@16
|
41 */
|
Chris@16
|
42 template<class Ptree, class Ch>
|
Chris@16
|
43 void read_info(std::basic_istream<Ch> &stream, Ptree &pt,
|
Chris@16
|
44 const Ptree &default_ptree)
|
Chris@16
|
45 {
|
Chris@16
|
46 try {
|
Chris@16
|
47 read_info(stream, pt);
|
Chris@16
|
48 } catch(file_parser_error &) {
|
Chris@16
|
49 pt = default_ptree;
|
Chris@16
|
50 }
|
Chris@16
|
51 }
|
Chris@16
|
52
|
Chris@16
|
53 /**
|
Chris@16
|
54 * Read INFO from a the given file and translate it to a property tree. The
|
Chris@16
|
55 * tree's key type must be a string type, i.e. it must have a nested
|
Chris@16
|
56 * value_type typedef that is a valid parameter for basic_ifstream.
|
Chris@16
|
57 * @note Replaces the existing contents. Strong exception guarantee.
|
Chris@16
|
58 * @throw info_parser_error If the file cannot be read, doesn't contain
|
Chris@16
|
59 * valid INFO, or a conversion fails.
|
Chris@16
|
60 */
|
Chris@16
|
61 template<class Ptree>
|
Chris@16
|
62 void read_info(const std::string &filename, Ptree &pt,
|
Chris@16
|
63 const std::locale &loc = std::locale())
|
Chris@16
|
64 {
|
Chris@16
|
65 std::basic_ifstream<typename Ptree::key_type::value_type>
|
Chris@16
|
66 stream(filename.c_str());
|
Chris@16
|
67 if (!stream) {
|
Chris@16
|
68 BOOST_PROPERTY_TREE_THROW(info_parser_error(
|
Chris@16
|
69 "cannot open file for reading", filename, 0));
|
Chris@16
|
70 }
|
Chris@16
|
71 stream.imbue(loc);
|
Chris@16
|
72 Ptree local;
|
Chris@16
|
73 read_info_internal(stream, local, filename, 0);
|
Chris@16
|
74 pt.swap(local);
|
Chris@16
|
75 }
|
Chris@16
|
76
|
Chris@16
|
77 /**
|
Chris@16
|
78 * Read INFO from a the given file and translate it to a property tree. The
|
Chris@16
|
79 * tree's key type must be a string type, i.e. it must have a nested
|
Chris@16
|
80 * value_type typedef that is a valid parameter for basic_ifstream.
|
Chris@16
|
81 * @note Replaces the existing contents. Strong exception guarantee.
|
Chris@16
|
82 * @param default_ptree If parsing fails, pt is set to a copy of this tree.
|
Chris@16
|
83 */
|
Chris@16
|
84 template<class Ptree>
|
Chris@16
|
85 void read_info(const std::string &filename,
|
Chris@16
|
86 Ptree &pt,
|
Chris@16
|
87 const Ptree &default_ptree,
|
Chris@16
|
88 const std::locale &loc = std::locale())
|
Chris@16
|
89 {
|
Chris@16
|
90 try {
|
Chris@16
|
91 read_info(filename, pt, loc);
|
Chris@16
|
92 } catch(file_parser_error &) {
|
Chris@16
|
93 pt = default_ptree;
|
Chris@16
|
94 }
|
Chris@16
|
95 }
|
Chris@16
|
96
|
Chris@16
|
97 /**
|
Chris@16
|
98 * Writes a tree to the stream in INFO format.
|
Chris@16
|
99 * @throw info_parser_error If the stream cannot be written to, or a
|
Chris@16
|
100 * conversion fails.
|
Chris@16
|
101 * @param settings The settings to use when writing the INFO data.
|
Chris@16
|
102 */
|
Chris@16
|
103 template<class Ptree, class Ch>
|
Chris@16
|
104 void write_info(std::basic_ostream<Ch> &stream,
|
Chris@16
|
105 const Ptree &pt,
|
Chris@16
|
106 const info_writer_settings<Ch> &settings =
|
Chris@16
|
107 info_writer_settings<Ch>())
|
Chris@16
|
108 {
|
Chris@16
|
109 write_info_internal(stream, pt, std::string(), settings);
|
Chris@16
|
110 }
|
Chris@16
|
111
|
Chris@16
|
112 /**
|
Chris@16
|
113 * Writes a tree to the file in INFO format. The tree's key type must be a
|
Chris@16
|
114 * string type, i.e. it must have a nested value_type typedef that is a
|
Chris@16
|
115 * valid parameter for basic_ofstream.
|
Chris@16
|
116 * @throw info_parser_error If the file cannot be written to, or a
|
Chris@16
|
117 * conversion fails.
|
Chris@16
|
118 * @param settings The settings to use when writing the INFO data.
|
Chris@16
|
119 */
|
Chris@16
|
120 template<class Ptree>
|
Chris@16
|
121 void write_info(const std::string &filename,
|
Chris@16
|
122 const Ptree &pt,
|
Chris@16
|
123 const std::locale &loc = std::locale(),
|
Chris@16
|
124 const info_writer_settings<
|
Chris@16
|
125 typename Ptree::key_type::value_type
|
Chris@16
|
126 > &settings =
|
Chris@16
|
127 info_writer_make_settings<
|
Chris@16
|
128 typename Ptree::key_type::value_type>())
|
Chris@16
|
129 {
|
Chris@16
|
130 std::basic_ofstream<typename Ptree::key_type::value_type>
|
Chris@16
|
131 stream(filename.c_str());
|
Chris@16
|
132 if (!stream) {
|
Chris@16
|
133 BOOST_PROPERTY_TREE_THROW(info_parser_error(
|
Chris@16
|
134 "cannot open file for writing", filename, 0));
|
Chris@16
|
135 }
|
Chris@16
|
136 stream.imbue(loc);
|
Chris@16
|
137 write_info_internal(stream, pt, filename, settings);
|
Chris@16
|
138 }
|
Chris@16
|
139
|
Chris@16
|
140 } } }
|
Chris@16
|
141
|
Chris@16
|
142 namespace boost { namespace property_tree
|
Chris@16
|
143 {
|
Chris@16
|
144 using info_parser::info_parser_error;
|
Chris@16
|
145 using info_parser::read_info;
|
Chris@16
|
146 using info_parser::write_info;
|
Chris@16
|
147 using info_parser::info_writer_settings;
|
Chris@16
|
148 using info_parser::info_writer_make_settings;
|
Chris@16
|
149 } }
|
Chris@16
|
150
|
Chris@16
|
151 #endif
|