fstream 53 KB

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