read.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
  2. // (C) Copyright 2005-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. // See http://www.boost.org/libs/iostreams for documentation.
  6. #ifndef BOOST_IOSTREAMS_READ_HPP_INCLUDED
  7. #define BOOST_IOSTREAMS_READ_HPP_INCLUDED
  8. #if defined(_MSC_VER)
  9. # pragma once
  10. #endif
  11. #include <boost/config.hpp> // DEDUCED_TYPENAME, MSVC.
  12. #include <boost/detail/workaround.hpp>
  13. #include <boost/iostreams/char_traits.hpp>
  14. #include <boost/iostreams/detail/char_traits.hpp>
  15. #include <boost/iostreams/detail/dispatch.hpp>
  16. #include <boost/iostreams/detail/ios.hpp> // streamsize.
  17. #include <boost/iostreams/detail/streambuf.hpp>
  18. #include <boost/iostreams/detail/wrap_unwrap.hpp>
  19. #include <boost/iostreams/operations_fwd.hpp>
  20. #include <boost/mpl/if.hpp>
  21. // Must come last.
  22. #include <boost/iostreams/detail/config/disable_warnings.hpp>
  23. namespace boost { namespace iostreams {
  24. namespace detail {
  25. template<typename T>
  26. struct read_device_impl;
  27. template<typename T>
  28. struct read_filter_impl;
  29. } // End namespace detail.
  30. template<typename T>
  31. typename int_type_of<T>::type get(T& t)
  32. { return detail::read_device_impl<T>::get(detail::unwrap(t)); }
  33. template<typename T>
  34. inline std::streamsize
  35. read(T& t, typename char_type_of<T>::type* s, std::streamsize n)
  36. { return detail::read_device_impl<T>::read(detail::unwrap(t), s, n); }
  37. template<typename T, typename Source>
  38. std::streamsize
  39. read(T& t, Source& src, typename char_type_of<T>::type* s, std::streamsize n)
  40. { return detail::read_filter_impl<T>::read(detail::unwrap(t), src, s, n); }
  41. template<typename T>
  42. bool putback(T& t, typename char_type_of<T>::type c)
  43. { return detail::read_device_impl<T>::putback(detail::unwrap(t), c); }
  44. //----------------------------------------------------------------------------//
  45. namespace detail {
  46. // Helper function for adding -1 as EOF indicator.
  47. inline std::streamsize check_eof(std::streamsize n) { return n != 0 ? n : -1; }
  48. // Helper templates for reading from streambufs.
  49. template<bool IsLinked>
  50. struct true_eof_impl;
  51. template<>
  52. struct true_eof_impl<true> {
  53. template<typename T>
  54. static bool true_eof(T& t) { return t.true_eof(); }
  55. };
  56. template<>
  57. struct true_eof_impl<false> {
  58. template<typename T>
  59. static bool true_eof(T&) { return true; }
  60. };
  61. template<typename T>
  62. inline bool true_eof(T& t)
  63. {
  64. const bool linked = is_linked<T>::value;
  65. return true_eof_impl<linked>::true_eof(t);
  66. }
  67. //------------------Definition of read_device_impl----------------------------//
  68. template<typename T>
  69. struct read_device_impl
  70. : mpl::if_<
  71. detail::is_custom<T>,
  72. operations<T>,
  73. read_device_impl<
  74. BOOST_DEDUCED_TYPENAME
  75. detail::dispatch<
  76. T, istream_tag, streambuf_tag, input
  77. >::type
  78. >
  79. >::type
  80. { };
  81. template<>
  82. struct read_device_impl<istream_tag> {
  83. template<typename T>
  84. static typename int_type_of<T>::type get(T& t)
  85. { return t.get(); }
  86. template<typename T>
  87. static std::streamsize
  88. read(T& t, typename char_type_of<T>::type* s, std::streamsize n)
  89. { return check_eof(t.rdbuf()->sgetn(s, n)); }
  90. template<typename T>
  91. static bool putback(T& t, typename char_type_of<T>::type c)
  92. {
  93. typedef typename char_type_of<T>::type char_type;
  94. typedef BOOST_IOSTREAMS_CHAR_TRAITS(char_type) traits_type;
  95. return !traits_type::eq_int_type( t.rdbuf()->sputbackc(c),
  96. traits_type::eof() );
  97. }
  98. };
  99. template<>
  100. struct read_device_impl<streambuf_tag> {
  101. template<typename T>
  102. static typename int_type_of<T>::type
  103. get(T& t)
  104. { // gcc 2.95 needs namespace qualification for char_traits.
  105. typedef typename char_type_of<T>::type char_type;
  106. typedef iostreams::char_traits<char_type> traits_type;
  107. typename int_type_of<T>::type c;
  108. return !traits_type::is_eof(c = t.sbumpc()) ||
  109. detail::true_eof(t)
  110. ?
  111. c : traits_type::would_block();
  112. }
  113. template<typename T>
  114. static std::streamsize
  115. read(T& t, typename char_type_of<T>::type* s, std::streamsize n)
  116. {
  117. std::streamsize amt;
  118. return (amt = t.sgetn(s, n)) != 0 ?
  119. amt :
  120. detail::true_eof(t) ?
  121. -1 :
  122. 0;
  123. }
  124. template<typename T>
  125. static bool putback(T& t, typename char_type_of<T>::type c)
  126. { // gcc 2.95 needs namespace qualification for char_traits.
  127. typedef typename char_type_of<T>::type char_type;
  128. typedef iostreams::char_traits<char_type> traits_type;
  129. return !traits_type::is_eof(t.sputbackc(c));
  130. }
  131. };
  132. template<>
  133. struct read_device_impl<input> {
  134. template<typename T>
  135. static typename int_type_of<T>::type
  136. get(T& t)
  137. { // gcc 2.95 needs namespace qualification for char_traits.
  138. typedef typename char_type_of<T>::type char_type;
  139. typedef iostreams::char_traits<char_type> traits_type;
  140. char_type c;
  141. std::streamsize amt;
  142. return (amt = t.read(&c, 1)) == 1 ?
  143. traits_type::to_int_type(c) :
  144. amt == -1 ?
  145. traits_type::eof() :
  146. traits_type::would_block();
  147. }
  148. template<typename T>
  149. static std::streamsize
  150. read(T& t, typename char_type_of<T>::type* s, std::streamsize n)
  151. { return t.read(s, n); }
  152. template<typename T>
  153. static bool putback(T& t, typename char_type_of<T>::type c)
  154. { // T must be Peekable.
  155. return t.putback(c);
  156. }
  157. };
  158. //------------------Definition of read_filter_impl----------------------------//
  159. template<typename T>
  160. struct read_filter_impl
  161. : mpl::if_<
  162. detail::is_custom<T>,
  163. operations<T>,
  164. read_filter_impl<
  165. BOOST_DEDUCED_TYPENAME
  166. detail::dispatch<
  167. T, multichar_tag, any_tag
  168. >::type
  169. >
  170. >::type
  171. { };
  172. template<>
  173. struct read_filter_impl<multichar_tag> {
  174. template<typename T, typename Source>
  175. static std::streamsize read
  176. (T& t, Source& src, typename char_type_of<T>::type* s, std::streamsize n)
  177. { return t.read(src, s, n); }
  178. };
  179. template<>
  180. struct read_filter_impl<any_tag> {
  181. template<typename T, typename Source>
  182. static std::streamsize read
  183. (T& t, Source& src, typename char_type_of<T>::type* s, std::streamsize n)
  184. { // gcc 2.95 needs namespace qualification for char_traits.
  185. typedef typename char_type_of<T>::type char_type;
  186. typedef iostreams::char_traits<char_type> traits_type;
  187. for (std::streamsize off = 0; off < n; ++off) {
  188. typename traits_type::int_type c = t.get(src);
  189. if (traits_type::is_eof(c))
  190. return check_eof(off);
  191. if (traits_type::would_block(c))
  192. return off;
  193. s[off] = traits_type::to_char_type(c);
  194. }
  195. return n;
  196. }
  197. };
  198. } // End namespace detail.
  199. } } // End namespaces iostreams, boost.
  200. #include <boost/iostreams/detail/config/enable_warnings.hpp>
  201. #endif // #ifndef BOOST_IOSTREAMS_READ_HPP_INCLUDED