Chris@16: // ---------------------------------------------------------------------------- Chris@16: // Copyright (C) 2002-2006 Marcin Kalicinski Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. Chris@16: // (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // For more information, see www.boost.org Chris@16: // ---------------------------------------------------------------------------- Chris@16: #ifndef BOOST_PROPERTY_TREE_DETAIL_PTREE_UTILS_HPP_INCLUDED Chris@16: #define BOOST_PROPERTY_TREE_DETAIL_PTREE_UTILS_HPP_INCLUDED Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { namespace property_tree { namespace detail Chris@16: { Chris@16: Chris@16: template Chris@16: struct less_nocase Chris@16: { Chris@16: typedef typename T::value_type Ch; Chris@16: std::locale m_locale; Chris@16: inline bool operator()(Ch c1, Ch c2) const Chris@16: { Chris@16: return std::toupper(c1, m_locale) < std::toupper(c2, m_locale); Chris@16: } Chris@16: inline bool operator()(const T &t1, const T &t2) const Chris@16: { Chris@16: return std::lexicographical_compare(t1.begin(), t1.end(), Chris@16: t2.begin(), t2.end(), *this); Chris@16: } Chris@16: }; Chris@16: Chris@16: template Chris@16: struct is_character : public boost::false_type {}; Chris@16: template <> Chris@16: struct is_character : public boost::true_type {}; Chris@16: template <> Chris@16: struct is_character : public boost::true_type {}; Chris@16: Chris@16: Chris@16: BOOST_MPL_HAS_XXX_TRAIT_DEF(internal_type) Chris@16: BOOST_MPL_HAS_XXX_TRAIT_DEF(external_type) Chris@16: template Chris@16: struct is_translator : public boost::mpl::and_< Chris@16: has_internal_type, has_external_type > {}; Chris@16: Chris@16: Chris@16: Chris@16: // Naively convert narrow string to another character type Chris@101: template Chris@101: Str widen(const char *text) Chris@16: { Chris@101: Str result; Chris@16: while (*text) Chris@16: { Chris@101: result += typename Str::value_type(*text); Chris@16: ++text; Chris@16: } Chris@16: return result; Chris@16: } Chris@16: Chris@16: // Naively convert string to narrow character type Chris@101: template Chris@101: Str narrow(const char_type *text) Chris@16: { Chris@101: Str result; Chris@16: while (*text) Chris@16: { Chris@16: if (*text < 0 || *text > (std::numeric_limits::max)()) Chris@16: result += '*'; Chris@16: else Chris@101: result += typename Str::value_type(*text); Chris@16: ++text; Chris@16: } Chris@16: return result; Chris@16: } Chris@16: Chris@16: // Remove trailing and leading spaces Chris@101: template Chris@101: Str trim(const Str &s, const std::locale &loc = std::locale()) Chris@16: { Chris@101: typename Str::const_iterator first = s.begin(); Chris@101: typename Str::const_iterator end = s.end(); Chris@16: while (first != end && std::isspace(*first, loc)) Chris@16: ++first; Chris@16: if (first == end) Chris@101: return Str(); Chris@101: typename Str::const_iterator last = end; Chris@16: do --last; while (std::isspace(*last, loc)); Chris@16: if (first != s.begin() || last + 1 != end) Chris@101: return Str(first, last + 1); Chris@16: else Chris@16: return s; Chris@16: } Chris@16: Chris@16: } } } Chris@16: Chris@16: #endif