fstream 55 KB

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