skip.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. // See http://www.boost.org/libs/iostreams for documentation.
  6. // To do: handle bidirection streams and output-seekable components.
  7. #ifndef BOOST_IOSTREAMS_SKIP_HPP_INCLUDED
  8. #define BOOST_IOSTREAMS_SKIP_HPP_INCLUDED
  9. #if defined(_MSC_VER)
  10. # pragma once
  11. #endif
  12. #include <boost/iostreams/char_traits.hpp>
  13. #include <boost/iostreams/detail/ios.hpp> // failure.
  14. #include <boost/iostreams/operations.hpp>
  15. #include <boost/iostreams/seek.hpp>
  16. #include <boost/iostreams/traits.hpp>
  17. #include <boost/mpl/and.hpp>
  18. #include <boost/mpl/bool.hpp>
  19. #include <boost/mpl/or.hpp>
  20. #include <boost/throw_exception.hpp>
  21. #include <boost/type_traits/is_convertible.hpp>
  22. namespace boost { namespace iostreams {
  23. namespace detail {
  24. template<typename Device>
  25. void skip(Device& dev, stream_offset off, mpl::true_)
  26. { iostreams::seek(dev, off, BOOST_IOS::cur); }
  27. template<typename Device>
  28. void skip(Device& dev, stream_offset off, mpl::false_)
  29. { // gcc 2.95 needs namespace qualification for char_traits.
  30. typedef typename char_type_of<Device>::type char_type;
  31. typedef iostreams::char_traits<char_type> traits_type;
  32. for (stream_offset z = 0; z < off; ) {
  33. typename traits_type::int_type c;
  34. if (traits_type::is_eof(c = iostreams::get(dev)))
  35. boost::throw_exception(BOOST_IOSTREAMS_FAILURE("bad skip offset"));
  36. if (!traits_type::would_block(c))
  37. ++z;
  38. }
  39. }
  40. template<typename Filter, typename Device>
  41. void skip( Filter& flt, Device& dev, stream_offset off,
  42. BOOST_IOS::openmode which, mpl::true_ )
  43. { boost::iostreams::seek(flt, dev, off, BOOST_IOS::cur, which); }
  44. template<typename Filter, typename Device>
  45. void skip( Filter& flt, Device& dev, stream_offset off,
  46. BOOST_IOS::openmode, mpl::false_ )
  47. {
  48. typedef typename char_type_of<Device>::type char_type;
  49. char_type c;
  50. for (stream_offset z = 0; z < off; ) {
  51. std::streamsize amt;
  52. if ((amt = iostreams::read(flt, dev, &c, 1)) == -1)
  53. boost::throw_exception(BOOST_IOSTREAMS_FAILURE("bad skip offset"));
  54. if (amt == 1)
  55. ++z;
  56. }
  57. }
  58. } // End namespace detail.
  59. template<typename Device>
  60. void skip(Device& dev, stream_offset off)
  61. {
  62. typedef typename mode_of<Device>::type mode;
  63. typedef mpl::or_<
  64. is_convertible<mode, input_seekable>,
  65. is_convertible<mode, output_seekable>
  66. > can_seek;
  67. BOOST_STATIC_ASSERT(
  68. (can_seek::value || is_convertible<mode, input>::value)
  69. );
  70. detail::skip(dev, off, can_seek());
  71. }
  72. template<typename Filter, typename Device>
  73. void skip( Filter& flt, Device& dev, stream_offset off,
  74. BOOST_IOS::openmode which = BOOST_IOS::in | BOOST_IOS::out )
  75. {
  76. typedef typename mode_of<Filter>::type filter_mode;
  77. typedef typename mode_of<Device>::type device_mode;
  78. typedef mpl::or_<
  79. mpl::and_<
  80. is_convertible<filter_mode, input_seekable>,
  81. is_convertible<device_mode, input_seekable>
  82. >,
  83. mpl::and_<
  84. is_convertible<filter_mode, output_seekable>,
  85. is_convertible<device_mode, output_seekable>
  86. >
  87. > can_seek;
  88. BOOST_STATIC_ASSERT(
  89. ( can_seek::value ||
  90. (is_convertible<filter_mode, input>::value &&
  91. is_convertible<device_mode, input>::value) )
  92. );
  93. detail::skip(flt, dev, off, which, can_seek());
  94. }
  95. } } // End namespaces iostreams, boost.
  96. #endif // #ifndef BOOST_IOSTREAMS_SKIP_HPP_INCLUDED //------------------------//