char_traits.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
  2. // (C) Copyright 2003-2007 Jonathan Turkanis
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
  5. // Provides std::char_traits for libraries without templated streams. Should not
  6. // be confused with <boost/iostreams/char_traits.hpp>, which defines the
  7. // template boost::iostreams::char_traits.
  8. // See http://www.boost.org/libs/iostreams for documentation.
  9. #ifndef BOOST_IOSTREAMS_DETAIL_CHAR_TRAITS_HPP_INCLUDED
  10. #define BOOST_IOSTREAMS_DETAIL_CHAR_TRAITS_HPP_INCLUDED
  11. #if defined(_MSC_VER)
  12. # pragma once
  13. #endif
  14. #include <iosfwd>
  15. #include <boost/iostreams/detail/config/wide_streams.hpp>
  16. #ifdef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES
  17. # include <boost/config.hpp> // Make sure size_t is in std.
  18. # include <cstddef>
  19. # include <cstring>
  20. # include <cstdio>
  21. #endif
  22. #ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //--------------------------------//
  23. # define BOOST_IOSTREAMS_CHAR_TRAITS(ch) std::char_traits< ch >
  24. #else
  25. # define BOOST_IOSTREAMS_CHAR_TRAITS(ch) boost::iostreams::detail::char_traits
  26. namespace boost { namespace iostreams { namespace detail {
  27. struct char_traits {
  28. typedef char char_type;
  29. typedef int int_type;
  30. typedef std::streampos pos_type;
  31. typedef std::streamoff off_type;
  32. // Note: this may not be not conforming, since it treats chars as unsigned,
  33. // but is only used to test for equality.
  34. static int compare(const char* lhs, const char* rhs, std::size_t n)
  35. { return std::strncmp(lhs, rhs, n); }
  36. static char* copy(char *dest, const char *src, std::size_t n)
  37. { return static_cast<char*>(std::memcpy(dest, src, n)); }
  38. static char* move(char *dest, const char *src, std::size_t n)
  39. { return static_cast<char*>(std::memmove(dest, src, n)); }
  40. static const char* find(const char* s, std::size_t n, const char& c)
  41. { return (const char*) (const void*) std::memchr(s, c, n); }
  42. static char to_char_type(const int& c) { return c; }
  43. static int to_int_type(const char& c) { return c; }
  44. static bool eq_int_type(const int& lhs, const int& rhs)
  45. { return lhs == rhs; }
  46. static int eof() { return EOF; }
  47. static int not_eof(const int& c) { return c != EOF ? c : '\n'; }
  48. };
  49. } } } // End namespaces detail, iostreams, boost.
  50. #endif // #ifdef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //-----------------------//
  51. #endif // #ifndef BOOST_IOSTREAMS_DETAIL_CHAR_TRAITS_HPP_INCLUDED