Chris@16: // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com) Chris@16: // (C) Copyright 2003-2007 Jonathan Turkanis Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.) Chris@16: Chris@16: // See http://www.boost.org/libs/iostreams for documentation. Chris@16: Chris@16: // Thanks to Gareth Sylvester-Bradley for the Dinkumware versions of the Chris@16: // positioning functions. Chris@16: Chris@16: #ifndef BOOST_IOSTREAMS_POSITIONING_HPP_INCLUDED Chris@16: #define BOOST_IOSTREAMS_POSITIONING_HPP_INCLUDED Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1020) Chris@16: # pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include // mbstate_t. Chris@16: #include Chris@16: #include // streamoff, streampos. Chris@16: Chris@16: // Must come last. Chris@16: #include Chris@16: Chris@16: #ifdef BOOST_NO_STDC_NAMESPACE Chris@16: namespace std { using ::fpos_t; } Chris@16: #endif Chris@16: Chris@16: namespace boost { namespace iostreams { Chris@16: Chris@16: //------------------Definition of stream_offset-------------------------------// Chris@16: Chris@16: typedef boost::intmax_t stream_offset; Chris@16: Chris@16: //------------------Definition of stream_offset_to_streamoff------------------// Chris@16: Chris@16: inline std::streamoff stream_offset_to_streamoff(stream_offset off) Chris@16: { return static_cast(off); } Chris@16: Chris@16: //------------------Definition of offset_to_position--------------------------// Chris@16: Chris@16: # ifndef BOOST_IOSTREAMS_HAS_DINKUMWARE_FPOS Chris@16: Chris@16: inline std::streampos offset_to_position(stream_offset off) { return off; } Chris@16: Chris@16: # else // # ifndef BOOST_IOSTREAMS_HAS_DINKUMWARE_FPOS Chris@16: Chris@16: inline std::streampos offset_to_position(stream_offset off) Chris@16: { return std::streampos(std::mbstate_t(), off); } Chris@16: Chris@16: # endif // # ifndef BOOST_IOSTREAMS_HAS_DINKUMWARE_FPOS Chris@16: Chris@16: //------------------Definition of position_to_offset--------------------------// Chris@16: Chris@16: // Hande custom pos_type's Chris@16: template Chris@16: inline stream_offset position_to_offset(PosType pos) Chris@16: { return std::streamoff(pos); } Chris@16: Chris@16: # ifndef BOOST_IOSTREAMS_HAS_DINKUMWARE_FPOS Chris@16: Chris@16: inline stream_offset position_to_offset(std::streampos pos) { return pos; } Chris@16: Chris@16: # else // # ifndef BOOST_IOSTREAMS_HAS_DINKUMWARE_FPOS Chris@16: Chris@16: // In the Dinkumware standard library, a std::streampos consists of two stream Chris@16: // offsets -- _Fpos, of type std::fpos_t, and _Myoff, of type std::streamoff -- Chris@16: // together with a conversion state. A std::streampos is converted to a Chris@16: // boost::iostreams::stream_offset by extracting the two stream offsets and Chris@16: // summing them. The value of _Fpos can be extracted using the implementation- Chris@16: // defined member functions seekpos() or get_fpos_t(), depending on the Chris@16: // Dinkumware version. The value of _Myoff cannot be extracted directly, but can Chris@16: // be calculated as the difference between the result of converting the Chris@16: // std::fpos to a std::streamoff and the result of converting the member _Fpos Chris@16: // to a long. The latter operation is accomplished with the macro _FPOSOFF, Chris@16: // which works correctly on platforms where std::fpos_t is an integral type and Chris@16: // platforms where it is a struct Chris@16: Chris@16: // Converts a std::fpos_t to a stream_offset Chris@16: inline stream_offset fpos_t_to_offset(std::fpos_t pos) Chris@16: { Chris@16: # if defined(_POSIX_) || (_INTEGRAL_MAX_BITS >= 64) || defined(__IBMCPP__) Chris@16: return pos; Chris@16: # else Chris@16: return _FPOSOFF(pos); Chris@16: # endif Chris@16: } Chris@16: Chris@16: // Extracts the member _Fpos from a std::fpos Chris@16: inline std::fpos_t streampos_to_fpos_t(std::streampos pos) Chris@16: { Chris@16: # if defined (_CPPLIB_VER) || defined(__IBMCPP__) Chris@16: return pos.seekpos(); Chris@16: # else Chris@16: return pos.get_fpos_t(); Chris@16: # endif Chris@16: } Chris@16: Chris@16: inline stream_offset position_to_offset(std::streampos pos) Chris@16: { Chris@16: return fpos_t_to_offset(streampos_to_fpos_t(pos)) + Chris@16: static_cast( Chris@16: static_cast(pos) - Chris@16: _FPOSOFF(streampos_to_fpos_t(pos)) Chris@16: ); Chris@16: } Chris@16: Chris@16: # endif // # ifndef BOOST_IOSTREAMS_HAS_DINKUMWARE_FPOS Chris@16: Chris@16: } } // End namespaces iostreams, boost. Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // #ifndef BOOST_IOSTREAMS_POSITIONING_HPP_INCLUDED