Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===------------------------- fstream ------------------------------------===// |
| 3 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_FSTREAM |
| 11 | #define _LIBCPP_FSTREAM |
| 12 | |
| 13 | /* |
| 14 | fstream synopsis |
| 15 | |
| 16 | template <class charT, class traits = char_traits<charT> > |
| 17 | class basic_filebuf |
| 18 | : public basic_streambuf<charT, traits> |
| 19 | { |
| 20 | public: |
| 21 | typedef charT char_type; |
| 22 | typedef traits traits_type; |
| 23 | typedef typename traits_type::int_type int_type; |
| 24 | typedef typename traits_type::pos_type pos_type; |
| 25 | typedef typename traits_type::off_type off_type; |
| 26 | |
| 27 | // 27.9.1.2 Constructors/destructor: |
| 28 | basic_filebuf(); |
| 29 | basic_filebuf(basic_filebuf&& rhs); |
| 30 | virtual ~basic_filebuf(); |
| 31 | |
| 32 | // 27.9.1.3 Assign/swap: |
| 33 | basic_filebuf& operator=(basic_filebuf&& rhs); |
| 34 | void swap(basic_filebuf& rhs); |
| 35 | |
| 36 | // 27.9.1.4 Members: |
| 37 | bool is_open() const; |
| 38 | basic_filebuf* open(const char* s, ios_base::openmode mode); |
| 39 | basic_filebuf* open(const string& s, ios_base::openmode mode); |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 40 | basic_filebuf* open(const filesystem::path& p, ios_base::openmode mode); // C++17 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 41 | basic_filebuf* close(); |
| 42 | |
| 43 | protected: |
| 44 | // 27.9.1.5 Overridden virtual functions: |
| 45 | virtual streamsize showmanyc(); |
| 46 | virtual int_type underflow(); |
| 47 | virtual int_type uflow(); |
| 48 | virtual int_type pbackfail(int_type c = traits_type::eof()); |
| 49 | virtual int_type overflow (int_type c = traits_type::eof()); |
| 50 | virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* s, streamsize n); |
| 51 | virtual pos_type seekoff(off_type off, ios_base::seekdir way, |
| 52 | ios_base::openmode which = ios_base::in | ios_base::out); |
| 53 | virtual pos_type seekpos(pos_type sp, |
| 54 | ios_base::openmode which = ios_base::in | ios_base::out); |
| 55 | virtual int sync(); |
| 56 | virtual void imbue(const locale& loc); |
| 57 | }; |
| 58 | |
| 59 | template <class charT, class traits> |
| 60 | void |
| 61 | swap(basic_filebuf<charT, traits>& x, basic_filebuf<charT, traits>& y); |
| 62 | |
| 63 | typedef basic_filebuf<char> filebuf; |
| 64 | typedef basic_filebuf<wchar_t> wfilebuf; |
| 65 | |
| 66 | template <class charT, class traits = char_traits<charT> > |
| 67 | class basic_ifstream |
| 68 | : public basic_istream<charT,traits> |
| 69 | { |
| 70 | public: |
| 71 | typedef charT char_type; |
| 72 | typedef traits traits_type; |
| 73 | typedef typename traits_type::int_type int_type; |
| 74 | typedef typename traits_type::pos_type pos_type; |
| 75 | typedef typename traits_type::off_type off_type; |
| 76 | |
| 77 | basic_ifstream(); |
| 78 | explicit basic_ifstream(const char* s, ios_base::openmode mode = ios_base::in); |
| 79 | explicit basic_ifstream(const string& s, ios_base::openmode mode = ios_base::in); |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 80 | explicit basic_ifstream(const filesystem::path& p, |
| 81 | ios_base::openmode mode = ios_base::in); // C++17 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 82 | basic_ifstream(basic_ifstream&& rhs); |
| 83 | |
| 84 | basic_ifstream& operator=(basic_ifstream&& rhs); |
| 85 | void swap(basic_ifstream& rhs); |
| 86 | |
| 87 | basic_filebuf<char_type, traits_type>* rdbuf() const; |
| 88 | bool is_open() const; |
| 89 | void open(const char* s, ios_base::openmode mode = ios_base::in); |
| 90 | void open(const string& s, ios_base::openmode mode = ios_base::in); |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 91 | void open(const filesystem::path& s, ios_base::openmode mode = ios_base::in); // C++17 |
| 92 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 93 | void close(); |
| 94 | }; |
| 95 | |
| 96 | template <class charT, class traits> |
| 97 | void |
| 98 | swap(basic_ifstream<charT, traits>& x, basic_ifstream<charT, traits>& y); |
| 99 | |
| 100 | typedef basic_ifstream<char> ifstream; |
| 101 | typedef basic_ifstream<wchar_t> wifstream; |
| 102 | |
| 103 | template <class charT, class traits = char_traits<charT> > |
| 104 | class basic_ofstream |
| 105 | : public basic_ostream<charT,traits> |
| 106 | { |
| 107 | public: |
| 108 | typedef charT char_type; |
| 109 | typedef traits traits_type; |
| 110 | typedef typename traits_type::int_type int_type; |
| 111 | typedef typename traits_type::pos_type pos_type; |
| 112 | typedef typename traits_type::off_type off_type; |
| 113 | |
| 114 | basic_ofstream(); |
| 115 | explicit basic_ofstream(const char* s, ios_base::openmode mode = ios_base::out); |
| 116 | explicit basic_ofstream(const string& s, ios_base::openmode mode = ios_base::out); |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 117 | explicit basic_ofstream(const filesystem::path& p, |
| 118 | ios_base::openmode mode = ios_base::out); // C++17 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 119 | basic_ofstream(basic_ofstream&& rhs); |
| 120 | |
| 121 | basic_ofstream& operator=(basic_ofstream&& rhs); |
| 122 | void swap(basic_ofstream& rhs); |
| 123 | |
| 124 | basic_filebuf<char_type, traits_type>* rdbuf() const; |
| 125 | bool is_open() const; |
| 126 | void open(const char* s, ios_base::openmode mode = ios_base::out); |
| 127 | void open(const string& s, ios_base::openmode mode = ios_base::out); |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 128 | void open(const filesystem::path& p, |
| 129 | ios_base::openmode mode = ios_base::out); // C++17 |
| 130 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 131 | void close(); |
| 132 | }; |
| 133 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 134 | template <class charT, class traits> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 135 | void |
| 136 | swap(basic_ofstream<charT, traits>& x, basic_ofstream<charT, traits>& y); |
| 137 | |
| 138 | typedef basic_ofstream<char> ofstream; |
| 139 | typedef basic_ofstream<wchar_t> wofstream; |
| 140 | |
| 141 | template <class charT, class traits=char_traits<charT> > |
| 142 | class basic_fstream |
| 143 | : public basic_iostream<charT,traits> |
| 144 | { |
| 145 | public: |
| 146 | typedef charT char_type; |
| 147 | typedef traits traits_type; |
| 148 | typedef typename traits_type::int_type int_type; |
| 149 | typedef typename traits_type::pos_type pos_type; |
| 150 | typedef typename traits_type::off_type off_type; |
| 151 | |
| 152 | basic_fstream(); |
| 153 | explicit basic_fstream(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out); |
| 154 | explicit basic_fstream(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out); |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 155 | explicit basic_fstream(const filesystem::path& p, |
| 156 | ios_base::openmode mode = ios_base::in|ios_base::out); C++17 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 157 | basic_fstream(basic_fstream&& rhs); |
| 158 | |
| 159 | basic_fstream& operator=(basic_fstream&& rhs); |
| 160 | void swap(basic_fstream& rhs); |
| 161 | |
| 162 | basic_filebuf<char_type, traits_type>* rdbuf() const; |
| 163 | bool is_open() const; |
| 164 | void open(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out); |
| 165 | void open(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out); |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 166 | void open(const filesystem::path& s, |
| 167 | ios_base::openmode mode = ios_base::in|ios_base::out); // C++17 |
| 168 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 169 | void close(); |
| 170 | }; |
| 171 | |
| 172 | template <class charT, class traits> |
| 173 | void swap(basic_fstream<charT, traits>& x, basic_fstream<charT, traits>& y); |
| 174 | |
| 175 | typedef basic_fstream<char> fstream; |
| 176 | typedef basic_fstream<wchar_t> wfstream; |
| 177 | |
| 178 | } // std |
| 179 | |
| 180 | */ |
| 181 | |
| 182 | #include <__config> |
Louis Dionne | 73912b2 | 2020-11-04 15:01:25 -0500 | [diff] [blame] | 183 | #include <__availability> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 184 | #include <ostream> |
| 185 | #include <istream> |
| 186 | #include <__locale> |
| 187 | #include <cstdio> |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 188 | #include <cstdlib> |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 189 | #include <filesystem> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 190 | |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 191 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 192 | #pragma GCC system_header |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 193 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 194 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 195 | _LIBCPP_PUSH_MACROS |
| 196 | #include <__undef_macros> |
| 197 | |
| 198 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 199 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 200 | |
| 201 | template <class _CharT, class _Traits> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 202 | class _LIBCPP_TEMPLATE_VIS basic_filebuf |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 203 | : public basic_streambuf<_CharT, _Traits> |
| 204 | { |
| 205 | public: |
| 206 | typedef _CharT char_type; |
| 207 | typedef _Traits traits_type; |
| 208 | typedef typename traits_type::int_type int_type; |
| 209 | typedef typename traits_type::pos_type pos_type; |
| 210 | typedef typename traits_type::off_type off_type; |
| 211 | typedef typename traits_type::state_type state_type; |
| 212 | |
| 213 | // 27.9.1.2 Constructors/destructor: |
| 214 | basic_filebuf(); |
Eric Fiselier | 78ccf77 | 2017-04-18 23:38:41 +0000 | [diff] [blame] | 215 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 216 | basic_filebuf(basic_filebuf&& __rhs); |
| 217 | #endif |
| 218 | virtual ~basic_filebuf(); |
| 219 | |
| 220 | // 27.9.1.3 Assign/swap: |
Eric Fiselier | 78ccf77 | 2017-04-18 23:38:41 +0000 | [diff] [blame] | 221 | #ifndef _LIBCPP_CXX03_LANG |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 222 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 223 | basic_filebuf& operator=(basic_filebuf&& __rhs); |
| 224 | #endif |
| 225 | void swap(basic_filebuf& __rhs); |
| 226 | |
| 227 | // 27.9.1.4 Members: |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 228 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 229 | bool is_open() const; |
Ed Schouten | 7009f4e | 2015-03-12 15:44:39 +0000 | [diff] [blame] | 230 | #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 231 | basic_filebuf* open(const char* __s, ios_base::openmode __mode); |
Peter Collingbourne | 63ebbd7 | 2018-01-23 02:07:27 +0000 | [diff] [blame] | 232 | #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR |
| 233 | basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode); |
| 234 | #endif |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 235 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 236 | basic_filebuf* open(const string& __s, ios_base::openmode __mode); |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 237 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 238 | #if _LIBCPP_STD_VER >= 17 |
Louis Dionne | f5ba8aa | 2019-03-20 14:34:00 +0000 | [diff] [blame] | 239 | _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 240 | basic_filebuf* open(const _VSTD_FS::path& __p, ios_base::openmode __mode) { |
| 241 | return open(__p.c_str(), __mode); |
| 242 | } |
| 243 | #endif |
Louis Dionne | 8a79be6 | 2020-10-21 11:11:45 -0400 | [diff] [blame] | 244 | inline _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 245 | basic_filebuf* __open(int __fd, ios_base::openmode __mode); |
Ed Schouten | 7009f4e | 2015-03-12 15:44:39 +0000 | [diff] [blame] | 246 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 247 | basic_filebuf* close(); |
| 248 | |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 249 | _LIBCPP_INLINE_VISIBILITY |
| 250 | inline static const char* |
| 251 | __make_mdstring(ios_base::openmode __mode) _NOEXCEPT; |
| 252 | |
| 253 | protected: |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 254 | // 27.9.1.5 Overridden virtual functions: |
| 255 | virtual int_type underflow(); |
| 256 | virtual int_type pbackfail(int_type __c = traits_type::eof()); |
| 257 | virtual int_type overflow (int_type __c = traits_type::eof()); |
| 258 | virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n); |
| 259 | virtual pos_type seekoff(off_type __off, ios_base::seekdir __way, |
| 260 | ios_base::openmode __wch = ios_base::in | ios_base::out); |
| 261 | virtual pos_type seekpos(pos_type __sp, |
| 262 | ios_base::openmode __wch = ios_base::in | ios_base::out); |
| 263 | virtual int sync(); |
| 264 | virtual void imbue(const locale& __loc); |
| 265 | |
| 266 | private: |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 267 | char* __extbuf_; |
| 268 | const char* __extbufnext_; |
| 269 | const char* __extbufend_; |
| 270 | char __extbuf_min_[8]; |
| 271 | size_t __ebs_; |
| 272 | char_type* __intbuf_; |
| 273 | size_t __ibs_; |
| 274 | FILE* __file_; |
| 275 | const codecvt<char_type, char, state_type>* __cv_; |
| 276 | state_type __st_; |
| 277 | state_type __st_last_; |
| 278 | ios_base::openmode __om_; |
| 279 | ios_base::openmode __cm_; |
| 280 | bool __owns_eb_; |
| 281 | bool __owns_ib_; |
| 282 | bool __always_noconv_; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 283 | |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 284 | bool __read_mode(); |
| 285 | void __write_mode(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 286 | }; |
| 287 | |
| 288 | template <class _CharT, class _Traits> |
| 289 | basic_filebuf<_CharT, _Traits>::basic_filebuf() |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 290 | : __extbuf_(nullptr), |
| 291 | __extbufnext_(nullptr), |
| 292 | __extbufend_(nullptr), |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 293 | __ebs_(0), |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 294 | __intbuf_(nullptr), |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 295 | __ibs_(0), |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 296 | __file_(nullptr), |
Howard Hinnant | 0090b12 | 2012-08-24 16:52:47 +0000 | [diff] [blame] | 297 | __cv_(nullptr), |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 298 | __st_(), |
Howard Hinnant | 2f7c18b | 2012-08-24 21:20:56 +0000 | [diff] [blame] | 299 | __st_last_(), |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 300 | __om_(0), |
| 301 | __cm_(0), |
| 302 | __owns_eb_(false), |
| 303 | __owns_ib_(false), |
Howard Hinnant | 0090b12 | 2012-08-24 16:52:47 +0000 | [diff] [blame] | 304 | __always_noconv_(false) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 305 | { |
Howard Hinnant | 0090b12 | 2012-08-24 16:52:47 +0000 | [diff] [blame] | 306 | if (has_facet<codecvt<char_type, char, state_type> >(this->getloc())) |
| 307 | { |
| 308 | __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc()); |
| 309 | __always_noconv_ = __cv_->always_noconv(); |
| 310 | } |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 311 | setbuf(nullptr, 4096); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Eric Fiselier | 78ccf77 | 2017-04-18 23:38:41 +0000 | [diff] [blame] | 314 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 315 | |
| 316 | template <class _CharT, class _Traits> |
| 317 | basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs) |
| 318 | : basic_streambuf<_CharT, _Traits>(__rhs) |
| 319 | { |
| 320 | if (__rhs.__extbuf_ == __rhs.__extbuf_min_) |
| 321 | { |
| 322 | __extbuf_ = __extbuf_min_; |
| 323 | __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_); |
| 324 | __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_); |
| 325 | } |
| 326 | else |
| 327 | { |
| 328 | __extbuf_ = __rhs.__extbuf_; |
| 329 | __extbufnext_ = __rhs.__extbufnext_; |
| 330 | __extbufend_ = __rhs.__extbufend_; |
| 331 | } |
| 332 | __ebs_ = __rhs.__ebs_; |
| 333 | __intbuf_ = __rhs.__intbuf_; |
| 334 | __ibs_ = __rhs.__ibs_; |
| 335 | __file_ = __rhs.__file_; |
| 336 | __cv_ = __rhs.__cv_; |
| 337 | __st_ = __rhs.__st_; |
Howard Hinnant | 2f7c18b | 2012-08-24 21:20:56 +0000 | [diff] [blame] | 338 | __st_last_ = __rhs.__st_last_; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 339 | __om_ = __rhs.__om_; |
| 340 | __cm_ = __rhs.__cm_; |
| 341 | __owns_eb_ = __rhs.__owns_eb_; |
| 342 | __owns_ib_ = __rhs.__owns_ib_; |
| 343 | __always_noconv_ = __rhs.__always_noconv_; |
| 344 | if (__rhs.pbase()) |
| 345 | { |
| 346 | if (__rhs.pbase() == __rhs.__intbuf_) |
| 347 | this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase())); |
| 348 | else |
| 349 | this->setp((char_type*)__extbuf_, |
| 350 | (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase())); |
Marshall Clow | 3393262 | 2017-09-12 15:00:43 +0000 | [diff] [blame] | 351 | this->__pbump(__rhs. pptr() - __rhs.pbase()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 352 | } |
| 353 | else if (__rhs.eback()) |
| 354 | { |
| 355 | if (__rhs.eback() == __rhs.__intbuf_) |
| 356 | this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()), |
| 357 | __intbuf_ + (__rhs.egptr() - __rhs.eback())); |
| 358 | else |
| 359 | this->setg((char_type*)__extbuf_, |
| 360 | (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()), |
| 361 | (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback())); |
| 362 | } |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 363 | __rhs.__extbuf_ = nullptr; |
| 364 | __rhs.__extbufnext_ = nullptr; |
| 365 | __rhs.__extbufend_ = nullptr; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 366 | __rhs.__ebs_ = 0; |
| 367 | __rhs.__intbuf_ = 0; |
| 368 | __rhs.__ibs_ = 0; |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 369 | __rhs.__file_ = nullptr; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 370 | __rhs.__st_ = state_type(); |
Howard Hinnant | 2f7c18b | 2012-08-24 21:20:56 +0000 | [diff] [blame] | 371 | __rhs.__st_last_ = state_type(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 372 | __rhs.__om_ = 0; |
| 373 | __rhs.__cm_ = 0; |
| 374 | __rhs.__owns_eb_ = false; |
| 375 | __rhs.__owns_ib_ = false; |
| 376 | __rhs.setg(0, 0, 0); |
| 377 | __rhs.setp(0, 0); |
| 378 | } |
| 379 | |
| 380 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 381 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 382 | basic_filebuf<_CharT, _Traits>& |
| 383 | basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs) |
| 384 | { |
| 385 | close(); |
| 386 | swap(__rhs); |
Argyrios Kyrtzidis | af90465 | 2012-10-13 02:03:45 +0000 | [diff] [blame] | 387 | return *this; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Eric Fiselier | 78ccf77 | 2017-04-18 23:38:41 +0000 | [diff] [blame] | 390 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 391 | |
| 392 | template <class _CharT, class _Traits> |
| 393 | basic_filebuf<_CharT, _Traits>::~basic_filebuf() |
| 394 | { |
| 395 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 396 | try |
| 397 | { |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 398 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 399 | close(); |
| 400 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 401 | } |
| 402 | catch (...) |
| 403 | { |
| 404 | } |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 405 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 406 | if (__owns_eb_) |
| 407 | delete [] __extbuf_; |
| 408 | if (__owns_ib_) |
| 409 | delete [] __intbuf_; |
| 410 | } |
| 411 | |
| 412 | template <class _CharT, class _Traits> |
| 413 | void |
| 414 | basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs) |
| 415 | { |
| 416 | basic_streambuf<char_type, traits_type>::swap(__rhs); |
| 417 | if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_) |
| 418 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 419 | _VSTD::swap(__extbuf_, __rhs.__extbuf_); |
| 420 | _VSTD::swap(__extbufnext_, __rhs.__extbufnext_); |
| 421 | _VSTD::swap(__extbufend_, __rhs.__extbufend_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 422 | } |
| 423 | else |
| 424 | { |
| 425 | ptrdiff_t __ln = __extbufnext_ - __extbuf_; |
| 426 | ptrdiff_t __le = __extbufend_ - __extbuf_; |
| 427 | ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_; |
| 428 | ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_; |
| 429 | if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_) |
| 430 | { |
| 431 | __extbuf_ = __rhs.__extbuf_; |
| 432 | __rhs.__extbuf_ = __rhs.__extbuf_min_; |
| 433 | } |
| 434 | else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_) |
| 435 | { |
| 436 | __rhs.__extbuf_ = __extbuf_; |
| 437 | __extbuf_ = __extbuf_min_; |
| 438 | } |
| 439 | __extbufnext_ = __extbuf_ + __rn; |
| 440 | __extbufend_ = __extbuf_ + __re; |
| 441 | __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln; |
| 442 | __rhs.__extbufend_ = __rhs.__extbuf_ + __le; |
| 443 | } |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 444 | _VSTD::swap(__ebs_, __rhs.__ebs_); |
| 445 | _VSTD::swap(__intbuf_, __rhs.__intbuf_); |
| 446 | _VSTD::swap(__ibs_, __rhs.__ibs_); |
| 447 | _VSTD::swap(__file_, __rhs.__file_); |
| 448 | _VSTD::swap(__cv_, __rhs.__cv_); |
| 449 | _VSTD::swap(__st_, __rhs.__st_); |
Howard Hinnant | 2f7c18b | 2012-08-24 21:20:56 +0000 | [diff] [blame] | 450 | _VSTD::swap(__st_last_, __rhs.__st_last_); |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 451 | _VSTD::swap(__om_, __rhs.__om_); |
| 452 | _VSTD::swap(__cm_, __rhs.__cm_); |
| 453 | _VSTD::swap(__owns_eb_, __rhs.__owns_eb_); |
| 454 | _VSTD::swap(__owns_ib_, __rhs.__owns_ib_); |
| 455 | _VSTD::swap(__always_noconv_, __rhs.__always_noconv_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 456 | if (this->eback() == (char_type*)__rhs.__extbuf_min_) |
| 457 | { |
| 458 | ptrdiff_t __n = this->gptr() - this->eback(); |
| 459 | ptrdiff_t __e = this->egptr() - this->eback(); |
| 460 | this->setg((char_type*)__extbuf_min_, |
| 461 | (char_type*)__extbuf_min_ + __n, |
| 462 | (char_type*)__extbuf_min_ + __e); |
| 463 | } |
| 464 | else if (this->pbase() == (char_type*)__rhs.__extbuf_min_) |
| 465 | { |
| 466 | ptrdiff_t __n = this->pptr() - this->pbase(); |
| 467 | ptrdiff_t __e = this->epptr() - this->pbase(); |
| 468 | this->setp((char_type*)__extbuf_min_, |
| 469 | (char_type*)__extbuf_min_ + __e); |
Marshall Clow | 3393262 | 2017-09-12 15:00:43 +0000 | [diff] [blame] | 470 | this->__pbump(__n); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 471 | } |
| 472 | if (__rhs.eback() == (char_type*)__extbuf_min_) |
| 473 | { |
| 474 | ptrdiff_t __n = __rhs.gptr() - __rhs.eback(); |
| 475 | ptrdiff_t __e = __rhs.egptr() - __rhs.eback(); |
| 476 | __rhs.setg((char_type*)__rhs.__extbuf_min_, |
| 477 | (char_type*)__rhs.__extbuf_min_ + __n, |
| 478 | (char_type*)__rhs.__extbuf_min_ + __e); |
| 479 | } |
| 480 | else if (__rhs.pbase() == (char_type*)__extbuf_min_) |
| 481 | { |
| 482 | ptrdiff_t __n = __rhs.pptr() - __rhs.pbase(); |
| 483 | ptrdiff_t __e = __rhs.epptr() - __rhs.pbase(); |
| 484 | __rhs.setp((char_type*)__rhs.__extbuf_min_, |
| 485 | (char_type*)__rhs.__extbuf_min_ + __e); |
Marshall Clow | 3393262 | 2017-09-12 15:00:43 +0000 | [diff] [blame] | 486 | __rhs.__pbump(__n); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | |
| 490 | template <class _CharT, class _Traits> |
| 491 | inline _LIBCPP_INLINE_VISIBILITY |
| 492 | void |
| 493 | swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y) |
| 494 | { |
| 495 | __x.swap(__y); |
| 496 | } |
| 497 | |
| 498 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 499 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 500 | bool |
| 501 | basic_filebuf<_CharT, _Traits>::is_open() const |
| 502 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 503 | return __file_ != nullptr; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 506 | template <class _CharT, class _Traits> |
| 507 | const char* basic_filebuf<_CharT, _Traits>::__make_mdstring( |
| 508 | ios_base::openmode __mode) _NOEXCEPT { |
| 509 | switch (__mode & ~ios_base::ate) { |
| 510 | case ios_base::out: |
| 511 | case ios_base::out | ios_base::trunc: |
Dan Albert | 9550df6 | 2019-09-16 19:26:41 +0000 | [diff] [blame] | 512 | return "w" _LIBCPP_FOPEN_CLOEXEC_MODE; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 513 | case ios_base::out | ios_base::app: |
| 514 | case ios_base::app: |
Dan Albert | 9550df6 | 2019-09-16 19:26:41 +0000 | [diff] [blame] | 515 | return "a" _LIBCPP_FOPEN_CLOEXEC_MODE; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 516 | case ios_base::in: |
Dan Albert | 9550df6 | 2019-09-16 19:26:41 +0000 | [diff] [blame] | 517 | return "r" _LIBCPP_FOPEN_CLOEXEC_MODE; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 518 | case ios_base::in | ios_base::out: |
Dan Albert | 9550df6 | 2019-09-16 19:26:41 +0000 | [diff] [blame] | 519 | return "r+" _LIBCPP_FOPEN_CLOEXEC_MODE; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 520 | case ios_base::in | ios_base::out | ios_base::trunc: |
Dan Albert | 9550df6 | 2019-09-16 19:26:41 +0000 | [diff] [blame] | 521 | return "w+" _LIBCPP_FOPEN_CLOEXEC_MODE; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 522 | case ios_base::in | ios_base::out | ios_base::app: |
| 523 | case ios_base::in | ios_base::app: |
Dan Albert | 9550df6 | 2019-09-16 19:26:41 +0000 | [diff] [blame] | 524 | return "a+" _LIBCPP_FOPEN_CLOEXEC_MODE; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 525 | case ios_base::out | ios_base::binary: |
| 526 | case ios_base::out | ios_base::trunc | ios_base::binary: |
Dan Albert | 9550df6 | 2019-09-16 19:26:41 +0000 | [diff] [blame] | 527 | return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 528 | case ios_base::out | ios_base::app | ios_base::binary: |
| 529 | case ios_base::app | ios_base::binary: |
Dan Albert | 9550df6 | 2019-09-16 19:26:41 +0000 | [diff] [blame] | 530 | return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 531 | case ios_base::in | ios_base::binary: |
Dan Albert | 9550df6 | 2019-09-16 19:26:41 +0000 | [diff] [blame] | 532 | return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 533 | case ios_base::in | ios_base::out | ios_base::binary: |
Dan Albert | 9550df6 | 2019-09-16 19:26:41 +0000 | [diff] [blame] | 534 | return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 535 | case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary: |
Dan Albert | 9550df6 | 2019-09-16 19:26:41 +0000 | [diff] [blame] | 536 | return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 537 | case ios_base::in | ios_base::out | ios_base::app | ios_base::binary: |
| 538 | case ios_base::in | ios_base::app | ios_base::binary: |
Dan Albert | 9550df6 | 2019-09-16 19:26:41 +0000 | [diff] [blame] | 539 | return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 540 | default: |
| 541 | return nullptr; |
| 542 | } |
| 543 | _LIBCPP_UNREACHABLE(); |
| 544 | } |
| 545 | |
Ed Schouten | 7009f4e | 2015-03-12 15:44:39 +0000 | [diff] [blame] | 546 | #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 547 | template <class _CharT, class _Traits> |
| 548 | basic_filebuf<_CharT, _Traits>* |
| 549 | basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) |
| 550 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 551 | basic_filebuf<_CharT, _Traits>* __rt = nullptr; |
| 552 | if (__file_ == nullptr) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 553 | { |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 554 | if (const char* __mdstr = __make_mdstring(__mode)) { |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 555 | __rt = this; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 556 | __file_ = fopen(__s, __mdstr); |
| 557 | if (__file_) { |
| 558 | __om_ = __mode; |
| 559 | if (__mode & ios_base::ate) { |
| 560 | if (fseek(__file_, 0, SEEK_END)) { |
| 561 | fclose(__file_); |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 562 | __file_ = nullptr; |
| 563 | __rt = nullptr; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 564 | } |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 565 | } |
| 566 | } else |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 567 | __rt = nullptr; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 568 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 569 | } |
| 570 | return __rt; |
| 571 | } |
| 572 | |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 573 | template <class _CharT, class _Traits> |
Louis Dionne | 8a79be6 | 2020-10-21 11:11:45 -0400 | [diff] [blame] | 574 | inline _LIBCPP_INLINE_VISIBILITY |
| 575 | basic_filebuf<_CharT, _Traits>* |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 576 | basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 577 | basic_filebuf<_CharT, _Traits>* __rt = nullptr; |
| 578 | if (__file_ == nullptr) { |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 579 | if (const char* __mdstr = __make_mdstring(__mode)) { |
| 580 | __rt = this; |
| 581 | __file_ = fdopen(__fd, __mdstr); |
| 582 | if (__file_) { |
| 583 | __om_ = __mode; |
| 584 | if (__mode & ios_base::ate) { |
| 585 | if (fseek(__file_, 0, SEEK_END)) { |
| 586 | fclose(__file_); |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 587 | __file_ = nullptr; |
| 588 | __rt = nullptr; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | } else |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 592 | __rt = nullptr; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | return __rt; |
| 596 | } |
| 597 | |
Peter Collingbourne | 63ebbd7 | 2018-01-23 02:07:27 +0000 | [diff] [blame] | 598 | #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR |
| 599 | // This is basically the same as the char* overload except that it uses _wfopen |
| 600 | // and long mode strings. |
| 601 | template <class _CharT, class _Traits> |
| 602 | basic_filebuf<_CharT, _Traits>* |
| 603 | basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) |
| 604 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 605 | basic_filebuf<_CharT, _Traits>* __rt = nullptr; |
| 606 | if (__file_ == nullptr) |
Peter Collingbourne | 63ebbd7 | 2018-01-23 02:07:27 +0000 | [diff] [blame] | 607 | { |
| 608 | __rt = this; |
| 609 | const wchar_t* __mdstr; |
| 610 | switch (__mode & ~ios_base::ate) |
| 611 | { |
| 612 | case ios_base::out: |
| 613 | case ios_base::out | ios_base::trunc: |
| 614 | __mdstr = L"w"; |
| 615 | break; |
| 616 | case ios_base::out | ios_base::app: |
| 617 | case ios_base::app: |
| 618 | __mdstr = L"a"; |
| 619 | break; |
| 620 | case ios_base::in: |
| 621 | __mdstr = L"r"; |
| 622 | break; |
| 623 | case ios_base::in | ios_base::out: |
| 624 | __mdstr = L"r+"; |
| 625 | break; |
| 626 | case ios_base::in | ios_base::out | ios_base::trunc: |
| 627 | __mdstr = L"w+"; |
| 628 | break; |
| 629 | case ios_base::in | ios_base::out | ios_base::app: |
| 630 | case ios_base::in | ios_base::app: |
| 631 | __mdstr = L"a+"; |
| 632 | break; |
| 633 | case ios_base::out | ios_base::binary: |
| 634 | case ios_base::out | ios_base::trunc | ios_base::binary: |
| 635 | __mdstr = L"wb"; |
| 636 | break; |
| 637 | case ios_base::out | ios_base::app | ios_base::binary: |
| 638 | case ios_base::app | ios_base::binary: |
| 639 | __mdstr = L"ab"; |
| 640 | break; |
| 641 | case ios_base::in | ios_base::binary: |
| 642 | __mdstr = L"rb"; |
| 643 | break; |
| 644 | case ios_base::in | ios_base::out | ios_base::binary: |
| 645 | __mdstr = L"r+b"; |
| 646 | break; |
| 647 | case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary: |
| 648 | __mdstr = L"w+b"; |
| 649 | break; |
| 650 | case ios_base::in | ios_base::out | ios_base::app | ios_base::binary: |
| 651 | case ios_base::in | ios_base::app | ios_base::binary: |
| 652 | __mdstr = L"a+b"; |
| 653 | break; |
| 654 | default: |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 655 | __rt = nullptr; |
Peter Collingbourne | 63ebbd7 | 2018-01-23 02:07:27 +0000 | [diff] [blame] | 656 | break; |
| 657 | } |
| 658 | if (__rt) |
| 659 | { |
| 660 | __file_ = _wfopen(__s, __mdstr); |
| 661 | if (__file_) |
| 662 | { |
| 663 | __om_ = __mode; |
| 664 | if (__mode & ios_base::ate) |
| 665 | { |
| 666 | if (fseek(__file_, 0, SEEK_END)) |
| 667 | { |
| 668 | fclose(__file_); |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 669 | __file_ = nullptr; |
| 670 | __rt = nullptr; |
Peter Collingbourne | 63ebbd7 | 2018-01-23 02:07:27 +0000 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | } |
| 674 | else |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 675 | __rt = nullptr; |
Peter Collingbourne | 63ebbd7 | 2018-01-23 02:07:27 +0000 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | return __rt; |
| 679 | } |
| 680 | #endif |
| 681 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 682 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 683 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 684 | basic_filebuf<_CharT, _Traits>* |
| 685 | basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) |
| 686 | { |
| 687 | return open(__s.c_str(), __mode); |
| 688 | } |
Ed Schouten | 7009f4e | 2015-03-12 15:44:39 +0000 | [diff] [blame] | 689 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 690 | |
| 691 | template <class _CharT, class _Traits> |
| 692 | basic_filebuf<_CharT, _Traits>* |
| 693 | basic_filebuf<_CharT, _Traits>::close() |
| 694 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 695 | basic_filebuf<_CharT, _Traits>* __rt = nullptr; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 696 | if (__file_) |
| 697 | { |
| 698 | __rt = this; |
| 699 | unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose); |
Howard Hinnant | c8697b6 | 2012-01-12 23:37:51 +0000 | [diff] [blame] | 700 | if (sync()) |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 701 | __rt = nullptr; |
Petr Hosek | ca6421b | 2019-07-22 19:54:34 +0000 | [diff] [blame] | 702 | if (fclose(__h.release())) |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 703 | __rt = nullptr; |
| 704 | __file_ = nullptr; |
Marshall Clow | 3c7658a | 2019-01-08 02:48:45 +0000 | [diff] [blame] | 705 | setbuf(0, 0); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 706 | } |
| 707 | return __rt; |
| 708 | } |
| 709 | |
| 710 | template <class _CharT, class _Traits> |
| 711 | typename basic_filebuf<_CharT, _Traits>::int_type |
| 712 | basic_filebuf<_CharT, _Traits>::underflow() |
| 713 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 714 | if (__file_ == nullptr) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 715 | return traits_type::eof(); |
| 716 | bool __initial = __read_mode(); |
| 717 | char_type __1buf; |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 718 | if (this->gptr() == nullptr) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 719 | this->setg(&__1buf, &__1buf+1, &__1buf+1); |
| 720 | const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4); |
| 721 | int_type __c = traits_type::eof(); |
| 722 | if (this->gptr() == this->egptr()) |
| 723 | { |
Arthur O'Dwyer | 2223663 | 2020-12-07 21:50:15 -0500 | [diff] [blame^] | 724 | _VSTD::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 725 | if (__always_noconv_) |
| 726 | { |
| 727 | size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz); |
| 728 | __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_); |
| 729 | if (__nmemb != 0) |
| 730 | { |
| 731 | this->setg(this->eback(), |
| 732 | this->eback() + __unget_sz, |
| 733 | this->eback() + __unget_sz + __nmemb); |
Howard Hinnant | 9da939d | 2011-02-02 17:37:16 +0000 | [diff] [blame] | 734 | __c = traits_type::to_int_type(*this->gptr()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | else |
| 738 | { |
Marshall Clow | 6184b9a | 2016-06-04 16:16:59 +0000 | [diff] [blame] | 739 | _LIBCPP_ASSERT ( !(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" ); |
| 740 | if (__extbufend_ != __extbufnext_) |
Arthur O'Dwyer | 2223663 | 2020-12-07 21:50:15 -0500 | [diff] [blame^] | 741 | _VSTD::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 742 | __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_); |
| 743 | __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_); |
Howard Hinnant | 50e1012 | 2012-08-24 20:37:00 +0000 | [diff] [blame] | 744 | size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz), |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 745 | static_cast<size_t>(__extbufend_ - __extbufnext_)); |
| 746 | codecvt_base::result __r; |
Howard Hinnant | 2f7c18b | 2012-08-24 21:20:56 +0000 | [diff] [blame] | 747 | __st_last_ = __st_; |
Marshall Clow | beda714 | 2017-06-14 20:00:36 +0000 | [diff] [blame] | 748 | size_t __nr = fread((void*) const_cast<char *>(__extbufnext_), 1, __nmemb, __file_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 749 | if (__nr != 0) |
| 750 | { |
Howard Hinnant | 0090b12 | 2012-08-24 16:52:47 +0000 | [diff] [blame] | 751 | if (!__cv_) |
Marshall Clow | 8fea161 | 2016-08-25 15:09:01 +0000 | [diff] [blame] | 752 | __throw_bad_cast(); |
| 753 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 754 | __extbufend_ = __extbufnext_ + __nr; |
| 755 | char_type* __inext; |
| 756 | __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_, |
| 757 | this->eback() + __unget_sz, |
Howard Hinnant | 50e1012 | 2012-08-24 20:37:00 +0000 | [diff] [blame] | 758 | this->eback() + __ibs_, __inext); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 759 | if (__r == codecvt_base::noconv) |
| 760 | { |
Louis Dionne | f5ba8aa | 2019-03-20 14:34:00 +0000 | [diff] [blame] | 761 | this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, |
Marshall Clow | beda714 | 2017-06-14 20:00:36 +0000 | [diff] [blame] | 762 | (char_type*)const_cast<char *>(__extbufend_)); |
Howard Hinnant | 9da939d | 2011-02-02 17:37:16 +0000 | [diff] [blame] | 763 | __c = traits_type::to_int_type(*this->gptr()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 764 | } |
| 765 | else if (__inext != this->eback() + __unget_sz) |
| 766 | { |
| 767 | this->setg(this->eback(), this->eback() + __unget_sz, __inext); |
Howard Hinnant | 9da939d | 2011-02-02 17:37:16 +0000 | [diff] [blame] | 768 | __c = traits_type::to_int_type(*this->gptr()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | else |
Howard Hinnant | 9da939d | 2011-02-02 17:37:16 +0000 | [diff] [blame] | 774 | __c = traits_type::to_int_type(*this->gptr()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 775 | if (this->eback() == &__1buf) |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 776 | this->setg(nullptr, nullptr, nullptr); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 777 | return __c; |
| 778 | } |
| 779 | |
| 780 | template <class _CharT, class _Traits> |
| 781 | typename basic_filebuf<_CharT, _Traits>::int_type |
| 782 | basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c) |
| 783 | { |
| 784 | if (__file_ && this->eback() < this->gptr()) |
| 785 | { |
| 786 | if (traits_type::eq_int_type(__c, traits_type::eof())) |
| 787 | { |
| 788 | this->gbump(-1); |
| 789 | return traits_type::not_eof(__c); |
| 790 | } |
| 791 | if ((__om_ & ios_base::out) || |
| 792 | traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1])) |
| 793 | { |
| 794 | this->gbump(-1); |
| 795 | *this->gptr() = traits_type::to_char_type(__c); |
| 796 | return __c; |
| 797 | } |
| 798 | } |
| 799 | return traits_type::eof(); |
| 800 | } |
| 801 | |
| 802 | template <class _CharT, class _Traits> |
| 803 | typename basic_filebuf<_CharT, _Traits>::int_type |
| 804 | basic_filebuf<_CharT, _Traits>::overflow(int_type __c) |
| 805 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 806 | if (__file_ == nullptr) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 807 | return traits_type::eof(); |
| 808 | __write_mode(); |
| 809 | char_type __1buf; |
| 810 | char_type* __pb_save = this->pbase(); |
| 811 | char_type* __epb_save = this->epptr(); |
| 812 | if (!traits_type::eq_int_type(__c, traits_type::eof())) |
| 813 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 814 | if (this->pptr() == nullptr) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 815 | this->setp(&__1buf, &__1buf+1); |
| 816 | *this->pptr() = traits_type::to_char_type(__c); |
| 817 | this->pbump(1); |
| 818 | } |
| 819 | if (this->pptr() != this->pbase()) |
| 820 | { |
| 821 | if (__always_noconv_) |
| 822 | { |
| 823 | size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase()); |
| 824 | if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb) |
| 825 | return traits_type::eof(); |
| 826 | } |
| 827 | else |
| 828 | { |
| 829 | char* __extbe = __extbuf_; |
| 830 | codecvt_base::result __r; |
| 831 | do |
| 832 | { |
Howard Hinnant | 0090b12 | 2012-08-24 16:52:47 +0000 | [diff] [blame] | 833 | if (!__cv_) |
Marshall Clow | 8fea161 | 2016-08-25 15:09:01 +0000 | [diff] [blame] | 834 | __throw_bad_cast(); |
| 835 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 836 | const char_type* __e; |
| 837 | __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e, |
| 838 | __extbuf_, __extbuf_ + __ebs_, __extbe); |
| 839 | if (__e == this->pbase()) |
| 840 | return traits_type::eof(); |
| 841 | if (__r == codecvt_base::noconv) |
| 842 | { |
| 843 | size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase()); |
| 844 | if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb) |
| 845 | return traits_type::eof(); |
| 846 | } |
| 847 | else if (__r == codecvt_base::ok || __r == codecvt_base::partial) |
| 848 | { |
| 849 | size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_); |
| 850 | if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb) |
| 851 | return traits_type::eof(); |
| 852 | if (__r == codecvt_base::partial) |
| 853 | { |
Marshall Clow | beda714 | 2017-06-14 20:00:36 +0000 | [diff] [blame] | 854 | this->setp(const_cast<char_type*>(__e), this->pptr()); |
Marshall Clow | 3393262 | 2017-09-12 15:00:43 +0000 | [diff] [blame] | 855 | this->__pbump(this->epptr() - this->pbase()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | else |
| 859 | return traits_type::eof(); |
| 860 | } while (__r == codecvt_base::partial); |
| 861 | } |
| 862 | this->setp(__pb_save, __epb_save); |
| 863 | } |
| 864 | return traits_type::not_eof(__c); |
| 865 | } |
| 866 | |
| 867 | template <class _CharT, class _Traits> |
| 868 | basic_streambuf<_CharT, _Traits>* |
| 869 | basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n) |
| 870 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 871 | this->setg(nullptr, nullptr, nullptr); |
| 872 | this->setp(nullptr, nullptr); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 873 | if (__owns_eb_) |
| 874 | delete [] __extbuf_; |
| 875 | if (__owns_ib_) |
| 876 | delete [] __intbuf_; |
| 877 | __ebs_ = __n; |
| 878 | if (__ebs_ > sizeof(__extbuf_min_)) |
| 879 | { |
| 880 | if (__always_noconv_ && __s) |
| 881 | { |
| 882 | __extbuf_ = (char*)__s; |
| 883 | __owns_eb_ = false; |
| 884 | } |
| 885 | else |
| 886 | { |
| 887 | __extbuf_ = new char[__ebs_]; |
| 888 | __owns_eb_ = true; |
| 889 | } |
| 890 | } |
| 891 | else |
| 892 | { |
| 893 | __extbuf_ = __extbuf_min_; |
| 894 | __ebs_ = sizeof(__extbuf_min_); |
| 895 | __owns_eb_ = false; |
| 896 | } |
| 897 | if (!__always_noconv_) |
| 898 | { |
| 899 | __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_)); |
| 900 | if (__s && __ibs_ >= sizeof(__extbuf_min_)) |
| 901 | { |
| 902 | __intbuf_ = __s; |
| 903 | __owns_ib_ = false; |
| 904 | } |
| 905 | else |
| 906 | { |
| 907 | __intbuf_ = new char_type[__ibs_]; |
| 908 | __owns_ib_ = true; |
| 909 | } |
| 910 | } |
| 911 | else |
| 912 | { |
| 913 | __ibs_ = 0; |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 914 | __intbuf_ = nullptr; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 915 | __owns_ib_ = false; |
| 916 | } |
| 917 | return this; |
| 918 | } |
| 919 | |
| 920 | template <class _CharT, class _Traits> |
| 921 | typename basic_filebuf<_CharT, _Traits>::pos_type |
| 922 | basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way, |
| 923 | ios_base::openmode) |
| 924 | { |
Howard Hinnant | 0090b12 | 2012-08-24 16:52:47 +0000 | [diff] [blame] | 925 | if (!__cv_) |
Marshall Clow | 8fea161 | 2016-08-25 15:09:01 +0000 | [diff] [blame] | 926 | __throw_bad_cast(); |
| 927 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 928 | int __width = __cv_->encoding(); |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 929 | if (__file_ == nullptr || (__width <= 0 && __off != 0) || sync()) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 930 | return pos_type(off_type(-1)); |
| 931 | // __width > 0 || __off == 0 |
| 932 | int __whence; |
| 933 | switch (__way) |
| 934 | { |
| 935 | case ios_base::beg: |
| 936 | __whence = SEEK_SET; |
| 937 | break; |
| 938 | case ios_base::cur: |
| 939 | __whence = SEEK_CUR; |
| 940 | break; |
| 941 | case ios_base::end: |
| 942 | __whence = SEEK_END; |
| 943 | break; |
| 944 | default: |
| 945 | return pos_type(off_type(-1)); |
| 946 | } |
Shoaib Meenai | 3879c9b | 2016-10-31 15:09:10 +0000 | [diff] [blame] | 947 | #if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS) |
Howard Hinnant | 456539a | 2013-04-02 22:14:51 +0000 | [diff] [blame] | 948 | if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence)) |
| 949 | return pos_type(off_type(-1)); |
| 950 | pos_type __r = ftell(__file_); |
| 951 | #else |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 952 | if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence)) |
| 953 | return pos_type(off_type(-1)); |
| 954 | pos_type __r = ftello(__file_); |
Howard Hinnant | 456539a | 2013-04-02 22:14:51 +0000 | [diff] [blame] | 955 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 956 | __r.state(__st_); |
| 957 | return __r; |
| 958 | } |
| 959 | |
| 960 | template <class _CharT, class _Traits> |
| 961 | typename basic_filebuf<_CharT, _Traits>::pos_type |
Howard Hinnant | f3aff4f | 2010-07-15 18:18:07 +0000 | [diff] [blame] | 962 | basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 963 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 964 | if (__file_ == nullptr || sync()) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 965 | return pos_type(off_type(-1)); |
Shoaib Meenai | 3879c9b | 2016-10-31 15:09:10 +0000 | [diff] [blame] | 966 | #if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS) |
Howard Hinnant | 456539a | 2013-04-02 22:14:51 +0000 | [diff] [blame] | 967 | if (fseek(__file_, __sp, SEEK_SET)) |
| 968 | return pos_type(off_type(-1)); |
| 969 | #else |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 970 | if (fseeko(__file_, __sp, SEEK_SET)) |
| 971 | return pos_type(off_type(-1)); |
Howard Hinnant | 456539a | 2013-04-02 22:14:51 +0000 | [diff] [blame] | 972 | #endif |
Howard Hinnant | 2f7c18b | 2012-08-24 21:20:56 +0000 | [diff] [blame] | 973 | __st_ = __sp.state(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 974 | return __sp; |
| 975 | } |
| 976 | |
| 977 | template <class _CharT, class _Traits> |
| 978 | int |
| 979 | basic_filebuf<_CharT, _Traits>::sync() |
| 980 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 981 | if (__file_ == nullptr) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 982 | return 0; |
Howard Hinnant | 0090b12 | 2012-08-24 16:52:47 +0000 | [diff] [blame] | 983 | if (!__cv_) |
Marshall Clow | 8fea161 | 2016-08-25 15:09:01 +0000 | [diff] [blame] | 984 | __throw_bad_cast(); |
| 985 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 986 | if (__cm_ & ios_base::out) |
| 987 | { |
| 988 | if (this->pptr() != this->pbase()) |
| 989 | if (overflow() == traits_type::eof()) |
| 990 | return -1; |
| 991 | codecvt_base::result __r; |
| 992 | do |
| 993 | { |
| 994 | char* __extbe; |
| 995 | __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe); |
| 996 | size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_); |
| 997 | if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb) |
| 998 | return -1; |
| 999 | } while (__r == codecvt_base::partial); |
| 1000 | if (__r == codecvt_base::error) |
| 1001 | return -1; |
| 1002 | if (fflush(__file_)) |
| 1003 | return -1; |
| 1004 | } |
| 1005 | else if (__cm_ & ios_base::in) |
| 1006 | { |
| 1007 | off_type __c; |
Howard Hinnant | 2f7c18b | 2012-08-24 21:20:56 +0000 | [diff] [blame] | 1008 | state_type __state = __st_last_; |
| 1009 | bool __update_st = false; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1010 | if (__always_noconv_) |
| 1011 | __c = this->egptr() - this->gptr(); |
| 1012 | else |
| 1013 | { |
| 1014 | int __width = __cv_->encoding(); |
| 1015 | __c = __extbufend_ - __extbufnext_; |
| 1016 | if (__width > 0) |
| 1017 | __c += __width * (this->egptr() - this->gptr()); |
| 1018 | else |
| 1019 | { |
| 1020 | if (this->gptr() != this->egptr()) |
| 1021 | { |
Howard Hinnant | 2f7c18b | 2012-08-24 21:20:56 +0000 | [diff] [blame] | 1022 | const int __off = __cv_->length(__state, __extbuf_, |
| 1023 | __extbufnext_, |
| 1024 | this->gptr() - this->eback()); |
| 1025 | __c += __extbufnext_ - __extbuf_ - __off; |
| 1026 | __update_st = true; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1027 | } |
| 1028 | } |
| 1029 | } |
Shoaib Meenai | 3879c9b | 2016-10-31 15:09:10 +0000 | [diff] [blame] | 1030 | #if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS) |
Howard Hinnant | 456539a | 2013-04-02 22:14:51 +0000 | [diff] [blame] | 1031 | if (fseek(__file_, -__c, SEEK_CUR)) |
| 1032 | return -1; |
| 1033 | #else |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1034 | if (fseeko(__file_, -__c, SEEK_CUR)) |
| 1035 | return -1; |
Howard Hinnant | 456539a | 2013-04-02 22:14:51 +0000 | [diff] [blame] | 1036 | #endif |
Howard Hinnant | 2f7c18b | 2012-08-24 21:20:56 +0000 | [diff] [blame] | 1037 | if (__update_st) |
| 1038 | __st_ = __state; |
| 1039 | __extbufnext_ = __extbufend_ = __extbuf_; |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 1040 | this->setg(nullptr, nullptr, nullptr); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1041 | __cm_ = 0; |
| 1042 | } |
| 1043 | return 0; |
| 1044 | } |
| 1045 | |
| 1046 | template <class _CharT, class _Traits> |
| 1047 | void |
| 1048 | basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc) |
| 1049 | { |
| 1050 | sync(); |
| 1051 | __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc); |
| 1052 | bool __old_anc = __always_noconv_; |
| 1053 | __always_noconv_ = __cv_->always_noconv(); |
| 1054 | if (__old_anc != __always_noconv_) |
| 1055 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 1056 | this->setg(nullptr, nullptr, nullptr); |
| 1057 | this->setp(nullptr, nullptr); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1058 | // invariant, char_type is char, else we couldn't get here |
| 1059 | if (__always_noconv_) // need to dump __intbuf_ |
| 1060 | { |
| 1061 | if (__owns_eb_) |
| 1062 | delete [] __extbuf_; |
| 1063 | __owns_eb_ = __owns_ib_; |
| 1064 | __ebs_ = __ibs_; |
| 1065 | __extbuf_ = (char*)__intbuf_; |
| 1066 | __ibs_ = 0; |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 1067 | __intbuf_ = nullptr; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1068 | __owns_ib_ = false; |
| 1069 | } |
| 1070 | else // need to obtain an __intbuf_. |
| 1071 | { // If __extbuf_ is user-supplied, use it, else new __intbuf_ |
| 1072 | if (!__owns_eb_ && __extbuf_ != __extbuf_min_) |
| 1073 | { |
| 1074 | __ibs_ = __ebs_; |
| 1075 | __intbuf_ = (char_type*)__extbuf_; |
| 1076 | __owns_ib_ = false; |
| 1077 | __extbuf_ = new char[__ebs_]; |
| 1078 | __owns_eb_ = true; |
| 1079 | } |
| 1080 | else |
| 1081 | { |
| 1082 | __ibs_ = __ebs_; |
| 1083 | __intbuf_ = new char_type[__ibs_]; |
| 1084 | __owns_ib_ = true; |
| 1085 | } |
| 1086 | } |
| 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | template <class _CharT, class _Traits> |
| 1091 | bool |
| 1092 | basic_filebuf<_CharT, _Traits>::__read_mode() |
| 1093 | { |
| 1094 | if (!(__cm_ & ios_base::in)) |
| 1095 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 1096 | this->setp(nullptr, nullptr); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1097 | if (__always_noconv_) |
| 1098 | this->setg((char_type*)__extbuf_, |
| 1099 | (char_type*)__extbuf_ + __ebs_, |
| 1100 | (char_type*)__extbuf_ + __ebs_); |
| 1101 | else |
| 1102 | this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_); |
| 1103 | __cm_ = ios_base::in; |
| 1104 | return true; |
| 1105 | } |
| 1106 | return false; |
| 1107 | } |
| 1108 | |
| 1109 | template <class _CharT, class _Traits> |
| 1110 | void |
| 1111 | basic_filebuf<_CharT, _Traits>::__write_mode() |
| 1112 | { |
| 1113 | if (!(__cm_ & ios_base::out)) |
| 1114 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 1115 | this->setg(nullptr, nullptr, nullptr); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1116 | if (__ebs_ > sizeof(__extbuf_min_)) |
| 1117 | { |
| 1118 | if (__always_noconv_) |
| 1119 | this->setp((char_type*)__extbuf_, |
| 1120 | (char_type*)__extbuf_ + (__ebs_ - 1)); |
| 1121 | else |
| 1122 | this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1)); |
| 1123 | } |
| 1124 | else |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 1125 | this->setp(nullptr, nullptr); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1126 | __cm_ = ios_base::out; |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | // basic_ifstream |
| 1131 | |
| 1132 | template <class _CharT, class _Traits> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 1133 | class _LIBCPP_TEMPLATE_VIS basic_ifstream |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1134 | : public basic_istream<_CharT, _Traits> |
| 1135 | { |
| 1136 | public: |
| 1137 | typedef _CharT char_type; |
| 1138 | typedef _Traits traits_type; |
| 1139 | typedef typename traits_type::int_type int_type; |
| 1140 | typedef typename traits_type::pos_type pos_type; |
| 1141 | typedef typename traits_type::off_type off_type; |
| 1142 | |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1143 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1144 | basic_ifstream(); |
Ed Schouten | 7009f4e | 2015-03-12 15:44:39 +0000 | [diff] [blame] | 1145 | #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1146 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1147 | explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in); |
Peter Collingbourne | 63ebbd7 | 2018-01-23 02:07:27 +0000 | [diff] [blame] | 1148 | #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR |
| 1149 | _LIBCPP_INLINE_VISIBILITY |
| 1150 | explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in); |
| 1151 | #endif |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1152 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1153 | explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in); |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1154 | #if _LIBCPP_STD_VER >= 17 |
Louis Dionne | f5ba8aa | 2019-03-20 14:34:00 +0000 | [diff] [blame] | 1155 | _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1156 | explicit basic_ifstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in) |
| 1157 | : basic_ifstream(__p.c_str(), __mode) {} |
| 1158 | #endif // _LIBCPP_STD_VER >= 17 |
Ed Schouten | 7009f4e | 2015-03-12 15:44:39 +0000 | [diff] [blame] | 1159 | #endif |
Eric Fiselier | 78ccf77 | 2017-04-18 23:38:41 +0000 | [diff] [blame] | 1160 | #ifndef _LIBCPP_CXX03_LANG |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1161 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1162 | basic_ifstream(basic_ifstream&& __rhs); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1163 | |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1164 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1165 | basic_ifstream& operator=(basic_ifstream&& __rhs); |
| 1166 | #endif |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1167 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1168 | void swap(basic_ifstream& __rhs); |
| 1169 | |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1170 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1171 | basic_filebuf<char_type, traits_type>* rdbuf() const; |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1172 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1173 | bool is_open() const; |
Ed Schouten | 7009f4e | 2015-03-12 15:44:39 +0000 | [diff] [blame] | 1174 | #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1175 | void open(const char* __s, ios_base::openmode __mode = ios_base::in); |
Peter Collingbourne | 63ebbd7 | 2018-01-23 02:07:27 +0000 | [diff] [blame] | 1176 | #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR |
| 1177 | void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in); |
| 1178 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1179 | void open(const string& __s, ios_base::openmode __mode = ios_base::in); |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1180 | #if _LIBCPP_STD_VER >= 17 |
Louis Dionne | f5ba8aa | 2019-03-20 14:34:00 +0000 | [diff] [blame] | 1181 | _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1182 | void open(const filesystem::path& __p, |
| 1183 | ios_base::openmode __mode = ios_base::in) { |
| 1184 | return open(__p.c_str(), __mode); |
| 1185 | } |
| 1186 | #endif // _LIBCPP_STD_VER >= 17 |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 1187 | |
| 1188 | _LIBCPP_INLINE_VISIBILITY |
| 1189 | void __open(int __fd, ios_base::openmode __mode); |
Ed Schouten | 7009f4e | 2015-03-12 15:44:39 +0000 | [diff] [blame] | 1190 | #endif |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1191 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1192 | void close(); |
| 1193 | |
| 1194 | private: |
| 1195 | basic_filebuf<char_type, traits_type> __sb_; |
| 1196 | }; |
| 1197 | |
| 1198 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1199 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1200 | basic_ifstream<_CharT, _Traits>::basic_ifstream() |
| 1201 | : basic_istream<char_type, traits_type>(&__sb_) |
| 1202 | { |
| 1203 | } |
| 1204 | |
Ed Schouten | 7009f4e | 2015-03-12 15:44:39 +0000 | [diff] [blame] | 1205 | #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1206 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1207 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1208 | basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode) |
| 1209 | : basic_istream<char_type, traits_type>(&__sb_) |
| 1210 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 1211 | if (__sb_.open(__s, __mode | ios_base::in) == nullptr) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1212 | this->setstate(ios_base::failbit); |
| 1213 | } |
| 1214 | |
Peter Collingbourne | 63ebbd7 | 2018-01-23 02:07:27 +0000 | [diff] [blame] | 1215 | #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR |
| 1216 | template <class _CharT, class _Traits> |
| 1217 | inline |
| 1218 | basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode) |
| 1219 | : basic_istream<char_type, traits_type>(&__sb_) |
| 1220 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 1221 | if (__sb_.open(__s, __mode | ios_base::in) == nullptr) |
Peter Collingbourne | 63ebbd7 | 2018-01-23 02:07:27 +0000 | [diff] [blame] | 1222 | this->setstate(ios_base::failbit); |
| 1223 | } |
| 1224 | #endif |
| 1225 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1226 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1227 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1228 | basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode) |
| 1229 | : basic_istream<char_type, traits_type>(&__sb_) |
| 1230 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 1231 | if (__sb_.open(__s, __mode | ios_base::in) == nullptr) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1232 | this->setstate(ios_base::failbit); |
| 1233 | } |
Ed Schouten | 7009f4e | 2015-03-12 15:44:39 +0000 | [diff] [blame] | 1234 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1235 | |
Eric Fiselier | 78ccf77 | 2017-04-18 23:38:41 +0000 | [diff] [blame] | 1236 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1237 | |
| 1238 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1239 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1240 | basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1241 | : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)), |
| 1242 | __sb_(_VSTD::move(__rhs.__sb_)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1243 | { |
| 1244 | this->set_rdbuf(&__sb_); |
| 1245 | } |
| 1246 | |
| 1247 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1248 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1249 | basic_ifstream<_CharT, _Traits>& |
| 1250 | basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs) |
| 1251 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1252 | basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs)); |
| 1253 | __sb_ = _VSTD::move(__rhs.__sb_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1254 | return *this; |
| 1255 | } |
| 1256 | |
Eric Fiselier | 78ccf77 | 2017-04-18 23:38:41 +0000 | [diff] [blame] | 1257 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1258 | |
| 1259 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1260 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1261 | void |
| 1262 | basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs) |
| 1263 | { |
| 1264 | basic_istream<char_type, traits_type>::swap(__rhs); |
| 1265 | __sb_.swap(__rhs.__sb_); |
| 1266 | } |
| 1267 | |
| 1268 | template <class _CharT, class _Traits> |
| 1269 | inline _LIBCPP_INLINE_VISIBILITY |
| 1270 | void |
| 1271 | swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y) |
| 1272 | { |
| 1273 | __x.swap(__y); |
| 1274 | } |
| 1275 | |
| 1276 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1277 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1278 | basic_filebuf<_CharT, _Traits>* |
| 1279 | basic_ifstream<_CharT, _Traits>::rdbuf() const |
| 1280 | { |
| 1281 | return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_); |
| 1282 | } |
| 1283 | |
| 1284 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1285 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1286 | bool |
| 1287 | basic_ifstream<_CharT, _Traits>::is_open() const |
| 1288 | { |
| 1289 | return __sb_.is_open(); |
| 1290 | } |
| 1291 | |
Ed Schouten | 7009f4e | 2015-03-12 15:44:39 +0000 | [diff] [blame] | 1292 | #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1293 | template <class _CharT, class _Traits> |
| 1294 | void |
| 1295 | basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) |
| 1296 | { |
| 1297 | if (__sb_.open(__s, __mode | ios_base::in)) |
| 1298 | this->clear(); |
| 1299 | else |
| 1300 | this->setstate(ios_base::failbit); |
| 1301 | } |
| 1302 | |
Peter Collingbourne | 63ebbd7 | 2018-01-23 02:07:27 +0000 | [diff] [blame] | 1303 | #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR |
| 1304 | template <class _CharT, class _Traits> |
| 1305 | void |
| 1306 | basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) |
| 1307 | { |
| 1308 | if (__sb_.open(__s, __mode | ios_base::in)) |
| 1309 | this->clear(); |
| 1310 | else |
| 1311 | this->setstate(ios_base::failbit); |
| 1312 | } |
| 1313 | #endif |
| 1314 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1315 | template <class _CharT, class _Traits> |
| 1316 | void |
| 1317 | basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) |
| 1318 | { |
| 1319 | if (__sb_.open(__s, __mode | ios_base::in)) |
| 1320 | this->clear(); |
| 1321 | else |
| 1322 | this->setstate(ios_base::failbit); |
| 1323 | } |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 1324 | |
| 1325 | template <class _CharT, class _Traits> |
| 1326 | void basic_ifstream<_CharT, _Traits>::__open(int __fd, |
| 1327 | ios_base::openmode __mode) { |
| 1328 | if (__sb_.__open(__fd, __mode | ios_base::in)) |
| 1329 | this->clear(); |
| 1330 | else |
| 1331 | this->setstate(ios_base::failbit); |
| 1332 | } |
Ed Schouten | 7009f4e | 2015-03-12 15:44:39 +0000 | [diff] [blame] | 1333 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1334 | |
| 1335 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1336 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1337 | void |
| 1338 | basic_ifstream<_CharT, _Traits>::close() |
| 1339 | { |
| 1340 | if (__sb_.close() == 0) |
| 1341 | this->setstate(ios_base::failbit); |
| 1342 | } |
| 1343 | |
| 1344 | // basic_ofstream |
| 1345 | |
| 1346 | template <class _CharT, class _Traits> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 1347 | class _LIBCPP_TEMPLATE_VIS basic_ofstream |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1348 | : public basic_ostream<_CharT, _Traits> |
| 1349 | { |
| 1350 | public: |
| 1351 | typedef _CharT char_type; |
| 1352 | typedef _Traits traits_type; |
| 1353 | typedef typename traits_type::int_type int_type; |
| 1354 | typedef typename traits_type::pos_type pos_type; |
| 1355 | typedef typename traits_type::off_type off_type; |
| 1356 | |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1357 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1358 | basic_ofstream(); |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1359 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1360 | explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out); |
Peter Collingbourne | 63ebbd7 | 2018-01-23 02:07:27 +0000 | [diff] [blame] | 1361 | #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR |
| 1362 | _LIBCPP_INLINE_VISIBILITY |
| 1363 | explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out); |
| 1364 | #endif |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1365 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1366 | explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out); |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1367 | |
| 1368 | #if _LIBCPP_STD_VER >= 17 |
Louis Dionne | f5ba8aa | 2019-03-20 14:34:00 +0000 | [diff] [blame] | 1369 | _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1370 | explicit basic_ofstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out) |
| 1371 | : basic_ofstream(__p.c_str(), __mode) {} |
| 1372 | #endif // _LIBCPP_STD_VER >= 17 |
| 1373 | |
Eric Fiselier | 78ccf77 | 2017-04-18 23:38:41 +0000 | [diff] [blame] | 1374 | #ifndef _LIBCPP_CXX03_LANG |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1375 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1376 | basic_ofstream(basic_ofstream&& __rhs); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1377 | |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1378 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1379 | basic_ofstream& operator=(basic_ofstream&& __rhs); |
| 1380 | #endif |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1381 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1382 | void swap(basic_ofstream& __rhs); |
| 1383 | |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1384 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1385 | basic_filebuf<char_type, traits_type>* rdbuf() const; |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1386 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1387 | bool is_open() const; |
Ed Schouten | 7009f4e | 2015-03-12 15:44:39 +0000 | [diff] [blame] | 1388 | #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1389 | void open(const char* __s, ios_base::openmode __mode = ios_base::out); |
Peter Collingbourne | 63ebbd7 | 2018-01-23 02:07:27 +0000 | [diff] [blame] | 1390 | #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR |
| 1391 | void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out); |
| 1392 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1393 | void open(const string& __s, ios_base::openmode __mode = ios_base::out); |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 1394 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1395 | #if _LIBCPP_STD_VER >= 17 |
Louis Dionne | f5ba8aa | 2019-03-20 14:34:00 +0000 | [diff] [blame] | 1396 | _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1397 | void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out) |
| 1398 | { return open(__p.c_str(), __mode); } |
| 1399 | #endif // _LIBCPP_STD_VER >= 17 |
| 1400 | |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 1401 | _LIBCPP_INLINE_VISIBILITY |
| 1402 | void __open(int __fd, ios_base::openmode __mode); |
Ed Schouten | 7009f4e | 2015-03-12 15:44:39 +0000 | [diff] [blame] | 1403 | #endif |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1404 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1405 | void close(); |
| 1406 | |
| 1407 | private: |
| 1408 | basic_filebuf<char_type, traits_type> __sb_; |
| 1409 | }; |
| 1410 | |
| 1411 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1412 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1413 | basic_ofstream<_CharT, _Traits>::basic_ofstream() |
| 1414 | : basic_ostream<char_type, traits_type>(&__sb_) |
| 1415 | { |
| 1416 | } |
| 1417 | |
Ed Schouten | 7009f4e | 2015-03-12 15:44:39 +0000 | [diff] [blame] | 1418 | #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1419 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1420 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1421 | basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode) |
| 1422 | : basic_ostream<char_type, traits_type>(&__sb_) |
| 1423 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 1424 | if (__sb_.open(__s, __mode | ios_base::out) == nullptr) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1425 | this->setstate(ios_base::failbit); |
| 1426 | } |
| 1427 | |
Peter Collingbourne | 63ebbd7 | 2018-01-23 02:07:27 +0000 | [diff] [blame] | 1428 | #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR |
| 1429 | template <class _CharT, class _Traits> |
| 1430 | inline |
| 1431 | basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode) |
| 1432 | : basic_ostream<char_type, traits_type>(&__sb_) |
| 1433 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 1434 | if (__sb_.open(__s, __mode | ios_base::out) == nullptr) |
Peter Collingbourne | 63ebbd7 | 2018-01-23 02:07:27 +0000 | [diff] [blame] | 1435 | this->setstate(ios_base::failbit); |
| 1436 | } |
| 1437 | #endif |
| 1438 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1439 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1440 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1441 | basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode) |
| 1442 | : basic_ostream<char_type, traits_type>(&__sb_) |
| 1443 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 1444 | if (__sb_.open(__s, __mode | ios_base::out) == nullptr) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1445 | this->setstate(ios_base::failbit); |
| 1446 | } |
Ed Schouten | 7009f4e | 2015-03-12 15:44:39 +0000 | [diff] [blame] | 1447 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1448 | |
Eric Fiselier | 78ccf77 | 2017-04-18 23:38:41 +0000 | [diff] [blame] | 1449 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1450 | |
| 1451 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1452 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1453 | basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1454 | : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)), |
| 1455 | __sb_(_VSTD::move(__rhs.__sb_)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1456 | { |
| 1457 | this->set_rdbuf(&__sb_); |
| 1458 | } |
| 1459 | |
| 1460 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1461 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1462 | basic_ofstream<_CharT, _Traits>& |
| 1463 | basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs) |
| 1464 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1465 | basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs)); |
| 1466 | __sb_ = _VSTD::move(__rhs.__sb_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1467 | return *this; |
| 1468 | } |
| 1469 | |
Eric Fiselier | 78ccf77 | 2017-04-18 23:38:41 +0000 | [diff] [blame] | 1470 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1471 | |
| 1472 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1473 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1474 | void |
| 1475 | basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs) |
| 1476 | { |
| 1477 | basic_ostream<char_type, traits_type>::swap(__rhs); |
| 1478 | __sb_.swap(__rhs.__sb_); |
| 1479 | } |
| 1480 | |
| 1481 | template <class _CharT, class _Traits> |
| 1482 | inline _LIBCPP_INLINE_VISIBILITY |
| 1483 | void |
| 1484 | swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y) |
| 1485 | { |
| 1486 | __x.swap(__y); |
| 1487 | } |
| 1488 | |
| 1489 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1490 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1491 | basic_filebuf<_CharT, _Traits>* |
| 1492 | basic_ofstream<_CharT, _Traits>::rdbuf() const |
| 1493 | { |
| 1494 | return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_); |
| 1495 | } |
| 1496 | |
| 1497 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1498 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1499 | bool |
| 1500 | basic_ofstream<_CharT, _Traits>::is_open() const |
| 1501 | { |
| 1502 | return __sb_.is_open(); |
| 1503 | } |
| 1504 | |
Ed Schouten | 7009f4e | 2015-03-12 15:44:39 +0000 | [diff] [blame] | 1505 | #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1506 | template <class _CharT, class _Traits> |
| 1507 | void |
| 1508 | basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) |
| 1509 | { |
| 1510 | if (__sb_.open(__s, __mode | ios_base::out)) |
| 1511 | this->clear(); |
| 1512 | else |
| 1513 | this->setstate(ios_base::failbit); |
| 1514 | } |
| 1515 | |
Peter Collingbourne | 63ebbd7 | 2018-01-23 02:07:27 +0000 | [diff] [blame] | 1516 | #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR |
| 1517 | template <class _CharT, class _Traits> |
| 1518 | void |
| 1519 | basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) |
| 1520 | { |
| 1521 | if (__sb_.open(__s, __mode | ios_base::out)) |
| 1522 | this->clear(); |
| 1523 | else |
| 1524 | this->setstate(ios_base::failbit); |
| 1525 | } |
| 1526 | #endif |
| 1527 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1528 | template <class _CharT, class _Traits> |
| 1529 | void |
| 1530 | basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) |
| 1531 | { |
| 1532 | if (__sb_.open(__s, __mode | ios_base::out)) |
| 1533 | this->clear(); |
| 1534 | else |
| 1535 | this->setstate(ios_base::failbit); |
| 1536 | } |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 1537 | |
| 1538 | template <class _CharT, class _Traits> |
| 1539 | void basic_ofstream<_CharT, _Traits>::__open(int __fd, |
| 1540 | ios_base::openmode __mode) { |
| 1541 | if (__sb_.__open(__fd, __mode | ios_base::out)) |
| 1542 | this->clear(); |
| 1543 | else |
| 1544 | this->setstate(ios_base::failbit); |
| 1545 | } |
Ed Schouten | 7009f4e | 2015-03-12 15:44:39 +0000 | [diff] [blame] | 1546 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1547 | |
| 1548 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1549 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1550 | void |
| 1551 | basic_ofstream<_CharT, _Traits>::close() |
| 1552 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 1553 | if (__sb_.close() == nullptr) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1554 | this->setstate(ios_base::failbit); |
| 1555 | } |
| 1556 | |
| 1557 | // basic_fstream |
| 1558 | |
| 1559 | template <class _CharT, class _Traits> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 1560 | class _LIBCPP_TEMPLATE_VIS basic_fstream |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1561 | : public basic_iostream<_CharT, _Traits> |
| 1562 | { |
| 1563 | public: |
| 1564 | typedef _CharT char_type; |
| 1565 | typedef _Traits traits_type; |
| 1566 | typedef typename traits_type::int_type int_type; |
| 1567 | typedef typename traits_type::pos_type pos_type; |
| 1568 | typedef typename traits_type::off_type off_type; |
| 1569 | |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1570 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1571 | basic_fstream(); |
Ed Schouten | 7009f4e | 2015-03-12 15:44:39 +0000 | [diff] [blame] | 1572 | #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1573 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1574 | explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out); |
Peter Collingbourne | 63ebbd7 | 2018-01-23 02:07:27 +0000 | [diff] [blame] | 1575 | #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR |
| 1576 | _LIBCPP_INLINE_VISIBILITY |
| 1577 | explicit basic_fstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out); |
| 1578 | #endif |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1579 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1580 | explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out); |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1581 | |
| 1582 | #if _LIBCPP_STD_VER >= 17 |
Louis Dionne | f5ba8aa | 2019-03-20 14:34:00 +0000 | [diff] [blame] | 1583 | _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1584 | explicit basic_fstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out) |
| 1585 | : basic_fstream(__p.c_str(), __mode) {} |
| 1586 | #endif // _LIBCPP_STD_VER >= 17 |
| 1587 | |
Ed Schouten | 7009f4e | 2015-03-12 15:44:39 +0000 | [diff] [blame] | 1588 | #endif |
Eric Fiselier | 78ccf77 | 2017-04-18 23:38:41 +0000 | [diff] [blame] | 1589 | #ifndef _LIBCPP_CXX03_LANG |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1590 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1591 | basic_fstream(basic_fstream&& __rhs); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1592 | |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1593 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1594 | basic_fstream& operator=(basic_fstream&& __rhs); |
| 1595 | #endif |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1596 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1597 | void swap(basic_fstream& __rhs); |
| 1598 | |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1599 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1600 | basic_filebuf<char_type, traits_type>* rdbuf() const; |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1601 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1602 | bool is_open() const; |
Ed Schouten | 7009f4e | 2015-03-12 15:44:39 +0000 | [diff] [blame] | 1603 | #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1604 | void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out); |
Peter Collingbourne | 63ebbd7 | 2018-01-23 02:07:27 +0000 | [diff] [blame] | 1605 | #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR |
| 1606 | void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out); |
| 1607 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1608 | void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out); |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1609 | |
| 1610 | #if _LIBCPP_STD_VER >= 17 |
Louis Dionne | f5ba8aa | 2019-03-20 14:34:00 +0000 | [diff] [blame] | 1611 | _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1612 | void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in|ios_base::out) |
| 1613 | { return open(__p.c_str(), __mode); } |
| 1614 | #endif // _LIBCPP_STD_VER >= 17 |
| 1615 | |
Ed Schouten | 7009f4e | 2015-03-12 15:44:39 +0000 | [diff] [blame] | 1616 | #endif |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1617 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1618 | void close(); |
| 1619 | |
| 1620 | private: |
| 1621 | basic_filebuf<char_type, traits_type> __sb_; |
| 1622 | }; |
| 1623 | |
| 1624 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1625 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1626 | basic_fstream<_CharT, _Traits>::basic_fstream() |
| 1627 | : basic_iostream<char_type, traits_type>(&__sb_) |
| 1628 | { |
| 1629 | } |
| 1630 | |
Ed Schouten | 7009f4e | 2015-03-12 15:44:39 +0000 | [diff] [blame] | 1631 | #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1632 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1633 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1634 | basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode) |
| 1635 | : basic_iostream<char_type, traits_type>(&__sb_) |
| 1636 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 1637 | if (__sb_.open(__s, __mode) == nullptr) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1638 | this->setstate(ios_base::failbit); |
| 1639 | } |
| 1640 | |
Peter Collingbourne | 63ebbd7 | 2018-01-23 02:07:27 +0000 | [diff] [blame] | 1641 | #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR |
| 1642 | template <class _CharT, class _Traits> |
| 1643 | inline |
| 1644 | basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode) |
| 1645 | : basic_iostream<char_type, traits_type>(&__sb_) |
| 1646 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 1647 | if (__sb_.open(__s, __mode) == nullptr) |
Peter Collingbourne | 63ebbd7 | 2018-01-23 02:07:27 +0000 | [diff] [blame] | 1648 | this->setstate(ios_base::failbit); |
| 1649 | } |
| 1650 | #endif |
| 1651 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1652 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1653 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1654 | basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode) |
| 1655 | : basic_iostream<char_type, traits_type>(&__sb_) |
| 1656 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 1657 | if (__sb_.open(__s, __mode) == nullptr) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1658 | this->setstate(ios_base::failbit); |
| 1659 | } |
Ed Schouten | 7009f4e | 2015-03-12 15:44:39 +0000 | [diff] [blame] | 1660 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1661 | |
Eric Fiselier | 78ccf77 | 2017-04-18 23:38:41 +0000 | [diff] [blame] | 1662 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1663 | |
| 1664 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1665 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1666 | basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1667 | : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)), |
| 1668 | __sb_(_VSTD::move(__rhs.__sb_)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1669 | { |
| 1670 | this->set_rdbuf(&__sb_); |
| 1671 | } |
| 1672 | |
| 1673 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1674 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1675 | basic_fstream<_CharT, _Traits>& |
| 1676 | basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs) |
| 1677 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1678 | basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs)); |
| 1679 | __sb_ = _VSTD::move(__rhs.__sb_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1680 | return *this; |
| 1681 | } |
| 1682 | |
Eric Fiselier | 78ccf77 | 2017-04-18 23:38:41 +0000 | [diff] [blame] | 1683 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1684 | |
| 1685 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1686 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1687 | void |
| 1688 | basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs) |
| 1689 | { |
| 1690 | basic_iostream<char_type, traits_type>::swap(__rhs); |
| 1691 | __sb_.swap(__rhs.__sb_); |
| 1692 | } |
| 1693 | |
| 1694 | template <class _CharT, class _Traits> |
| 1695 | inline _LIBCPP_INLINE_VISIBILITY |
| 1696 | void |
| 1697 | swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y) |
| 1698 | { |
| 1699 | __x.swap(__y); |
| 1700 | } |
| 1701 | |
| 1702 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1703 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1704 | basic_filebuf<_CharT, _Traits>* |
| 1705 | basic_fstream<_CharT, _Traits>::rdbuf() const |
| 1706 | { |
| 1707 | return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_); |
| 1708 | } |
| 1709 | |
| 1710 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1711 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1712 | bool |
| 1713 | basic_fstream<_CharT, _Traits>::is_open() const |
| 1714 | { |
| 1715 | return __sb_.is_open(); |
| 1716 | } |
| 1717 | |
Ed Schouten | 7009f4e | 2015-03-12 15:44:39 +0000 | [diff] [blame] | 1718 | #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1719 | template <class _CharT, class _Traits> |
| 1720 | void |
| 1721 | basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) |
| 1722 | { |
| 1723 | if (__sb_.open(__s, __mode)) |
| 1724 | this->clear(); |
| 1725 | else |
| 1726 | this->setstate(ios_base::failbit); |
| 1727 | } |
| 1728 | |
Peter Collingbourne | 63ebbd7 | 2018-01-23 02:07:27 +0000 | [diff] [blame] | 1729 | #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR |
| 1730 | template <class _CharT, class _Traits> |
| 1731 | void |
| 1732 | basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) |
| 1733 | { |
| 1734 | if (__sb_.open(__s, __mode)) |
| 1735 | this->clear(); |
| 1736 | else |
| 1737 | this->setstate(ios_base::failbit); |
| 1738 | } |
| 1739 | #endif |
| 1740 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1741 | template <class _CharT, class _Traits> |
| 1742 | void |
| 1743 | basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) |
| 1744 | { |
| 1745 | if (__sb_.open(__s, __mode)) |
| 1746 | this->clear(); |
| 1747 | else |
| 1748 | this->setstate(ios_base::failbit); |
| 1749 | } |
Ed Schouten | 7009f4e | 2015-03-12 15:44:39 +0000 | [diff] [blame] | 1750 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1751 | |
| 1752 | template <class _CharT, class _Traits> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 1753 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1754 | void |
| 1755 | basic_fstream<_CharT, _Traits>::close() |
| 1756 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 1757 | if (__sb_.close() == nullptr) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1758 | this->setstate(ios_base::failbit); |
| 1759 | } |
| 1760 | |
Louis Dionne | 8a79be6 | 2020-10-21 11:11:45 -0400 | [diff] [blame] | 1761 | #if defined(_LIBCPP_ABI_ENABLE_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1) |
| 1762 | _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ifstream<char>) |
| 1763 | _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ofstream<char>) |
| 1764 | _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_filebuf<char>) |
| 1765 | #endif |
| 1766 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1767 | _LIBCPP_END_NAMESPACE_STD |
| 1768 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 1769 | _LIBCPP_POP_MACROS |
| 1770 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1771 | #endif // _LIBCPP_FSTREAM |