positioning.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // See http://www.boost.org/libs/iostreams for documentation.
  6. // Thanks to Gareth Sylvester-Bradley for the Dinkumware versions of the
  7. // positioning functions.
  8. #ifndef BOOST_IOSTREAMS_POSITIONING_HPP_INCLUDED
  9. #define BOOST_IOSTREAMS_POSITIONING_HPP_INCLUDED
  10. #if defined(_MSC_VER)
  11. # pragma once
  12. #endif
  13. #include <boost/config.hpp>
  14. #include <boost/cstdint.hpp>
  15. #include <boost/integer_traits.hpp>
  16. #include <boost/iostreams/detail/config/codecvt.hpp> // mbstate_t.
  17. #include <boost/iostreams/detail/config/fpos.hpp>
  18. #include <boost/iostreams/detail/ios.hpp> // streamoff, streampos.
  19. // Must come last.
  20. #include <boost/iostreams/detail/config/disable_warnings.hpp>
  21. #ifdef BOOST_NO_STDC_NAMESPACE
  22. namespace std { using ::fpos_t; }
  23. #endif
  24. namespace boost { namespace iostreams {
  25. //------------------Definition of stream_offset-------------------------------//
  26. typedef boost::intmax_t stream_offset;
  27. //------------------Definition of stream_offset_to_streamoff------------------//
  28. inline std::streamoff stream_offset_to_streamoff(stream_offset off)
  29. { return static_cast<stream_offset>(off); }
  30. //------------------Definition of offset_to_position--------------------------//
  31. # ifndef BOOST_IOSTREAMS_HAS_DINKUMWARE_FPOS
  32. inline std::streampos offset_to_position(stream_offset off) { return off; }
  33. # else // # ifndef BOOST_IOSTREAMS_HAS_DINKUMWARE_FPOS
  34. inline std::streampos offset_to_position(stream_offset off)
  35. { return std::streampos(std::mbstate_t(), off); }
  36. # endif // # ifndef BOOST_IOSTREAMS_HAS_DINKUMWARE_FPOS
  37. //------------------Definition of position_to_offset--------------------------//
  38. // Hande custom pos_type's
  39. template<typename PosType>
  40. inline stream_offset position_to_offset(PosType pos)
  41. { return std::streamoff(pos); }
  42. # ifndef BOOST_IOSTREAMS_HAS_DINKUMWARE_FPOS
  43. inline stream_offset position_to_offset(std::streampos pos) { return pos; }
  44. # else // # ifndef BOOST_IOSTREAMS_HAS_DINKUMWARE_FPOS
  45. // In the Dinkumware standard library, a std::streampos consists of two stream
  46. // offsets -- _Fpos, of type std::fpos_t, and _Myoff, of type std::streamoff --
  47. // together with a conversion state. A std::streampos is converted to a
  48. // boost::iostreams::stream_offset by extracting the two stream offsets and
  49. // summing them. The value of _Fpos can be extracted using the implementation-
  50. // defined member functions seekpos() or get_fpos_t(), depending on the
  51. // Dinkumware version. The value of _Myoff cannot be extracted directly, but can
  52. // be calculated as the difference between the result of converting the
  53. // std::fpos to a std::streamoff and the result of converting the member _Fpos
  54. // to a long. The latter operation is accomplished with the macro BOOST_IOSTREAMS_FPOSOFF,
  55. // which works correctly on platforms where std::fpos_t is an integral type and
  56. // platforms where it is a struct
  57. // Converts a std::fpos_t to a stream_offset
  58. inline stream_offset fpos_t_to_offset(std::fpos_t pos)
  59. {
  60. # if defined(_POSIX_) || (_INTEGRAL_MAX_BITS >= 64) || defined(__IBMCPP__)
  61. return pos;
  62. # else
  63. return BOOST_IOSTREAMS_FPOSOFF(pos);
  64. # endif
  65. }
  66. // Extracts the member _Fpos from a std::fpos
  67. inline std::fpos_t streampos_to_fpos_t(std::streampos pos)
  68. {
  69. # if defined (_CPPLIB_VER) || defined(__IBMCPP__)
  70. return pos.seekpos();
  71. # else
  72. return pos.get_fpos_t();
  73. # endif
  74. }
  75. inline stream_offset position_to_offset(std::streampos pos)
  76. {
  77. return fpos_t_to_offset(streampos_to_fpos_t(pos)) +
  78. static_cast<stream_offset>(
  79. static_cast<std::streamoff>(pos) -
  80. BOOST_IOSTREAMS_FPOSOFF(streampos_to_fpos_t(pos))
  81. );
  82. }
  83. # endif // # ifndef BOOST_IOSTREAMS_HAS_DINKUMWARE_FPOS
  84. } } // End namespaces iostreams, boost.
  85. #include <boost/iostreams/detail/config/enable_warnings.hpp>
  86. #endif // #ifndef BOOST_IOSTREAMS_POSITIONING_HPP_INCLUDED