PageRenderTime 21ms CodeModel.GetById 12ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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