| 1 | // (C) Copyright Jonathan Turkanis 2003. | 
|---|
| 2 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | 
|---|
| 3 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.) | 
|---|
| 4 |  | 
|---|
| 5 | // See http://www.boost.org/libs/iostreams for documentation. | 
|---|
| 6 |  | 
|---|
| 7 | // To do: handle bidirection streams and output-seekable components. | 
|---|
| 8 |  | 
|---|
| 9 | #ifndef BOOST_IOSTREAMS_SKIP_HPP_INCLUDED | 
|---|
| 10 | #define BOOST_IOSTREAMS_SKIP_HPP_INCLUDED | 
|---|
| 11 |  | 
|---|
| 12 | #if defined(_MSC_VER) && (_MSC_VER >= 1020) | 
|---|
| 13 | # pragma once | 
|---|
| 14 | #endif | 
|---|
| 15 |  | 
|---|
| 16 | #include <boost/iostreams/char_traits.hpp> | 
|---|
| 17 | #include <boost/iostreams/detail/ios.hpp>  // failure. | 
|---|
| 18 | #include <boost/iostreams/operations.hpp> | 
|---|
| 19 | #include <boost/iostreams/traits.hpp> | 
|---|
| 20 | #include <boost/mpl/and.hpp> | 
|---|
| 21 | #include <boost/mpl/bool.hpp> | 
|---|
| 22 | #include <boost/type_traits/is_convertible.hpp> | 
|---|
| 23 |  | 
|---|
| 24 | namespace boost { namespace iostreams { | 
|---|
| 25 |  | 
|---|
| 26 | namespace detail { | 
|---|
| 27 |  | 
|---|
| 28 | template<typename Device> | 
|---|
| 29 | void skip(Device& dev, stream_offset off, mpl::true_) | 
|---|
| 30 | { iostreams::seek(dev, off, BOOST_IOS::cur); } | 
|---|
| 31 |  | 
|---|
| 32 | template<typename Device> | 
|---|
| 33 | void skip(Device& dev, stream_offset off, mpl::false_) | 
|---|
| 34 | {   // gcc 2.95 needs namespace qualification for char_traits. | 
|---|
| 35 |     typedef typename char_type_of<Device>::type  char_type; | 
|---|
| 36 |     typedef iostreams::char_traits<char_type>    traits_type; | 
|---|
| 37 |     for (stream_offset z = 0; z < off; ) { | 
|---|
| 38 |         typename traits_type::int_type c; | 
|---|
| 39 |         if (traits_type::is_eof(c = iostreams::get(dev))) | 
|---|
| 40 |             throw BOOST_IOSTREAMS_FAILURE("bad skip offset"); | 
|---|
| 41 |         if (!traits_type::would_block(c)) | 
|---|
| 42 |             ++z; | 
|---|
| 43 |     } | 
|---|
| 44 | } | 
|---|
| 45 |  | 
|---|
| 46 | template<typename Filter, typename Device> | 
|---|
| 47 | void skip(Filter& flt, Device& dev, stream_offset off, mpl::true_) | 
|---|
| 48 | { flt.seek(dev, off, BOOST_IOS::cur); } | 
|---|
| 49 |  | 
|---|
| 50 | template<typename Filter, typename Device> | 
|---|
| 51 | void skip(Filter& flt, Device& dev, stream_offset off, mpl::false_) | 
|---|
| 52 | {  | 
|---|
| 53 |     typedef typename char_type_of<Device>::type char_type; | 
|---|
| 54 |     char_type c; | 
|---|
| 55 |     for (stream_offset z = 0; z < off; ) { | 
|---|
| 56 |         std::streamsize amt; | 
|---|
| 57 |         if ((amt = iostreams::read(flt, dev, &c, 1)) == -1) | 
|---|
| 58 |             throw BOOST_IOSTREAMS_FAILURE("bad skip offset"); | 
|---|
| 59 |         if (amt == 1) | 
|---|
| 60 |             ++z; | 
|---|
| 61 |     } | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | } // End namespace detail. | 
|---|
| 65 |  | 
|---|
| 66 | template<typename Device> | 
|---|
| 67 | void skip(Device& dev, stream_offset off) | 
|---|
| 68 | {  | 
|---|
| 69 |     typedef typename mode_of<Device>::type mode; | 
|---|
| 70 |     detail::skip(dev, off, is_convertible<mode, seekable>()); | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | template<typename Filter, typename Device> | 
|---|
| 74 | void skip(Filter& flt, Device& dev, stream_offset off) | 
|---|
| 75 | {  | 
|---|
| 76 |     typedef typename mode_of<Filter>::type                     filter_mode; | 
|---|
| 77 |     typedef typename mode_of<Device>::type                     device_mode; | 
|---|
| 78 |     typedef mpl::and_< | 
|---|
| 79 |                 is_convertible<filter_mode, output_seekable>, | 
|---|
| 80 |                 is_convertible<device_mode, output_seekable> | 
|---|
| 81 |             >                                                  can_seek; | 
|---|
| 82 |     detail::skip(flt, dev, off, can_seek()); | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | } } // End namespaces iostreams, boost. | 
|---|
| 86 |  | 
|---|
| 87 | #endif // #ifndef BOOST_IOSTREAMS_SKIP_HPP_INCLUDED //------------------------// | 
|---|