fstream.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // boost/filesystem/fstream.hpp ------------------------------------------------------//
  2. // Copyright Beman Dawes 2002
  3. // Copyright Andrey Semashev 2021-2023
  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<Char>' : 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 Char, class Traits = std::char_traits< Char > >
  50. class basic_filebuf :
  51. public std::basic_filebuf< Char, Traits >
  52. {
  53. private:
  54. typedef std::basic_filebuf< Char, Traits > base_type;
  55. public:
  56. basic_filebuf() = default;
  57. #if !defined(BOOST_FILESYSTEM_DETAIL_NO_CXX11_MOVABLE_FSTREAMS)
  58. basic_filebuf(basic_filebuf&&) = default;
  59. basic_filebuf& operator= (basic_filebuf&&) = default;
  60. #endif // !defined(BOOST_FILESYSTEM_DETAIL_NO_CXX11_MOVABLE_FSTREAMS)
  61. basic_filebuf(basic_filebuf const&) = delete;
  62. basic_filebuf const& operator= (basic_filebuf const&) = delete;
  63. public:
  64. basic_filebuf* open(path const& p, std::ios_base::openmode mode)
  65. {
  66. return base_type::open(BOOST_FILESYSTEM_C_STR(p), mode) ? this : nullptr;
  67. }
  68. };
  69. //--------------------------------------------------------------------------------------//
  70. // basic_ifstream //
  71. //--------------------------------------------------------------------------------------//
  72. template< class Char, class Traits = std::char_traits< Char > >
  73. class basic_ifstream :
  74. public std::basic_ifstream< Char, Traits >
  75. {
  76. private:
  77. typedef std::basic_ifstream< Char, Traits > base_type;
  78. public:
  79. basic_ifstream() = default;
  80. // use two signatures, rather than one signature with default second
  81. // argument, to workaround VC++ 7.1 bug (ID VSWhidbey 38416)
  82. explicit basic_ifstream(path const& p) :
  83. base_type(BOOST_FILESYSTEM_C_STR(p), std::ios_base::in) {}
  84. basic_ifstream(path const& p, std::ios_base::openmode mode) :
  85. base_type(BOOST_FILESYSTEM_C_STR(p), mode) {}
  86. #if !defined(BOOST_FILESYSTEM_DETAIL_NO_CXX11_MOVABLE_FSTREAMS)
  87. basic_ifstream(basic_ifstream&& that) :
  88. base_type(static_cast< base_type&& >(that)) {}
  89. basic_ifstream& operator= (basic_ifstream&& that)
  90. {
  91. *static_cast< base_type* >(this) = static_cast< base_type&& >(that);
  92. return *this;
  93. }
  94. #endif
  95. basic_ifstream(basic_ifstream const&) = delete;
  96. basic_ifstream const& operator= (basic_ifstream const&) = delete;
  97. public:
  98. void open(path const& p)
  99. {
  100. base_type::open(BOOST_FILESYSTEM_C_STR(p), std::ios_base::in);
  101. }
  102. void open(path const& p, std::ios_base::openmode mode)
  103. {
  104. base_type::open(BOOST_FILESYSTEM_C_STR(p), mode);
  105. }
  106. };
  107. //--------------------------------------------------------------------------------------//
  108. // basic_ofstream //
  109. //--------------------------------------------------------------------------------------//
  110. template< class Char, class Traits = std::char_traits< Char > >
  111. class basic_ofstream :
  112. public std::basic_ofstream< Char, Traits >
  113. {
  114. private:
  115. typedef std::basic_ofstream< Char, Traits > base_type;
  116. public:
  117. basic_ofstream() = default;
  118. // use two signatures, rather than one signature with default second
  119. // argument, to workaround VC++ 7.1 bug (ID VSWhidbey 38416)
  120. explicit basic_ofstream(path const& p) :
  121. base_type(BOOST_FILESYSTEM_C_STR(p), std::ios_base::out) {}
  122. basic_ofstream(path const& p, std::ios_base::openmode mode) :
  123. base_type(BOOST_FILESYSTEM_C_STR(p), mode) {}
  124. #if !defined(BOOST_FILESYSTEM_DETAIL_NO_CXX11_MOVABLE_FSTREAMS)
  125. basic_ofstream(basic_ofstream&& that) :
  126. base_type(static_cast< base_type&& >(that)) {}
  127. basic_ofstream& operator= (basic_ofstream&& that)
  128. {
  129. *static_cast< base_type* >(this) = static_cast< base_type&& >(that);
  130. return *this;
  131. }
  132. #endif
  133. basic_ofstream(basic_ofstream const&) = delete;
  134. basic_ofstream const& operator= (basic_ofstream const&) = delete;
  135. public:
  136. void open(path const& p)
  137. {
  138. base_type::open(BOOST_FILESYSTEM_C_STR(p), std::ios_base::out);
  139. }
  140. void open(path const& p, std::ios_base::openmode mode)
  141. {
  142. base_type::open(BOOST_FILESYSTEM_C_STR(p), mode);
  143. }
  144. };
  145. //--------------------------------------------------------------------------------------//
  146. // basic_fstream //
  147. //--------------------------------------------------------------------------------------//
  148. template< class Char, class Traits = std::char_traits< Char > >
  149. class basic_fstream :
  150. public std::basic_fstream< Char, Traits >
  151. {
  152. private:
  153. typedef std::basic_fstream< Char, Traits > base_type;
  154. public:
  155. basic_fstream() = default;
  156. // use two signatures, rather than one signature with default second
  157. // argument, to workaround VC++ 7.1 bug (ID VSWhidbey 38416)
  158. explicit basic_fstream(path const& p) :
  159. base_type(BOOST_FILESYSTEM_C_STR(p), std::ios_base::in | std::ios_base::out) {}
  160. basic_fstream(path const& p, std::ios_base::openmode mode) :
  161. base_type(BOOST_FILESYSTEM_C_STR(p), mode) {}
  162. #if !defined(BOOST_FILESYSTEM_DETAIL_NO_CXX11_MOVABLE_FSTREAMS)
  163. basic_fstream(basic_fstream&& that) :
  164. base_type(static_cast< base_type&& >(that)) {}
  165. basic_fstream& operator= (basic_fstream&& that)
  166. {
  167. *static_cast< base_type* >(this) = static_cast< base_type&& >(that);
  168. return *this;
  169. }
  170. #endif
  171. basic_fstream(basic_fstream const&) = delete;
  172. basic_fstream const& operator= (basic_fstream const&) = delete;
  173. public:
  174. void open(path const& p)
  175. {
  176. base_type::open(BOOST_FILESYSTEM_C_STR(p), std::ios_base::in | std::ios_base::out);
  177. }
  178. void open(path const& p, std::ios_base::openmode mode)
  179. {
  180. base_type::open(BOOST_FILESYSTEM_C_STR(p), mode);
  181. }
  182. };
  183. //--------------------------------------------------------------------------------------//
  184. // typedefs //
  185. //--------------------------------------------------------------------------------------//
  186. typedef basic_filebuf< char > filebuf;
  187. typedef basic_ifstream< char > ifstream;
  188. typedef basic_ofstream< char > ofstream;
  189. typedef basic_fstream< char > fstream;
  190. typedef basic_filebuf< wchar_t > wfilebuf;
  191. typedef basic_ifstream< wchar_t > wifstream;
  192. typedef basic_ofstream< wchar_t > wofstream;
  193. typedef basic_fstream< wchar_t > wfstream;
  194. } // namespace filesystem
  195. } // namespace boost
  196. #if defined(BOOST_MSVC)
  197. #pragma warning(pop)
  198. #endif
  199. #include <boost/filesystem/detail/footer.hpp>
  200. #endif // BOOST_FILESYSTEM_FSTREAM_HPP