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