Chris@16
|
1 // ----------------------------------------------------------------------------
|
Chris@16
|
2 // Copyright (C) 2002-2006 Marcin Kalicinski
|
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 #ifndef BOOST_PROPERTY_TREE_DETAIL_PTREE_UTILS_HPP_INCLUDED
|
Chris@16
|
11 #define BOOST_PROPERTY_TREE_DETAIL_PTREE_UTILS_HPP_INCLUDED
|
Chris@16
|
12
|
Chris@16
|
13 #include <boost/limits.hpp>
|
Chris@16
|
14 #include <boost/type_traits/integral_constant.hpp>
|
Chris@16
|
15 #include <boost/mpl/has_xxx.hpp>
|
Chris@16
|
16 #include <boost/mpl/and.hpp>
|
Chris@16
|
17 #include <string>
|
Chris@16
|
18 #include <algorithm>
|
Chris@16
|
19 #include <locale>
|
Chris@16
|
20
|
Chris@16
|
21 namespace boost { namespace property_tree { namespace detail
|
Chris@16
|
22 {
|
Chris@16
|
23
|
Chris@16
|
24 template<class T>
|
Chris@16
|
25 struct less_nocase
|
Chris@16
|
26 {
|
Chris@16
|
27 typedef typename T::value_type Ch;
|
Chris@16
|
28 std::locale m_locale;
|
Chris@16
|
29 inline bool operator()(Ch c1, Ch c2) const
|
Chris@16
|
30 {
|
Chris@16
|
31 return std::toupper(c1, m_locale) < std::toupper(c2, m_locale);
|
Chris@16
|
32 }
|
Chris@16
|
33 inline bool operator()(const T &t1, const T &t2) const
|
Chris@16
|
34 {
|
Chris@16
|
35 return std::lexicographical_compare(t1.begin(), t1.end(),
|
Chris@16
|
36 t2.begin(), t2.end(), *this);
|
Chris@16
|
37 }
|
Chris@16
|
38 };
|
Chris@16
|
39
|
Chris@16
|
40 template <typename Ch>
|
Chris@16
|
41 struct is_character : public boost::false_type {};
|
Chris@16
|
42 template <>
|
Chris@16
|
43 struct is_character<char> : public boost::true_type {};
|
Chris@16
|
44 template <>
|
Chris@16
|
45 struct is_character<wchar_t> : public boost::true_type {};
|
Chris@16
|
46
|
Chris@16
|
47
|
Chris@16
|
48 BOOST_MPL_HAS_XXX_TRAIT_DEF(internal_type)
|
Chris@16
|
49 BOOST_MPL_HAS_XXX_TRAIT_DEF(external_type)
|
Chris@16
|
50 template <typename T>
|
Chris@16
|
51 struct is_translator : public boost::mpl::and_<
|
Chris@16
|
52 has_internal_type<T>, has_external_type<T> > {};
|
Chris@16
|
53
|
Chris@16
|
54
|
Chris@16
|
55
|
Chris@16
|
56 // Naively convert narrow string to another character type
|
Chris@101
|
57 template<typename Str>
|
Chris@101
|
58 Str widen(const char *text)
|
Chris@16
|
59 {
|
Chris@101
|
60 Str result;
|
Chris@16
|
61 while (*text)
|
Chris@16
|
62 {
|
Chris@101
|
63 result += typename Str::value_type(*text);
|
Chris@16
|
64 ++text;
|
Chris@16
|
65 }
|
Chris@16
|
66 return result;
|
Chris@16
|
67 }
|
Chris@16
|
68
|
Chris@16
|
69 // Naively convert string to narrow character type
|
Chris@101
|
70 template<typename Str, typename char_type>
|
Chris@101
|
71 Str narrow(const char_type *text)
|
Chris@16
|
72 {
|
Chris@101
|
73 Str result;
|
Chris@16
|
74 while (*text)
|
Chris@16
|
75 {
|
Chris@16
|
76 if (*text < 0 || *text > (std::numeric_limits<char>::max)())
|
Chris@16
|
77 result += '*';
|
Chris@16
|
78 else
|
Chris@101
|
79 result += typename Str::value_type(*text);
|
Chris@16
|
80 ++text;
|
Chris@16
|
81 }
|
Chris@16
|
82 return result;
|
Chris@16
|
83 }
|
Chris@16
|
84
|
Chris@16
|
85 // Remove trailing and leading spaces
|
Chris@101
|
86 template<class Str>
|
Chris@101
|
87 Str trim(const Str &s, const std::locale &loc = std::locale())
|
Chris@16
|
88 {
|
Chris@101
|
89 typename Str::const_iterator first = s.begin();
|
Chris@101
|
90 typename Str::const_iterator end = s.end();
|
Chris@16
|
91 while (first != end && std::isspace(*first, loc))
|
Chris@16
|
92 ++first;
|
Chris@16
|
93 if (first == end)
|
Chris@101
|
94 return Str();
|
Chris@101
|
95 typename Str::const_iterator last = end;
|
Chris@16
|
96 do --last; while (std::isspace(*last, loc));
|
Chris@16
|
97 if (first != s.begin() || last + 1 != end)
|
Chris@101
|
98 return Str(first, last + 1);
|
Chris@16
|
99 else
|
Chris@16
|
100 return s;
|
Chris@16
|
101 }
|
Chris@16
|
102
|
Chris@16
|
103 } } }
|
Chris@16
|
104
|
Chris@16
|
105 #endif
|