fstream 54 KB

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