Chris@16: // ---------------------------------------------------------------------------- Chris@16: // Copyright (C) 2009 Sebastian Redl Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. Chris@16: // (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // For more information, see www.boost.org Chris@16: // ---------------------------------------------------------------------------- Chris@16: Chris@16: #ifndef BOOST_PROPERTY_TREE_STRING_PATH_HPP_INCLUDED Chris@16: #define BOOST_PROPERTY_TREE_STRING_PATH_HPP_INCLUDED Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { namespace property_tree Chris@16: { Chris@16: namespace detail Chris@16: { Chris@16: template Chris@16: void append_and_preserve_iter(Sequence &s, const Sequence &r, Chris@16: Iterator &, std::forward_iterator_tag) Chris@16: { Chris@16: // Here we boldly assume that anything that is not random-access Chris@16: // preserves validity. This is valid for the STL sequences. Chris@16: s.insert(s.end(), r.begin(), r.end()); Chris@16: } Chris@16: template Chris@16: void append_and_preserve_iter(Sequence &s, const Sequence &r, Chris@16: Iterator &it, Chris@16: std::random_access_iterator_tag) Chris@16: { Chris@16: // Convert the iterator to an index, and later back. Chris@16: typename std::iterator_traits::difference_type idx = Chris@16: it - s.begin(); Chris@16: s.insert(s.end(), r.begin(), r.end()); Chris@16: it = s.begin() + idx; Chris@16: } Chris@16: Chris@16: template Chris@16: inline std::string dump_sequence(const Sequence &) Chris@16: { Chris@16: return ""; Chris@16: } Chris@16: inline std::string dump_sequence(const std::string &s) Chris@16: { Chris@16: return s; Chris@16: } Chris@16: #ifndef BOOST_NO_STD_WSTRING Chris@16: inline std::string dump_sequence(const std::wstring &s) Chris@16: { Chris@101: return narrow(s.c_str()); Chris@16: } Chris@16: #endif Chris@16: } Chris@16: Chris@16: /// Default path class. A path is a sequence of values. Groups of values Chris@16: /// are separated by the separator value, which defaults to '.' cast to Chris@16: /// the sequence's value type. The group of values is then passed to the Chris@16: /// translator to get a key. Chris@16: /// Chris@16: /// If instantiated with std::string and id_translator\, Chris@16: /// it accepts paths of the form "one.two.three.four". Chris@16: /// Chris@16: /// @tparam String Any Sequence. If the sequence does not support random- Chris@16: /// access iteration, concatenation of paths assumes that Chris@16: /// insertions at the end preserve iterator validity. Chris@16: /// @tparam Translator A translator with internal_type == String. Chris@16: template Chris@16: class string_path Chris@16: { Chris@16: BOOST_STATIC_ASSERT((is_same::value)); Chris@16: public: Chris@16: typedef typename Translator::external_type key_type; Chris@16: typedef typename String::value_type char_type; Chris@16: Chris@16: /// Create an empty path. Chris@16: explicit string_path(char_type separator = char_type('.')); Chris@16: /// Create a path by parsing the given string. Chris@16: /// @param value A sequence, possibly with separators, that describes Chris@16: /// the path, e.g. "one.two.three". Chris@16: /// @param separator The separator used in parsing. Defaults to '.'. Chris@16: /// @param tr The translator used by this path to convert the individual Chris@16: /// parts to keys. Chris@16: string_path(const String &value, char_type separator = char_type('.'), Chris@16: Translator tr = Translator()); Chris@16: /// Create a path by parsing the given string. Chris@16: /// @param value A zero-terminated array of values. Only use if zero- Chris@16: /// termination makes sense for your type, and your Chris@16: /// sequence supports construction from it. Intended for Chris@16: /// string literals. Chris@16: /// @param separator The separator used in parsing. Defaults to '.'. Chris@16: /// @param tr The translator used by this path to convert the individual Chris@16: /// parts to keys. Chris@16: string_path(const char_type *value, Chris@16: char_type separator = char_type('.'), Chris@16: Translator tr = Translator()); Chris@16: Chris@16: // Default copying doesn't do the right thing with the iterator Chris@16: string_path(const string_path &o); Chris@16: string_path& operator =(const string_path &o); Chris@16: Chris@16: /// Take a single element off the path at the front and return it. Chris@16: key_type reduce(); Chris@16: Chris@16: /// Test if the path is empty. Chris@16: bool empty() const; Chris@16: Chris@16: /// Test if the path contains a single element, i.e. no separators. Chris@16: bool single() const; Chris@16: Chris@16: /// Get the separator used by this path. Chris@16: char_type separator() const { return m_separator; } Chris@16: Chris@16: std::string dump() const { Chris@16: return detail::dump_sequence(m_value); Chris@16: } Chris@16: Chris@16: /// Append a second path to this one. Chris@16: /// @pre o's separator is the same as this one's, or o has no separators Chris@16: string_path& operator /=(const string_path &o) { Chris@16: // If it's single, there's no separator. This allows to do Chris@16: // p /= "piece"; Chris@16: // even for non-default separators. Chris@16: BOOST_ASSERT((m_separator == o.m_separator Chris@16: || o.empty() Chris@16: || o.single()) Chris@16: && "Incompatible paths."); Chris@16: if(!o.empty()) { Chris@16: String sub; Chris@16: if(!this->empty()) { Chris@16: sub.push_back(m_separator); Chris@16: } Chris@16: sub.insert(sub.end(), o.cstart(), o.m_value.end()); Chris@16: detail::append_and_preserve_iter(m_value, sub, m_start, Chris@16: typename std::iterator_traits::iterator_category()); Chris@16: } Chris@16: return *this; Chris@16: } Chris@16: Chris@16: private: Chris@16: typedef typename String::iterator s_iter; Chris@16: typedef typename String::const_iterator s_c_iter; Chris@16: String m_value; Chris@16: char_type m_separator; Chris@16: Translator m_tr; Chris@16: s_iter m_start; Chris@16: s_c_iter cstart() const { return m_start; } Chris@16: }; Chris@16: Chris@16: template inline Chris@16: string_path::string_path(char_type separator) Chris@16: : m_separator(separator), m_start(m_value.begin()) Chris@16: {} Chris@16: Chris@16: template inline Chris@16: string_path::string_path(const String &value, Chris@16: char_type separator, Chris@16: Translator tr) Chris@16: : m_value(value), m_separator(separator), Chris@16: m_tr(tr), m_start(m_value.begin()) Chris@16: {} Chris@16: Chris@16: template inline Chris@16: string_path::string_path(const char_type *value, Chris@16: char_type separator, Chris@16: Translator tr) Chris@16: : m_value(value), m_separator(separator), Chris@16: m_tr(tr), m_start(m_value.begin()) Chris@16: {} Chris@16: Chris@16: template inline Chris@16: string_path::string_path(const string_path &o) Chris@16: : m_value(o.m_value), m_separator(o.m_separator), Chris@16: m_tr(o.m_tr), m_start(m_value.begin()) Chris@16: { Chris@16: std::advance(m_start, std::distance(o.m_value.begin(), o.cstart())); Chris@16: } Chris@16: Chris@16: template inline Chris@16: string_path& Chris@16: string_path::operator =(const string_path &o) Chris@16: { Chris@16: m_value = o.m_value; Chris@16: m_separator = o.m_separator; Chris@16: m_tr = o.m_tr; Chris@16: m_start = m_value.begin(); Chris@16: std::advance(m_start, std::distance(o.m_value.begin(), o.cstart())); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: template Chris@16: typename Translator::external_type string_path::reduce() Chris@16: { Chris@16: BOOST_ASSERT(!empty() && "Reducing empty path"); Chris@16: Chris@16: s_iter next_sep = std::find(m_start, m_value.end(), m_separator); Chris@16: String part(m_start, next_sep); Chris@16: m_start = next_sep; Chris@16: if(!empty()) { Chris@16: // Unless we're at the end, skip the separator we found. Chris@16: ++m_start; Chris@16: } Chris@16: Chris@16: if(optional key = m_tr.get_value(part)) { Chris@16: return *key; Chris@16: } Chris@16: BOOST_PROPERTY_TREE_THROW(ptree_bad_path("Path syntax error", *this)); Chris@16: } Chris@16: Chris@16: template inline Chris@16: bool string_path::empty() const Chris@16: { Chris@16: return m_start == m_value.end(); Chris@16: } Chris@16: Chris@16: template inline Chris@16: bool string_path::single() const Chris@16: { Chris@16: return std::find(static_cast(m_start), Chris@16: m_value.end(), m_separator) Chris@16: == m_value.end(); Chris@16: } Chris@16: Chris@16: // By default, this is the path for strings. You can override this by Chris@16: // specializing path_of for a more specific form of std::basic_string. Chris@16: template Chris@16: struct path_of< std::basic_string > Chris@16: { Chris@16: typedef std::basic_string _string; Chris@16: typedef string_path< _string, id_translator<_string> > type; Chris@16: }; Chris@16: Chris@16: template inline Chris@16: string_path operator /( Chris@16: string_path p1, Chris@16: const string_path &p2) Chris@16: { Chris@16: p1 /= p2; Chris@16: return p1; Chris@16: } Chris@16: Chris@16: // These shouldn't be necessary, but GCC won't find the one above. Chris@16: template inline Chris@16: string_path operator /( Chris@16: string_path p1, Chris@16: const typename String::value_type *p2) Chris@16: { Chris@16: p1 /= p2; Chris@16: return p1; Chris@16: } Chris@16: Chris@16: template inline Chris@16: string_path operator /( Chris@16: const typename String::value_type *p1, Chris@16: const string_path &p2) Chris@16: { Chris@16: string_path t(p1); Chris@16: t /= p2; Chris@16: return t; Chris@16: } Chris@16: Chris@16: }} Chris@16: Chris@16: #endif