1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2002-2006 Marcin Kalicinski
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)
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
19 // Path-to-string converter for basic_path
21 std::string
path_to_string(const basic_path
<Key
> &path
)
23 return path
.to_string();
28 ///////////////////////////////////////////////////////////////////////
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
45 basic_path
<Key
>::basic_path()
50 basic_path
<Key
>::basic_path(const Key
&path
, char_type separator
)
52 parse(path
.begin(), path
.end(), separator
);
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 ///////////////////////////////////////////////////////////////////////
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
);
73 std::string basic_path
<Key
>::to_string() const
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());
81 s
+= '.', s
+= detail::narrow(it
->c_str());
86 ///////////////////////////////////////////////////////////////////////
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())
102 pt
= &(it_child
->second
);
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
);
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
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
;
142 pt
= &it_child
->second
;
146 return &pt
->push_back(typename
ptree_type::value_type(m_path
.back(), child
))->second
;
149 typename
ptree_type::iterator it
= pt
->find(m_path
.back());
151 return &pt
->push_back(typename
ptree_type::value_type(m_path
.back(), child
))->second
;
161 ///////////////////////////////////////////////////////////////////////
165 template<class RanIt
>
166 void basic_path
<Key
>::parse(RanIt begin
, RanIt end
, char_type separator
)
171 RanIt it
= std::find(begin
, end
, separator
);
172 m_path
.push_back(Key(begin
, it
));