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 
6 // See http://www.boost.org/libs/iostreams for documentation.
7 
8 // Thanks to Gareth Sylvester-Bradley for the Dinkumware versions of the
9 // positioning functions.
10 
11 #ifndef BOOST_IOSTREAMS_POSITIONING_HPP_INCLUDED
12 #define BOOST_IOSTREAMS_POSITIONING_HPP_INCLUDED
13 
14 #if defined(_MSC_VER)
15 # pragma once
16 #endif
17 
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.
24 
25 // Must come last.
26 #include <boost/iostreams/detail/config/disable_warnings.hpp>
27 
28 #ifdef BOOST_NO_STDC_NAMESPACE
29 namespace std { using ::fpos_t; }
30 #endif
31 
32 namespace boost { namespace iostreams {
33 
34 //------------------Definition of stream_offset-------------------------------//
35 
36 typedef boost::intmax_t stream_offset;
37 
38 //------------------Definition of stream_offset_to_streamoff------------------//
39 
stream_offset_to_streamoff(stream_offset off)40 inline std::streamoff stream_offset_to_streamoff(stream_offset off)
41 { return static_cast<stream_offset>(off); }
42 
43 //------------------Definition of offset_to_position--------------------------//
44 
45 # ifndef BOOST_IOSTREAMS_HAS_DINKUMWARE_FPOS
46 
offset_to_position(stream_offset off)47 inline std::streampos offset_to_position(stream_offset off) { return off; }
48 
49 # else // # ifndef BOOST_IOSTREAMS_HAS_DINKUMWARE_FPOS
50 
offset_to_position(stream_offset off)51 inline std::streampos offset_to_position(stream_offset off)
52 { return std::streampos(std::mbstate_t(), off); }
53 
54 # endif // # ifndef BOOST_IOSTREAMS_HAS_DINKUMWARE_FPOS
55 
56 //------------------Definition of position_to_offset--------------------------//
57 
58 // Hande custom pos_type's
59 template<typename PosType>
position_to_offset(PosType pos)60 inline stream_offset position_to_offset(PosType pos)
61 { return std::streamoff(pos); }
62 
63 # ifndef BOOST_IOSTREAMS_HAS_DINKUMWARE_FPOS
64 
position_to_offset(std::streampos pos)65 inline stream_offset position_to_offset(std::streampos pos) { return pos; }
66 
67 # else // # ifndef BOOST_IOSTREAMS_HAS_DINKUMWARE_FPOS
68 
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 BOOST_IOSTREAMS_FPOSOFF,
79 // which works correctly on platforms where std::fpos_t is an integral type and
80 // platforms where it is a struct
81 
82 // Converts a std::fpos_t to a stream_offset
fpos_t_to_offset(std::fpos_t pos)83 inline stream_offset fpos_t_to_offset(std::fpos_t pos)
84 {
85 #  if defined(_POSIX_) || (_INTEGRAL_MAX_BITS >= 64) || defined(__IBMCPP__)
86     return pos;
87 #  else
88     return BOOST_IOSTREAMS_FPOSOFF(pos);
89 #  endif
90 }
91 
92 // Extracts the member _Fpos from a std::fpos
streampos_to_fpos_t(std::streampos pos)93 inline std::fpos_t streampos_to_fpos_t(std::streampos pos)
94 {
95 #  if defined (_CPPLIB_VER) || defined(__IBMCPP__)
96     return pos.seekpos();
97 #  else
98     return pos.get_fpos_t();
99 #  endif
100 }
101 
position_to_offset(std::streampos pos)102 inline stream_offset position_to_offset(std::streampos pos)
103 {
104     return fpos_t_to_offset(streampos_to_fpos_t(pos)) +
105         static_cast<stream_offset>(
106             static_cast<std::streamoff>(pos) -
107             BOOST_IOSTREAMS_FPOSOFF(streampos_to_fpos_t(pos))
108         );
109 }
110 
111 # endif // # ifndef BOOST_IOSTREAMS_HAS_DINKUMWARE_FPOS
112 
113 } } // End namespaces iostreams, boost.
114 
115 #include <boost/iostreams/detail/config/enable_warnings.hpp>
116 
117 #endif // #ifndef BOOST_IOSTREAMS_POSITIONING_HPP_INCLUDED
118