fstream 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  1. // -*- C++ -*-
  2. //===------------------------- fstream ------------------------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is dual licensed under the MIT and the University of Illinois Open
  7. // Source Licenses. See LICENSE.TXT for details.
  8. //
  9. //===----------------------------------------------------------------------===//
  10. #ifndef _LIBCPP_FSTREAM
  11. #define _LIBCPP_FSTREAM
  12. /*
  13. fstream synopsis
  14. template <class charT, class traits = char_traits<charT> >
  15. class basic_filebuf
  16. : public basic_streambuf<charT, traits>
  17. {
  18. public:
  19. typedef charT char_type;
  20. typedef traits traits_type;
  21. typedef typename traits_type::int_type int_type;
  22. typedef typename traits_type::pos_type pos_type;
  23. typedef typename traits_type::off_type off_type;
  24. // 27.9.1.2 Constructors/destructor:
  25. basic_filebuf();
  26. basic_filebuf(basic_filebuf&& rhs);
  27. virtual ~basic_filebuf();
  28. // 27.9.1.3 Assign/swap:
  29. basic_filebuf& operator=(basic_filebuf&& rhs);
  30. void swap(basic_filebuf& rhs);
  31. // 27.9.1.4 Members:
  32. bool is_open() const;
  33. basic_filebuf* open(const char* s, ios_base::openmode mode);
  34. basic_filebuf* open(const string& s, ios_base::openmode mode);
  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. basic_ifstream(basic_ifstream&& rhs);
  70. basic_ifstream& operator=(basic_ifstream&& rhs);
  71. void swap(basic_ifstream& rhs);
  72. basic_filebuf<char_type, traits_type>* rdbuf() const;
  73. bool is_open() const;
  74. void open(const char* s, ios_base::openmode mode = ios_base::in);
  75. void open(const string& s, ios_base::openmode mode = ios_base::in);
  76. void close();
  77. };
  78. template <class charT, class traits>
  79. void
  80. swap(basic_ifstream<charT, traits>& x, basic_ifstream<charT, traits>& y);
  81. typedef basic_ifstream<char> ifstream;
  82. typedef basic_ifstream<wchar_t> wifstream;
  83. template <class charT, class traits = char_traits<charT> >
  84. class basic_ofstream
  85. : public basic_ostream<charT,traits>
  86. {
  87. public:
  88. typedef charT char_type;
  89. typedef traits traits_type;
  90. typedef typename traits_type::int_type int_type;
  91. typedef typename traits_type::pos_type pos_type;
  92. typedef typename traits_type::off_type off_type;
  93. basic_ofstream();
  94. explicit basic_ofstream(const char* s, ios_base::openmode mode = ios_base::out);
  95. explicit basic_ofstream(const string& s, ios_base::openmode mode = ios_base::out);
  96. basic_ofstream(basic_ofstream&& rhs);
  97. basic_ofstream& operator=(basic_ofstream&& rhs);
  98. void swap(basic_ofstream& rhs);
  99. basic_filebuf<char_type, traits_type>* rdbuf() const;
  100. bool is_open() const;
  101. void open(const char* s, ios_base::openmode mode = ios_base::out);
  102. void open(const string& s, ios_base::openmode mode = ios_base::out);
  103. void close();
  104. };
  105. template <class charT, class traits>
  106. void
  107. swap(basic_ofstream<charT, traits>& x, basic_ofstream<charT, traits>& y);
  108. typedef basic_ofstream<char> ofstream;
  109. typedef basic_ofstream<wchar_t> wofstream;
  110. template <class charT, class traits=char_traits<charT> >
  111. class basic_fstream
  112. : public basic_iostream<charT,traits>
  113. {
  114. public:
  115. typedef charT char_type;
  116. typedef traits traits_type;
  117. typedef typename traits_type::int_type int_type;
  118. typedef typename traits_type::pos_type pos_type;
  119. typedef typename traits_type::off_type off_type;
  120. basic_fstream();
  121. explicit basic_fstream(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out);
  122. explicit basic_fstream(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);
  123. basic_fstream(basic_fstream&& rhs);
  124. basic_fstream& operator=(basic_fstream&& rhs);
  125. void swap(basic_fstream& rhs);
  126. basic_filebuf<char_type, traits_type>* rdbuf() const;
  127. bool is_open() const;
  128. void open(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out);
  129. void open(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);
  130. void close();
  131. };
  132. template <class charT, class traits>
  133. void swap(basic_fstream<charT, traits>& x, basic_fstream<charT, traits>& y);
  134. typedef basic_fstream<char> fstream;
  135. typedef basic_fstream<wchar_t> wfstream;
  136. } // std
  137. */
  138. // Not supported in SGX.
  139. #include <__config>
  140. #if !defined(_LIBCPP_SGX_CONFIG)
  141. #include <ostream>
  142. #include <istream>
  143. #include <__locale>
  144. #include <cstdio>
  145. #include <__undef_min_max>
  146. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  147. #pragma GCC system_header
  148. #endif
  149. _LIBCPP_BEGIN_NAMESPACE_STD
  150. template <class _CharT, class _Traits>
  151. class _LIBCPP_TYPE_VIS_ONLY basic_filebuf
  152. : public basic_streambuf<_CharT, _Traits>
  153. {
  154. public:
  155. typedef _CharT char_type;
  156. typedef _Traits traits_type;
  157. typedef typename traits_type::int_type int_type;
  158. typedef typename traits_type::pos_type pos_type;
  159. typedef typename traits_type::off_type off_type;
  160. typedef typename traits_type::state_type state_type;
  161. // 27.9.1.2 Constructors/destructor:
  162. basic_filebuf();
  163. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  164. basic_filebuf(basic_filebuf&& __rhs);
  165. #endif
  166. virtual ~basic_filebuf();
  167. // 27.9.1.3 Assign/swap:
  168. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  169. _LIBCPP_INLINE_VISIBILITY
  170. basic_filebuf& operator=(basic_filebuf&& __rhs);
  171. #endif
  172. void swap(basic_filebuf& __rhs);
  173. // 27.9.1.4 Members:
  174. _LIBCPP_INLINE_VISIBILITY
  175. bool is_open() const;
  176. #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
  177. basic_filebuf* open(const char* __s, ios_base::openmode __mode);
  178. _LIBCPP_INLINE_VISIBILITY
  179. basic_filebuf* open(const string& __s, ios_base::openmode __mode);
  180. #endif
  181. basic_filebuf* close();
  182. protected:
  183. // 27.9.1.5 Overridden virtual functions:
  184. virtual int_type underflow();
  185. virtual int_type pbackfail(int_type __c = traits_type::eof());
  186. virtual int_type overflow (int_type __c = traits_type::eof());
  187. virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n);
  188. virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
  189. ios_base::openmode __wch = ios_base::in | ios_base::out);
  190. virtual pos_type seekpos(pos_type __sp,
  191. ios_base::openmode __wch = ios_base::in | ios_base::out);
  192. virtual int sync();
  193. virtual void imbue(const locale& __loc);
  194. private:
  195. char* __extbuf_;
  196. const char* __extbufnext_;
  197. const char* __extbufend_;
  198. char __extbuf_min_[8];
  199. size_t __ebs_;
  200. char_type* __intbuf_;
  201. size_t __ibs_;
  202. FILE* __file_;
  203. const codecvt<char_type, char, state_type>* __cv_;
  204. state_type __st_;
  205. state_type __st_last_;
  206. ios_base::openmode __om_;
  207. ios_base::openmode __cm_;
  208. bool __owns_eb_;
  209. bool __owns_ib_;
  210. bool __always_noconv_;
  211. bool __read_mode();
  212. void __write_mode();
  213. };
  214. template <class _CharT, class _Traits>
  215. basic_filebuf<_CharT, _Traits>::basic_filebuf()
  216. : __extbuf_(0),
  217. __extbufnext_(0),
  218. __extbufend_(0),
  219. __ebs_(0),
  220. __intbuf_(0),
  221. __ibs_(0),
  222. __file_(0),
  223. __cv_(nullptr),
  224. __st_(),
  225. __st_last_(),
  226. __om_(0),
  227. __cm_(0),
  228. __owns_eb_(false),
  229. __owns_ib_(false),
  230. __always_noconv_(false)
  231. {
  232. if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
  233. {
  234. __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
  235. __always_noconv_ = __cv_->always_noconv();
  236. }
  237. setbuf(0, 4096);
  238. }
  239. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  240. template <class _CharT, class _Traits>
  241. basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
  242. : basic_streambuf<_CharT, _Traits>(__rhs)
  243. {
  244. if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
  245. {
  246. __extbuf_ = __extbuf_min_;
  247. __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
  248. __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
  249. }
  250. else
  251. {
  252. __extbuf_ = __rhs.__extbuf_;
  253. __extbufnext_ = __rhs.__extbufnext_;
  254. __extbufend_ = __rhs.__extbufend_;
  255. }
  256. __ebs_ = __rhs.__ebs_;
  257. __intbuf_ = __rhs.__intbuf_;
  258. __ibs_ = __rhs.__ibs_;
  259. __file_ = __rhs.__file_;
  260. __cv_ = __rhs.__cv_;
  261. __st_ = __rhs.__st_;
  262. __st_last_ = __rhs.__st_last_;
  263. __om_ = __rhs.__om_;
  264. __cm_ = __rhs.__cm_;
  265. __owns_eb_ = __rhs.__owns_eb_;
  266. __owns_ib_ = __rhs.__owns_ib_;
  267. __always_noconv_ = __rhs.__always_noconv_;
  268. if (__rhs.pbase())
  269. {
  270. if (__rhs.pbase() == __rhs.__intbuf_)
  271. this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
  272. else
  273. this->setp((char_type*)__extbuf_,
  274. (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
  275. this->pbump(__rhs. pptr() - __rhs.pbase());
  276. }
  277. else if (__rhs.eback())
  278. {
  279. if (__rhs.eback() == __rhs.__intbuf_)
  280. this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
  281. __intbuf_ + (__rhs.egptr() - __rhs.eback()));
  282. else
  283. this->setg((char_type*)__extbuf_,
  284. (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
  285. (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
  286. }
  287. __rhs.__extbuf_ = 0;
  288. __rhs.__extbufnext_ = 0;
  289. __rhs.__extbufend_ = 0;
  290. __rhs.__ebs_ = 0;
  291. __rhs.__intbuf_ = 0;
  292. __rhs.__ibs_ = 0;
  293. __rhs.__file_ = 0;
  294. __rhs.__st_ = state_type();
  295. __rhs.__st_last_ = state_type();
  296. __rhs.__om_ = 0;
  297. __rhs.__cm_ = 0;
  298. __rhs.__owns_eb_ = false;
  299. __rhs.__owns_ib_ = false;
  300. __rhs.setg(0, 0, 0);
  301. __rhs.setp(0, 0);
  302. }
  303. template <class _CharT, class _Traits>
  304. inline
  305. basic_filebuf<_CharT, _Traits>&
  306. basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
  307. {
  308. close();
  309. swap(__rhs);
  310. return *this;
  311. }
  312. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  313. template <class _CharT, class _Traits>
  314. basic_filebuf<_CharT, _Traits>::~basic_filebuf()
  315. {
  316. #ifndef _LIBCPP_NO_EXCEPTIONS
  317. try
  318. {
  319. #endif // _LIBCPP_NO_EXCEPTIONS
  320. close();
  321. #ifndef _LIBCPP_NO_EXCEPTIONS
  322. }
  323. catch (...)
  324. {
  325. }
  326. #endif // _LIBCPP_NO_EXCEPTIONS
  327. if (__owns_eb_)
  328. delete [] __extbuf_;
  329. if (__owns_ib_)
  330. delete [] __intbuf_;
  331. }
  332. template <class _CharT, class _Traits>
  333. void
  334. basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
  335. {
  336. basic_streambuf<char_type, traits_type>::swap(__rhs);
  337. if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
  338. {
  339. _VSTD::swap(__extbuf_, __rhs.__extbuf_);
  340. _VSTD::swap(__extbufnext_, __rhs.__extbufnext_);
  341. _VSTD::swap(__extbufend_, __rhs.__extbufend_);
  342. }
  343. else
  344. {
  345. ptrdiff_t __ln = __extbufnext_ - __extbuf_;
  346. ptrdiff_t __le = __extbufend_ - __extbuf_;
  347. ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_;
  348. ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_;
  349. if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
  350. {
  351. __extbuf_ = __rhs.__extbuf_;
  352. __rhs.__extbuf_ = __rhs.__extbuf_min_;
  353. }
  354. else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
  355. {
  356. __rhs.__extbuf_ = __extbuf_;
  357. __extbuf_ = __extbuf_min_;
  358. }
  359. __extbufnext_ = __extbuf_ + __rn;
  360. __extbufend_ = __extbuf_ + __re;
  361. __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
  362. __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
  363. }
  364. _VSTD::swap(__ebs_, __rhs.__ebs_);
  365. _VSTD::swap(__intbuf_, __rhs.__intbuf_);
  366. _VSTD::swap(__ibs_, __rhs.__ibs_);
  367. _VSTD::swap(__file_, __rhs.__file_);
  368. _VSTD::swap(__cv_, __rhs.__cv_);
  369. _VSTD::swap(__st_, __rhs.__st_);
  370. _VSTD::swap(__st_last_, __rhs.__st_last_);
  371. _VSTD::swap(__om_, __rhs.__om_);
  372. _VSTD::swap(__cm_, __rhs.__cm_);
  373. _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
  374. _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
  375. _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
  376. if (this->eback() == (char_type*)__rhs.__extbuf_min_)
  377. {
  378. ptrdiff_t __n = this->gptr() - this->eback();
  379. ptrdiff_t __e = this->egptr() - this->eback();
  380. this->setg((char_type*)__extbuf_min_,
  381. (char_type*)__extbuf_min_ + __n,
  382. (char_type*)__extbuf_min_ + __e);
  383. }
  384. else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
  385. {
  386. ptrdiff_t __n = this->pptr() - this->pbase();
  387. ptrdiff_t __e = this->epptr() - this->pbase();
  388. this->setp((char_type*)__extbuf_min_,
  389. (char_type*)__extbuf_min_ + __e);
  390. this->pbump(__n);
  391. }
  392. if (__rhs.eback() == (char_type*)__extbuf_min_)
  393. {
  394. ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
  395. ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
  396. __rhs.setg((char_type*)__rhs.__extbuf_min_,
  397. (char_type*)__rhs.__extbuf_min_ + __n,
  398. (char_type*)__rhs.__extbuf_min_ + __e);
  399. }
  400. else if (__rhs.pbase() == (char_type*)__extbuf_min_)
  401. {
  402. ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
  403. ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
  404. __rhs.setp((char_type*)__rhs.__extbuf_min_,
  405. (char_type*)__rhs.__extbuf_min_ + __e);
  406. __rhs.pbump(__n);
  407. }
  408. }
  409. template <class _CharT, class _Traits>
  410. inline _LIBCPP_INLINE_VISIBILITY
  411. void
  412. swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
  413. {
  414. __x.swap(__y);
  415. }
  416. template <class _CharT, class _Traits>
  417. inline
  418. bool
  419. basic_filebuf<_CharT, _Traits>::is_open() const
  420. {
  421. return __file_ != 0;
  422. }
  423. #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
  424. template <class _CharT, class _Traits>
  425. basic_filebuf<_CharT, _Traits>*
  426. basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
  427. {
  428. basic_filebuf<_CharT, _Traits>* __rt = 0;
  429. if (__file_ == 0)
  430. {
  431. __rt = this;
  432. const char* __mdstr;
  433. switch (__mode & ~ios_base::ate)
  434. {
  435. case ios_base::out:
  436. case ios_base::out | ios_base::trunc:
  437. __mdstr = "w";
  438. break;
  439. case ios_base::out | ios_base::app:
  440. case ios_base::app:
  441. __mdstr = "a";
  442. break;
  443. case ios_base::in:
  444. __mdstr = "r";
  445. break;
  446. case ios_base::in | ios_base::out:
  447. __mdstr = "r+";
  448. break;
  449. case ios_base::in | ios_base::out | ios_base::trunc:
  450. __mdstr = "w+";
  451. break;
  452. case ios_base::in | ios_base::out | ios_base::app:
  453. case ios_base::in | ios_base::app:
  454. __mdstr = "a+";
  455. break;
  456. case ios_base::out | ios_base::binary:
  457. case ios_base::out | ios_base::trunc | ios_base::binary:
  458. __mdstr = "wb";
  459. break;
  460. case ios_base::out | ios_base::app | ios_base::binary:
  461. case ios_base::app | ios_base::binary:
  462. __mdstr = "ab";
  463. break;
  464. case ios_base::in | ios_base::binary:
  465. __mdstr = "rb";
  466. break;
  467. case ios_base::in | ios_base::out | ios_base::binary:
  468. __mdstr = "r+b";
  469. break;
  470. case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
  471. __mdstr = "w+b";
  472. break;
  473. case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
  474. case ios_base::in | ios_base::app | ios_base::binary:
  475. __mdstr = "a+b";
  476. break;
  477. default:
  478. __rt = 0;
  479. break;
  480. }
  481. if (__rt)
  482. {
  483. __file_ = fopen(__s, __mdstr);
  484. if (__file_)
  485. {
  486. __om_ = __mode;
  487. if (__mode & ios_base::ate)
  488. {
  489. if (fseek(__file_, 0, SEEK_END))
  490. {
  491. fclose(__file_);
  492. __file_ = 0;
  493. __rt = 0;
  494. }
  495. }
  496. }
  497. else
  498. __rt = 0;
  499. }
  500. }
  501. return __rt;
  502. }
  503. template <class _CharT, class _Traits>
  504. inline
  505. basic_filebuf<_CharT, _Traits>*
  506. basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
  507. {
  508. return open(__s.c_str(), __mode);
  509. }
  510. #endif
  511. template <class _CharT, class _Traits>
  512. basic_filebuf<_CharT, _Traits>*
  513. basic_filebuf<_CharT, _Traits>::close()
  514. {
  515. basic_filebuf<_CharT, _Traits>* __rt = 0;
  516. if (__file_)
  517. {
  518. __rt = this;
  519. unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
  520. if (sync())
  521. __rt = 0;
  522. if (fclose(__h.release()) == 0)
  523. __file_ = 0;
  524. else
  525. __rt = 0;
  526. }
  527. return __rt;
  528. }
  529. template <class _CharT, class _Traits>
  530. typename basic_filebuf<_CharT, _Traits>::int_type
  531. basic_filebuf<_CharT, _Traits>::underflow()
  532. {
  533. if (__file_ == 0)
  534. return traits_type::eof();
  535. bool __initial = __read_mode();
  536. char_type __1buf;
  537. if (this->gptr() == 0)
  538. this->setg(&__1buf, &__1buf+1, &__1buf+1);
  539. const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
  540. int_type __c = traits_type::eof();
  541. if (this->gptr() == this->egptr())
  542. {
  543. memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
  544. if (__always_noconv_)
  545. {
  546. size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
  547. __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
  548. if (__nmemb != 0)
  549. {
  550. this->setg(this->eback(),
  551. this->eback() + __unget_sz,
  552. this->eback() + __unget_sz + __nmemb);
  553. __c = traits_type::to_int_type(*this->gptr());
  554. }
  555. }
  556. else
  557. {
  558. _LIBCPP_ASSERT ( !(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
  559. if (__extbufend_ != __extbufnext_)
  560. memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
  561. __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
  562. __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
  563. size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
  564. static_cast<size_t>(__extbufend_ - __extbufnext_));
  565. codecvt_base::result __r;
  566. __st_last_ = __st_;
  567. size_t __nr = fread((void*)__extbufnext_, 1, __nmemb, __file_);
  568. if (__nr != 0)
  569. {
  570. #ifndef _LIBCPP_NO_EXCEPTIONS
  571. if (!__cv_)
  572. throw bad_cast();
  573. #endif
  574. __extbufend_ = __extbufnext_ + __nr;
  575. char_type* __inext;
  576. __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
  577. this->eback() + __unget_sz,
  578. this->eback() + __ibs_, __inext);
  579. if (__r == codecvt_base::noconv)
  580. {
  581. this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)__extbufend_);
  582. __c = traits_type::to_int_type(*this->gptr());
  583. }
  584. else if (__inext != this->eback() + __unget_sz)
  585. {
  586. this->setg(this->eback(), this->eback() + __unget_sz, __inext);
  587. __c = traits_type::to_int_type(*this->gptr());
  588. }
  589. }
  590. }
  591. }
  592. else
  593. __c = traits_type::to_int_type(*this->gptr());
  594. if (this->eback() == &__1buf)
  595. this->setg(0, 0, 0);
  596. return __c;
  597. }
  598. template <class _CharT, class _Traits>
  599. typename basic_filebuf<_CharT, _Traits>::int_type
  600. basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
  601. {
  602. if (__file_ && this->eback() < this->gptr())
  603. {
  604. if (traits_type::eq_int_type(__c, traits_type::eof()))
  605. {
  606. this->gbump(-1);
  607. return traits_type::not_eof(__c);
  608. }
  609. if ((__om_ & ios_base::out) ||
  610. traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
  611. {
  612. this->gbump(-1);
  613. *this->gptr() = traits_type::to_char_type(__c);
  614. return __c;
  615. }
  616. }
  617. return traits_type::eof();
  618. }
  619. template <class _CharT, class _Traits>
  620. typename basic_filebuf<_CharT, _Traits>::int_type
  621. basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
  622. {
  623. if (__file_ == 0)
  624. return traits_type::eof();
  625. __write_mode();
  626. char_type __1buf;
  627. char_type* __pb_save = this->pbase();
  628. char_type* __epb_save = this->epptr();
  629. if (!traits_type::eq_int_type(__c, traits_type::eof()))
  630. {
  631. if (this->pptr() == 0)
  632. this->setp(&__1buf, &__1buf+1);
  633. *this->pptr() = traits_type::to_char_type(__c);
  634. this->pbump(1);
  635. }
  636. if (this->pptr() != this->pbase())
  637. {
  638. if (__always_noconv_)
  639. {
  640. size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
  641. if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
  642. return traits_type::eof();
  643. }
  644. else
  645. {
  646. char* __extbe = __extbuf_;
  647. codecvt_base::result __r;
  648. do
  649. {
  650. #ifndef _LIBCPP_NO_EXCEPTIONS
  651. if (!__cv_)
  652. throw bad_cast();
  653. #endif
  654. const char_type* __e;
  655. __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
  656. __extbuf_, __extbuf_ + __ebs_, __extbe);
  657. if (__e == this->pbase())
  658. return traits_type::eof();
  659. if (__r == codecvt_base::noconv)
  660. {
  661. size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
  662. if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
  663. return traits_type::eof();
  664. }
  665. else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
  666. {
  667. size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
  668. if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
  669. return traits_type::eof();
  670. if (__r == codecvt_base::partial)
  671. {
  672. this->setp((char_type*)__e, this->pptr());
  673. this->pbump(this->epptr() - this->pbase());
  674. }
  675. }
  676. else
  677. return traits_type::eof();
  678. } while (__r == codecvt_base::partial);
  679. }
  680. this->setp(__pb_save, __epb_save);
  681. }
  682. return traits_type::not_eof(__c);
  683. }
  684. template <class _CharT, class _Traits>
  685. basic_streambuf<_CharT, _Traits>*
  686. basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
  687. {
  688. this->setg(0, 0, 0);
  689. this->setp(0, 0);
  690. if (__owns_eb_)
  691. delete [] __extbuf_;
  692. if (__owns_ib_)
  693. delete [] __intbuf_;
  694. __ebs_ = __n;
  695. if (__ebs_ > sizeof(__extbuf_min_))
  696. {
  697. if (__always_noconv_ && __s)
  698. {
  699. __extbuf_ = (char*)__s;
  700. __owns_eb_ = false;
  701. }
  702. else
  703. {
  704. __extbuf_ = new char[__ebs_];
  705. __owns_eb_ = true;
  706. }
  707. }
  708. else
  709. {
  710. __extbuf_ = __extbuf_min_;
  711. __ebs_ = sizeof(__extbuf_min_);
  712. __owns_eb_ = false;
  713. }
  714. if (!__always_noconv_)
  715. {
  716. __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
  717. if (__s && __ibs_ >= sizeof(__extbuf_min_))
  718. {
  719. __intbuf_ = __s;
  720. __owns_ib_ = false;
  721. }
  722. else
  723. {
  724. __intbuf_ = new char_type[__ibs_];
  725. __owns_ib_ = true;
  726. }
  727. }
  728. else
  729. {
  730. __ibs_ = 0;
  731. __intbuf_ = 0;
  732. __owns_ib_ = false;
  733. }
  734. return this;
  735. }
  736. template <class _CharT, class _Traits>
  737. typename basic_filebuf<_CharT, _Traits>::pos_type
  738. basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
  739. ios_base::openmode)
  740. {
  741. #ifndef _LIBCPP_NO_EXCEPTIONS
  742. if (!__cv_)
  743. throw bad_cast();
  744. #endif
  745. int __width = __cv_->encoding();
  746. if (__file_ == 0 || (__width <= 0 && __off != 0) || sync())
  747. return pos_type(off_type(-1));
  748. // __width > 0 || __off == 0
  749. int __whence;
  750. switch (__way)
  751. {
  752. case ios_base::beg:
  753. __whence = SEEK_SET;
  754. break;
  755. case ios_base::cur:
  756. __whence = SEEK_CUR;
  757. break;
  758. case ios_base::end:
  759. __whence = SEEK_END;
  760. break;
  761. default:
  762. return pos_type(off_type(-1));
  763. }
  764. #if defined(_WIN32) || defined(_NEWLIB_VERSION)
  765. if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
  766. return pos_type(off_type(-1));
  767. pos_type __r = ftell(__file_);
  768. #else
  769. if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
  770. return pos_type(off_type(-1));
  771. pos_type __r = ftello(__file_);
  772. #endif
  773. __r.state(__st_);
  774. return __r;
  775. }
  776. template <class _CharT, class _Traits>
  777. typename basic_filebuf<_CharT, _Traits>::pos_type
  778. basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
  779. {
  780. if (__file_ == 0 || sync())
  781. return pos_type(off_type(-1));
  782. #if defined(_WIN32) || defined(_NEWLIB_VERSION)
  783. if (fseek(__file_, __sp, SEEK_SET))
  784. return pos_type(off_type(-1));
  785. #else
  786. if (fseeko(__file_, __sp, SEEK_SET))
  787. return pos_type(off_type(-1));
  788. #endif
  789. __st_ = __sp.state();
  790. return __sp;
  791. }
  792. template <class _CharT, class _Traits>
  793. int
  794. basic_filebuf<_CharT, _Traits>::sync()
  795. {
  796. if (__file_ == 0)
  797. return 0;
  798. #ifndef _LIBCPP_NO_EXCEPTIONS
  799. if (!__cv_)
  800. throw bad_cast();
  801. #endif
  802. if (__cm_ & ios_base::out)
  803. {
  804. if (this->pptr() != this->pbase())
  805. if (overflow() == traits_type::eof())
  806. return -1;
  807. codecvt_base::result __r;
  808. do
  809. {
  810. char* __extbe;
  811. __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
  812. size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
  813. if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
  814. return -1;
  815. } while (__r == codecvt_base::partial);
  816. if (__r == codecvt_base::error)
  817. return -1;
  818. if (fflush(__file_))
  819. return -1;
  820. }
  821. else if (__cm_ & ios_base::in)
  822. {
  823. off_type __c;
  824. state_type __state = __st_last_;
  825. bool __update_st = false;
  826. if (__always_noconv_)
  827. __c = this->egptr() - this->gptr();
  828. else
  829. {
  830. int __width = __cv_->encoding();
  831. __c = __extbufend_ - __extbufnext_;
  832. if (__width > 0)
  833. __c += __width * (this->egptr() - this->gptr());
  834. else
  835. {
  836. if (this->gptr() != this->egptr())
  837. {
  838. const int __off = __cv_->length(__state, __extbuf_,
  839. __extbufnext_,
  840. this->gptr() - this->eback());
  841. __c += __extbufnext_ - __extbuf_ - __off;
  842. __update_st = true;
  843. }
  844. }
  845. }
  846. #if defined(_WIN32) || defined(_NEWLIB_VERSION)
  847. if (fseek(__file_, -__c, SEEK_CUR))
  848. return -1;
  849. #else
  850. if (fseeko(__file_, -__c, SEEK_CUR))
  851. return -1;
  852. #endif
  853. if (__update_st)
  854. __st_ = __state;
  855. __extbufnext_ = __extbufend_ = __extbuf_;
  856. this->setg(0, 0, 0);
  857. __cm_ = 0;
  858. }
  859. return 0;
  860. }
  861. template <class _CharT, class _Traits>
  862. void
  863. basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
  864. {
  865. sync();
  866. __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
  867. bool __old_anc = __always_noconv_;
  868. __always_noconv_ = __cv_->always_noconv();
  869. if (__old_anc != __always_noconv_)
  870. {
  871. this->setg(0, 0, 0);
  872. this->setp(0, 0);
  873. // invariant, char_type is char, else we couldn't get here
  874. if (__always_noconv_) // need to dump __intbuf_
  875. {
  876. if (__owns_eb_)
  877. delete [] __extbuf_;
  878. __owns_eb_ = __owns_ib_;
  879. __ebs_ = __ibs_;
  880. __extbuf_ = (char*)__intbuf_;
  881. __ibs_ = 0;
  882. __intbuf_ = 0;
  883. __owns_ib_ = false;
  884. }
  885. else // need to obtain an __intbuf_.
  886. { // If __extbuf_ is user-supplied, use it, else new __intbuf_
  887. if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
  888. {
  889. __ibs_ = __ebs_;
  890. __intbuf_ = (char_type*)__extbuf_;
  891. __owns_ib_ = false;
  892. __extbuf_ = new char[__ebs_];
  893. __owns_eb_ = true;
  894. }
  895. else
  896. {
  897. __ibs_ = __ebs_;
  898. __intbuf_ = new char_type[__ibs_];
  899. __owns_ib_ = true;
  900. }
  901. }
  902. }
  903. }
  904. template <class _CharT, class _Traits>
  905. bool
  906. basic_filebuf<_CharT, _Traits>::__read_mode()
  907. {
  908. if (!(__cm_ & ios_base::in))
  909. {
  910. this->setp(0, 0);
  911. if (__always_noconv_)
  912. this->setg((char_type*)__extbuf_,
  913. (char_type*)__extbuf_ + __ebs_,
  914. (char_type*)__extbuf_ + __ebs_);
  915. else
  916. this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
  917. __cm_ = ios_base::in;
  918. return true;
  919. }
  920. return false;
  921. }
  922. template <class _CharT, class _Traits>
  923. void
  924. basic_filebuf<_CharT, _Traits>::__write_mode()
  925. {
  926. if (!(__cm_ & ios_base::out))
  927. {
  928. this->setg(0, 0, 0);
  929. if (__ebs_ > sizeof(__extbuf_min_))
  930. {
  931. if (__always_noconv_)
  932. this->setp((char_type*)__extbuf_,
  933. (char_type*)__extbuf_ + (__ebs_ - 1));
  934. else
  935. this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
  936. }
  937. else
  938. this->setp(0, 0);
  939. __cm_ = ios_base::out;
  940. }
  941. }
  942. // basic_ifstream
  943. template <class _CharT, class _Traits>
  944. class _LIBCPP_TYPE_VIS_ONLY basic_ifstream
  945. : public basic_istream<_CharT, _Traits>
  946. {
  947. public:
  948. typedef _CharT char_type;
  949. typedef _Traits traits_type;
  950. typedef typename traits_type::int_type int_type;
  951. typedef typename traits_type::pos_type pos_type;
  952. typedef typename traits_type::off_type off_type;
  953. _LIBCPP_INLINE_VISIBILITY
  954. basic_ifstream();
  955. #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
  956. _LIBCPP_INLINE_VISIBILITY
  957. explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
  958. _LIBCPP_INLINE_VISIBILITY
  959. explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
  960. #endif
  961. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  962. _LIBCPP_INLINE_VISIBILITY
  963. basic_ifstream(basic_ifstream&& __rhs);
  964. #endif
  965. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  966. _LIBCPP_INLINE_VISIBILITY
  967. basic_ifstream& operator=(basic_ifstream&& __rhs);
  968. #endif
  969. _LIBCPP_INLINE_VISIBILITY
  970. void swap(basic_ifstream& __rhs);
  971. _LIBCPP_INLINE_VISIBILITY
  972. basic_filebuf<char_type, traits_type>* rdbuf() const;
  973. _LIBCPP_INLINE_VISIBILITY
  974. bool is_open() const;
  975. #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
  976. void open(const char* __s, ios_base::openmode __mode = ios_base::in);
  977. void open(const string& __s, ios_base::openmode __mode = ios_base::in);
  978. #endif
  979. _LIBCPP_INLINE_VISIBILITY
  980. void close();
  981. private:
  982. basic_filebuf<char_type, traits_type> __sb_;
  983. };
  984. template <class _CharT, class _Traits>
  985. inline
  986. basic_ifstream<_CharT, _Traits>::basic_ifstream()
  987. : basic_istream<char_type, traits_type>(&__sb_)
  988. {
  989. }
  990. #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
  991. template <class _CharT, class _Traits>
  992. inline
  993. basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
  994. : basic_istream<char_type, traits_type>(&__sb_)
  995. {
  996. if (__sb_.open(__s, __mode | ios_base::in) == 0)
  997. this->setstate(ios_base::failbit);
  998. }
  999. template <class _CharT, class _Traits>
  1000. inline
  1001. basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
  1002. : basic_istream<char_type, traits_type>(&__sb_)
  1003. {
  1004. if (__sb_.open(__s, __mode | ios_base::in) == 0)
  1005. this->setstate(ios_base::failbit);
  1006. }
  1007. #endif
  1008. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1009. template <class _CharT, class _Traits>
  1010. inline
  1011. basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
  1012. : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
  1013. __sb_(_VSTD::move(__rhs.__sb_))
  1014. {
  1015. this->set_rdbuf(&__sb_);
  1016. }
  1017. template <class _CharT, class _Traits>
  1018. inline
  1019. basic_ifstream<_CharT, _Traits>&
  1020. basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
  1021. {
  1022. basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
  1023. __sb_ = _VSTD::move(__rhs.__sb_);
  1024. return *this;
  1025. }
  1026. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1027. template <class _CharT, class _Traits>
  1028. inline
  1029. void
  1030. basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
  1031. {
  1032. basic_istream<char_type, traits_type>::swap(__rhs);
  1033. __sb_.swap(__rhs.__sb_);
  1034. }
  1035. template <class _CharT, class _Traits>
  1036. inline _LIBCPP_INLINE_VISIBILITY
  1037. void
  1038. swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
  1039. {
  1040. __x.swap(__y);
  1041. }
  1042. template <class _CharT, class _Traits>
  1043. inline
  1044. basic_filebuf<_CharT, _Traits>*
  1045. basic_ifstream<_CharT, _Traits>::rdbuf() const
  1046. {
  1047. return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
  1048. }
  1049. template <class _CharT, class _Traits>
  1050. inline
  1051. bool
  1052. basic_ifstream<_CharT, _Traits>::is_open() const
  1053. {
  1054. return __sb_.is_open();
  1055. }
  1056. #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
  1057. template <class _CharT, class _Traits>
  1058. void
  1059. basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
  1060. {
  1061. if (__sb_.open(__s, __mode | ios_base::in))
  1062. this->clear();
  1063. else
  1064. this->setstate(ios_base::failbit);
  1065. }
  1066. template <class _CharT, class _Traits>
  1067. void
  1068. basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
  1069. {
  1070. if (__sb_.open(__s, __mode | ios_base::in))
  1071. this->clear();
  1072. else
  1073. this->setstate(ios_base::failbit);
  1074. }
  1075. #endif
  1076. template <class _CharT, class _Traits>
  1077. inline
  1078. void
  1079. basic_ifstream<_CharT, _Traits>::close()
  1080. {
  1081. if (__sb_.close() == 0)
  1082. this->setstate(ios_base::failbit);
  1083. }
  1084. // basic_ofstream
  1085. template <class _CharT, class _Traits>
  1086. class _LIBCPP_TYPE_VIS_ONLY basic_ofstream
  1087. : public basic_ostream<_CharT, _Traits>
  1088. {
  1089. public:
  1090. typedef _CharT char_type;
  1091. typedef _Traits traits_type;
  1092. typedef typename traits_type::int_type int_type;
  1093. typedef typename traits_type::pos_type pos_type;
  1094. typedef typename traits_type::off_type off_type;
  1095. _LIBCPP_INLINE_VISIBILITY
  1096. basic_ofstream();
  1097. _LIBCPP_INLINE_VISIBILITY
  1098. explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
  1099. _LIBCPP_INLINE_VISIBILITY
  1100. explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
  1101. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1102. _LIBCPP_INLINE_VISIBILITY
  1103. basic_ofstream(basic_ofstream&& __rhs);
  1104. #endif
  1105. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1106. _LIBCPP_INLINE_VISIBILITY
  1107. basic_ofstream& operator=(basic_ofstream&& __rhs);
  1108. #endif
  1109. _LIBCPP_INLINE_VISIBILITY
  1110. void swap(basic_ofstream& __rhs);
  1111. _LIBCPP_INLINE_VISIBILITY
  1112. basic_filebuf<char_type, traits_type>* rdbuf() const;
  1113. _LIBCPP_INLINE_VISIBILITY
  1114. bool is_open() const;
  1115. #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
  1116. void open(const char* __s, ios_base::openmode __mode = ios_base::out);
  1117. void open(const string& __s, ios_base::openmode __mode = ios_base::out);
  1118. #endif
  1119. _LIBCPP_INLINE_VISIBILITY
  1120. void close();
  1121. private:
  1122. basic_filebuf<char_type, traits_type> __sb_;
  1123. };
  1124. template <class _CharT, class _Traits>
  1125. inline
  1126. basic_ofstream<_CharT, _Traits>::basic_ofstream()
  1127. : basic_ostream<char_type, traits_type>(&__sb_)
  1128. {
  1129. }
  1130. #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
  1131. template <class _CharT, class _Traits>
  1132. inline
  1133. basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
  1134. : basic_ostream<char_type, traits_type>(&__sb_)
  1135. {
  1136. if (__sb_.open(__s, __mode | ios_base::out) == 0)
  1137. this->setstate(ios_base::failbit);
  1138. }
  1139. template <class _CharT, class _Traits>
  1140. inline
  1141. basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
  1142. : basic_ostream<char_type, traits_type>(&__sb_)
  1143. {
  1144. if (__sb_.open(__s, __mode | ios_base::out) == 0)
  1145. this->setstate(ios_base::failbit);
  1146. }
  1147. #endif
  1148. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1149. template <class _CharT, class _Traits>
  1150. inline
  1151. basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
  1152. : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
  1153. __sb_(_VSTD::move(__rhs.__sb_))
  1154. {
  1155. this->set_rdbuf(&__sb_);
  1156. }
  1157. template <class _CharT, class _Traits>
  1158. inline
  1159. basic_ofstream<_CharT, _Traits>&
  1160. basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
  1161. {
  1162. basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
  1163. __sb_ = _VSTD::move(__rhs.__sb_);
  1164. return *this;
  1165. }
  1166. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1167. template <class _CharT, class _Traits>
  1168. inline
  1169. void
  1170. basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
  1171. {
  1172. basic_ostream<char_type, traits_type>::swap(__rhs);
  1173. __sb_.swap(__rhs.__sb_);
  1174. }
  1175. template <class _CharT, class _Traits>
  1176. inline _LIBCPP_INLINE_VISIBILITY
  1177. void
  1178. swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
  1179. {
  1180. __x.swap(__y);
  1181. }
  1182. template <class _CharT, class _Traits>
  1183. inline
  1184. basic_filebuf<_CharT, _Traits>*
  1185. basic_ofstream<_CharT, _Traits>::rdbuf() const
  1186. {
  1187. return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
  1188. }
  1189. template <class _CharT, class _Traits>
  1190. inline
  1191. bool
  1192. basic_ofstream<_CharT, _Traits>::is_open() const
  1193. {
  1194. return __sb_.is_open();
  1195. }
  1196. #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
  1197. template <class _CharT, class _Traits>
  1198. void
  1199. basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
  1200. {
  1201. if (__sb_.open(__s, __mode | ios_base::out))
  1202. this->clear();
  1203. else
  1204. this->setstate(ios_base::failbit);
  1205. }
  1206. template <class _CharT, class _Traits>
  1207. void
  1208. basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
  1209. {
  1210. if (__sb_.open(__s, __mode | ios_base::out))
  1211. this->clear();
  1212. else
  1213. this->setstate(ios_base::failbit);
  1214. }
  1215. #endif
  1216. template <class _CharT, class _Traits>
  1217. inline
  1218. void
  1219. basic_ofstream<_CharT, _Traits>::close()
  1220. {
  1221. if (__sb_.close() == 0)
  1222. this->setstate(ios_base::failbit);
  1223. }
  1224. // basic_fstream
  1225. template <class _CharT, class _Traits>
  1226. class _LIBCPP_TYPE_VIS_ONLY basic_fstream
  1227. : public basic_iostream<_CharT, _Traits>
  1228. {
  1229. public:
  1230. typedef _CharT char_type;
  1231. typedef _Traits traits_type;
  1232. typedef typename traits_type::int_type int_type;
  1233. typedef typename traits_type::pos_type pos_type;
  1234. typedef typename traits_type::off_type off_type;
  1235. _LIBCPP_INLINE_VISIBILITY
  1236. basic_fstream();
  1237. #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
  1238. _LIBCPP_INLINE_VISIBILITY
  1239. explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
  1240. _LIBCPP_INLINE_VISIBILITY
  1241. explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
  1242. #endif
  1243. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1244. _LIBCPP_INLINE_VISIBILITY
  1245. basic_fstream(basic_fstream&& __rhs);
  1246. #endif
  1247. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1248. _LIBCPP_INLINE_VISIBILITY
  1249. basic_fstream& operator=(basic_fstream&& __rhs);
  1250. #endif
  1251. _LIBCPP_INLINE_VISIBILITY
  1252. void swap(basic_fstream& __rhs);
  1253. _LIBCPP_INLINE_VISIBILITY
  1254. basic_filebuf<char_type, traits_type>* rdbuf() const;
  1255. _LIBCPP_INLINE_VISIBILITY
  1256. bool is_open() const;
  1257. #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
  1258. void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
  1259. void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
  1260. #endif
  1261. _LIBCPP_INLINE_VISIBILITY
  1262. void close();
  1263. private:
  1264. basic_filebuf<char_type, traits_type> __sb_;
  1265. };
  1266. template <class _CharT, class _Traits>
  1267. inline
  1268. basic_fstream<_CharT, _Traits>::basic_fstream()
  1269. : basic_iostream<char_type, traits_type>(&__sb_)
  1270. {
  1271. }
  1272. #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
  1273. template <class _CharT, class _Traits>
  1274. inline
  1275. basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
  1276. : basic_iostream<char_type, traits_type>(&__sb_)
  1277. {
  1278. if (__sb_.open(__s, __mode) == 0)
  1279. this->setstate(ios_base::failbit);
  1280. }
  1281. template <class _CharT, class _Traits>
  1282. inline
  1283. basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
  1284. : basic_iostream<char_type, traits_type>(&__sb_)
  1285. {
  1286. if (__sb_.open(__s, __mode) == 0)
  1287. this->setstate(ios_base::failbit);
  1288. }
  1289. #endif
  1290. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1291. template <class _CharT, class _Traits>
  1292. inline
  1293. basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
  1294. : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
  1295. __sb_(_VSTD::move(__rhs.__sb_))
  1296. {
  1297. this->set_rdbuf(&__sb_);
  1298. }
  1299. template <class _CharT, class _Traits>
  1300. inline
  1301. basic_fstream<_CharT, _Traits>&
  1302. basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
  1303. {
  1304. basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
  1305. __sb_ = _VSTD::move(__rhs.__sb_);
  1306. return *this;
  1307. }
  1308. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1309. template <class _CharT, class _Traits>
  1310. inline
  1311. void
  1312. basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
  1313. {
  1314. basic_iostream<char_type, traits_type>::swap(__rhs);
  1315. __sb_.swap(__rhs.__sb_);
  1316. }
  1317. template <class _CharT, class _Traits>
  1318. inline _LIBCPP_INLINE_VISIBILITY
  1319. void
  1320. swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
  1321. {
  1322. __x.swap(__y);
  1323. }
  1324. template <class _CharT, class _Traits>
  1325. inline
  1326. basic_filebuf<_CharT, _Traits>*
  1327. basic_fstream<_CharT, _Traits>::rdbuf() const
  1328. {
  1329. return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
  1330. }
  1331. template <class _CharT, class _Traits>
  1332. inline
  1333. bool
  1334. basic_fstream<_CharT, _Traits>::is_open() const
  1335. {
  1336. return __sb_.is_open();
  1337. }
  1338. #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
  1339. template <class _CharT, class _Traits>
  1340. void
  1341. basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
  1342. {
  1343. if (__sb_.open(__s, __mode))
  1344. this->clear();
  1345. else
  1346. this->setstate(ios_base::failbit);
  1347. }
  1348. template <class _CharT, class _Traits>
  1349. void
  1350. basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
  1351. {
  1352. if (__sb_.open(__s, __mode))
  1353. this->clear();
  1354. else
  1355. this->setstate(ios_base::failbit);
  1356. }
  1357. #endif
  1358. template <class _CharT, class _Traits>
  1359. inline
  1360. void
  1361. basic_fstream<_CharT, _Traits>::close()
  1362. {
  1363. if (__sb_.close() == 0)
  1364. this->setstate(ios_base::failbit);
  1365. }
  1366. _LIBCPP_END_NAMESPACE_STD
  1367. #endif // !defined(_LIBCPP_SGX_CONFIG)
  1368. #endif // _LIBCPP_FSTREAM