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 // Thanks to Gareth Sylvester-Bradley for the Dinkumware versions of the
9 // positioning functions.
11 #ifndef BOOST_IOSTREAMS_POSITIONING_HPP_INCLUDED
12 #define BOOST_IOSTREAMS_POSITIONING_HPP_INCLUDED
14 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
18 #include <boost/config.hpp>
19 #include <boost/cstdint.hpp>
20 #include <boost/integer_traits.hpp>
21 #include <boost/iostreams/detail/config/codecvt.hpp> // mbstate_t.
22 #include <boost/iostreams/detail/config/fpos.hpp>
23 #include <boost/iostreams/detail/ios.hpp> // streamoff, streampos.
26 #include <boost/iostreams/detail/config/disable_warnings.hpp>
28 #ifdef BOOST_NO_STDC_NAMESPACE
29 namespace std
{ using ::fpos_t; }
32 namespace boost
{ namespace iostreams
{
34 //------------------Definition of stream_offset-------------------------------//
36 typedef boost::intmax_t stream_offset
;
38 //------------------Definition of stream_offset_to_streamoff------------------//
40 inline std::streamoff
stream_offset_to_streamoff(stream_offset off
)
41 { return static_cast<stream_offset
>(off
); }
43 //------------------Definition of offset_to_position--------------------------//
45 # ifndef BOOST_IOSTREAMS_HAS_DINKUMWARE_FPOS
47 inline std::streampos
offset_to_position(stream_offset off
) { return off
; }
49 # else // # ifndef BOOST_IOSTREAMS_HAS_DINKUMWARE_FPOS
51 inline std::streampos
offset_to_position(stream_offset off
)
52 { return std::streampos(std::mbstate_t(), off
); }
54 # endif // # ifndef BOOST_IOSTREAMS_HAS_DINKUMWARE_FPOS
56 //------------------Definition of position_to_offset--------------------------//
58 // Hande custom pos_type's
59 template<typename PosType
>
60 inline stream_offset
position_to_offset(PosType pos
)
61 { return std::streamoff(pos
); }
63 # ifndef BOOST_IOSTREAMS_HAS_DINKUMWARE_FPOS
65 inline stream_offset
position_to_offset(std::streampos pos
) { return pos
; }
67 # else // # ifndef BOOST_IOSTREAMS_HAS_DINKUMWARE_FPOS
69 // In the Dinkumware standard library, a std::streampos consists of two stream
70 // offsets -- _Fpos, of type std::fpos_t, and _Myoff, of type std::streamoff --
71 // together with a conversion state. A std::streampos is converted to a
72 // boost::iostreams::stream_offset by extracting the two stream offsets and
73 // summing them. The value of _Fpos can be extracted using the implementation-
74 // defined member functions seekpos() or get_fpos_t(), depending on the
75 // Dinkumware version. The value of _Myoff cannot be extracted directly, but can
76 // be calculated as the difference between the result of converting the
77 // std::fpos to a std::streamoff and the result of converting the member _Fpos
78 // to a long. The latter operation is accomplished with the macro _FPOSOFF,
79 // which works correctly on platforms where std::fpos_t is an integral type and
80 // platforms where it is a struct
82 // Converts a std::fpos_t to a stream_offset
83 inline stream_offset
fpos_t_to_offset(std::fpos_t pos
)
85 # if defined(_POSIX_) || (_INTEGRAL_MAX_BITS >= 64) || defined(__IBMCPP__)
92 // Extracts the member _Fpos from a std::fpos
93 inline std::fpos_t streampos_to_fpos_t(std::streampos pos
)
95 # if defined (_CPPLIB_VER) || defined(__IBMCPP__)
98 return pos
.get_fpos_t();
102 inline stream_offset
position_to_offset(std::streampos pos
)
104 return fpos_t_to_offset(streampos_to_fpos_t(pos
)) +
105 static_cast<stream_offset
>(static_cast<std::streamoff
>(pos
)) -
106 static_cast<stream_offset
>(_FPOSOFF(streampos_to_fpos_t(pos
)));
109 # endif // # ifndef BOOST_IOSTREAMS_HAS_DINKUMWARE_FPOS
111 } } // End namespaces iostreams, boost.
113 #include <boost/iostreams/detail/config/enable_warnings.hpp>
115 #endif // #ifndef BOOST_IOSTREAMS_POSITIONING_HPP_INCLUDED