fstream.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // boost/filesystem/fstream.hpp ------------------------------------------------------//
  2. // Copyright Beman Dawes 2002
  3. // Copyright Andrey Semashev 2021
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // See http://www.boost.org/LICENSE_1_0.txt
  6. // Library home page: http://www.boost.org/libs/filesystem
  7. //--------------------------------------------------------------------------------------//
  8. #ifndef BOOST_FILESYSTEM_FSTREAM_HPP
  9. #define BOOST_FILESYSTEM_FSTREAM_HPP
  10. #include <boost/filesystem/config.hpp>
  11. #include <boost/filesystem/path.hpp>
  12. #include <iosfwd>
  13. #include <fstream>
  14. #include <boost/filesystem/detail/header.hpp> // must be the last #include
  15. #if defined(BOOST_WINDOWS_API)
  16. // On Windows, except for standard libaries known to have wchar_t overloads for
  17. // file stream I/O, use path::string() to get a narrow character c_str()
  18. #if (defined(_CPPLIB_VER) && _CPPLIB_VER >= 405 && !defined(_STLPORT_VERSION)) || \
  19. (defined(_LIBCPP_VERSION) && _LIBCPP_VERSION >= 7000 && defined(_LIBCPP_HAS_OPEN_WITH_WCHAR))
  20. // Use wide characters directly
  21. // Note: We don't use C++17 std::filesystem::path as a means to pass wide paths
  22. // to file streams because of various problems:
  23. // - std::filesystem is available in gcc 8 but it is broken there (fails to compile path definition
  24. // on Windows). Compilation errors seem to be fixed since gcc 9.
  25. // - In gcc 10.2 and clang 8.0.1 on Cygwin64, the path attempts to convert the wide string to narrow
  26. // and fails in runtime. This may be system locale dependent, and performing character code conversion
  27. // is against the purpose of using std::filesystem::path anyway.
  28. // - Other std::filesystem implementations were not tested, so it is not known if they actually work
  29. // with wide paths.
  30. #define BOOST_FILESYSTEM_C_STR(p) p.c_str()
  31. #else
  32. // Use narrow characters, since wide not available
  33. #define BOOST_FILESYSTEM_C_STR(p) p.string().c_str()
  34. #endif
  35. #endif // defined(BOOST_WINDOWS_API)
  36. #if !defined(BOOST_FILESYSTEM_C_STR)
  37. #define BOOST_FILESYSTEM_C_STR(p) p.c_str()
  38. #endif
  39. #if defined(BOOST_MSVC)
  40. #pragma warning(push)
  41. // 'boost::filesystem::basic_fstream<charT>' : inherits 'std::basic_istream<_Elem,_Traits>::std::basic_istream<_Elem,_Traits>::_Add_vtordisp1' via dominance
  42. #pragma warning(disable : 4250)
  43. #endif
  44. namespace boost {
  45. namespace filesystem {
  46. //--------------------------------------------------------------------------------------//
  47. // basic_filebuf //
  48. //--------------------------------------------------------------------------------------//
  49. template< class charT, class traits = std::char_traits< charT > >
  50. class basic_filebuf :
  51. public std::basic_filebuf< charT, traits >
  52. {
  53. public:
  54. BOOST_DEFAULTED_FUNCTION(basic_filebuf(), {})
  55. BOOST_DELETED_FUNCTION(basic_filebuf(basic_filebuf const&))
  56. BOOST_DELETED_FUNCTION(basic_filebuf const& operator=(basic_filebuf const&))
  57. public:
  58. basic_filebuf< charT, traits >* open(path const& p, std::ios_base::openmode mode)
  59. {
  60. return std::basic_filebuf< charT, traits >::open(BOOST_FILESYSTEM_C_STR(p), mode) ? this : 0;
  61. }
  62. };
  63. //--------------------------------------------------------------------------------------//
  64. // basic_ifstream //
  65. //--------------------------------------------------------------------------------------//
  66. template< class charT, class traits = std::char_traits< charT > >
  67. class basic_ifstream :
  68. public std::basic_ifstream< charT, traits >
  69. {
  70. public:
  71. BOOST_DEFAULTED_FUNCTION(basic_ifstream(), {})
  72. // use two signatures, rather than one signature with default second
  73. // argument, to workaround VC++ 7.1 bug (ID VSWhidbey 38416)
  74. explicit basic_ifstream(path const& p) :
  75. std::basic_ifstream< charT, traits >(BOOST_FILESYSTEM_C_STR(p), std::ios_base::in) {}
  76. basic_ifstream(path const& p, std::ios_base::openmode mode) :
  77. std::basic_ifstream< charT, traits >(BOOST_FILESYSTEM_C_STR(p), mode) {}
  78. BOOST_DELETED_FUNCTION(basic_ifstream(basic_ifstream const&))
  79. BOOST_DELETED_FUNCTION(basic_ifstream const& operator=(basic_ifstream const&))
  80. public:
  81. void open(path const& p)
  82. {
  83. std::basic_ifstream< charT, traits >::open(BOOST_FILESYSTEM_C_STR(p), std::ios_base::in);
  84. }
  85. void open(path const& p, std::ios_base::openmode mode)
  86. {
  87. std::basic_ifstream< charT, traits >::open(BOOST_FILESYSTEM_C_STR(p), mode);
  88. }
  89. };
  90. //--------------------------------------------------------------------------------------//
  91. // basic_ofstream //
  92. //--------------------------------------------------------------------------------------//
  93. template< class charT, class traits = std::char_traits< charT > >
  94. class basic_ofstream :
  95. public std::basic_ofstream< charT, traits >
  96. {
  97. public:
  98. BOOST_DEFAULTED_FUNCTION(basic_ofstream(), {})
  99. // use two signatures, rather than one signature with default second
  100. // argument, to workaround VC++ 7.1 bug (ID VSWhidbey 38416)
  101. explicit basic_ofstream(path const& p) :
  102. std::basic_ofstream< charT, traits >(BOOST_FILESYSTEM_C_STR(p), std::ios_base::out) {}
  103. basic_ofstream(path const& p, std::ios_base::openmode mode) :
  104. std::basic_ofstream< charT, traits >(BOOST_FILESYSTEM_C_STR(p), mode) {}
  105. BOOST_DELETED_FUNCTION(basic_ofstream(basic_ofstream const&))
  106. BOOST_DELETED_FUNCTION(basic_ofstream const& operator=(basic_ofstream const&))
  107. public:
  108. void open(path const& p)
  109. {
  110. std::basic_ofstream< charT, traits >::open(BOOST_FILESYSTEM_C_STR(p), std::ios_base::out);
  111. }
  112. void open(path const& p, std::ios_base::openmode mode)
  113. {
  114. std::basic_ofstream< charT, traits >::open(BOOST_FILESYSTEM_C_STR(p), mode);
  115. }
  116. };
  117. //--------------------------------------------------------------------------------------//
  118. // basic_fstream //
  119. //--------------------------------------------------------------------------------------//
  120. template< class charT, class traits = std::char_traits< charT > >
  121. class basic_fstream :
  122. public std::basic_fstream< charT, traits >
  123. {
  124. public:
  125. BOOST_DEFAULTED_FUNCTION(basic_fstream(), {})
  126. // use two signatures, rather than one signature with default second
  127. // argument, to workaround VC++ 7.1 bug (ID VSWhidbey 38416)
  128. explicit basic_fstream(path const& p) :
  129. std::basic_fstream< charT, traits >(BOOST_FILESYSTEM_C_STR(p), std::ios_base::in | std::ios_base::out) {}
  130. basic_fstream(path const& p, std::ios_base::openmode mode) :
  131. std::basic_fstream< charT, traits >(BOOST_FILESYSTEM_C_STR(p), mode) {}
  132. BOOST_DELETED_FUNCTION(basic_fstream(basic_fstream const&))
  133. BOOST_DELETED_FUNCTION(basic_fstream const& operator=(basic_fstream const&))
  134. public:
  135. void open(path const& p)
  136. {
  137. std::basic_fstream< charT, traits >::open(BOOST_FILESYSTEM_C_STR(p), std::ios_base::in | std::ios_base::out);
  138. }
  139. void open(path const& p, std::ios_base::openmode mode)
  140. {
  141. std::basic_fstream< charT, traits >::open(BOOST_FILESYSTEM_C_STR(p), mode);
  142. }
  143. };
  144. //--------------------------------------------------------------------------------------//
  145. // typedefs //
  146. //--------------------------------------------------------------------------------------//
  147. typedef basic_filebuf< char > filebuf;
  148. typedef basic_ifstream< char > ifstream;
  149. typedef basic_ofstream< char > ofstream;
  150. typedef basic_fstream< char > fstream;
  151. typedef basic_filebuf< wchar_t > wfilebuf;
  152. typedef basic_ifstream< wchar_t > wifstream;
  153. typedef basic_ofstream< wchar_t > wofstream;
  154. typedef basic_fstream< wchar_t > wfstream;
  155. } // namespace filesystem
  156. } // namespace boost
  157. #if defined(BOOST_MSVC)
  158. #pragma warning(pop)
  159. #endif
  160. #include <boost/filesystem/detail/footer.hpp>
  161. #endif // BOOST_FILESYSTEM_FSTREAM_HPP