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