10b57cec5SDimitry Andric// -*- C++ -*- 2349cc55cSDimitry Andric//===----------------------------------------------------------------------===// 30b57cec5SDimitry Andric// 40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information. 60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 70b57cec5SDimitry Andric// 80b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 90b57cec5SDimitry Andric 100b57cec5SDimitry Andric#ifndef _LIBCPP_FSTREAM 110b57cec5SDimitry Andric#define _LIBCPP_FSTREAM 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric/* 140b57cec5SDimitry Andric fstream synopsis 150b57cec5SDimitry Andric 160b57cec5SDimitry Andrictemplate <class charT, class traits = char_traits<charT> > 170b57cec5SDimitry Andricclass basic_filebuf 180b57cec5SDimitry Andric : public basic_streambuf<charT, traits> 190b57cec5SDimitry Andric{ 200b57cec5SDimitry Andricpublic: 210b57cec5SDimitry Andric typedef charT char_type; 220b57cec5SDimitry Andric typedef traits traits_type; 230b57cec5SDimitry Andric typedef typename traits_type::int_type int_type; 240b57cec5SDimitry Andric typedef typename traits_type::pos_type pos_type; 250b57cec5SDimitry Andric typedef typename traits_type::off_type off_type; 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric // 27.9.1.2 Constructors/destructor: 280b57cec5SDimitry Andric basic_filebuf(); 290b57cec5SDimitry Andric basic_filebuf(basic_filebuf&& rhs); 300b57cec5SDimitry Andric virtual ~basic_filebuf(); 310b57cec5SDimitry Andric 320b57cec5SDimitry Andric // 27.9.1.3 Assign/swap: 330b57cec5SDimitry Andric basic_filebuf& operator=(basic_filebuf&& rhs); 340b57cec5SDimitry Andric void swap(basic_filebuf& rhs); 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric // 27.9.1.4 Members: 370b57cec5SDimitry Andric bool is_open() const; 380b57cec5SDimitry Andric basic_filebuf* open(const char* s, ios_base::openmode mode); 390b57cec5SDimitry Andric basic_filebuf* open(const string& s, ios_base::openmode mode); 400b57cec5SDimitry Andric basic_filebuf* open(const filesystem::path& p, ios_base::openmode mode); // C++17 410b57cec5SDimitry Andric basic_filebuf* close(); 420b57cec5SDimitry Andric 430b57cec5SDimitry Andricprotected: 440b57cec5SDimitry Andric // 27.9.1.5 Overridden virtual functions: 450b57cec5SDimitry Andric virtual streamsize showmanyc(); 460b57cec5SDimitry Andric virtual int_type underflow(); 470b57cec5SDimitry Andric virtual int_type uflow(); 480b57cec5SDimitry Andric virtual int_type pbackfail(int_type c = traits_type::eof()); 490b57cec5SDimitry Andric virtual int_type overflow (int_type c = traits_type::eof()); 500b57cec5SDimitry Andric virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* s, streamsize n); 510b57cec5SDimitry Andric virtual pos_type seekoff(off_type off, ios_base::seekdir way, 520b57cec5SDimitry Andric ios_base::openmode which = ios_base::in | ios_base::out); 530b57cec5SDimitry Andric virtual pos_type seekpos(pos_type sp, 540b57cec5SDimitry Andric ios_base::openmode which = ios_base::in | ios_base::out); 550b57cec5SDimitry Andric virtual int sync(); 560b57cec5SDimitry Andric virtual void imbue(const locale& loc); 570b57cec5SDimitry Andric}; 580b57cec5SDimitry Andric 590b57cec5SDimitry Andrictemplate <class charT, class traits> 600b57cec5SDimitry Andric void 610b57cec5SDimitry Andric swap(basic_filebuf<charT, traits>& x, basic_filebuf<charT, traits>& y); 620b57cec5SDimitry Andric 630b57cec5SDimitry Andrictypedef basic_filebuf<char> filebuf; 640b57cec5SDimitry Andrictypedef basic_filebuf<wchar_t> wfilebuf; 650b57cec5SDimitry Andric 660b57cec5SDimitry Andrictemplate <class charT, class traits = char_traits<charT> > 670b57cec5SDimitry Andricclass basic_ifstream 680b57cec5SDimitry Andric : public basic_istream<charT,traits> 690b57cec5SDimitry Andric{ 700b57cec5SDimitry Andricpublic: 710b57cec5SDimitry Andric typedef charT char_type; 720b57cec5SDimitry Andric typedef traits traits_type; 730b57cec5SDimitry Andric typedef typename traits_type::int_type int_type; 740b57cec5SDimitry Andric typedef typename traits_type::pos_type pos_type; 750b57cec5SDimitry Andric typedef typename traits_type::off_type off_type; 76*1db9f3b2SDimitry Andric using native_handle_type = typename basic_filebuf<charT, traits>::native_handle_type; // Since C++26 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric basic_ifstream(); 790b57cec5SDimitry Andric explicit basic_ifstream(const char* s, ios_base::openmode mode = ios_base::in); 800b57cec5SDimitry Andric explicit basic_ifstream(const string& s, ios_base::openmode mode = ios_base::in); 810b57cec5SDimitry Andric explicit basic_ifstream(const filesystem::path& p, 820b57cec5SDimitry Andric ios_base::openmode mode = ios_base::in); // C++17 830b57cec5SDimitry Andric basic_ifstream(basic_ifstream&& rhs); 840b57cec5SDimitry Andric 850b57cec5SDimitry Andric basic_ifstream& operator=(basic_ifstream&& rhs); 860b57cec5SDimitry Andric void swap(basic_ifstream& rhs); 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric basic_filebuf<char_type, traits_type>* rdbuf() const; 89*1db9f3b2SDimitry Andric native_handle_type native_handle() const noexcept; // Since C++26 900b57cec5SDimitry Andric bool is_open() const; 910b57cec5SDimitry Andric void open(const char* s, ios_base::openmode mode = ios_base::in); 920b57cec5SDimitry Andric void open(const string& s, ios_base::openmode mode = ios_base::in); 930b57cec5SDimitry Andric void open(const filesystem::path& s, ios_base::openmode mode = ios_base::in); // C++17 940b57cec5SDimitry Andric 950b57cec5SDimitry Andric void close(); 960b57cec5SDimitry Andric}; 970b57cec5SDimitry Andric 980b57cec5SDimitry Andrictemplate <class charT, class traits> 990b57cec5SDimitry Andric void 1000b57cec5SDimitry Andric swap(basic_ifstream<charT, traits>& x, basic_ifstream<charT, traits>& y); 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andrictypedef basic_ifstream<char> ifstream; 1030b57cec5SDimitry Andrictypedef basic_ifstream<wchar_t> wifstream; 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andrictemplate <class charT, class traits = char_traits<charT> > 1060b57cec5SDimitry Andricclass basic_ofstream 1070b57cec5SDimitry Andric : public basic_ostream<charT,traits> 1080b57cec5SDimitry Andric{ 1090b57cec5SDimitry Andricpublic: 1100b57cec5SDimitry Andric typedef charT char_type; 1110b57cec5SDimitry Andric typedef traits traits_type; 1120b57cec5SDimitry Andric typedef typename traits_type::int_type int_type; 1130b57cec5SDimitry Andric typedef typename traits_type::pos_type pos_type; 1140b57cec5SDimitry Andric typedef typename traits_type::off_type off_type; 115*1db9f3b2SDimitry Andric using native_handle_type = typename basic_filebuf<charT, traits>::native_handle_type; // Since C++26 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric basic_ofstream(); 1180b57cec5SDimitry Andric explicit basic_ofstream(const char* s, ios_base::openmode mode = ios_base::out); 1190b57cec5SDimitry Andric explicit basic_ofstream(const string& s, ios_base::openmode mode = ios_base::out); 1200b57cec5SDimitry Andric explicit basic_ofstream(const filesystem::path& p, 1210b57cec5SDimitry Andric ios_base::openmode mode = ios_base::out); // C++17 1220b57cec5SDimitry Andric basic_ofstream(basic_ofstream&& rhs); 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric basic_ofstream& operator=(basic_ofstream&& rhs); 1250b57cec5SDimitry Andric void swap(basic_ofstream& rhs); 1260b57cec5SDimitry Andric 1270b57cec5SDimitry Andric basic_filebuf<char_type, traits_type>* rdbuf() const; 128*1db9f3b2SDimitry Andric native_handle_type native_handle() const noexcept; // Since C++26 129*1db9f3b2SDimitry Andric 1300b57cec5SDimitry Andric bool is_open() const; 1310b57cec5SDimitry Andric void open(const char* s, ios_base::openmode mode = ios_base::out); 1320b57cec5SDimitry Andric void open(const string& s, ios_base::openmode mode = ios_base::out); 1330b57cec5SDimitry Andric void open(const filesystem::path& p, 1340b57cec5SDimitry Andric ios_base::openmode mode = ios_base::out); // C++17 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric void close(); 1370b57cec5SDimitry Andric}; 1380b57cec5SDimitry Andric 1390b57cec5SDimitry Andrictemplate <class charT, class traits> 1400b57cec5SDimitry Andric void 1410b57cec5SDimitry Andric swap(basic_ofstream<charT, traits>& x, basic_ofstream<charT, traits>& y); 1420b57cec5SDimitry Andric 1430b57cec5SDimitry Andrictypedef basic_ofstream<char> ofstream; 1440b57cec5SDimitry Andrictypedef basic_ofstream<wchar_t> wofstream; 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andrictemplate <class charT, class traits=char_traits<charT> > 1470b57cec5SDimitry Andricclass basic_fstream 1480b57cec5SDimitry Andric : public basic_iostream<charT,traits> 1490b57cec5SDimitry Andric{ 1500b57cec5SDimitry Andricpublic: 1510b57cec5SDimitry Andric typedef charT char_type; 1520b57cec5SDimitry Andric typedef traits traits_type; 1530b57cec5SDimitry Andric typedef typename traits_type::int_type int_type; 1540b57cec5SDimitry Andric typedef typename traits_type::pos_type pos_type; 1550b57cec5SDimitry Andric typedef typename traits_type::off_type off_type; 156*1db9f3b2SDimitry Andric using native_handle_type = typename basic_filebuf<charT, traits>::native_handle_type; // Since C++26 1570b57cec5SDimitry Andric 1580b57cec5SDimitry Andric basic_fstream(); 1590b57cec5SDimitry Andric explicit basic_fstream(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out); 1600b57cec5SDimitry Andric explicit basic_fstream(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out); 1610b57cec5SDimitry Andric explicit basic_fstream(const filesystem::path& p, 1620b57cec5SDimitry Andric ios_base::openmode mode = ios_base::in|ios_base::out); C++17 1630b57cec5SDimitry Andric basic_fstream(basic_fstream&& rhs); 1640b57cec5SDimitry Andric 1650b57cec5SDimitry Andric basic_fstream& operator=(basic_fstream&& rhs); 1660b57cec5SDimitry Andric void swap(basic_fstream& rhs); 1670b57cec5SDimitry Andric 1680b57cec5SDimitry Andric basic_filebuf<char_type, traits_type>* rdbuf() const; 169*1db9f3b2SDimitry Andric native_handle_type native_handle() const noexcept; // Since C++26 1700b57cec5SDimitry Andric bool is_open() const; 1710b57cec5SDimitry Andric void open(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out); 1720b57cec5SDimitry Andric void open(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out); 1730b57cec5SDimitry Andric void open(const filesystem::path& s, 1740b57cec5SDimitry Andric ios_base::openmode mode = ios_base::in|ios_base::out); // C++17 1750b57cec5SDimitry Andric 1760b57cec5SDimitry Andric void close(); 1770b57cec5SDimitry Andric}; 1780b57cec5SDimitry Andric 1790b57cec5SDimitry Andrictemplate <class charT, class traits> 1800b57cec5SDimitry Andric void swap(basic_fstream<charT, traits>& x, basic_fstream<charT, traits>& y); 1810b57cec5SDimitry Andric 1820b57cec5SDimitry Andrictypedef basic_fstream<char> fstream; 1830b57cec5SDimitry Andrictypedef basic_fstream<wchar_t> wfstream; 1840b57cec5SDimitry Andric 1850b57cec5SDimitry Andric} // std 1860b57cec5SDimitry Andric 1870b57cec5SDimitry Andric*/ 1880b57cec5SDimitry Andric 18981ad6265SDimitry Andric#include <__algorithm/max.h> 19081ad6265SDimitry Andric#include <__assert> // all public C++ headers provide the assertion handler 191e8d8bef9SDimitry Andric#include <__availability> 192fe6060f1SDimitry Andric#include <__config> 19306c3fb27SDimitry Andric#include <__fwd/fstream.h> 1940b57cec5SDimitry Andric#include <__locale> 19581ad6265SDimitry Andric#include <__utility/move.h> 19681ad6265SDimitry Andric#include <__utility/swap.h> 19781ad6265SDimitry Andric#include <__utility/unreachable.h> 1980b57cec5SDimitry Andric#include <cstdio> 19906c3fb27SDimitry Andric#include <filesystem> 200fe6060f1SDimitry Andric#include <istream> 201fe6060f1SDimitry Andric#include <ostream> 202bdd1243dSDimitry Andric#include <typeinfo> 20304eeddc0SDimitry Andric#include <version> 204e8d8bef9SDimitry Andric 2050b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 2060b57cec5SDimitry Andric# pragma GCC system_header 2070b57cec5SDimitry Andric#endif 2080b57cec5SDimitry Andric 2090b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS 2100b57cec5SDimitry Andric#include <__undef_macros> 2110b57cec5SDimitry Andric 212fe6060f1SDimitry Andric#if defined(_LIBCPP_MSVCRT) || defined(_NEWLIB_VERSION) 213fe6060f1SDimitry Andric# define _LIBCPP_HAS_NO_OFF_T_FUNCTIONS 214fe6060f1SDimitry Andric#endif 2150b57cec5SDimitry Andric 21606c3fb27SDimitry Andric#if !defined(_LIBCPP_HAS_NO_FILESYSTEM) 217bdd1243dSDimitry Andric 2180b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD 2190b57cec5SDimitry Andric 220*1db9f3b2SDimitry Andric# if _LIBCPP_STD_VER >= 26 && defined(_LIBCPP_WIN32API) 221*1db9f3b2SDimitry Andric_LIBCPP_EXPORTED_FROM_ABI void* __filebuf_windows_native_handle(FILE* __file) noexcept; 222*1db9f3b2SDimitry Andric# endif 223*1db9f3b2SDimitry Andric 2240b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 225cb14a3feSDimitry Andricclass _LIBCPP_TEMPLATE_VIS basic_filebuf : public basic_streambuf<_CharT, _Traits> { 2260b57cec5SDimitry Andricpublic: 2270b57cec5SDimitry Andric typedef _CharT char_type; 2280b57cec5SDimitry Andric typedef _Traits traits_type; 2290b57cec5SDimitry Andric typedef typename traits_type::int_type int_type; 2300b57cec5SDimitry Andric typedef typename traits_type::pos_type pos_type; 2310b57cec5SDimitry Andric typedef typename traits_type::off_type off_type; 2320b57cec5SDimitry Andric typedef typename traits_type::state_type state_type; 233*1db9f3b2SDimitry Andric# if _LIBCPP_STD_VER >= 26 234*1db9f3b2SDimitry Andric# if defined(_LIBCPP_WIN32API) 235*1db9f3b2SDimitry Andric using native_handle_type = void*; // HANDLE 236*1db9f3b2SDimitry Andric# elif __has_include(<unistd.h>) 237*1db9f3b2SDimitry Andric using native_handle_type = int; // POSIX file descriptor 238*1db9f3b2SDimitry Andric# else 239*1db9f3b2SDimitry Andric# error "Provide a native file handle!" 240*1db9f3b2SDimitry Andric# endif 241*1db9f3b2SDimitry Andric# endif 2420b57cec5SDimitry Andric 2430b57cec5SDimitry Andric // 27.9.1.2 Constructors/destructor: 2440b57cec5SDimitry Andric basic_filebuf(); 2450b57cec5SDimitry Andric basic_filebuf(basic_filebuf&& __rhs); 246bdd1243dSDimitry Andric ~basic_filebuf() override; 2470b57cec5SDimitry Andric 2480b57cec5SDimitry Andric // 27.9.1.3 Assign/swap: 249cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_filebuf& operator=(basic_filebuf&& __rhs); 2500b57cec5SDimitry Andric void swap(basic_filebuf& __rhs); 2510b57cec5SDimitry Andric 2520b57cec5SDimitry Andric // 27.9.1.4 Members: 253cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI bool is_open() const; 2540b57cec5SDimitry Andric basic_filebuf* open(const char* __s, ios_base::openmode __mode); 2550b57cec5SDimitry Andric# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 2560b57cec5SDimitry Andric basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode); 2570b57cec5SDimitry Andric# endif 258cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_filebuf* open(const string& __s, ios_base::openmode __mode); 2590b57cec5SDimitry Andric 26006c3fb27SDimitry Andric# if _LIBCPP_STD_VER >= 17 261cb14a3feSDimitry Andric _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI basic_filebuf* 262cb14a3feSDimitry Andric open(const filesystem::path& __p, ios_base::openmode __mode) { 2630b57cec5SDimitry Andric return open(__p.c_str(), __mode); 2640b57cec5SDimitry Andric } 2650b57cec5SDimitry Andric# endif 266cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_filebuf* __open(int __fd, ios_base::openmode __mode); 2670b57cec5SDimitry Andric basic_filebuf* close(); 268*1db9f3b2SDimitry Andric# if _LIBCPP_STD_VER >= 26 269*1db9f3b2SDimitry Andric _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { 270*1db9f3b2SDimitry Andric _LIBCPP_ASSERT_UNCATEGORIZED(this->is_open(), "File must be opened"); 271*1db9f3b2SDimitry Andric# if defined(_LIBCPP_WIN32API) 272*1db9f3b2SDimitry Andric return std::__filebuf_windows_native_handle(__file_); 273*1db9f3b2SDimitry Andric# elif __has_include(<unistd.h>) 274*1db9f3b2SDimitry Andric return fileno(__file_); 275*1db9f3b2SDimitry Andric# else 276*1db9f3b2SDimitry Andric# error "Provide a way to determine the file native handle!" 277*1db9f3b2SDimitry Andric# endif 278*1db9f3b2SDimitry Andric } 279*1db9f3b2SDimitry Andric# endif // _LIBCPP_STD_VER >= 26 2800b57cec5SDimitry Andric 281cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI inline static const char* __make_mdstring(ios_base::openmode __mode) _NOEXCEPT; 2820b57cec5SDimitry Andric 2830b57cec5SDimitry Andricprotected: 2840b57cec5SDimitry Andric // 27.9.1.5 Overridden virtual functions: 285bdd1243dSDimitry Andric int_type underflow() override; 286bdd1243dSDimitry Andric int_type pbackfail(int_type __c = traits_type::eof()) override; 287bdd1243dSDimitry Andric int_type overflow(int_type __c = traits_type::eof()) override; 288bdd1243dSDimitry Andric basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n) override; 289cb14a3feSDimitry Andric pos_type 290cb14a3feSDimitry Andric seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __wch = ios_base::in | ios_base::out) override; 291cb14a3feSDimitry Andric pos_type seekpos(pos_type __sp, ios_base::openmode __wch = ios_base::in | ios_base::out) override; 292bdd1243dSDimitry Andric int sync() override; 293bdd1243dSDimitry Andric void imbue(const locale& __loc) override; 2940b57cec5SDimitry Andric 2950b57cec5SDimitry Andricprivate: 2960b57cec5SDimitry Andric char* __extbuf_; 2970b57cec5SDimitry Andric const char* __extbufnext_; 2980b57cec5SDimitry Andric const char* __extbufend_; 2990b57cec5SDimitry Andric char __extbuf_min_[8]; 3000b57cec5SDimitry Andric size_t __ebs_; 3010b57cec5SDimitry Andric char_type* __intbuf_; 3020b57cec5SDimitry Andric size_t __ibs_; 3030b57cec5SDimitry Andric FILE* __file_; 3040b57cec5SDimitry Andric const codecvt<char_type, char, state_type>* __cv_; 3050b57cec5SDimitry Andric state_type __st_; 3060b57cec5SDimitry Andric state_type __st_last_; 3070b57cec5SDimitry Andric ios_base::openmode __om_; 3080b57cec5SDimitry Andric ios_base::openmode __cm_; 3090b57cec5SDimitry Andric bool __owns_eb_; 3100b57cec5SDimitry Andric bool __owns_ib_; 3110b57cec5SDimitry Andric bool __always_noconv_; 3120b57cec5SDimitry Andric 3130b57cec5SDimitry Andric bool __read_mode(); 3140b57cec5SDimitry Andric void __write_mode(); 315cb14a3feSDimitry Andric 316cb14a3feSDimitry Andric _LIBCPP_EXPORTED_FROM_ABI friend FILE* __get_ostream_file(ostream&); 3170b57cec5SDimitry Andric}; 3180b57cec5SDimitry Andric 3190b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 3200b57cec5SDimitry Andricbasic_filebuf<_CharT, _Traits>::basic_filebuf() 321e8d8bef9SDimitry Andric : __extbuf_(nullptr), 322e8d8bef9SDimitry Andric __extbufnext_(nullptr), 323e8d8bef9SDimitry Andric __extbufend_(nullptr), 3240b57cec5SDimitry Andric __ebs_(0), 325e8d8bef9SDimitry Andric __intbuf_(nullptr), 3260b57cec5SDimitry Andric __ibs_(0), 327e8d8bef9SDimitry Andric __file_(nullptr), 3280b57cec5SDimitry Andric __cv_(nullptr), 3290b57cec5SDimitry Andric __st_(), 3300b57cec5SDimitry Andric __st_last_(), 3310b57cec5SDimitry Andric __om_(0), 3320b57cec5SDimitry Andric __cm_(0), 3330b57cec5SDimitry Andric __owns_eb_(false), 3340b57cec5SDimitry Andric __owns_ib_(false), 335cb14a3feSDimitry Andric __always_noconv_(false) { 336cb14a3feSDimitry Andric if (std::has_facet<codecvt<char_type, char, state_type> >(this->getloc())) { 337bdd1243dSDimitry Andric __cv_ = &std::use_facet<codecvt<char_type, char, state_type> >(this->getloc()); 3380b57cec5SDimitry Andric __always_noconv_ = __cv_->always_noconv(); 3390b57cec5SDimitry Andric } 340e8d8bef9SDimitry Andric setbuf(nullptr, 4096); 3410b57cec5SDimitry Andric} 3420b57cec5SDimitry Andric 3430b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 344cb14a3feSDimitry Andricbasic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs) : basic_streambuf<_CharT, _Traits>(__rhs) { 345cb14a3feSDimitry Andric if (__rhs.__extbuf_ == __rhs.__extbuf_min_) { 3460b57cec5SDimitry Andric __extbuf_ = __extbuf_min_; 3470b57cec5SDimitry Andric __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_); 3480b57cec5SDimitry Andric __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_); 349cb14a3feSDimitry Andric } else { 3500b57cec5SDimitry Andric __extbuf_ = __rhs.__extbuf_; 3510b57cec5SDimitry Andric __extbufnext_ = __rhs.__extbufnext_; 3520b57cec5SDimitry Andric __extbufend_ = __rhs.__extbufend_; 3530b57cec5SDimitry Andric } 3540b57cec5SDimitry Andric __ebs_ = __rhs.__ebs_; 3550b57cec5SDimitry Andric __intbuf_ = __rhs.__intbuf_; 3560b57cec5SDimitry Andric __ibs_ = __rhs.__ibs_; 3570b57cec5SDimitry Andric __file_ = __rhs.__file_; 3580b57cec5SDimitry Andric __cv_ = __rhs.__cv_; 3590b57cec5SDimitry Andric __st_ = __rhs.__st_; 3600b57cec5SDimitry Andric __st_last_ = __rhs.__st_last_; 3610b57cec5SDimitry Andric __om_ = __rhs.__om_; 3620b57cec5SDimitry Andric __cm_ = __rhs.__cm_; 3630b57cec5SDimitry Andric __owns_eb_ = __rhs.__owns_eb_; 3640b57cec5SDimitry Andric __owns_ib_ = __rhs.__owns_ib_; 3650b57cec5SDimitry Andric __always_noconv_ = __rhs.__always_noconv_; 366cb14a3feSDimitry Andric if (__rhs.pbase()) { 3670b57cec5SDimitry Andric if (__rhs.pbase() == __rhs.__intbuf_) 3680b57cec5SDimitry Andric this->setp(__intbuf_, __intbuf_ + (__rhs.epptr() - __rhs.pbase())); 3690b57cec5SDimitry Andric else 370cb14a3feSDimitry Andric this->setp((char_type*)__extbuf_, (char_type*)__extbuf_ + (__rhs.epptr() - __rhs.pbase())); 3710b57cec5SDimitry Andric this->__pbump(__rhs.pptr() - __rhs.pbase()); 372cb14a3feSDimitry Andric } else if (__rhs.eback()) { 3730b57cec5SDimitry Andric if (__rhs.eback() == __rhs.__intbuf_) 374cb14a3feSDimitry Andric this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()), __intbuf_ + (__rhs.egptr() - __rhs.eback())); 3750b57cec5SDimitry Andric else 3760b57cec5SDimitry Andric this->setg((char_type*)__extbuf_, 3770b57cec5SDimitry Andric (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()), 3780b57cec5SDimitry Andric (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback())); 3790b57cec5SDimitry Andric } 380e8d8bef9SDimitry Andric __rhs.__extbuf_ = nullptr; 381e8d8bef9SDimitry Andric __rhs.__extbufnext_ = nullptr; 382e8d8bef9SDimitry Andric __rhs.__extbufend_ = nullptr; 3830b57cec5SDimitry Andric __rhs.__ebs_ = 0; 3840b57cec5SDimitry Andric __rhs.__intbuf_ = 0; 3850b57cec5SDimitry Andric __rhs.__ibs_ = 0; 386e8d8bef9SDimitry Andric __rhs.__file_ = nullptr; 3870b57cec5SDimitry Andric __rhs.__st_ = state_type(); 3880b57cec5SDimitry Andric __rhs.__st_last_ = state_type(); 3890b57cec5SDimitry Andric __rhs.__om_ = 0; 3900b57cec5SDimitry Andric __rhs.__cm_ = 0; 3910b57cec5SDimitry Andric __rhs.__owns_eb_ = false; 3920b57cec5SDimitry Andric __rhs.__owns_ib_ = false; 3930b57cec5SDimitry Andric __rhs.setg(0, 0, 0); 3940b57cec5SDimitry Andric __rhs.setp(0, 0); 3950b57cec5SDimitry Andric} 3960b57cec5SDimitry Andric 3970b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 398cb14a3feSDimitry Andricinline basic_filebuf<_CharT, _Traits>& basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs) { 3990b57cec5SDimitry Andric close(); 4000b57cec5SDimitry Andric swap(__rhs); 4010b57cec5SDimitry Andric return *this; 4020b57cec5SDimitry Andric} 4030b57cec5SDimitry Andric 4040b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 405cb14a3feSDimitry Andricbasic_filebuf<_CharT, _Traits>::~basic_filebuf() { 40606c3fb27SDimitry Andric# ifndef _LIBCPP_HAS_NO_EXCEPTIONS 407cb14a3feSDimitry Andric try { 40806c3fb27SDimitry Andric# endif // _LIBCPP_HAS_NO_EXCEPTIONS 4090b57cec5SDimitry Andric close(); 41006c3fb27SDimitry Andric# ifndef _LIBCPP_HAS_NO_EXCEPTIONS 411cb14a3feSDimitry Andric } catch (...) { 4120b57cec5SDimitry Andric } 41306c3fb27SDimitry Andric# endif // _LIBCPP_HAS_NO_EXCEPTIONS 4140b57cec5SDimitry Andric if (__owns_eb_) 4150b57cec5SDimitry Andric delete[] __extbuf_; 4160b57cec5SDimitry Andric if (__owns_ib_) 4170b57cec5SDimitry Andric delete[] __intbuf_; 4180b57cec5SDimitry Andric} 4190b57cec5SDimitry Andric 4200b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 421cb14a3feSDimitry Andricvoid basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs) { 4220b57cec5SDimitry Andric basic_streambuf<char_type, traits_type>::swap(__rhs); 423cb14a3feSDimitry Andric if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_) { 42481ad6265SDimitry Andric // Neither *this nor __rhs uses the small buffer, so we can simply swap the pointers. 42581ad6265SDimitry Andric std::swap(__extbuf_, __rhs.__extbuf_); 42681ad6265SDimitry Andric std::swap(__extbufnext_, __rhs.__extbufnext_); 42781ad6265SDimitry Andric std::swap(__extbufend_, __rhs.__extbufend_); 428cb14a3feSDimitry Andric } else { 42981ad6265SDimitry Andric ptrdiff_t __ln = __extbufnext_ ? __extbufnext_ - __extbuf_ : 0; 43081ad6265SDimitry Andric ptrdiff_t __le = __extbufend_ ? __extbufend_ - __extbuf_ : 0; 43181ad6265SDimitry Andric ptrdiff_t __rn = __rhs.__extbufnext_ ? __rhs.__extbufnext_ - __rhs.__extbuf_ : 0; 43281ad6265SDimitry Andric ptrdiff_t __re = __rhs.__extbufend_ ? __rhs.__extbufend_ - __rhs.__extbuf_ : 0; 433cb14a3feSDimitry Andric if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_) { 43481ad6265SDimitry Andric // *this uses the small buffer, but __rhs doesn't. 4350b57cec5SDimitry Andric __extbuf_ = __rhs.__extbuf_; 4360b57cec5SDimitry Andric __rhs.__extbuf_ = __rhs.__extbuf_min_; 43781ad6265SDimitry Andric std::memmove(__rhs.__extbuf_min_, __extbuf_min_, sizeof(__extbuf_min_)); 438cb14a3feSDimitry Andric } else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_) { 43981ad6265SDimitry Andric // *this doesn't use the small buffer, but __rhs does. 4400b57cec5SDimitry Andric __rhs.__extbuf_ = __extbuf_; 4410b57cec5SDimitry Andric __extbuf_ = __extbuf_min_; 44281ad6265SDimitry Andric std::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_)); 443cb14a3feSDimitry Andric } else { 44481ad6265SDimitry Andric // Both *this and __rhs use the small buffer. 44581ad6265SDimitry Andric char __tmp[sizeof(__extbuf_min_)]; 44681ad6265SDimitry Andric std::memmove(__tmp, __extbuf_min_, sizeof(__extbuf_min_)); 44781ad6265SDimitry Andric std::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_)); 44881ad6265SDimitry Andric std::memmove(__rhs.__extbuf_min_, __tmp, sizeof(__extbuf_min_)); 4490b57cec5SDimitry Andric } 4500b57cec5SDimitry Andric __extbufnext_ = __extbuf_ + __rn; 4510b57cec5SDimitry Andric __extbufend_ = __extbuf_ + __re; 4520b57cec5SDimitry Andric __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln; 4530b57cec5SDimitry Andric __rhs.__extbufend_ = __rhs.__extbuf_ + __le; 4540b57cec5SDimitry Andric } 4555f757f3fSDimitry Andric std::swap(__ebs_, __rhs.__ebs_); 4565f757f3fSDimitry Andric std::swap(__intbuf_, __rhs.__intbuf_); 4575f757f3fSDimitry Andric std::swap(__ibs_, __rhs.__ibs_); 4585f757f3fSDimitry Andric std::swap(__file_, __rhs.__file_); 4595f757f3fSDimitry Andric std::swap(__cv_, __rhs.__cv_); 4605f757f3fSDimitry Andric std::swap(__st_, __rhs.__st_); 4615f757f3fSDimitry Andric std::swap(__st_last_, __rhs.__st_last_); 4625f757f3fSDimitry Andric std::swap(__om_, __rhs.__om_); 4635f757f3fSDimitry Andric std::swap(__cm_, __rhs.__cm_); 4645f757f3fSDimitry Andric std::swap(__owns_eb_, __rhs.__owns_eb_); 4655f757f3fSDimitry Andric std::swap(__owns_ib_, __rhs.__owns_ib_); 4665f757f3fSDimitry Andric std::swap(__always_noconv_, __rhs.__always_noconv_); 467cb14a3feSDimitry Andric if (this->eback() == (char_type*)__rhs.__extbuf_min_) { 4680b57cec5SDimitry Andric ptrdiff_t __n = this->gptr() - this->eback(); 4690b57cec5SDimitry Andric ptrdiff_t __e = this->egptr() - this->eback(); 470cb14a3feSDimitry Andric this->setg((char_type*)__extbuf_min_, (char_type*)__extbuf_min_ + __n, (char_type*)__extbuf_min_ + __e); 471cb14a3feSDimitry Andric } else if (this->pbase() == (char_type*)__rhs.__extbuf_min_) { 4720b57cec5SDimitry Andric ptrdiff_t __n = this->pptr() - this->pbase(); 4730b57cec5SDimitry Andric ptrdiff_t __e = this->epptr() - this->pbase(); 474cb14a3feSDimitry Andric this->setp((char_type*)__extbuf_min_, (char_type*)__extbuf_min_ + __e); 4750b57cec5SDimitry Andric this->__pbump(__n); 4760b57cec5SDimitry Andric } 477cb14a3feSDimitry Andric if (__rhs.eback() == (char_type*)__extbuf_min_) { 4780b57cec5SDimitry Andric ptrdiff_t __n = __rhs.gptr() - __rhs.eback(); 4790b57cec5SDimitry Andric ptrdiff_t __e = __rhs.egptr() - __rhs.eback(); 480cb14a3feSDimitry Andric __rhs.setg( 481cb14a3feSDimitry Andric (char_type*)__rhs.__extbuf_min_, (char_type*)__rhs.__extbuf_min_ + __n, (char_type*)__rhs.__extbuf_min_ + __e); 482cb14a3feSDimitry Andric } else if (__rhs.pbase() == (char_type*)__extbuf_min_) { 4830b57cec5SDimitry Andric ptrdiff_t __n = __rhs.pptr() - __rhs.pbase(); 4840b57cec5SDimitry Andric ptrdiff_t __e = __rhs.epptr() - __rhs.pbase(); 485cb14a3feSDimitry Andric __rhs.setp((char_type*)__rhs.__extbuf_min_, (char_type*)__rhs.__extbuf_min_ + __e); 4860b57cec5SDimitry Andric __rhs.__pbump(__n); 4870b57cec5SDimitry Andric } 4880b57cec5SDimitry Andric} 4890b57cec5SDimitry Andric 4900b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 491cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI void swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y) { 4920b57cec5SDimitry Andric __x.swap(__y); 4930b57cec5SDimitry Andric} 4940b57cec5SDimitry Andric 4950b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 496cb14a3feSDimitry Andricinline bool basic_filebuf<_CharT, _Traits>::is_open() const { 497e8d8bef9SDimitry Andric return __file_ != nullptr; 4980b57cec5SDimitry Andric} 4990b57cec5SDimitry Andric 5000b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 501cb14a3feSDimitry Andricconst char* basic_filebuf<_CharT, _Traits>::__make_mdstring(ios_base::openmode __mode) _NOEXCEPT { 5020b57cec5SDimitry Andric switch (__mode & ~ios_base::ate) { 5030b57cec5SDimitry Andric case ios_base::out: 5040b57cec5SDimitry Andric case ios_base::out | ios_base::trunc: 505e40139ffSDimitry Andric return "w" _LIBCPP_FOPEN_CLOEXEC_MODE; 5060b57cec5SDimitry Andric case ios_base::out | ios_base::app: 5070b57cec5SDimitry Andric case ios_base::app: 508e40139ffSDimitry Andric return "a" _LIBCPP_FOPEN_CLOEXEC_MODE; 5090b57cec5SDimitry Andric case ios_base::in: 510e40139ffSDimitry Andric return "r" _LIBCPP_FOPEN_CLOEXEC_MODE; 5110b57cec5SDimitry Andric case ios_base::in | ios_base::out: 512e40139ffSDimitry Andric return "r+" _LIBCPP_FOPEN_CLOEXEC_MODE; 5130b57cec5SDimitry Andric case ios_base::in | ios_base::out | ios_base::trunc: 514e40139ffSDimitry Andric return "w+" _LIBCPP_FOPEN_CLOEXEC_MODE; 5150b57cec5SDimitry Andric case ios_base::in | ios_base::out | ios_base::app: 5160b57cec5SDimitry Andric case ios_base::in | ios_base::app: 517e40139ffSDimitry Andric return "a+" _LIBCPP_FOPEN_CLOEXEC_MODE; 5180b57cec5SDimitry Andric case ios_base::out | ios_base::binary: 5190b57cec5SDimitry Andric case ios_base::out | ios_base::trunc | ios_base::binary: 520e40139ffSDimitry Andric return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE; 5210b57cec5SDimitry Andric case ios_base::out | ios_base::app | ios_base::binary: 5220b57cec5SDimitry Andric case ios_base::app | ios_base::binary: 523e40139ffSDimitry Andric return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE; 5240b57cec5SDimitry Andric case ios_base::in | ios_base::binary: 525e40139ffSDimitry Andric return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE; 5260b57cec5SDimitry Andric case ios_base::in | ios_base::out | ios_base::binary: 527e40139ffSDimitry Andric return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE; 5280b57cec5SDimitry Andric case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary: 529e40139ffSDimitry Andric return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE; 5300b57cec5SDimitry Andric case ios_base::in | ios_base::out | ios_base::app | ios_base::binary: 5310b57cec5SDimitry Andric case ios_base::in | ios_base::app | ios_base::binary: 532e40139ffSDimitry Andric return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE; 5335f757f3fSDimitry Andric# if _LIBCPP_STD_VER >= 23 5345f757f3fSDimitry Andric case ios_base::out | ios_base::noreplace: 5355f757f3fSDimitry Andric case ios_base::out | ios_base::trunc | ios_base::noreplace: 5365f757f3fSDimitry Andric return "wx" _LIBCPP_FOPEN_CLOEXEC_MODE; 5375f757f3fSDimitry Andric case ios_base::in | ios_base::out | ios_base::trunc | ios_base::noreplace: 5385f757f3fSDimitry Andric return "w+x" _LIBCPP_FOPEN_CLOEXEC_MODE; 5395f757f3fSDimitry Andric case ios_base::out | ios_base::binary | ios_base::noreplace: 5405f757f3fSDimitry Andric case ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace: 5415f757f3fSDimitry Andric return "wbx" _LIBCPP_FOPEN_CLOEXEC_MODE; 5425f757f3fSDimitry Andric case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace: 5435f757f3fSDimitry Andric return "w+bx" _LIBCPP_FOPEN_CLOEXEC_MODE; 5445f757f3fSDimitry Andric# endif // _LIBCPP_STD_VER >= 23 5450b57cec5SDimitry Andric default: 5460b57cec5SDimitry Andric return nullptr; 5470b57cec5SDimitry Andric } 54881ad6265SDimitry Andric __libcpp_unreachable(); 5490b57cec5SDimitry Andric} 5500b57cec5SDimitry Andric 5510b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 552cb14a3feSDimitry Andricbasic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) { 553e8d8bef9SDimitry Andric basic_filebuf<_CharT, _Traits>* __rt = nullptr; 554cb14a3feSDimitry Andric if (__file_ == nullptr) { 5550b57cec5SDimitry Andric if (const char* __mdstr = __make_mdstring(__mode)) { 5560b57cec5SDimitry Andric __rt = this; 5570b57cec5SDimitry Andric __file_ = fopen(__s, __mdstr); 5580b57cec5SDimitry Andric if (__file_) { 5590b57cec5SDimitry Andric __om_ = __mode; 5600b57cec5SDimitry Andric if (__mode & ios_base::ate) { 5610b57cec5SDimitry Andric if (fseek(__file_, 0, SEEK_END)) { 5620b57cec5SDimitry Andric fclose(__file_); 563e8d8bef9SDimitry Andric __file_ = nullptr; 564e8d8bef9SDimitry Andric __rt = nullptr; 5650b57cec5SDimitry Andric } 5660b57cec5SDimitry Andric } 5670b57cec5SDimitry Andric } else 568e8d8bef9SDimitry Andric __rt = nullptr; 5690b57cec5SDimitry Andric } 5700b57cec5SDimitry Andric } 5710b57cec5SDimitry Andric return __rt; 5720b57cec5SDimitry Andric} 5730b57cec5SDimitry Andric 5740b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 575cb14a3feSDimitry Andricinline basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) { 576e8d8bef9SDimitry Andric basic_filebuf<_CharT, _Traits>* __rt = nullptr; 577e8d8bef9SDimitry Andric if (__file_ == nullptr) { 5780b57cec5SDimitry Andric if (const char* __mdstr = __make_mdstring(__mode)) { 5790b57cec5SDimitry Andric __rt = this; 5800b57cec5SDimitry Andric __file_ = fdopen(__fd, __mdstr); 5810b57cec5SDimitry Andric if (__file_) { 5820b57cec5SDimitry Andric __om_ = __mode; 5830b57cec5SDimitry Andric if (__mode & ios_base::ate) { 5840b57cec5SDimitry Andric if (fseek(__file_, 0, SEEK_END)) { 5850b57cec5SDimitry Andric fclose(__file_); 586e8d8bef9SDimitry Andric __file_ = nullptr; 587e8d8bef9SDimitry Andric __rt = nullptr; 5880b57cec5SDimitry Andric } 5890b57cec5SDimitry Andric } 5900b57cec5SDimitry Andric } else 591e8d8bef9SDimitry Andric __rt = nullptr; 5920b57cec5SDimitry Andric } 5930b57cec5SDimitry Andric } 5940b57cec5SDimitry Andric return __rt; 5950b57cec5SDimitry Andric} 5960b57cec5SDimitry Andric 5970b57cec5SDimitry Andric# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 5980b57cec5SDimitry Andric// This is basically the same as the char* overload except that it uses _wfopen 5990b57cec5SDimitry Andric// and long mode strings. 6000b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 601cb14a3feSDimitry Andricbasic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) { 602e8d8bef9SDimitry Andric basic_filebuf<_CharT, _Traits>* __rt = nullptr; 603cb14a3feSDimitry Andric if (__file_ == nullptr) { 6040b57cec5SDimitry Andric __rt = this; 6050b57cec5SDimitry Andric const wchar_t* __mdstr; 606cb14a3feSDimitry Andric switch (__mode & ~ios_base::ate) { 6070b57cec5SDimitry Andric case ios_base::out: 6080b57cec5SDimitry Andric case ios_base::out | ios_base::trunc: 6090b57cec5SDimitry Andric __mdstr = L"w"; 6100b57cec5SDimitry Andric break; 6110b57cec5SDimitry Andric case ios_base::out | ios_base::app: 6120b57cec5SDimitry Andric case ios_base::app: 6130b57cec5SDimitry Andric __mdstr = L"a"; 6140b57cec5SDimitry Andric break; 6150b57cec5SDimitry Andric case ios_base::in: 6160b57cec5SDimitry Andric __mdstr = L"r"; 6170b57cec5SDimitry Andric break; 6180b57cec5SDimitry Andric case ios_base::in | ios_base::out: 6190b57cec5SDimitry Andric __mdstr = L"r+"; 6200b57cec5SDimitry Andric break; 6210b57cec5SDimitry Andric case ios_base::in | ios_base::out | ios_base::trunc: 6220b57cec5SDimitry Andric __mdstr = L"w+"; 6230b57cec5SDimitry Andric break; 6240b57cec5SDimitry Andric case ios_base::in | ios_base::out | ios_base::app: 6250b57cec5SDimitry Andric case ios_base::in | ios_base::app: 6260b57cec5SDimitry Andric __mdstr = L"a+"; 6270b57cec5SDimitry Andric break; 6280b57cec5SDimitry Andric case ios_base::out | ios_base::binary: 6290b57cec5SDimitry Andric case ios_base::out | ios_base::trunc | ios_base::binary: 6300b57cec5SDimitry Andric __mdstr = L"wb"; 6310b57cec5SDimitry Andric break; 6320b57cec5SDimitry Andric case ios_base::out | ios_base::app | ios_base::binary: 6330b57cec5SDimitry Andric case ios_base::app | ios_base::binary: 6340b57cec5SDimitry Andric __mdstr = L"ab"; 6350b57cec5SDimitry Andric break; 6360b57cec5SDimitry Andric case ios_base::in | ios_base::binary: 6370b57cec5SDimitry Andric __mdstr = L"rb"; 6380b57cec5SDimitry Andric break; 6390b57cec5SDimitry Andric case ios_base::in | ios_base::out | ios_base::binary: 6400b57cec5SDimitry Andric __mdstr = L"r+b"; 6410b57cec5SDimitry Andric break; 6420b57cec5SDimitry Andric case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary: 6430b57cec5SDimitry Andric __mdstr = L"w+b"; 6440b57cec5SDimitry Andric break; 6450b57cec5SDimitry Andric case ios_base::in | ios_base::out | ios_base::app | ios_base::binary: 6460b57cec5SDimitry Andric case ios_base::in | ios_base::app | ios_base::binary: 6470b57cec5SDimitry Andric __mdstr = L"a+b"; 6480b57cec5SDimitry Andric break; 6495f757f3fSDimitry Andric# if _LIBCPP_STD_VER >= 23 6505f757f3fSDimitry Andric case ios_base::out | ios_base::noreplace: 6515f757f3fSDimitry Andric case ios_base::out | ios_base::trunc | ios_base::noreplace: 6525f757f3fSDimitry Andric __mdstr = L"wx"; 6535f757f3fSDimitry Andric break; 6545f757f3fSDimitry Andric case ios_base::in | ios_base::out | ios_base::trunc | ios_base::noreplace: 6555f757f3fSDimitry Andric __mdstr = L"w+x"; 6565f757f3fSDimitry Andric break; 6575f757f3fSDimitry Andric case ios_base::out | ios_base::binary | ios_base::noreplace: 6585f757f3fSDimitry Andric case ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace: 6595f757f3fSDimitry Andric __mdstr = L"wbx"; 6605f757f3fSDimitry Andric break; 6615f757f3fSDimitry Andric case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace: 6625f757f3fSDimitry Andric __mdstr = L"w+bx"; 6635f757f3fSDimitry Andric break; 6645f757f3fSDimitry Andric# endif // _LIBCPP_STD_VER >= 23 6650b57cec5SDimitry Andric default: 666e8d8bef9SDimitry Andric __rt = nullptr; 6670b57cec5SDimitry Andric break; 6680b57cec5SDimitry Andric } 669cb14a3feSDimitry Andric if (__rt) { 6700b57cec5SDimitry Andric __file_ = _wfopen(__s, __mdstr); 671cb14a3feSDimitry Andric if (__file_) { 6720b57cec5SDimitry Andric __om_ = __mode; 673cb14a3feSDimitry Andric if (__mode & ios_base::ate) { 674cb14a3feSDimitry Andric if (fseek(__file_, 0, SEEK_END)) { 6750b57cec5SDimitry Andric fclose(__file_); 676e8d8bef9SDimitry Andric __file_ = nullptr; 677e8d8bef9SDimitry Andric __rt = nullptr; 6780b57cec5SDimitry Andric } 6790b57cec5SDimitry Andric } 680cb14a3feSDimitry Andric } else 681e8d8bef9SDimitry Andric __rt = nullptr; 6820b57cec5SDimitry Andric } 6830b57cec5SDimitry Andric } 6840b57cec5SDimitry Andric return __rt; 6850b57cec5SDimitry Andric} 6860b57cec5SDimitry Andric# endif 6870b57cec5SDimitry Andric 6880b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 689cb14a3feSDimitry Andricinline basic_filebuf<_CharT, _Traits>* 690cb14a3feSDimitry Andricbasic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) { 6910b57cec5SDimitry Andric return open(__s.c_str(), __mode); 6920b57cec5SDimitry Andric} 6930b57cec5SDimitry Andric 6940b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 695cb14a3feSDimitry Andricbasic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::close() { 696e8d8bef9SDimitry Andric basic_filebuf<_CharT, _Traits>* __rt = nullptr; 697cb14a3feSDimitry Andric if (__file_) { 6980b57cec5SDimitry Andric __rt = this; 6990b57cec5SDimitry Andric unique_ptr<FILE, int (*)(FILE*)> __h(__file_, fclose); 7000b57cec5SDimitry Andric if (sync()) 701e8d8bef9SDimitry Andric __rt = nullptr; 702e40139ffSDimitry Andric if (fclose(__h.release())) 703e8d8bef9SDimitry Andric __rt = nullptr; 704e8d8bef9SDimitry Andric __file_ = nullptr; 7050b57cec5SDimitry Andric setbuf(0, 0); 7060b57cec5SDimitry Andric } 7070b57cec5SDimitry Andric return __rt; 7080b57cec5SDimitry Andric} 7090b57cec5SDimitry Andric 7100b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 711cb14a3feSDimitry Andrictypename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::underflow() { 712e8d8bef9SDimitry Andric if (__file_ == nullptr) 7130b57cec5SDimitry Andric return traits_type::eof(); 7140b57cec5SDimitry Andric bool __initial = __read_mode(); 7150b57cec5SDimitry Andric char_type __1buf; 716e8d8bef9SDimitry Andric if (this->gptr() == nullptr) 7170b57cec5SDimitry Andric this->setg(&__1buf, &__1buf + 1, &__1buf + 1); 718bdd1243dSDimitry Andric const size_t __unget_sz = __initial ? 0 : std::min<size_t>((this->egptr() - this->eback()) / 2, 4); 7190b57cec5SDimitry Andric int_type __c = traits_type::eof(); 720cb14a3feSDimitry Andric if (this->gptr() == this->egptr()) { 7215f757f3fSDimitry Andric std::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type)); 722cb14a3feSDimitry Andric if (__always_noconv_) { 7230b57cec5SDimitry Andric size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz); 724bdd1243dSDimitry Andric __nmemb = ::fread(this->eback() + __unget_sz, 1, __nmemb, __file_); 725cb14a3feSDimitry Andric if (__nmemb != 0) { 726cb14a3feSDimitry Andric this->setg(this->eback(), this->eback() + __unget_sz, this->eback() + __unget_sz + __nmemb); 7270b57cec5SDimitry Andric __c = traits_type::to_int_type(*this->gptr()); 7280b57cec5SDimitry Andric } 729cb14a3feSDimitry Andric } else { 730bdd1243dSDimitry Andric if (__extbufend_ != __extbufnext_) { 7315f757f3fSDimitry Andric _LIBCPP_ASSERT_NON_NULL(__extbufnext_ != nullptr, "underflow moving from nullptr"); 7325f757f3fSDimitry Andric _LIBCPP_ASSERT_NON_NULL(__extbuf_ != nullptr, "underflow moving into nullptr"); 7335f757f3fSDimitry Andric std::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_); 734bdd1243dSDimitry Andric } 7350b57cec5SDimitry Andric __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_); 7360b57cec5SDimitry Andric __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_); 737cb14a3feSDimitry Andric size_t __nmemb = 738cb14a3feSDimitry Andric std::min(static_cast<size_t>(__ibs_ - __unget_sz), static_cast<size_t>(__extbufend_ - __extbufnext_)); 7390b57cec5SDimitry Andric codecvt_base::result __r; 7400b57cec5SDimitry Andric __st_last_ = __st_; 7410b57cec5SDimitry Andric size_t __nr = fread((void*)const_cast<char*>(__extbufnext_), 1, __nmemb, __file_); 742cb14a3feSDimitry Andric if (__nr != 0) { 7430b57cec5SDimitry Andric if (!__cv_) 7440b57cec5SDimitry Andric __throw_bad_cast(); 7450b57cec5SDimitry Andric 7460b57cec5SDimitry Andric __extbufend_ = __extbufnext_ + __nr; 7470b57cec5SDimitry Andric char_type* __inext; 748cb14a3feSDimitry Andric __r = __cv_->in( 749cb14a3feSDimitry Andric __st_, __extbuf_, __extbufend_, __extbufnext_, this->eback() + __unget_sz, this->eback() + __ibs_, __inext); 750cb14a3feSDimitry Andric if (__r == codecvt_base::noconv) { 751cb14a3feSDimitry Andric this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)const_cast<char*>(__extbufend_)); 7520b57cec5SDimitry Andric __c = traits_type::to_int_type(*this->gptr()); 753cb14a3feSDimitry Andric } else if (__inext != this->eback() + __unget_sz) { 7540b57cec5SDimitry Andric this->setg(this->eback(), this->eback() + __unget_sz, __inext); 7550b57cec5SDimitry Andric __c = traits_type::to_int_type(*this->gptr()); 7560b57cec5SDimitry Andric } 7570b57cec5SDimitry Andric } 7580b57cec5SDimitry Andric } 759cb14a3feSDimitry Andric } else 7600b57cec5SDimitry Andric __c = traits_type::to_int_type(*this->gptr()); 7610b57cec5SDimitry Andric if (this->eback() == &__1buf) 762e8d8bef9SDimitry Andric this->setg(nullptr, nullptr, nullptr); 7630b57cec5SDimitry Andric return __c; 7640b57cec5SDimitry Andric} 7650b57cec5SDimitry Andric 7660b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 767cb14a3feSDimitry Andrictypename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c) { 768cb14a3feSDimitry Andric if (__file_ && this->eback() < this->gptr()) { 769cb14a3feSDimitry Andric if (traits_type::eq_int_type(__c, traits_type::eof())) { 7700b57cec5SDimitry Andric this->gbump(-1); 7710b57cec5SDimitry Andric return traits_type::not_eof(__c); 7720b57cec5SDimitry Andric } 773cb14a3feSDimitry Andric if ((__om_ & ios_base::out) || traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1])) { 7740b57cec5SDimitry Andric this->gbump(-1); 7750b57cec5SDimitry Andric *this->gptr() = traits_type::to_char_type(__c); 7760b57cec5SDimitry Andric return __c; 7770b57cec5SDimitry Andric } 7780b57cec5SDimitry Andric } 7790b57cec5SDimitry Andric return traits_type::eof(); 7800b57cec5SDimitry Andric} 7810b57cec5SDimitry Andric 7820b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 783cb14a3feSDimitry Andrictypename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::overflow(int_type __c) { 784e8d8bef9SDimitry Andric if (__file_ == nullptr) 7850b57cec5SDimitry Andric return traits_type::eof(); 7860b57cec5SDimitry Andric __write_mode(); 7870b57cec5SDimitry Andric char_type __1buf; 7880b57cec5SDimitry Andric char_type* __pb_save = this->pbase(); 7890b57cec5SDimitry Andric char_type* __epb_save = this->epptr(); 790cb14a3feSDimitry Andric if (!traits_type::eq_int_type(__c, traits_type::eof())) { 791e8d8bef9SDimitry Andric if (this->pptr() == nullptr) 7920b57cec5SDimitry Andric this->setp(&__1buf, &__1buf + 1); 7930b57cec5SDimitry Andric *this->pptr() = traits_type::to_char_type(__c); 7940b57cec5SDimitry Andric this->pbump(1); 7950b57cec5SDimitry Andric } 796cb14a3feSDimitry Andric if (this->pptr() != this->pbase()) { 797cb14a3feSDimitry Andric if (__always_noconv_) { 7980b57cec5SDimitry Andric size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase()); 799bdd1243dSDimitry Andric if (std::fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb) 8000b57cec5SDimitry Andric return traits_type::eof(); 801cb14a3feSDimitry Andric } else { 8020b57cec5SDimitry Andric char* __extbe = __extbuf_; 8030b57cec5SDimitry Andric codecvt_base::result __r; 804cb14a3feSDimitry Andric do { 8050b57cec5SDimitry Andric if (!__cv_) 8060b57cec5SDimitry Andric __throw_bad_cast(); 8070b57cec5SDimitry Andric 8080b57cec5SDimitry Andric const char_type* __e; 809cb14a3feSDimitry Andric __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e, __extbuf_, __extbuf_ + __ebs_, __extbe); 8100b57cec5SDimitry Andric if (__e == this->pbase()) 8110b57cec5SDimitry Andric return traits_type::eof(); 812cb14a3feSDimitry Andric if (__r == codecvt_base::noconv) { 8130b57cec5SDimitry Andric size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase()); 814bdd1243dSDimitry Andric if (std::fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb) 8150b57cec5SDimitry Andric return traits_type::eof(); 816cb14a3feSDimitry Andric } else if (__r == codecvt_base::ok || __r == codecvt_base::partial) { 8170b57cec5SDimitry Andric size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_); 8180b57cec5SDimitry Andric if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb) 8190b57cec5SDimitry Andric return traits_type::eof(); 820cb14a3feSDimitry Andric if (__r == codecvt_base::partial) { 8210b57cec5SDimitry Andric this->setp(const_cast<char_type*>(__e), this->pptr()); 8220b57cec5SDimitry Andric this->__pbump(this->epptr() - this->pbase()); 8230b57cec5SDimitry Andric } 824cb14a3feSDimitry Andric } else 8250b57cec5SDimitry Andric return traits_type::eof(); 8260b57cec5SDimitry Andric } while (__r == codecvt_base::partial); 8270b57cec5SDimitry Andric } 8280b57cec5SDimitry Andric this->setp(__pb_save, __epb_save); 8290b57cec5SDimitry Andric } 8300b57cec5SDimitry Andric return traits_type::not_eof(__c); 8310b57cec5SDimitry Andric} 8320b57cec5SDimitry Andric 8330b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 834cb14a3feSDimitry Andricbasic_streambuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n) { 835e8d8bef9SDimitry Andric this->setg(nullptr, nullptr, nullptr); 836e8d8bef9SDimitry Andric this->setp(nullptr, nullptr); 8370b57cec5SDimitry Andric if (__owns_eb_) 8380b57cec5SDimitry Andric delete[] __extbuf_; 8390b57cec5SDimitry Andric if (__owns_ib_) 8400b57cec5SDimitry Andric delete[] __intbuf_; 8410b57cec5SDimitry Andric __ebs_ = __n; 842cb14a3feSDimitry Andric if (__ebs_ > sizeof(__extbuf_min_)) { 843cb14a3feSDimitry Andric if (__always_noconv_ && __s) { 8440b57cec5SDimitry Andric __extbuf_ = (char*)__s; 8450b57cec5SDimitry Andric __owns_eb_ = false; 846cb14a3feSDimitry Andric } else { 8470b57cec5SDimitry Andric __extbuf_ = new char[__ebs_]; 8480b57cec5SDimitry Andric __owns_eb_ = true; 8490b57cec5SDimitry Andric } 850cb14a3feSDimitry Andric } else { 8510b57cec5SDimitry Andric __extbuf_ = __extbuf_min_; 8520b57cec5SDimitry Andric __ebs_ = sizeof(__extbuf_min_); 8530b57cec5SDimitry Andric __owns_eb_ = false; 8540b57cec5SDimitry Andric } 855cb14a3feSDimitry Andric if (!__always_noconv_) { 8560b57cec5SDimitry Andric __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_)); 857cb14a3feSDimitry Andric if (__s && __ibs_ > sizeof(__extbuf_min_)) { 8580b57cec5SDimitry Andric __intbuf_ = __s; 8590b57cec5SDimitry Andric __owns_ib_ = false; 860cb14a3feSDimitry Andric } else { 8610b57cec5SDimitry Andric __intbuf_ = new char_type[__ibs_]; 8620b57cec5SDimitry Andric __owns_ib_ = true; 8630b57cec5SDimitry Andric } 864cb14a3feSDimitry Andric } else { 8650b57cec5SDimitry Andric __ibs_ = 0; 866e8d8bef9SDimitry Andric __intbuf_ = nullptr; 8670b57cec5SDimitry Andric __owns_ib_ = false; 8680b57cec5SDimitry Andric } 8690b57cec5SDimitry Andric return this; 8700b57cec5SDimitry Andric} 8710b57cec5SDimitry Andric 8720b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 8730b57cec5SDimitry Andrictypename basic_filebuf<_CharT, _Traits>::pos_type 874cb14a3feSDimitry Andricbasic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode) { 8750b57cec5SDimitry Andric if (!__cv_) 8760b57cec5SDimitry Andric __throw_bad_cast(); 8770b57cec5SDimitry Andric 8780b57cec5SDimitry Andric int __width = __cv_->encoding(); 879e8d8bef9SDimitry Andric if (__file_ == nullptr || (__width <= 0 && __off != 0) || sync()) 8800b57cec5SDimitry Andric return pos_type(off_type(-1)); 8810b57cec5SDimitry Andric // __width > 0 || __off == 0 8820b57cec5SDimitry Andric int __whence; 883cb14a3feSDimitry Andric switch (__way) { 8840b57cec5SDimitry Andric case ios_base::beg: 8850b57cec5SDimitry Andric __whence = SEEK_SET; 8860b57cec5SDimitry Andric break; 8870b57cec5SDimitry Andric case ios_base::cur: 8880b57cec5SDimitry Andric __whence = SEEK_CUR; 8890b57cec5SDimitry Andric break; 8900b57cec5SDimitry Andric case ios_base::end: 8910b57cec5SDimitry Andric __whence = SEEK_END; 8920b57cec5SDimitry Andric break; 8930b57cec5SDimitry Andric default: 8940b57cec5SDimitry Andric return pos_type(off_type(-1)); 8950b57cec5SDimitry Andric } 8960b57cec5SDimitry Andric# if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS) 8970b57cec5SDimitry Andric if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence)) 8980b57cec5SDimitry Andric return pos_type(off_type(-1)); 8990b57cec5SDimitry Andric pos_type __r = ftell(__file_); 9000b57cec5SDimitry Andric# else 901bdd1243dSDimitry Andric if (::fseeko(__file_, __width > 0 ? __width * __off : 0, __whence)) 9020b57cec5SDimitry Andric return pos_type(off_type(-1)); 9030b57cec5SDimitry Andric pos_type __r = ftello(__file_); 9040b57cec5SDimitry Andric# endif 9050b57cec5SDimitry Andric __r.state(__st_); 9060b57cec5SDimitry Andric return __r; 9070b57cec5SDimitry Andric} 9080b57cec5SDimitry Andric 9090b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 9100b57cec5SDimitry Andrictypename basic_filebuf<_CharT, _Traits>::pos_type 911cb14a3feSDimitry Andricbasic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode) { 912e8d8bef9SDimitry Andric if (__file_ == nullptr || sync()) 9130b57cec5SDimitry Andric return pos_type(off_type(-1)); 9140b57cec5SDimitry Andric# if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS) 9150b57cec5SDimitry Andric if (fseek(__file_, __sp, SEEK_SET)) 9160b57cec5SDimitry Andric return pos_type(off_type(-1)); 9170b57cec5SDimitry Andric# else 918bdd1243dSDimitry Andric if (::fseeko(__file_, __sp, SEEK_SET)) 9190b57cec5SDimitry Andric return pos_type(off_type(-1)); 9200b57cec5SDimitry Andric# endif 9210b57cec5SDimitry Andric __st_ = __sp.state(); 9220b57cec5SDimitry Andric return __sp; 9230b57cec5SDimitry Andric} 9240b57cec5SDimitry Andric 9250b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 926cb14a3feSDimitry Andricint basic_filebuf<_CharT, _Traits>::sync() { 927e8d8bef9SDimitry Andric if (__file_ == nullptr) 9280b57cec5SDimitry Andric return 0; 9290b57cec5SDimitry Andric if (!__cv_) 9300b57cec5SDimitry Andric __throw_bad_cast(); 9310b57cec5SDimitry Andric 932cb14a3feSDimitry Andric if (__cm_ & ios_base::out) { 9330b57cec5SDimitry Andric if (this->pptr() != this->pbase()) 9340b57cec5SDimitry Andric if (overflow() == traits_type::eof()) 9350b57cec5SDimitry Andric return -1; 9360b57cec5SDimitry Andric codecvt_base::result __r; 937cb14a3feSDimitry Andric do { 9380b57cec5SDimitry Andric char* __extbe; 9390b57cec5SDimitry Andric __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe); 9400b57cec5SDimitry Andric size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_); 9410b57cec5SDimitry Andric if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb) 9420b57cec5SDimitry Andric return -1; 9430b57cec5SDimitry Andric } while (__r == codecvt_base::partial); 9440b57cec5SDimitry Andric if (__r == codecvt_base::error) 9450b57cec5SDimitry Andric return -1; 9460b57cec5SDimitry Andric if (fflush(__file_)) 9470b57cec5SDimitry Andric return -1; 948cb14a3feSDimitry Andric } else if (__cm_ & ios_base::in) { 9490b57cec5SDimitry Andric off_type __c; 9500b57cec5SDimitry Andric state_type __state = __st_last_; 9510b57cec5SDimitry Andric bool __update_st = false; 9520b57cec5SDimitry Andric if (__always_noconv_) 9530b57cec5SDimitry Andric __c = this->egptr() - this->gptr(); 954cb14a3feSDimitry Andric else { 9550b57cec5SDimitry Andric int __width = __cv_->encoding(); 9560b57cec5SDimitry Andric __c = __extbufend_ - __extbufnext_; 9570b57cec5SDimitry Andric if (__width > 0) 9580b57cec5SDimitry Andric __c += __width * (this->egptr() - this->gptr()); 959cb14a3feSDimitry Andric else { 960cb14a3feSDimitry Andric if (this->gptr() != this->egptr()) { 961cb14a3feSDimitry Andric const int __off = __cv_->length(__state, __extbuf_, __extbufnext_, this->gptr() - this->eback()); 9620b57cec5SDimitry Andric __c += __extbufnext_ - __extbuf_ - __off; 9630b57cec5SDimitry Andric __update_st = true; 9640b57cec5SDimitry Andric } 9650b57cec5SDimitry Andric } 9660b57cec5SDimitry Andric } 9670b57cec5SDimitry Andric# if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS) 9680b57cec5SDimitry Andric if (fseek(__file_, -__c, SEEK_CUR)) 9690b57cec5SDimitry Andric return -1; 9700b57cec5SDimitry Andric# else 971bdd1243dSDimitry Andric if (::fseeko(__file_, -__c, SEEK_CUR)) 9720b57cec5SDimitry Andric return -1; 9730b57cec5SDimitry Andric# endif 9740b57cec5SDimitry Andric if (__update_st) 9750b57cec5SDimitry Andric __st_ = __state; 9760b57cec5SDimitry Andric __extbufnext_ = __extbufend_ = __extbuf_; 977e8d8bef9SDimitry Andric this->setg(nullptr, nullptr, nullptr); 9780b57cec5SDimitry Andric __cm_ = 0; 9790b57cec5SDimitry Andric } 9800b57cec5SDimitry Andric return 0; 9810b57cec5SDimitry Andric} 9820b57cec5SDimitry Andric 9830b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 984cb14a3feSDimitry Andricvoid basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc) { 9850b57cec5SDimitry Andric sync(); 986bdd1243dSDimitry Andric __cv_ = &std::use_facet<codecvt<char_type, char, state_type> >(__loc); 9870b57cec5SDimitry Andric bool __old_anc = __always_noconv_; 9880b57cec5SDimitry Andric __always_noconv_ = __cv_->always_noconv(); 989cb14a3feSDimitry Andric if (__old_anc != __always_noconv_) { 990e8d8bef9SDimitry Andric this->setg(nullptr, nullptr, nullptr); 991e8d8bef9SDimitry Andric this->setp(nullptr, nullptr); 9920b57cec5SDimitry Andric // invariant, char_type is char, else we couldn't get here 9930b57cec5SDimitry Andric if (__always_noconv_) // need to dump __intbuf_ 9940b57cec5SDimitry Andric { 9950b57cec5SDimitry Andric if (__owns_eb_) 9960b57cec5SDimitry Andric delete[] __extbuf_; 9970b57cec5SDimitry Andric __owns_eb_ = __owns_ib_; 9980b57cec5SDimitry Andric __ebs_ = __ibs_; 9990b57cec5SDimitry Andric __extbuf_ = (char*)__intbuf_; 10000b57cec5SDimitry Andric __ibs_ = 0; 1001e8d8bef9SDimitry Andric __intbuf_ = nullptr; 10020b57cec5SDimitry Andric __owns_ib_ = false; 1003cb14a3feSDimitry Andric } else // need to obtain an __intbuf_. 10040b57cec5SDimitry Andric { // If __extbuf_ is user-supplied, use it, else new __intbuf_ 1005cb14a3feSDimitry Andric if (!__owns_eb_ && __extbuf_ != __extbuf_min_) { 10060b57cec5SDimitry Andric __ibs_ = __ebs_; 10070b57cec5SDimitry Andric __intbuf_ = (char_type*)__extbuf_; 10080b57cec5SDimitry Andric __owns_ib_ = false; 10090b57cec5SDimitry Andric __extbuf_ = new char[__ebs_]; 10100b57cec5SDimitry Andric __owns_eb_ = true; 1011cb14a3feSDimitry Andric } else { 10120b57cec5SDimitry Andric __ibs_ = __ebs_; 10130b57cec5SDimitry Andric __intbuf_ = new char_type[__ibs_]; 10140b57cec5SDimitry Andric __owns_ib_ = true; 10150b57cec5SDimitry Andric } 10160b57cec5SDimitry Andric } 10170b57cec5SDimitry Andric } 10180b57cec5SDimitry Andric} 10190b57cec5SDimitry Andric 10200b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1021cb14a3feSDimitry Andricbool basic_filebuf<_CharT, _Traits>::__read_mode() { 1022cb14a3feSDimitry Andric if (!(__cm_ & ios_base::in)) { 1023e8d8bef9SDimitry Andric this->setp(nullptr, nullptr); 10240b57cec5SDimitry Andric if (__always_noconv_) 1025cb14a3feSDimitry Andric this->setg((char_type*)__extbuf_, (char_type*)__extbuf_ + __ebs_, (char_type*)__extbuf_ + __ebs_); 10260b57cec5SDimitry Andric else 10270b57cec5SDimitry Andric this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_); 10280b57cec5SDimitry Andric __cm_ = ios_base::in; 10290b57cec5SDimitry Andric return true; 10300b57cec5SDimitry Andric } 10310b57cec5SDimitry Andric return false; 10320b57cec5SDimitry Andric} 10330b57cec5SDimitry Andric 10340b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1035cb14a3feSDimitry Andricvoid basic_filebuf<_CharT, _Traits>::__write_mode() { 1036cb14a3feSDimitry Andric if (!(__cm_ & ios_base::out)) { 1037e8d8bef9SDimitry Andric this->setg(nullptr, nullptr, nullptr); 1038cb14a3feSDimitry Andric if (__ebs_ > sizeof(__extbuf_min_)) { 10390b57cec5SDimitry Andric if (__always_noconv_) 1040cb14a3feSDimitry Andric this->setp((char_type*)__extbuf_, (char_type*)__extbuf_ + (__ebs_ - 1)); 10410b57cec5SDimitry Andric else 10420b57cec5SDimitry Andric this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1)); 1043cb14a3feSDimitry Andric } else 1044e8d8bef9SDimitry Andric this->setp(nullptr, nullptr); 10450b57cec5SDimitry Andric __cm_ = ios_base::out; 10460b57cec5SDimitry Andric } 10470b57cec5SDimitry Andric} 10480b57cec5SDimitry Andric 10490b57cec5SDimitry Andric// basic_ifstream 10500b57cec5SDimitry Andric 10510b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1052cb14a3feSDimitry Andricclass _LIBCPP_TEMPLATE_VIS basic_ifstream : public basic_istream<_CharT, _Traits> { 10530b57cec5SDimitry Andricpublic: 10540b57cec5SDimitry Andric typedef _CharT char_type; 10550b57cec5SDimitry Andric typedef _Traits traits_type; 10560b57cec5SDimitry Andric typedef typename traits_type::int_type int_type; 10570b57cec5SDimitry Andric typedef typename traits_type::pos_type pos_type; 10580b57cec5SDimitry Andric typedef typename traits_type::off_type off_type; 1059*1db9f3b2SDimitry Andric# if _LIBCPP_STD_VER >= 26 1060*1db9f3b2SDimitry Andric using native_handle_type = typename basic_filebuf<_CharT, _Traits>::native_handle_type; 1061*1db9f3b2SDimitry Andric# endif 10620b57cec5SDimitry Andric 1063cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_ifstream(); 1064cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in); 10650b57cec5SDimitry Andric# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 1066cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in); 10670b57cec5SDimitry Andric# endif 1068cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in); 106906c3fb27SDimitry Andric# if _LIBCPP_STD_VER >= 17 1070cb14a3feSDimitry Andric _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream( 1071cb14a3feSDimitry Andric const filesystem::path& __p, ios_base::openmode __mode = ios_base::in) 10720b57cec5SDimitry Andric : basic_ifstream(__p.c_str(), __mode) {} 10730b57cec5SDimitry Andric# endif // _LIBCPP_STD_VER >= 17 1074cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_ifstream(basic_ifstream&& __rhs); 1075cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_ifstream& operator=(basic_ifstream&& __rhs); 1076cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI void swap(basic_ifstream& __rhs); 10770b57cec5SDimitry Andric 1078cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const; 1079*1db9f3b2SDimitry Andric# if _LIBCPP_STD_VER >= 26 1080*1db9f3b2SDimitry Andric _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { return rdbuf()->native_handle(); } 1081*1db9f3b2SDimitry Andric# endif 1082cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI bool is_open() const; 10830b57cec5SDimitry Andric void open(const char* __s, ios_base::openmode __mode = ios_base::in); 10840b57cec5SDimitry Andric# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 10850b57cec5SDimitry Andric void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in); 10860b57cec5SDimitry Andric# endif 10870b57cec5SDimitry Andric void open(const string& __s, ios_base::openmode __mode = ios_base::in); 108806c3fb27SDimitry Andric# if _LIBCPP_STD_VER >= 17 1089cb14a3feSDimitry Andric _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI void 1090cb14a3feSDimitry Andric open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in) { 10910b57cec5SDimitry Andric return open(__p.c_str(), __mode); 10920b57cec5SDimitry Andric } 10930b57cec5SDimitry Andric# endif // _LIBCPP_STD_VER >= 17 10940b57cec5SDimitry Andric 1095cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI void __open(int __fd, ios_base::openmode __mode); 1096cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI void close(); 10970b57cec5SDimitry Andric 10980b57cec5SDimitry Andricprivate: 10990b57cec5SDimitry Andric basic_filebuf<char_type, traits_type> __sb_; 11000b57cec5SDimitry Andric}; 11010b57cec5SDimitry Andric 11020b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1103cb14a3feSDimitry Andricinline basic_ifstream<_CharT, _Traits>::basic_ifstream() : basic_istream<char_type, traits_type>(&__sb_) {} 11040b57cec5SDimitry Andric 11050b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1106cb14a3feSDimitry Andricinline basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode) 1107cb14a3feSDimitry Andric : basic_istream<char_type, traits_type>(&__sb_) { 1108e8d8bef9SDimitry Andric if (__sb_.open(__s, __mode | ios_base::in) == nullptr) 11090b57cec5SDimitry Andric this->setstate(ios_base::failbit); 11100b57cec5SDimitry Andric} 11110b57cec5SDimitry Andric 11120b57cec5SDimitry Andric# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 11130b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1114cb14a3feSDimitry Andricinline basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode) 1115cb14a3feSDimitry Andric : basic_istream<char_type, traits_type>(&__sb_) { 1116e8d8bef9SDimitry Andric if (__sb_.open(__s, __mode | ios_base::in) == nullptr) 11170b57cec5SDimitry Andric this->setstate(ios_base::failbit); 11180b57cec5SDimitry Andric} 11190b57cec5SDimitry Andric# endif 11200b57cec5SDimitry Andric 11210b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1122cb14a3feSDimitry Andricinline basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode) 1123cb14a3feSDimitry Andric : basic_istream<char_type, traits_type>(&__sb_) { 1124e8d8bef9SDimitry Andric if (__sb_.open(__s, __mode | ios_base::in) == nullptr) 11250b57cec5SDimitry Andric this->setstate(ios_base::failbit); 11260b57cec5SDimitry Andric} 11270b57cec5SDimitry Andric 11280b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1129cb14a3feSDimitry Andricinline basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs) 1130cb14a3feSDimitry Andric : basic_istream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) { 11310b57cec5SDimitry Andric this->set_rdbuf(&__sb_); 11320b57cec5SDimitry Andric} 11330b57cec5SDimitry Andric 11340b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1135cb14a3feSDimitry Andricinline basic_ifstream<_CharT, _Traits>& basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs) { 11365f757f3fSDimitry Andric basic_istream<char_type, traits_type>::operator=(std::move(__rhs)); 11375f757f3fSDimitry Andric __sb_ = std::move(__rhs.__sb_); 11380b57cec5SDimitry Andric return *this; 11390b57cec5SDimitry Andric} 11400b57cec5SDimitry Andric 11410b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1142cb14a3feSDimitry Andricinline void basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs) { 11430b57cec5SDimitry Andric basic_istream<char_type, traits_type>::swap(__rhs); 11440b57cec5SDimitry Andric __sb_.swap(__rhs.__sb_); 11450b57cec5SDimitry Andric} 11460b57cec5SDimitry Andric 11470b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1148cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI void swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y) { 11490b57cec5SDimitry Andric __x.swap(__y); 11500b57cec5SDimitry Andric} 11510b57cec5SDimitry Andric 11520b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1153cb14a3feSDimitry Andricinline basic_filebuf<_CharT, _Traits>* basic_ifstream<_CharT, _Traits>::rdbuf() const { 11540b57cec5SDimitry Andric return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_); 11550b57cec5SDimitry Andric} 11560b57cec5SDimitry Andric 11570b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1158cb14a3feSDimitry Andricinline bool basic_ifstream<_CharT, _Traits>::is_open() const { 11590b57cec5SDimitry Andric return __sb_.is_open(); 11600b57cec5SDimitry Andric} 11610b57cec5SDimitry Andric 11620b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1163cb14a3feSDimitry Andricvoid basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) { 11640b57cec5SDimitry Andric if (__sb_.open(__s, __mode | ios_base::in)) 11650b57cec5SDimitry Andric this->clear(); 11660b57cec5SDimitry Andric else 11670b57cec5SDimitry Andric this->setstate(ios_base::failbit); 11680b57cec5SDimitry Andric} 11690b57cec5SDimitry Andric 11700b57cec5SDimitry Andric# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 11710b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1172cb14a3feSDimitry Andricvoid basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) { 11730b57cec5SDimitry Andric if (__sb_.open(__s, __mode | ios_base::in)) 11740b57cec5SDimitry Andric this->clear(); 11750b57cec5SDimitry Andric else 11760b57cec5SDimitry Andric this->setstate(ios_base::failbit); 11770b57cec5SDimitry Andric} 11780b57cec5SDimitry Andric# endif 11790b57cec5SDimitry Andric 11800b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1181cb14a3feSDimitry Andricvoid basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) { 11820b57cec5SDimitry Andric if (__sb_.open(__s, __mode | ios_base::in)) 11830b57cec5SDimitry Andric this->clear(); 11840b57cec5SDimitry Andric else 11850b57cec5SDimitry Andric this->setstate(ios_base::failbit); 11860b57cec5SDimitry Andric} 11870b57cec5SDimitry Andric 11880b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1189cb14a3feSDimitry Andricinline void basic_ifstream<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) { 11900b57cec5SDimitry Andric if (__sb_.__open(__fd, __mode | ios_base::in)) 11910b57cec5SDimitry Andric this->clear(); 11920b57cec5SDimitry Andric else 11930b57cec5SDimitry Andric this->setstate(ios_base::failbit); 11940b57cec5SDimitry Andric} 11950b57cec5SDimitry Andric 11960b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1197cb14a3feSDimitry Andricinline void basic_ifstream<_CharT, _Traits>::close() { 11980b57cec5SDimitry Andric if (__sb_.close() == 0) 11990b57cec5SDimitry Andric this->setstate(ios_base::failbit); 12000b57cec5SDimitry Andric} 12010b57cec5SDimitry Andric 12020b57cec5SDimitry Andric// basic_ofstream 12030b57cec5SDimitry Andric 12040b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1205cb14a3feSDimitry Andricclass _LIBCPP_TEMPLATE_VIS basic_ofstream : public basic_ostream<_CharT, _Traits> { 12060b57cec5SDimitry Andricpublic: 12070b57cec5SDimitry Andric typedef _CharT char_type; 12080b57cec5SDimitry Andric typedef _Traits traits_type; 12090b57cec5SDimitry Andric typedef typename traits_type::int_type int_type; 12100b57cec5SDimitry Andric typedef typename traits_type::pos_type pos_type; 12110b57cec5SDimitry Andric typedef typename traits_type::off_type off_type; 1212*1db9f3b2SDimitry Andric# if _LIBCPP_STD_VER >= 26 1213*1db9f3b2SDimitry Andric using native_handle_type = typename basic_filebuf<_CharT, _Traits>::native_handle_type; 1214*1db9f3b2SDimitry Andric# endif 12150b57cec5SDimitry Andric 1216cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_ofstream(); 1217cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out); 12180b57cec5SDimitry Andric# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 1219cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out); 12200b57cec5SDimitry Andric# endif 1221cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out); 12220b57cec5SDimitry Andric 122306c3fb27SDimitry Andric# if _LIBCPP_STD_VER >= 17 1224cb14a3feSDimitry Andric _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream( 1225cb14a3feSDimitry Andric const filesystem::path& __p, ios_base::openmode __mode = ios_base::out) 12260b57cec5SDimitry Andric : basic_ofstream(__p.c_str(), __mode) {} 12270b57cec5SDimitry Andric# endif // _LIBCPP_STD_VER >= 17 12280b57cec5SDimitry Andric 1229cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_ofstream(basic_ofstream&& __rhs); 1230cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_ofstream& operator=(basic_ofstream&& __rhs); 1231cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI void swap(basic_ofstream& __rhs); 12320b57cec5SDimitry Andric 1233cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const; 1234*1db9f3b2SDimitry Andric# if _LIBCPP_STD_VER >= 26 1235*1db9f3b2SDimitry Andric _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { return rdbuf()->native_handle(); } 1236*1db9f3b2SDimitry Andric# endif 1237cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI bool is_open() const; 12380b57cec5SDimitry Andric void open(const char* __s, ios_base::openmode __mode = ios_base::out); 12390b57cec5SDimitry Andric# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 12400b57cec5SDimitry Andric void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out); 12410b57cec5SDimitry Andric# endif 12420b57cec5SDimitry Andric void open(const string& __s, ios_base::openmode __mode = ios_base::out); 12430b57cec5SDimitry Andric 124406c3fb27SDimitry Andric# if _LIBCPP_STD_VER >= 17 1245cb14a3feSDimitry Andric _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI void 1246cb14a3feSDimitry Andric open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out) { 1247cb14a3feSDimitry Andric return open(__p.c_str(), __mode); 1248cb14a3feSDimitry Andric } 12490b57cec5SDimitry Andric# endif // _LIBCPP_STD_VER >= 17 12500b57cec5SDimitry Andric 1251cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI void __open(int __fd, ios_base::openmode __mode); 1252cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI void close(); 12530b57cec5SDimitry Andric 12540b57cec5SDimitry Andricprivate: 12550b57cec5SDimitry Andric basic_filebuf<char_type, traits_type> __sb_; 12560b57cec5SDimitry Andric}; 12570b57cec5SDimitry Andric 12580b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1259cb14a3feSDimitry Andricinline basic_ofstream<_CharT, _Traits>::basic_ofstream() : basic_ostream<char_type, traits_type>(&__sb_) {} 12600b57cec5SDimitry Andric 12610b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1262cb14a3feSDimitry Andricinline basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode) 1263cb14a3feSDimitry Andric : basic_ostream<char_type, traits_type>(&__sb_) { 1264e8d8bef9SDimitry Andric if (__sb_.open(__s, __mode | ios_base::out) == nullptr) 12650b57cec5SDimitry Andric this->setstate(ios_base::failbit); 12660b57cec5SDimitry Andric} 12670b57cec5SDimitry Andric 12680b57cec5SDimitry Andric# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 12690b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1270cb14a3feSDimitry Andricinline basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode) 1271cb14a3feSDimitry Andric : basic_ostream<char_type, traits_type>(&__sb_) { 1272e8d8bef9SDimitry Andric if (__sb_.open(__s, __mode | ios_base::out) == nullptr) 12730b57cec5SDimitry Andric this->setstate(ios_base::failbit); 12740b57cec5SDimitry Andric} 12750b57cec5SDimitry Andric# endif 12760b57cec5SDimitry Andric 12770b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1278cb14a3feSDimitry Andricinline basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode) 1279cb14a3feSDimitry Andric : basic_ostream<char_type, traits_type>(&__sb_) { 1280e8d8bef9SDimitry Andric if (__sb_.open(__s, __mode | ios_base::out) == nullptr) 12810b57cec5SDimitry Andric this->setstate(ios_base::failbit); 12820b57cec5SDimitry Andric} 12830b57cec5SDimitry Andric 12840b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1285cb14a3feSDimitry Andricinline basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs) 1286cb14a3feSDimitry Andric : basic_ostream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) { 12870b57cec5SDimitry Andric this->set_rdbuf(&__sb_); 12880b57cec5SDimitry Andric} 12890b57cec5SDimitry Andric 12900b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1291cb14a3feSDimitry Andricinline basic_ofstream<_CharT, _Traits>& basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs) { 12925f757f3fSDimitry Andric basic_ostream<char_type, traits_type>::operator=(std::move(__rhs)); 12935f757f3fSDimitry Andric __sb_ = std::move(__rhs.__sb_); 12940b57cec5SDimitry Andric return *this; 12950b57cec5SDimitry Andric} 12960b57cec5SDimitry Andric 12970b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1298cb14a3feSDimitry Andricinline void basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs) { 12990b57cec5SDimitry Andric basic_ostream<char_type, traits_type>::swap(__rhs); 13000b57cec5SDimitry Andric __sb_.swap(__rhs.__sb_); 13010b57cec5SDimitry Andric} 13020b57cec5SDimitry Andric 13030b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1304cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI void swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y) { 13050b57cec5SDimitry Andric __x.swap(__y); 13060b57cec5SDimitry Andric} 13070b57cec5SDimitry Andric 13080b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1309cb14a3feSDimitry Andricinline basic_filebuf<_CharT, _Traits>* basic_ofstream<_CharT, _Traits>::rdbuf() const { 13100b57cec5SDimitry Andric return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_); 13110b57cec5SDimitry Andric} 13120b57cec5SDimitry Andric 13130b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1314cb14a3feSDimitry Andricinline bool basic_ofstream<_CharT, _Traits>::is_open() const { 13150b57cec5SDimitry Andric return __sb_.is_open(); 13160b57cec5SDimitry Andric} 13170b57cec5SDimitry Andric 13180b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1319cb14a3feSDimitry Andricvoid basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) { 13200b57cec5SDimitry Andric if (__sb_.open(__s, __mode | ios_base::out)) 13210b57cec5SDimitry Andric this->clear(); 13220b57cec5SDimitry Andric else 13230b57cec5SDimitry Andric this->setstate(ios_base::failbit); 13240b57cec5SDimitry Andric} 13250b57cec5SDimitry Andric 13260b57cec5SDimitry Andric# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 13270b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1328cb14a3feSDimitry Andricvoid basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) { 13290b57cec5SDimitry Andric if (__sb_.open(__s, __mode | ios_base::out)) 13300b57cec5SDimitry Andric this->clear(); 13310b57cec5SDimitry Andric else 13320b57cec5SDimitry Andric this->setstate(ios_base::failbit); 13330b57cec5SDimitry Andric} 13340b57cec5SDimitry Andric# endif 13350b57cec5SDimitry Andric 13360b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1337cb14a3feSDimitry Andricvoid basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) { 13380b57cec5SDimitry Andric if (__sb_.open(__s, __mode | ios_base::out)) 13390b57cec5SDimitry Andric this->clear(); 13400b57cec5SDimitry Andric else 13410b57cec5SDimitry Andric this->setstate(ios_base::failbit); 13420b57cec5SDimitry Andric} 13430b57cec5SDimitry Andric 13440b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1345cb14a3feSDimitry Andricinline void basic_ofstream<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) { 13460b57cec5SDimitry Andric if (__sb_.__open(__fd, __mode | ios_base::out)) 13470b57cec5SDimitry Andric this->clear(); 13480b57cec5SDimitry Andric else 13490b57cec5SDimitry Andric this->setstate(ios_base::failbit); 13500b57cec5SDimitry Andric} 13510b57cec5SDimitry Andric 13520b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1353cb14a3feSDimitry Andricinline void basic_ofstream<_CharT, _Traits>::close() { 1354e8d8bef9SDimitry Andric if (__sb_.close() == nullptr) 13550b57cec5SDimitry Andric this->setstate(ios_base::failbit); 13560b57cec5SDimitry Andric} 13570b57cec5SDimitry Andric 13580b57cec5SDimitry Andric// basic_fstream 13590b57cec5SDimitry Andric 13600b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1361cb14a3feSDimitry Andricclass _LIBCPP_TEMPLATE_VIS basic_fstream : public basic_iostream<_CharT, _Traits> { 13620b57cec5SDimitry Andricpublic: 13630b57cec5SDimitry Andric typedef _CharT char_type; 13640b57cec5SDimitry Andric typedef _Traits traits_type; 13650b57cec5SDimitry Andric typedef typename traits_type::int_type int_type; 13660b57cec5SDimitry Andric typedef typename traits_type::pos_type pos_type; 13670b57cec5SDimitry Andric typedef typename traits_type::off_type off_type; 1368*1db9f3b2SDimitry Andric# if _LIBCPP_STD_VER >= 26 1369*1db9f3b2SDimitry Andric using native_handle_type = typename basic_filebuf<_CharT, _Traits>::native_handle_type; 1370*1db9f3b2SDimitry Andric# endif 13710b57cec5SDimitry Andric 1372cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_fstream(); 1373cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const char* __s, 1374cb14a3feSDimitry Andric ios_base::openmode __mode = ios_base::in | ios_base::out); 13750b57cec5SDimitry Andric# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 1376cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const wchar_t* __s, 1377cb14a3feSDimitry Andric ios_base::openmode __mode = ios_base::in | ios_base::out); 13780b57cec5SDimitry Andric# endif 1379cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const string& __s, 1380cb14a3feSDimitry Andric ios_base::openmode __mode = ios_base::in | ios_base::out); 13810b57cec5SDimitry Andric 138206c3fb27SDimitry Andric# if _LIBCPP_STD_VER >= 17 1383cb14a3feSDimitry Andric _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI explicit basic_fstream( 1384cb14a3feSDimitry Andric const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out) 13850b57cec5SDimitry Andric : basic_fstream(__p.c_str(), __mode) {} 13860b57cec5SDimitry Andric# endif // _LIBCPP_STD_VER >= 17 13870b57cec5SDimitry Andric 1388cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_fstream(basic_fstream&& __rhs); 13890b57cec5SDimitry Andric 1390cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_fstream& operator=(basic_fstream&& __rhs); 1391fe6060f1SDimitry Andric 1392cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI void swap(basic_fstream& __rhs); 13930b57cec5SDimitry Andric 1394cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const; 1395*1db9f3b2SDimitry Andric# if _LIBCPP_STD_VER >= 26 1396*1db9f3b2SDimitry Andric _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { return rdbuf()->native_handle(); } 1397*1db9f3b2SDimitry Andric# endif 1398cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI bool is_open() const; 139906c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out); 14000b57cec5SDimitry Andric# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 14010b57cec5SDimitry Andric void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out); 14020b57cec5SDimitry Andric# endif 140306c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out); 14040b57cec5SDimitry Andric 140506c3fb27SDimitry Andric# if _LIBCPP_STD_VER >= 17 1406cb14a3feSDimitry Andric _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI void 1407cb14a3feSDimitry Andric open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out) { 1408cb14a3feSDimitry Andric return open(__p.c_str(), __mode); 1409cb14a3feSDimitry Andric } 14100b57cec5SDimitry Andric# endif // _LIBCPP_STD_VER >= 17 14110b57cec5SDimitry Andric 1412cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI void close(); 14130b57cec5SDimitry Andric 14140b57cec5SDimitry Andricprivate: 14150b57cec5SDimitry Andric basic_filebuf<char_type, traits_type> __sb_; 14160b57cec5SDimitry Andric}; 14170b57cec5SDimitry Andric 14180b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1419cb14a3feSDimitry Andricinline basic_fstream<_CharT, _Traits>::basic_fstream() : basic_iostream<char_type, traits_type>(&__sb_) {} 14200b57cec5SDimitry Andric 14210b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1422cb14a3feSDimitry Andricinline basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode) 1423cb14a3feSDimitry Andric : basic_iostream<char_type, traits_type>(&__sb_) { 1424e8d8bef9SDimitry Andric if (__sb_.open(__s, __mode) == nullptr) 14250b57cec5SDimitry Andric this->setstate(ios_base::failbit); 14260b57cec5SDimitry Andric} 14270b57cec5SDimitry Andric 14280b57cec5SDimitry Andric# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 14290b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1430cb14a3feSDimitry Andricinline basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode) 1431cb14a3feSDimitry Andric : basic_iostream<char_type, traits_type>(&__sb_) { 1432e8d8bef9SDimitry Andric if (__sb_.open(__s, __mode) == nullptr) 14330b57cec5SDimitry Andric this->setstate(ios_base::failbit); 14340b57cec5SDimitry Andric} 14350b57cec5SDimitry Andric# endif 14360b57cec5SDimitry Andric 14370b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1438cb14a3feSDimitry Andricinline basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode) 1439cb14a3feSDimitry Andric : basic_iostream<char_type, traits_type>(&__sb_) { 1440e8d8bef9SDimitry Andric if (__sb_.open(__s, __mode) == nullptr) 14410b57cec5SDimitry Andric this->setstate(ios_base::failbit); 14420b57cec5SDimitry Andric} 14430b57cec5SDimitry Andric 14440b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1445cb14a3feSDimitry Andricinline basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs) 1446cb14a3feSDimitry Andric : basic_iostream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) { 14470b57cec5SDimitry Andric this->set_rdbuf(&__sb_); 14480b57cec5SDimitry Andric} 14490b57cec5SDimitry Andric 14500b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1451cb14a3feSDimitry Andricinline basic_fstream<_CharT, _Traits>& basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs) { 14525f757f3fSDimitry Andric basic_iostream<char_type, traits_type>::operator=(std::move(__rhs)); 14535f757f3fSDimitry Andric __sb_ = std::move(__rhs.__sb_); 14540b57cec5SDimitry Andric return *this; 14550b57cec5SDimitry Andric} 14560b57cec5SDimitry Andric 14570b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1458cb14a3feSDimitry Andricinline void basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs) { 14590b57cec5SDimitry Andric basic_iostream<char_type, traits_type>::swap(__rhs); 14600b57cec5SDimitry Andric __sb_.swap(__rhs.__sb_); 14610b57cec5SDimitry Andric} 14620b57cec5SDimitry Andric 14630b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1464cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI void swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y) { 14650b57cec5SDimitry Andric __x.swap(__y); 14660b57cec5SDimitry Andric} 14670b57cec5SDimitry Andric 14680b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1469cb14a3feSDimitry Andricinline basic_filebuf<_CharT, _Traits>* basic_fstream<_CharT, _Traits>::rdbuf() const { 14700b57cec5SDimitry Andric return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_); 14710b57cec5SDimitry Andric} 14720b57cec5SDimitry Andric 14730b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1474cb14a3feSDimitry Andricinline bool basic_fstream<_CharT, _Traits>::is_open() const { 14750b57cec5SDimitry Andric return __sb_.is_open(); 14760b57cec5SDimitry Andric} 14770b57cec5SDimitry Andric 14780b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1479cb14a3feSDimitry Andricvoid basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) { 14800b57cec5SDimitry Andric if (__sb_.open(__s, __mode)) 14810b57cec5SDimitry Andric this->clear(); 14820b57cec5SDimitry Andric else 14830b57cec5SDimitry Andric this->setstate(ios_base::failbit); 14840b57cec5SDimitry Andric} 14850b57cec5SDimitry Andric 14860b57cec5SDimitry Andric# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 14870b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1488cb14a3feSDimitry Andricvoid basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) { 14890b57cec5SDimitry Andric if (__sb_.open(__s, __mode)) 14900b57cec5SDimitry Andric this->clear(); 14910b57cec5SDimitry Andric else 14920b57cec5SDimitry Andric this->setstate(ios_base::failbit); 14930b57cec5SDimitry Andric} 14940b57cec5SDimitry Andric# endif 14950b57cec5SDimitry Andric 14960b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1497cb14a3feSDimitry Andricvoid basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) { 14980b57cec5SDimitry Andric if (__sb_.open(__s, __mode)) 14990b57cec5SDimitry Andric this->clear(); 15000b57cec5SDimitry Andric else 15010b57cec5SDimitry Andric this->setstate(ios_base::failbit); 15020b57cec5SDimitry Andric} 15030b57cec5SDimitry Andric 15040b57cec5SDimitry Andrictemplate <class _CharT, class _Traits> 1505cb14a3feSDimitry Andricinline void basic_fstream<_CharT, _Traits>::close() { 1506e8d8bef9SDimitry Andric if (__sb_.close() == nullptr) 15070b57cec5SDimitry Andric this->setstate(ios_base::failbit); 15080b57cec5SDimitry Andric} 15090b57cec5SDimitry Andric 15105f757f3fSDimitry Andric# if _LIBCPP_AVAILABILITY_HAS_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1 151181ad6265SDimitry Andricextern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ifstream<char>; 151281ad6265SDimitry Andricextern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ofstream<char>; 151381ad6265SDimitry Andricextern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_filebuf<char>; 1514e8d8bef9SDimitry Andric# endif 1515e8d8bef9SDimitry Andric 15160b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD 15170b57cec5SDimitry Andric 151806c3fb27SDimitry Andric#endif // _LIBCPP_HAS_NO_FILESYSTEM 1519bdd1243dSDimitry Andric 15200b57cec5SDimitry Andric_LIBCPP_POP_MACROS 15210b57cec5SDimitry Andric 1522bdd1243dSDimitry Andric#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 1523bdd1243dSDimitry Andric# include <atomic> 1524bdd1243dSDimitry Andric# include <concepts> 152506c3fb27SDimitry Andric# include <cstdlib> 1526bdd1243dSDimitry Andric# include <iosfwd> 1527bdd1243dSDimitry Andric# include <limits> 15285f757f3fSDimitry Andric# include <mutex> 1529bdd1243dSDimitry Andric# include <new> 1530bdd1243dSDimitry Andric# include <stdexcept> 1531bdd1243dSDimitry Andric# include <type_traits> 1532bdd1243dSDimitry Andric#endif 1533bdd1243dSDimitry Andric 15340b57cec5SDimitry Andric#endif // _LIBCPP_FSTREAM 1535