ptree_utils.hpp 3.1 KB

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