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