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