/Src/Dependencies/Boost/boost/property_tree/detail/ptree_utils.hpp

http://hadesmem.googlecode.com/ · C++ Header · 106 lines · 81 code · 13 blank · 12 comment · 14 complexity · bad6a1cfef236f2d0542bbc3995417b1 MD5 · raw file

  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<class Ch>
  49. std::basic_string<Ch> widen(const char *text)
  50. {
  51. std::basic_string<Ch> result;
  52. while (*text)
  53. {
  54. result += Ch(*text);
  55. ++text;
  56. }
  57. return result;
  58. }
  59. // Naively convert string to narrow character type
  60. template<class Ch>
  61. std::string narrow(const Ch *text)
  62. {
  63. std::string result;
  64. while (*text)
  65. {
  66. if (*text < 0 || *text > (std::numeric_limits<char>::max)())
  67. result += '*';
  68. else
  69. result += char(*text);
  70. ++text;
  71. }
  72. return result;
  73. }
  74. // Remove trailing and leading spaces
  75. template<class Ch>
  76. std::basic_string<Ch> trim(const std::basic_string<Ch> &s,
  77. const std::locale &loc = std::locale())
  78. {
  79. typename std::basic_string<Ch>::const_iterator first = s.begin();
  80. typename std::basic_string<Ch>::const_iterator end = s.end();
  81. while (first != end && std::isspace(*first, loc))
  82. ++first;
  83. if (first == end)
  84. return std::basic_string<Ch>();
  85. typename std::basic_string<Ch>::const_iterator last = end;
  86. do --last; while (std::isspace(*last, loc));
  87. if (first != s.begin() || last + 1 != end)
  88. return std::basic_string<Ch>(first, last + 1);
  89. else
  90. return s;
  91. }
  92. } } }
  93. #endif