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