File indexing completed on 2025-01-18 09:50:19
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_PROPERTY_TREE_STRING_PATH_HPP_INCLUDED
0012 #define BOOST_PROPERTY_TREE_STRING_PATH_HPP_INCLUDED
0013
0014 #include <boost/property_tree/ptree_fwd.hpp>
0015 #include <boost/property_tree/id_translator.hpp>
0016 #include <boost/property_tree/exceptions.hpp>
0017 #include <boost/property_tree/detail/ptree_utils.hpp>
0018
0019 #include <boost/static_assert.hpp>
0020 #include <boost/assert.hpp>
0021 #include <boost/type_traits/is_same.hpp>
0022 #include <boost/optional/optional.hpp>
0023 #include <boost/throw_exception.hpp>
0024 #include <algorithm>
0025 #include <string>
0026 #include <iterator>
0027
0028 namespace boost { namespace property_tree
0029 {
0030 namespace detail
0031 {
0032 template <typename Sequence, typename Iterator>
0033 void append_and_preserve_iter(Sequence &s, const Sequence &r,
0034 Iterator &, std::forward_iterator_tag)
0035 {
0036
0037
0038 s.insert(s.end(), r.begin(), r.end());
0039 }
0040 template <typename Sequence, typename Iterator>
0041 void append_and_preserve_iter(Sequence &s, const Sequence &r,
0042 Iterator &it,
0043 std::random_access_iterator_tag)
0044 {
0045
0046 typename std::iterator_traits<Iterator>::difference_type idx =
0047 it - s.begin();
0048 s.insert(s.end(), r.begin(), r.end());
0049 it = s.begin() + idx;
0050 }
0051
0052 template <typename Sequence>
0053 inline std::string dump_sequence(const Sequence &)
0054 {
0055 return "<undumpable sequence>";
0056 }
0057 inline std::string dump_sequence(const std::string &s)
0058 {
0059 return s;
0060 }
0061 #ifndef BOOST_NO_STD_WSTRING
0062 inline std::string dump_sequence(const std::wstring &s)
0063 {
0064 return narrow<std::string>(s.c_str());
0065 }
0066 #endif
0067 }
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081 template <typename String, typename Translator>
0082 class string_path
0083 {
0084 BOOST_STATIC_ASSERT((is_same<String,
0085 typename Translator::internal_type>::value));
0086 public:
0087 typedef typename Translator::external_type key_type;
0088 typedef typename String::value_type char_type;
0089
0090
0091 explicit string_path(char_type separator = char_type('.'));
0092
0093
0094
0095
0096
0097
0098 string_path(const String &value, char_type separator = char_type('.'),
0099 Translator tr = Translator());
0100
0101
0102
0103
0104
0105
0106
0107
0108 string_path(const char_type *value,
0109 char_type separator = char_type('.'),
0110 Translator tr = Translator());
0111
0112
0113 string_path(const string_path &o);
0114 string_path& operator =(const string_path &o);
0115
0116
0117 key_type reduce();
0118
0119
0120 bool empty() const;
0121
0122
0123 bool single() const;
0124
0125
0126 char_type separator() const { return m_separator; }
0127
0128 std::string dump() const {
0129 return detail::dump_sequence(m_value);
0130 }
0131
0132
0133
0134 string_path& operator /=(const string_path &o) {
0135
0136
0137
0138 BOOST_ASSERT((m_separator == o.m_separator
0139 || o.empty()
0140 || o.single())
0141 && "Incompatible paths.");
0142 if(!o.empty()) {
0143 String sub;
0144 if(!this->empty()) {
0145 sub.push_back(m_separator);
0146 }
0147 sub.insert(sub.end(), o.cstart(), o.m_value.end());
0148 detail::append_and_preserve_iter(m_value, sub, m_start,
0149 typename std::iterator_traits<s_iter>::iterator_category());
0150 }
0151 return *this;
0152 }
0153
0154 private:
0155 typedef typename String::iterator s_iter;
0156 typedef typename String::const_iterator s_c_iter;
0157 String m_value;
0158 char_type m_separator;
0159 Translator m_tr;
0160 s_iter m_start;
0161 s_c_iter cstart() const { return m_start; }
0162 };
0163
0164 template <typename String, typename Translator> inline
0165 string_path<String, Translator>::string_path(char_type separator)
0166 : m_separator(separator), m_start(m_value.begin())
0167 {}
0168
0169 template <typename String, typename Translator> inline
0170 string_path<String, Translator>::string_path(const String &value,
0171 char_type separator,
0172 Translator tr)
0173 : m_value(value), m_separator(separator),
0174 m_tr(tr), m_start(m_value.begin())
0175 {}
0176
0177 template <typename String, typename Translator> inline
0178 string_path<String, Translator>::string_path(const char_type *value,
0179 char_type separator,
0180 Translator tr)
0181 : m_value(value), m_separator(separator),
0182 m_tr(tr), m_start(m_value.begin())
0183 {}
0184
0185 template <typename String, typename Translator> inline
0186 string_path<String, Translator>::string_path(const string_path &o)
0187 : m_value(o.m_value), m_separator(o.m_separator),
0188 m_tr(o.m_tr), m_start(m_value.begin())
0189 {
0190 std::advance(m_start, std::distance(o.m_value.begin(), o.cstart()));
0191 }
0192
0193 template <typename String, typename Translator> inline
0194 string_path<String, Translator>&
0195 string_path<String, Translator>::operator =(const string_path &o)
0196 {
0197 m_value = o.m_value;
0198 m_separator = o.m_separator;
0199 m_tr = o.m_tr;
0200 m_start = m_value.begin();
0201 std::advance(m_start, std::distance(o.m_value.begin(), o.cstart()));
0202 return *this;
0203 }
0204
0205 template <typename String, typename Translator>
0206 typename Translator::external_type string_path<String, Translator>::reduce()
0207 {
0208 BOOST_ASSERT(!empty() && "Reducing empty path");
0209
0210 s_iter next_sep = std::find(m_start, m_value.end(), m_separator);
0211 String part(m_start, next_sep);
0212 m_start = next_sep;
0213 if(!empty()) {
0214
0215 ++m_start;
0216 }
0217
0218 if(optional<key_type> key = m_tr.get_value(part)) {
0219 return *key;
0220 }
0221 BOOST_PROPERTY_TREE_THROW(ptree_bad_path("Path syntax error", *this));
0222 }
0223
0224 template <typename String, typename Translator> inline
0225 bool string_path<String, Translator>::empty() const
0226 {
0227 return m_start == m_value.end();
0228 }
0229
0230 template <typename String, typename Translator> inline
0231 bool string_path<String, Translator>::single() const
0232 {
0233 return std::find(static_cast<s_c_iter>(m_start),
0234 m_value.end(), m_separator)
0235 == m_value.end();
0236 }
0237
0238
0239
0240 template <typename Ch, typename Traits, typename Alloc>
0241 struct path_of< std::basic_string<Ch, Traits, Alloc> >
0242 {
0243 typedef std::basic_string<Ch, Traits, Alloc> _string;
0244 typedef string_path< _string, id_translator<_string> > type;
0245 };
0246
0247 template <typename String, typename Translator> inline
0248 string_path<String, Translator> operator /(
0249 string_path<String, Translator> p1,
0250 const string_path<String, Translator> &p2)
0251 {
0252 p1 /= p2;
0253 return p1;
0254 }
0255
0256
0257 template <typename String, typename Translator> inline
0258 string_path<String, Translator> operator /(
0259 string_path<String, Translator> p1,
0260 const typename String::value_type *p2)
0261 {
0262 p1 /= p2;
0263 return p1;
0264 }
0265
0266 template <typename String, typename Translator> inline
0267 string_path<String, Translator> operator /(
0268 const typename String::value_type *p1,
0269 const string_path<String, Translator> &p2)
0270 {
0271 string_path<String, Translator> t(p1);
0272 t /= p2;
0273 return t;
0274 }
0275
0276 }}
0277
0278 #endif