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