fstream 54 KB

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