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