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