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