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