read.hpp 6.9 KB

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