Chris@16
|
1 // ----------------------------------------------------------------------------
|
Chris@16
|
2 // Copyright (C) 2009 Sebastian Redl
|
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
|
Chris@16
|
11 #ifndef BOOST_PROPERTY_TREE_STRING_PATH_HPP_INCLUDED
|
Chris@16
|
12 #define BOOST_PROPERTY_TREE_STRING_PATH_HPP_INCLUDED
|
Chris@16
|
13
|
Chris@16
|
14 #include <boost/property_tree/ptree_fwd.hpp>
|
Chris@16
|
15 #include <boost/property_tree/id_translator.hpp>
|
Chris@16
|
16 #include <boost/property_tree/exceptions.hpp>
|
Chris@16
|
17 #include <boost/property_tree/detail/ptree_utils.hpp>
|
Chris@16
|
18
|
Chris@16
|
19 #include <boost/static_assert.hpp>
|
Chris@16
|
20 #include <boost/assert.hpp>
|
Chris@16
|
21 #include <boost/type_traits/is_same.hpp>
|
Chris@16
|
22 #include <boost/optional.hpp>
|
Chris@16
|
23 #include <boost/throw_exception.hpp>
|
Chris@16
|
24 #include <algorithm>
|
Chris@16
|
25 #include <string>
|
Chris@16
|
26 #include <iterator>
|
Chris@16
|
27
|
Chris@16
|
28 namespace boost { namespace property_tree
|
Chris@16
|
29 {
|
Chris@16
|
30 namespace detail
|
Chris@16
|
31 {
|
Chris@16
|
32 template <typename Sequence, typename Iterator>
|
Chris@16
|
33 void append_and_preserve_iter(Sequence &s, const Sequence &r,
|
Chris@16
|
34 Iterator &, std::forward_iterator_tag)
|
Chris@16
|
35 {
|
Chris@16
|
36 // Here we boldly assume that anything that is not random-access
|
Chris@16
|
37 // preserves validity. This is valid for the STL sequences.
|
Chris@16
|
38 s.insert(s.end(), r.begin(), r.end());
|
Chris@16
|
39 }
|
Chris@16
|
40 template <typename Sequence, typename Iterator>
|
Chris@16
|
41 void append_and_preserve_iter(Sequence &s, const Sequence &r,
|
Chris@16
|
42 Iterator &it,
|
Chris@16
|
43 std::random_access_iterator_tag)
|
Chris@16
|
44 {
|
Chris@16
|
45 // Convert the iterator to an index, and later back.
|
Chris@16
|
46 typename std::iterator_traits<Iterator>::difference_type idx =
|
Chris@16
|
47 it - s.begin();
|
Chris@16
|
48 s.insert(s.end(), r.begin(), r.end());
|
Chris@16
|
49 it = s.begin() + idx;
|
Chris@16
|
50 }
|
Chris@16
|
51
|
Chris@16
|
52 template <typename Sequence>
|
Chris@16
|
53 inline std::string dump_sequence(const Sequence &)
|
Chris@16
|
54 {
|
Chris@16
|
55 return "<undumpable sequence>";
|
Chris@16
|
56 }
|
Chris@16
|
57 inline std::string dump_sequence(const std::string &s)
|
Chris@16
|
58 {
|
Chris@16
|
59 return s;
|
Chris@16
|
60 }
|
Chris@16
|
61 #ifndef BOOST_NO_STD_WSTRING
|
Chris@16
|
62 inline std::string dump_sequence(const std::wstring &s)
|
Chris@16
|
63 {
|
Chris@101
|
64 return narrow<std::string>(s.c_str());
|
Chris@16
|
65 }
|
Chris@16
|
66 #endif
|
Chris@16
|
67 }
|
Chris@16
|
68
|
Chris@16
|
69 /// Default path class. A path is a sequence of values. Groups of values
|
Chris@16
|
70 /// are separated by the separator value, which defaults to '.' cast to
|
Chris@16
|
71 /// the sequence's value type. The group of values is then passed to the
|
Chris@16
|
72 /// translator to get a key.
|
Chris@16
|
73 ///
|
Chris@16
|
74 /// If instantiated with std::string and id_translator\<std::string\>,
|
Chris@16
|
75 /// it accepts paths of the form "one.two.three.four".
|
Chris@16
|
76 ///
|
Chris@16
|
77 /// @tparam String Any Sequence. If the sequence does not support random-
|
Chris@16
|
78 /// access iteration, concatenation of paths assumes that
|
Chris@16
|
79 /// insertions at the end preserve iterator validity.
|
Chris@16
|
80 /// @tparam Translator A translator with internal_type == String.
|
Chris@16
|
81 template <typename String, typename Translator>
|
Chris@16
|
82 class string_path
|
Chris@16
|
83 {
|
Chris@16
|
84 BOOST_STATIC_ASSERT((is_same<String,
|
Chris@16
|
85 typename Translator::internal_type>::value));
|
Chris@16
|
86 public:
|
Chris@16
|
87 typedef typename Translator::external_type key_type;
|
Chris@16
|
88 typedef typename String::value_type char_type;
|
Chris@16
|
89
|
Chris@16
|
90 /// Create an empty path.
|
Chris@16
|
91 explicit string_path(char_type separator = char_type('.'));
|
Chris@16
|
92 /// Create a path by parsing the given string.
|
Chris@16
|
93 /// @param value A sequence, possibly with separators, that describes
|
Chris@16
|
94 /// the path, e.g. "one.two.three".
|
Chris@16
|
95 /// @param separator The separator used in parsing. Defaults to '.'.
|
Chris@16
|
96 /// @param tr The translator used by this path to convert the individual
|
Chris@16
|
97 /// parts to keys.
|
Chris@16
|
98 string_path(const String &value, char_type separator = char_type('.'),
|
Chris@16
|
99 Translator tr = Translator());
|
Chris@16
|
100 /// Create a path by parsing the given string.
|
Chris@16
|
101 /// @param value A zero-terminated array of values. Only use if zero-
|
Chris@16
|
102 /// termination makes sense for your type, and your
|
Chris@16
|
103 /// sequence supports construction from it. Intended for
|
Chris@16
|
104 /// string literals.
|
Chris@16
|
105 /// @param separator The separator used in parsing. Defaults to '.'.
|
Chris@16
|
106 /// @param tr The translator used by this path to convert the individual
|
Chris@16
|
107 /// parts to keys.
|
Chris@16
|
108 string_path(const char_type *value,
|
Chris@16
|
109 char_type separator = char_type('.'),
|
Chris@16
|
110 Translator tr = Translator());
|
Chris@16
|
111
|
Chris@16
|
112 // Default copying doesn't do the right thing with the iterator
|
Chris@16
|
113 string_path(const string_path &o);
|
Chris@16
|
114 string_path& operator =(const string_path &o);
|
Chris@16
|
115
|
Chris@16
|
116 /// Take a single element off the path at the front and return it.
|
Chris@16
|
117 key_type reduce();
|
Chris@16
|
118
|
Chris@16
|
119 /// Test if the path is empty.
|
Chris@16
|
120 bool empty() const;
|
Chris@16
|
121
|
Chris@16
|
122 /// Test if the path contains a single element, i.e. no separators.
|
Chris@16
|
123 bool single() const;
|
Chris@16
|
124
|
Chris@16
|
125 /// Get the separator used by this path.
|
Chris@16
|
126 char_type separator() const { return m_separator; }
|
Chris@16
|
127
|
Chris@16
|
128 std::string dump() const {
|
Chris@16
|
129 return detail::dump_sequence(m_value);
|
Chris@16
|
130 }
|
Chris@16
|
131
|
Chris@16
|
132 /// Append a second path to this one.
|
Chris@16
|
133 /// @pre o's separator is the same as this one's, or o has no separators
|
Chris@16
|
134 string_path& operator /=(const string_path &o) {
|
Chris@16
|
135 // If it's single, there's no separator. This allows to do
|
Chris@16
|
136 // p /= "piece";
|
Chris@16
|
137 // even for non-default separators.
|
Chris@16
|
138 BOOST_ASSERT((m_separator == o.m_separator
|
Chris@16
|
139 || o.empty()
|
Chris@16
|
140 || o.single())
|
Chris@16
|
141 && "Incompatible paths.");
|
Chris@16
|
142 if(!o.empty()) {
|
Chris@16
|
143 String sub;
|
Chris@16
|
144 if(!this->empty()) {
|
Chris@16
|
145 sub.push_back(m_separator);
|
Chris@16
|
146 }
|
Chris@16
|
147 sub.insert(sub.end(), o.cstart(), o.m_value.end());
|
Chris@16
|
148 detail::append_and_preserve_iter(m_value, sub, m_start,
|
Chris@16
|
149 typename std::iterator_traits<s_iter>::iterator_category());
|
Chris@16
|
150 }
|
Chris@16
|
151 return *this;
|
Chris@16
|
152 }
|
Chris@16
|
153
|
Chris@16
|
154 private:
|
Chris@16
|
155 typedef typename String::iterator s_iter;
|
Chris@16
|
156 typedef typename String::const_iterator s_c_iter;
|
Chris@16
|
157 String m_value;
|
Chris@16
|
158 char_type m_separator;
|
Chris@16
|
159 Translator m_tr;
|
Chris@16
|
160 s_iter m_start;
|
Chris@16
|
161 s_c_iter cstart() const { return m_start; }
|
Chris@16
|
162 };
|
Chris@16
|
163
|
Chris@16
|
164 template <typename String, typename Translator> inline
|
Chris@16
|
165 string_path<String, Translator>::string_path(char_type separator)
|
Chris@16
|
166 : m_separator(separator), m_start(m_value.begin())
|
Chris@16
|
167 {}
|
Chris@16
|
168
|
Chris@16
|
169 template <typename String, typename Translator> inline
|
Chris@16
|
170 string_path<String, Translator>::string_path(const String &value,
|
Chris@16
|
171 char_type separator,
|
Chris@16
|
172 Translator tr)
|
Chris@16
|
173 : m_value(value), m_separator(separator),
|
Chris@16
|
174 m_tr(tr), m_start(m_value.begin())
|
Chris@16
|
175 {}
|
Chris@16
|
176
|
Chris@16
|
177 template <typename String, typename Translator> inline
|
Chris@16
|
178 string_path<String, Translator>::string_path(const char_type *value,
|
Chris@16
|
179 char_type separator,
|
Chris@16
|
180 Translator tr)
|
Chris@16
|
181 : m_value(value), m_separator(separator),
|
Chris@16
|
182 m_tr(tr), m_start(m_value.begin())
|
Chris@16
|
183 {}
|
Chris@16
|
184
|
Chris@16
|
185 template <typename String, typename Translator> inline
|
Chris@16
|
186 string_path<String, Translator>::string_path(const string_path &o)
|
Chris@16
|
187 : m_value(o.m_value), m_separator(o.m_separator),
|
Chris@16
|
188 m_tr(o.m_tr), m_start(m_value.begin())
|
Chris@16
|
189 {
|
Chris@16
|
190 std::advance(m_start, std::distance(o.m_value.begin(), o.cstart()));
|
Chris@16
|
191 }
|
Chris@16
|
192
|
Chris@16
|
193 template <typename String, typename Translator> inline
|
Chris@16
|
194 string_path<String, Translator>&
|
Chris@16
|
195 string_path<String, Translator>::operator =(const string_path &o)
|
Chris@16
|
196 {
|
Chris@16
|
197 m_value = o.m_value;
|
Chris@16
|
198 m_separator = o.m_separator;
|
Chris@16
|
199 m_tr = o.m_tr;
|
Chris@16
|
200 m_start = m_value.begin();
|
Chris@16
|
201 std::advance(m_start, std::distance(o.m_value.begin(), o.cstart()));
|
Chris@16
|
202 return *this;
|
Chris@16
|
203 }
|
Chris@16
|
204
|
Chris@16
|
205 template <typename String, typename Translator>
|
Chris@16
|
206 typename Translator::external_type string_path<String, Translator>::reduce()
|
Chris@16
|
207 {
|
Chris@16
|
208 BOOST_ASSERT(!empty() && "Reducing empty path");
|
Chris@16
|
209
|
Chris@16
|
210 s_iter next_sep = std::find(m_start, m_value.end(), m_separator);
|
Chris@16
|
211 String part(m_start, next_sep);
|
Chris@16
|
212 m_start = next_sep;
|
Chris@16
|
213 if(!empty()) {
|
Chris@16
|
214 // Unless we're at the end, skip the separator we found.
|
Chris@16
|
215 ++m_start;
|
Chris@16
|
216 }
|
Chris@16
|
217
|
Chris@16
|
218 if(optional<key_type> key = m_tr.get_value(part)) {
|
Chris@16
|
219 return *key;
|
Chris@16
|
220 }
|
Chris@16
|
221 BOOST_PROPERTY_TREE_THROW(ptree_bad_path("Path syntax error", *this));
|
Chris@16
|
222 }
|
Chris@16
|
223
|
Chris@16
|
224 template <typename String, typename Translator> inline
|
Chris@16
|
225 bool string_path<String, Translator>::empty() const
|
Chris@16
|
226 {
|
Chris@16
|
227 return m_start == m_value.end();
|
Chris@16
|
228 }
|
Chris@16
|
229
|
Chris@16
|
230 template <typename String, typename Translator> inline
|
Chris@16
|
231 bool string_path<String, Translator>::single() const
|
Chris@16
|
232 {
|
Chris@16
|
233 return std::find(static_cast<s_c_iter>(m_start),
|
Chris@16
|
234 m_value.end(), m_separator)
|
Chris@16
|
235 == m_value.end();
|
Chris@16
|
236 }
|
Chris@16
|
237
|
Chris@16
|
238 // By default, this is the path for strings. You can override this by
|
Chris@16
|
239 // specializing path_of for a more specific form of std::basic_string.
|
Chris@16
|
240 template <typename Ch, typename Traits, typename Alloc>
|
Chris@16
|
241 struct path_of< std::basic_string<Ch, Traits, Alloc> >
|
Chris@16
|
242 {
|
Chris@16
|
243 typedef std::basic_string<Ch, Traits, Alloc> _string;
|
Chris@16
|
244 typedef string_path< _string, id_translator<_string> > type;
|
Chris@16
|
245 };
|
Chris@16
|
246
|
Chris@16
|
247 template <typename String, typename Translator> inline
|
Chris@16
|
248 string_path<String, Translator> operator /(
|
Chris@16
|
249 string_path<String, Translator> p1,
|
Chris@16
|
250 const string_path<String, Translator> &p2)
|
Chris@16
|
251 {
|
Chris@16
|
252 p1 /= p2;
|
Chris@16
|
253 return p1;
|
Chris@16
|
254 }
|
Chris@16
|
255
|
Chris@16
|
256 // These shouldn't be necessary, but GCC won't find the one above.
|
Chris@16
|
257 template <typename String, typename Translator> inline
|
Chris@16
|
258 string_path<String, Translator> operator /(
|
Chris@16
|
259 string_path<String, Translator> p1,
|
Chris@16
|
260 const typename String::value_type *p2)
|
Chris@16
|
261 {
|
Chris@16
|
262 p1 /= p2;
|
Chris@16
|
263 return p1;
|
Chris@16
|
264 }
|
Chris@16
|
265
|
Chris@16
|
266 template <typename String, typename Translator> inline
|
Chris@16
|
267 string_path<String, Translator> operator /(
|
Chris@16
|
268 const typename String::value_type *p1,
|
Chris@16
|
269 const string_path<String, Translator> &p2)
|
Chris@16
|
270 {
|
Chris@16
|
271 string_path<String, Translator> t(p1);
|
Chris@16
|
272 t /= p2;
|
Chris@16
|
273 return t;
|
Chris@16
|
274 }
|
Chris@16
|
275
|
Chris@16
|
276 }}
|
Chris@16
|
277
|
Chris@16
|
278 #endif
|