fstream 53 KB

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