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