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_DETAIL_INFO_PARSER_WRITE_HPP_INCLUDED
|
Chris@16
|
11 #define BOOST_PROPERTY_TREE_DETAIL_INFO_PARSER_WRITE_HPP_INCLUDED
|
Chris@16
|
12
|
Chris@16
|
13 #include "boost/property_tree/ptree.hpp"
|
Chris@16
|
14 #include "boost/property_tree/detail/info_parser_utils.hpp"
|
Chris@16
|
15 #include <string>
|
Chris@16
|
16
|
Chris@16
|
17 namespace boost { namespace property_tree { namespace info_parser
|
Chris@16
|
18 {
|
Chris@16
|
19 template<class Ch>
|
Chris@16
|
20 void write_info_indent(std::basic_ostream<Ch> &stream,
|
Chris@16
|
21 int indent,
|
Chris@16
|
22 const info_writer_settings<Ch> &settings
|
Chris@16
|
23 )
|
Chris@16
|
24 {
|
Chris@16
|
25 stream << std::basic_string<Ch>(indent * settings.indent_count, settings.indent_char);
|
Chris@16
|
26 }
|
Chris@16
|
27
|
Chris@16
|
28 // Create necessary escape sequences from illegal characters
|
Chris@16
|
29 template<class Ch>
|
Chris@16
|
30 std::basic_string<Ch> create_escapes(const std::basic_string<Ch> &s)
|
Chris@16
|
31 {
|
Chris@16
|
32 std::basic_string<Ch> result;
|
Chris@16
|
33 typename std::basic_string<Ch>::const_iterator b = s.begin();
|
Chris@16
|
34 typename std::basic_string<Ch>::const_iterator e = s.end();
|
Chris@16
|
35 while (b != e)
|
Chris@16
|
36 {
|
Chris@16
|
37 if (*b == Ch('\0')) result += Ch('\\'), result += Ch('0');
|
Chris@16
|
38 else if (*b == Ch('\a')) result += Ch('\\'), result += Ch('a');
|
Chris@16
|
39 else if (*b == Ch('\b')) result += Ch('\\'), result += Ch('b');
|
Chris@16
|
40 else if (*b == Ch('\f')) result += Ch('\\'), result += Ch('f');
|
Chris@16
|
41 else if (*b == Ch('\n')) result += Ch('\\'), result += Ch('n');
|
Chris@16
|
42 else if (*b == Ch('\r')) result += Ch('\\'), result += Ch('r');
|
Chris@16
|
43 else if (*b == Ch('\v')) result += Ch('\\'), result += Ch('v');
|
Chris@16
|
44 else if (*b == Ch('"')) result += Ch('\\'), result += Ch('"');
|
Chris@16
|
45 else if (*b == Ch('\\')) result += Ch('\\'), result += Ch('\\');
|
Chris@16
|
46 else
|
Chris@16
|
47 result += *b;
|
Chris@16
|
48 ++b;
|
Chris@16
|
49 }
|
Chris@16
|
50 return result;
|
Chris@16
|
51 }
|
Chris@16
|
52
|
Chris@16
|
53 template<class Ch>
|
Chris@16
|
54 bool is_simple_key(const std::basic_string<Ch> &key)
|
Chris@16
|
55 {
|
Chris@16
|
56 const static std::basic_string<Ch> chars = convert_chtype<Ch, char>(" \t{};\n\"");
|
Chris@16
|
57 return !key.empty() && key.find_first_of(chars) == key.npos;
|
Chris@16
|
58 }
|
Chris@16
|
59
|
Chris@16
|
60 template<class Ch>
|
Chris@16
|
61 bool is_simple_data(const std::basic_string<Ch> &data)
|
Chris@16
|
62 {
|
Chris@16
|
63 const static std::basic_string<Ch> chars = convert_chtype<Ch, char>(" \t{};\n\"");
|
Chris@16
|
64 return !data.empty() && data.find_first_of(chars) == data.npos;
|
Chris@16
|
65 }
|
Chris@16
|
66
|
Chris@16
|
67 template<class Ptree>
|
Chris@16
|
68 void write_info_helper(std::basic_ostream<typename Ptree::key_type::value_type> &stream,
|
Chris@16
|
69 const Ptree &pt,
|
Chris@16
|
70 int indent,
|
Chris@16
|
71 const info_writer_settings<typename Ptree::key_type::value_type> &settings)
|
Chris@16
|
72 {
|
Chris@16
|
73
|
Chris@16
|
74 // Character type
|
Chris@16
|
75 typedef typename Ptree::key_type::value_type Ch;
|
Chris@16
|
76
|
Chris@16
|
77 // Write data
|
Chris@16
|
78 if (indent >= 0)
|
Chris@16
|
79 {
|
Chris@16
|
80 if (!pt.data().empty())
|
Chris@16
|
81 {
|
Chris@16
|
82 std::basic_string<Ch> data = create_escapes(pt.template get_value<std::basic_string<Ch> >());
|
Chris@16
|
83 if (is_simple_data(data))
|
Chris@16
|
84 stream << Ch(' ') << data << Ch('\n');
|
Chris@16
|
85 else
|
Chris@16
|
86 stream << Ch(' ') << Ch('\"') << data << Ch('\"') << Ch('\n');
|
Chris@16
|
87 }
|
Chris@16
|
88 else if (pt.empty())
|
Chris@16
|
89 stream << Ch(' ') << Ch('\"') << Ch('\"') << Ch('\n');
|
Chris@16
|
90 else
|
Chris@16
|
91 stream << Ch('\n');
|
Chris@16
|
92 }
|
Chris@16
|
93
|
Chris@16
|
94 // Write keys
|
Chris@16
|
95 if (!pt.empty())
|
Chris@16
|
96 {
|
Chris@16
|
97
|
Chris@16
|
98 // Open brace
|
Chris@16
|
99 if (indent >= 0)
|
Chris@16
|
100 {
|
Chris@16
|
101 write_info_indent( stream, indent, settings);
|
Chris@16
|
102 stream << Ch('{') << Ch('\n');
|
Chris@16
|
103 }
|
Chris@16
|
104
|
Chris@16
|
105 // Write keys
|
Chris@16
|
106 typename Ptree::const_iterator it = pt.begin();
|
Chris@16
|
107 for (; it != pt.end(); ++it)
|
Chris@16
|
108 {
|
Chris@16
|
109
|
Chris@16
|
110 // Output key
|
Chris@16
|
111 std::basic_string<Ch> key = create_escapes(it->first);
|
Chris@16
|
112 write_info_indent( stream, indent+1, settings);
|
Chris@16
|
113 if (is_simple_key(key))
|
Chris@16
|
114 stream << key;
|
Chris@16
|
115 else
|
Chris@16
|
116 stream << Ch('\"') << key << Ch('\"');
|
Chris@16
|
117
|
Chris@16
|
118 // Output data and children
|
Chris@16
|
119 write_info_helper(stream, it->second, indent + 1, settings);
|
Chris@16
|
120
|
Chris@16
|
121 }
|
Chris@16
|
122
|
Chris@16
|
123 // Close brace
|
Chris@16
|
124 if (indent >= 0)
|
Chris@16
|
125 {
|
Chris@16
|
126 write_info_indent( stream, indent, settings);
|
Chris@16
|
127 stream << Ch('}') << Ch('\n');
|
Chris@16
|
128 }
|
Chris@16
|
129
|
Chris@16
|
130 }
|
Chris@16
|
131 }
|
Chris@16
|
132
|
Chris@16
|
133 // Write ptree to info stream
|
Chris@16
|
134 template<class Ptree>
|
Chris@16
|
135 void write_info_internal(std::basic_ostream<typename Ptree::key_type::value_type> &stream,
|
Chris@16
|
136 const Ptree &pt,
|
Chris@16
|
137 const std::string &filename,
|
Chris@16
|
138 const info_writer_settings<typename Ptree::key_type::value_type> &settings)
|
Chris@16
|
139 {
|
Chris@16
|
140 write_info_helper(stream, pt, -1, settings);
|
Chris@16
|
141 if (!stream.good())
|
Chris@16
|
142 BOOST_PROPERTY_TREE_THROW(info_parser_error("write error", filename, 0));
|
Chris@16
|
143 }
|
Chris@16
|
144
|
Chris@16
|
145 } } }
|
Chris@16
|
146
|
Chris@16
|
147 #endif
|