fix doc example typo
[boost.git] / boost / property_tree / detail / path_implementation.hpp
blob040a58020761e97f0c989f58f84de6dd6194fd8a
1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2002-2006 Marcin Kalicinski
3 //
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // For more information, see www.boost.org
9 // ----------------------------------------------------------------------------
10 #ifndef BOOST_PROPERTY_TREE_DETAIL_PATH_IMPLEMENTATION_HPP_INCLUDED
11 #define BOOST_PROPERTY_TREE_DETAIL_PATH_IMPLEMENTATION_HPP_INCLUDED
13 namespace boost { namespace property_tree
16 namespace detail
19 // Path-to-string converter for basic_path
20 template<class Key>
21 std::string path_to_string(const basic_path<Key> &path)
23 return path.to_string();
28 ///////////////////////////////////////////////////////////////////////
29 // Free functions
31 inline path operator /(const path &p1, const path &p2)
33 return path(p1) /= p2;
36 inline wpath operator /(const wpath &p1, const wpath &p2)
38 return wpath(p1) /= p2;
41 ///////////////////////////////////////////////////////////////////////
42 // Construction & destruction
44 template<class Key>
45 basic_path<Key>::basic_path()
49 template<class Key>
50 basic_path<Key>::basic_path(const Key &path, char_type separator)
52 parse(path.begin(), path.end(), separator);
55 template<class Key>
56 basic_path<Key>::basic_path(const char_type *path, char_type separator)
58 parse(path, path + std::char_traits<char_type>::length(path), separator);
61 ///////////////////////////////////////////////////////////////////////
62 // Path manipulation
64 template<class Key>
65 basic_path<Key> &basic_path<Key>::operator /=(const basic_path<Key> &rhs)
67 for (typename std::vector<Key>::const_iterator it = rhs.m_path.begin(); it != rhs.m_path.end(); ++it)
68 m_path.push_back(*it);
69 return *this;
72 template<class Key>
73 std::string basic_path<Key>::to_string() const
75 std::string s;
76 for (typename std::vector<Key>::const_iterator it = m_path.begin(); it != m_path.end(); ++it)
78 if (it == m_path.begin())
79 s += detail::narrow(it->c_str());
80 else
81 s += '.', s += detail::narrow(it->c_str());
83 return s;
86 ///////////////////////////////////////////////////////////////////////
87 // Operations
89 template<class Key>
90 template<class C, class D, class X>
91 basic_ptree<C, Key, basic_path<Key>, D, X> *
92 basic_path<Key>::get_child(basic_ptree<C, Key, basic_path<Key>, D, X> &root) const
94 typedef basic_ptree<C, Key, basic_path<Key>, D, X> ptree_type;
95 ptree_type *pt = &root;
96 for (typename std::vector<Key>::const_iterator it = m_path.begin(); it != m_path.end(); ++it)
98 typename ptree_type::iterator it_child = pt->find(*it);
99 if (it_child == pt->end())
100 return 0;
101 else
102 pt = &(it_child->second);
104 return pt;
107 template<class Key>
108 template<class C, class D, class X>
109 const basic_ptree<C, Key, basic_path<Key>, D, X> *
110 basic_path<Key>::get_child(const basic_ptree<C, Key, basic_path<Key>, D, X> &root) const
112 typedef basic_ptree<C, Key, basic_path<Key>, D, X> ptree_type;
113 basic_path<Key> *nc_this = const_cast<basic_path<Key> *>(this);
114 ptree_type &nc_root = const_cast<ptree_type &>(root);
115 return nc_this->get_child(nc_root);
118 template<class Key>
119 template<class C, class D, class X>
120 basic_ptree<C, Key, basic_path<Key>, D, X> *
121 basic_path<Key>::put_child(basic_ptree<C, Key, basic_path<Key>, D, X> &root,
122 const basic_ptree<C, Key, basic_path<Key>, D, X> &child,
123 bool do_not_replace) const
125 if (m_path.empty())
127 return 0;
129 else
132 typedef basic_ptree<C, Key, basic_path<Key>, D, X> ptree_type;
133 typedef typename std::vector<Key>::const_iterator path_iterator;
135 ptree_type *pt = &root;
136 for (path_iterator it = m_path.begin(), end = m_path.end() - 1; it != end; ++it)
138 typename ptree_type::iterator it_child = pt->find(*it);
139 if (it_child == pt->end())
140 pt = &pt->push_back(typename ptree_type::value_type(*it, empty_ptree<ptree_type>()))->second;
141 else
142 pt = &it_child->second;
145 if (do_not_replace)
146 return &pt->push_back(typename ptree_type::value_type(m_path.back(), child))->second;
147 else
149 typename ptree_type::iterator it = pt->find(m_path.back());
150 if (it == pt->end())
151 return &pt->push_back(typename ptree_type::value_type(m_path.back(), child))->second;
152 else
154 it->second = child;
155 return &it->second;
161 ///////////////////////////////////////////////////////////////////////
162 // Internal
164 template<class Key>
165 template<class RanIt>
166 void basic_path<Key>::parse(RanIt begin, RanIt end, char_type separator)
168 m_path.reserve(8);
169 while (1)
171 RanIt it = std::find(begin, end, separator);
172 m_path.push_back(Key(begin, it));
173 if (it == end)
174 break;
175 begin = it + 1;
181 #endif