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