fstream 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  1. // File based streams -*- C++ -*-
  2. // Copyright (C) 1997-2023 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/fstream
  21. * This is a Standard C++ Library header.
  22. */
  23. //
  24. // ISO C++ 14882: 27.8 File-based streams
  25. //
  26. #ifndef _GLIBCXX_FSTREAM
  27. #define _GLIBCXX_FSTREAM 1
  28. #pragma GCC system_header
  29. #include <bits/requires_hosted.h> // iostreams
  30. #include <istream>
  31. #include <ostream>
  32. #include <bits/codecvt.h>
  33. #include <cstdio> // For BUFSIZ
  34. #include <bits/basic_file.h> // For __basic_file, __c_lock
  35. #if __cplusplus >= 201103L
  36. #include <string> // For std::string overloads.
  37. #endif
  38. // This can be overridden by the target's os_defines.h
  39. #ifndef _GLIBCXX_BUFSIZ
  40. # define _GLIBCXX_BUFSIZ BUFSIZ
  41. #endif
  42. namespace std _GLIBCXX_VISIBILITY(default)
  43. {
  44. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  45. #if __cplusplus >= 201703L
  46. // Enable if _Path is a filesystem::path or experimental::filesystem::path
  47. template<typename _Path, typename _Result = _Path, typename _Path2
  48. = decltype(std::declval<_Path&>().make_preferred().filename())>
  49. using _If_fs_path = enable_if_t<is_same_v<_Path, _Path2>, _Result>;
  50. #endif // C++17
  51. // [27.8.1.1] template class basic_filebuf
  52. /**
  53. * @brief The actual work of input and output (for files).
  54. * @ingroup io
  55. *
  56. * @tparam _CharT Type of character stream.
  57. * @tparam _Traits Traits for character type, defaults to
  58. * char_traits<_CharT>.
  59. *
  60. * This class associates both its input and output sequence with an
  61. * external disk file, and maintains a joint file position for both
  62. * sequences. Many of its semantics are described in terms of similar
  63. * behavior in the Standard C Library's @c FILE streams.
  64. *
  65. * Requirements on traits_type, specific to this class:
  66. * - traits_type::pos_type must be fpos<traits_type::state_type>
  67. * - traits_type::off_type must be streamoff
  68. * - traits_type::state_type must be Assignable and DefaultConstructible,
  69. * - traits_type::state_type() must be the initial state for codecvt.
  70. */
  71. template<typename _CharT, typename _Traits>
  72. class basic_filebuf : public basic_streambuf<_CharT, _Traits>
  73. {
  74. #if __cplusplus >= 201103L
  75. template<typename _Tp>
  76. using __chk_state = __and_<is_copy_assignable<_Tp>,
  77. is_copy_constructible<_Tp>,
  78. is_default_constructible<_Tp>>;
  79. static_assert(__chk_state<typename _Traits::state_type>::value,
  80. "state_type must be CopyAssignable, CopyConstructible"
  81. " and DefaultConstructible");
  82. static_assert(is_same<typename _Traits::pos_type,
  83. fpos<typename _Traits::state_type>>::value,
  84. "pos_type must be fpos<state_type>");
  85. #endif
  86. public:
  87. // Types:
  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. typedef basic_streambuf<char_type, traits_type> __streambuf_type;
  94. typedef basic_filebuf<char_type, traits_type> __filebuf_type;
  95. typedef __basic_file<char> __file_type;
  96. typedef typename traits_type::state_type __state_type;
  97. typedef codecvt<char_type, char, __state_type> __codecvt_type;
  98. friend class ios_base; // For sync_with_stdio.
  99. protected:
  100. // Data Members:
  101. // MT lock inherited from libio or other low-level io library.
  102. __c_lock _M_lock;
  103. // External buffer.
  104. __file_type _M_file;
  105. /// Place to stash in || out || in | out settings for current filebuf.
  106. ios_base::openmode _M_mode;
  107. // Beginning state type for codecvt.
  108. __state_type _M_state_beg;
  109. // During output, the state that corresponds to pptr(),
  110. // during input, the state that corresponds to egptr() and
  111. // _M_ext_next.
  112. __state_type _M_state_cur;
  113. // Not used for output. During input, the state that corresponds
  114. // to eback() and _M_ext_buf.
  115. __state_type _M_state_last;
  116. /// Pointer to the beginning of internal buffer.
  117. char_type* _M_buf;
  118. /**
  119. * Actual size of internal buffer. This number is equal to the size
  120. * of the put area + 1 position, reserved for the overflow char of
  121. * a full area.
  122. */
  123. size_t _M_buf_size;
  124. // Set iff _M_buf is allocated memory from _M_allocate_internal_buffer.
  125. bool _M_buf_allocated;
  126. /**
  127. * _M_reading == false && _M_writing == false for @b uncommitted mode;
  128. * _M_reading == true for @b read mode;
  129. * _M_writing == true for @b write mode;
  130. *
  131. * NB: _M_reading == true && _M_writing == true is unused.
  132. */
  133. bool _M_reading;
  134. bool _M_writing;
  135. ///@{
  136. /**
  137. * Necessary bits for putback buffer management.
  138. *
  139. * @note pbacks of over one character are not currently supported.
  140. */
  141. char_type _M_pback;
  142. char_type* _M_pback_cur_save;
  143. char_type* _M_pback_end_save;
  144. bool _M_pback_init;
  145. ///@}
  146. // Cached codecvt facet.
  147. const __codecvt_type* _M_codecvt;
  148. /**
  149. * Buffer for external characters. Used for input when
  150. * codecvt::always_noconv() == false. When valid, this corresponds
  151. * to eback().
  152. */
  153. char* _M_ext_buf;
  154. /**
  155. * Size of buffer held by _M_ext_buf.
  156. */
  157. streamsize _M_ext_buf_size;
  158. /**
  159. * Pointers into the buffer held by _M_ext_buf that delimit a
  160. * subsequence of bytes that have been read but not yet converted.
  161. * When valid, _M_ext_next corresponds to egptr().
  162. */
  163. const char* _M_ext_next;
  164. char* _M_ext_end;
  165. /**
  166. * Initializes pback buffers, and moves normal buffers to safety.
  167. * Assumptions:
  168. * _M_in_cur has already been moved back
  169. */
  170. void
  171. _M_create_pback()
  172. {
  173. if (!_M_pback_init)
  174. {
  175. _M_pback_cur_save = this->gptr();
  176. _M_pback_end_save = this->egptr();
  177. this->setg(&_M_pback, &_M_pback, &_M_pback + 1);
  178. _M_pback_init = true;
  179. }
  180. }
  181. /**
  182. * Deactivates pback buffer contents, and restores normal buffer.
  183. * Assumptions:
  184. * The pback buffer has only moved forward.
  185. */
  186. void
  187. _M_destroy_pback() throw()
  188. {
  189. if (_M_pback_init)
  190. {
  191. // Length _M_in_cur moved in the pback buffer.
  192. _M_pback_cur_save += this->gptr() != this->eback();
  193. this->setg(_M_buf, _M_pback_cur_save, _M_pback_end_save);
  194. _M_pback_init = false;
  195. }
  196. }
  197. public:
  198. // Constructors/destructor:
  199. /**
  200. * @brief Does not open any files.
  201. *
  202. * The default constructor initializes the parent class using its
  203. * own default ctor.
  204. */
  205. basic_filebuf();
  206. #if __cplusplus >= 201103L
  207. basic_filebuf(const basic_filebuf&) = delete;
  208. basic_filebuf(basic_filebuf&&);
  209. #endif
  210. /**
  211. * @brief The destructor closes the file first.
  212. */
  213. virtual
  214. ~basic_filebuf()
  215. {
  216. __try
  217. { this->close(); }
  218. __catch(...)
  219. { }
  220. }
  221. #if __cplusplus >= 201103L
  222. basic_filebuf& operator=(const basic_filebuf&) = delete;
  223. basic_filebuf& operator=(basic_filebuf&&);
  224. void swap(basic_filebuf&);
  225. #endif
  226. // Members:
  227. /**
  228. * @brief Returns true if the external file is open.
  229. */
  230. bool
  231. is_open() const throw()
  232. { return _M_file.is_open(); }
  233. /**
  234. * @brief Opens an external file.
  235. * @param __s The name of the file.
  236. * @param __mode The open mode flags.
  237. * @return @c this on success, NULL on failure
  238. *
  239. * If a file is already open, this function immediately fails.
  240. * Otherwise it tries to open the file named @a __s using the flags
  241. * given in @a __mode.
  242. *
  243. * Table 92, adapted here, gives the relation between openmode
  244. * combinations and the equivalent @c fopen() flags.
  245. * (NB: lines app, in|out|app, in|app, binary|app, binary|in|out|app,
  246. * and binary|in|app per DR 596)
  247. * <pre>
  248. * +---------------------------------------------------------+
  249. * | ios_base Flag combination stdio equivalent |
  250. * |binary in out trunc app |
  251. * +---------------------------------------------------------+
  252. * | + w |
  253. * | + + a |
  254. * | + a |
  255. * | + + w |
  256. * | + r |
  257. * | + + r+ |
  258. * | + + + w+ |
  259. * | + + + a+ |
  260. * | + + a+ |
  261. * +---------------------------------------------------------+
  262. * | + + wb |
  263. * | + + + ab |
  264. * | + + ab |
  265. * | + + + wb |
  266. * | + + rb |
  267. * | + + + r+b |
  268. * | + + + + w+b |
  269. * | + + + + a+b |
  270. * | + + + a+b |
  271. * +---------------------------------------------------------+
  272. * </pre>
  273. */
  274. __filebuf_type*
  275. open(const char* __s, ios_base::openmode __mode);
  276. #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
  277. /**
  278. * @brief Opens an external file.
  279. * @param __s The name of the file, as a wide character string.
  280. * @param __mode The open mode flags.
  281. * @return @c this on success, NULL on failure
  282. */
  283. __filebuf_type*
  284. open(const wchar_t* __s, ios_base::openmode __mode);
  285. #endif
  286. #if __cplusplus >= 201103L
  287. /**
  288. * @brief Opens an external file.
  289. * @param __s The name of the file.
  290. * @param __mode The open mode flags.
  291. * @return @c this on success, NULL on failure
  292. */
  293. __filebuf_type*
  294. open(const std::string& __s, ios_base::openmode __mode)
  295. { return open(__s.c_str(), __mode); }
  296. #if __cplusplus >= 201703L
  297. /**
  298. * @brief Opens an external file.
  299. * @param __s The name of the file, as a filesystem::path.
  300. * @param __mode The open mode flags.
  301. * @return @c this on success, NULL on failure
  302. */
  303. template<typename _Path>
  304. _If_fs_path<_Path, __filebuf_type*>
  305. open(const _Path& __s, ios_base::openmode __mode)
  306. { return open(__s.c_str(), __mode); }
  307. #endif // C++17
  308. #endif // C++11
  309. /**
  310. * @brief Closes the currently associated file.
  311. * @return @c this on success, NULL on failure
  312. *
  313. * If no file is currently open, this function immediately fails.
  314. *
  315. * If a <em>put buffer area</em> exists, @c overflow(eof) is
  316. * called to flush all the characters. The file is then
  317. * closed.
  318. *
  319. * If any operations fail, this function also fails.
  320. */
  321. __filebuf_type*
  322. close();
  323. protected:
  324. void
  325. _M_allocate_internal_buffer();
  326. void
  327. _M_destroy_internal_buffer() throw();
  328. // [27.8.1.4] overridden virtual functions
  329. virtual streamsize
  330. showmanyc();
  331. // Stroustrup, 1998, p. 628
  332. // underflow() and uflow() functions are called to get the next
  333. // character from the real input source when the buffer is empty.
  334. // Buffered input uses underflow()
  335. virtual int_type
  336. underflow();
  337. virtual int_type
  338. pbackfail(int_type __c = _Traits::eof());
  339. // Stroustrup, 1998, p 648
  340. // The overflow() function is called to transfer characters to the
  341. // real output destination when the buffer is full. A call to
  342. // overflow(c) outputs the contents of the buffer plus the
  343. // character c.
  344. // 27.5.2.4.5
  345. // Consume some sequence of the characters in the pending sequence.
  346. virtual int_type
  347. overflow(int_type __c = _Traits::eof());
  348. // Convert internal byte sequence to external, char-based
  349. // sequence via codecvt.
  350. bool
  351. _M_convert_to_external(char_type*, streamsize);
  352. /**
  353. * @brief Manipulates the buffer.
  354. * @param __s Pointer to a buffer area.
  355. * @param __n Size of @a __s.
  356. * @return @c this
  357. *
  358. * If no file has been opened, and both @a __s and @a __n are zero, then
  359. * the stream becomes unbuffered. Otherwise, @c __s is used as a
  360. * buffer; see
  361. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
  362. * for more.
  363. */
  364. virtual __streambuf_type*
  365. setbuf(char_type* __s, streamsize __n);
  366. virtual pos_type
  367. seekoff(off_type __off, ios_base::seekdir __way,
  368. ios_base::openmode __mode = ios_base::in | ios_base::out);
  369. virtual pos_type
  370. seekpos(pos_type __pos,
  371. ios_base::openmode __mode = ios_base::in | ios_base::out);
  372. // Common code for seekoff, seekpos, and overflow
  373. pos_type
  374. _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state);
  375. int
  376. _M_get_ext_pos(__state_type &__state);
  377. virtual int
  378. sync();
  379. virtual void
  380. imbue(const locale& __loc);
  381. virtual streamsize
  382. xsgetn(char_type* __s, streamsize __n);
  383. virtual streamsize
  384. xsputn(const char_type* __s, streamsize __n);
  385. // Flushes output buffer, then writes unshift sequence.
  386. bool
  387. _M_terminate_output();
  388. /**
  389. * This function sets the pointers of the internal buffer, both get
  390. * and put areas. Typically:
  391. *
  392. * __off == egptr() - eback() upon underflow/uflow (@b read mode);
  393. * __off == 0 upon overflow (@b write mode);
  394. * __off == -1 upon open, setbuf, seekoff/pos (@b uncommitted mode).
  395. *
  396. * NB: epptr() - pbase() == _M_buf_size - 1, since _M_buf_size
  397. * reflects the actual allocated memory and the last cell is reserved
  398. * for the overflow char of a full put area.
  399. */
  400. void
  401. _M_set_buffer(streamsize __off)
  402. {
  403. const bool __testin = _M_mode & ios_base::in;
  404. const bool __testout = (_M_mode & ios_base::out
  405. || _M_mode & ios_base::app);
  406. if (__testin && __off > 0)
  407. this->setg(_M_buf, _M_buf, _M_buf + __off);
  408. else
  409. this->setg(_M_buf, _M_buf, _M_buf);
  410. if (__testout && __off == 0 && _M_buf_size > 1 )
  411. this->setp(_M_buf, _M_buf + _M_buf_size - 1);
  412. else
  413. this->setp(0, 0);
  414. }
  415. };
  416. // [27.8.1.5] Template class basic_ifstream
  417. /**
  418. * @brief Controlling input for files.
  419. * @ingroup io
  420. *
  421. * @tparam _CharT Type of character stream.
  422. * @tparam _Traits Traits for character type, defaults to
  423. * char_traits<_CharT>.
  424. *
  425. * This class supports reading from named files, using the inherited
  426. * functions from std::basic_istream. To control the associated
  427. * sequence, an instance of std::basic_filebuf is used, which this page
  428. * refers to as @c sb.
  429. */
  430. template<typename _CharT, typename _Traits>
  431. class basic_ifstream : public basic_istream<_CharT, _Traits>
  432. {
  433. public:
  434. // Types:
  435. typedef _CharT char_type;
  436. typedef _Traits traits_type;
  437. typedef typename traits_type::int_type int_type;
  438. typedef typename traits_type::pos_type pos_type;
  439. typedef typename traits_type::off_type off_type;
  440. // Non-standard types:
  441. typedef basic_filebuf<char_type, traits_type> __filebuf_type;
  442. typedef basic_istream<char_type, traits_type> __istream_type;
  443. private:
  444. __filebuf_type _M_filebuf;
  445. public:
  446. // Constructors/Destructors:
  447. /**
  448. * @brief Default constructor.
  449. *
  450. * Initializes @c sb using its default constructor, and passes
  451. * @c &sb to the base class initializer. Does not open any files
  452. * (you haven't given it a filename to open).
  453. */
  454. basic_ifstream() : __istream_type(), _M_filebuf()
  455. { this->init(&_M_filebuf); }
  456. /**
  457. * @brief Create an input file stream.
  458. * @param __s Null terminated string specifying the filename.
  459. * @param __mode Open file in specified mode (see std::ios_base).
  460. *
  461. * @c ios_base::in is automatically included in @a __mode.
  462. */
  463. explicit
  464. basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in)
  465. : __istream_type(), _M_filebuf()
  466. {
  467. this->init(&_M_filebuf);
  468. this->open(__s, __mode);
  469. }
  470. #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
  471. /**
  472. * @param Create an input file stream.
  473. * @param __s Wide string specifying the filename.
  474. * @param __mode Open file in specified mode (see std::ios_base).
  475. *
  476. * @c ios_base::in is automatically included in @a __mode.
  477. */
  478. basic_ifstream(const wchar_t* __s,
  479. ios_base::openmode __mode = ios_base::in)
  480. : __istream_type(), _M_filebuf()
  481. {
  482. this->init(&_M_filebuf);
  483. this->open(__s, __mode);
  484. }
  485. #endif
  486. #if __cplusplus >= 201103L
  487. /**
  488. * @brief Create an input file stream.
  489. * @param __s std::string specifying the filename.
  490. * @param __mode Open file in specified mode (see std::ios_base).
  491. *
  492. * @c ios_base::in is automatically included in @a __mode.
  493. */
  494. explicit
  495. basic_ifstream(const std::string& __s,
  496. ios_base::openmode __mode = ios_base::in)
  497. : __istream_type(), _M_filebuf()
  498. {
  499. this->init(&_M_filebuf);
  500. this->open(__s, __mode);
  501. }
  502. #if __cplusplus >= 201703L
  503. /**
  504. * @brief Create an input file stream.
  505. * @param __s filesystem::path specifying the filename.
  506. * @param __mode Open file in specified mode (see std::ios_base).
  507. *
  508. * @c ios_base::in is automatically included in @a __mode.
  509. */
  510. template<typename _Path, typename _Require = _If_fs_path<_Path>>
  511. basic_ifstream(const _Path& __s,
  512. ios_base::openmode __mode = ios_base::in)
  513. : basic_ifstream(__s.c_str(), __mode)
  514. { }
  515. #endif // C++17
  516. basic_ifstream(const basic_ifstream&) = delete;
  517. basic_ifstream(basic_ifstream&& __rhs)
  518. : __istream_type(std::move(__rhs)),
  519. _M_filebuf(std::move(__rhs._M_filebuf))
  520. { __istream_type::set_rdbuf(&_M_filebuf); }
  521. #endif // C++11
  522. /**
  523. * @brief The destructor does nothing.
  524. *
  525. * The file is closed by the filebuf object, not the formatting
  526. * stream.
  527. */
  528. ~basic_ifstream()
  529. { }
  530. #if __cplusplus >= 201103L
  531. // 27.8.3.2 Assign and swap:
  532. basic_ifstream&
  533. operator=(const basic_ifstream&) = delete;
  534. basic_ifstream&
  535. operator=(basic_ifstream&& __rhs)
  536. {
  537. __istream_type::operator=(std::move(__rhs));
  538. _M_filebuf = std::move(__rhs._M_filebuf);
  539. return *this;
  540. }
  541. void
  542. swap(basic_ifstream& __rhs)
  543. {
  544. __istream_type::swap(__rhs);
  545. _M_filebuf.swap(__rhs._M_filebuf);
  546. }
  547. #endif
  548. // Members:
  549. /**
  550. * @brief Accessing the underlying buffer.
  551. * @return The current basic_filebuf buffer.
  552. *
  553. * This hides both signatures of std::basic_ios::rdbuf().
  554. */
  555. __filebuf_type*
  556. rdbuf() const
  557. { return const_cast<__filebuf_type*>(&_M_filebuf); }
  558. /**
  559. * @brief Wrapper to test for an open file.
  560. * @return @c rdbuf()->is_open()
  561. */
  562. bool
  563. is_open()
  564. { return _M_filebuf.is_open(); }
  565. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  566. // 365. Lack of const-qualification in clause 27
  567. bool
  568. is_open() const
  569. { return _M_filebuf.is_open(); }
  570. /**
  571. * @brief Opens an external file.
  572. * @param __s The name of the file.
  573. * @param __mode The open mode flags.
  574. *
  575. * Calls @c std::basic_filebuf::open(s,__mode|in). If that function
  576. * fails, @c failbit is set in the stream's error state.
  577. */
  578. void
  579. open(const char* __s, ios_base::openmode __mode = ios_base::in)
  580. {
  581. if (!_M_filebuf.open(__s, __mode | ios_base::in))
  582. this->setstate(ios_base::failbit);
  583. else
  584. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  585. // 409. Closing an fstream should clear error state
  586. this->clear();
  587. }
  588. #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
  589. /**
  590. * @brief Opens an external file.
  591. * @param __s The name of the file, as a wide character string.
  592. * @param __mode The open mode flags.
  593. *
  594. * Calls @c std::basic_filebuf::open(__s,__mode|in). If that function
  595. * fails, @c failbit is set in the stream's error state.
  596. */
  597. void
  598. open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in)
  599. {
  600. if (!_M_filebuf.open(__s, __mode | ios_base::in))
  601. this->setstate(ios_base::failbit);
  602. else
  603. this->clear();
  604. }
  605. #endif
  606. #if __cplusplus >= 201103L
  607. /**
  608. * @brief Opens an external file.
  609. * @param __s The name of the file.
  610. * @param __mode The open mode flags.
  611. *
  612. * Calls @c std::basic_filebuf::open(__s,__mode|in). If that function
  613. * fails, @c failbit is set in the stream's error state.
  614. */
  615. void
  616. open(const std::string& __s, ios_base::openmode __mode = ios_base::in)
  617. {
  618. if (!_M_filebuf.open(__s, __mode | ios_base::in))
  619. this->setstate(ios_base::failbit);
  620. else
  621. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  622. // 409. Closing an fstream should clear error state
  623. this->clear();
  624. }
  625. #if __cplusplus >= 201703L
  626. /**
  627. * @brief Opens an external file.
  628. * @param __s The name of the file, as a filesystem::path.
  629. * @param __mode The open mode flags.
  630. *
  631. * Calls @c std::basic_filebuf::open(__s,__mode|in). If that function
  632. * fails, @c failbit is set in the stream's error state.
  633. */
  634. template<typename _Path>
  635. _If_fs_path<_Path, void>
  636. open(const _Path& __s, ios_base::openmode __mode = ios_base::in)
  637. { open(__s.c_str(), __mode); }
  638. #endif // C++17
  639. #endif // C++11
  640. /**
  641. * @brief Close the file.
  642. *
  643. * Calls @c std::basic_filebuf::close(). If that function
  644. * fails, @c failbit is set in the stream's error state.
  645. */
  646. void
  647. close()
  648. {
  649. if (!_M_filebuf.close())
  650. this->setstate(ios_base::failbit);
  651. }
  652. };
  653. // [27.8.1.8] Template class basic_ofstream
  654. /**
  655. * @brief Controlling output for files.
  656. * @ingroup io
  657. *
  658. * @tparam _CharT Type of character stream.
  659. * @tparam _Traits Traits for character type, defaults to
  660. * char_traits<_CharT>.
  661. *
  662. * This class supports reading from named files, using the inherited
  663. * functions from std::basic_ostream. To control the associated
  664. * sequence, an instance of std::basic_filebuf is used, which this page
  665. * refers to as @c sb.
  666. */
  667. template<typename _CharT, typename _Traits>
  668. class basic_ofstream : public basic_ostream<_CharT,_Traits>
  669. {
  670. public:
  671. // Types:
  672. typedef _CharT char_type;
  673. typedef _Traits traits_type;
  674. typedef typename traits_type::int_type int_type;
  675. typedef typename traits_type::pos_type pos_type;
  676. typedef typename traits_type::off_type off_type;
  677. // Non-standard types:
  678. typedef basic_filebuf<char_type, traits_type> __filebuf_type;
  679. typedef basic_ostream<char_type, traits_type> __ostream_type;
  680. private:
  681. __filebuf_type _M_filebuf;
  682. public:
  683. // Constructors:
  684. /**
  685. * @brief Default constructor.
  686. *
  687. * Initializes @c sb using its default constructor, and passes
  688. * @c &sb to the base class initializer. Does not open any files
  689. * (you haven't given it a filename to open).
  690. */
  691. basic_ofstream(): __ostream_type(), _M_filebuf()
  692. { this->init(&_M_filebuf); }
  693. /**
  694. * @brief Create an output file stream.
  695. * @param __s Null terminated string specifying the filename.
  696. * @param __mode Open file in specified mode (see std::ios_base).
  697. *
  698. * @c ios_base::out is automatically included in @a __mode.
  699. */
  700. explicit
  701. basic_ofstream(const char* __s,
  702. ios_base::openmode __mode = ios_base::out)
  703. : __ostream_type(), _M_filebuf()
  704. {
  705. this->init(&_M_filebuf);
  706. this->open(__s, __mode);
  707. }
  708. #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
  709. /**
  710. * @param Create an output file stream.
  711. * @param __s Wide string specifying the filename.
  712. * @param __mode Open file in specified mode (see std::ios_base).
  713. *
  714. * @c ios_base::out | @c ios_base::trunc is automatically included in
  715. * @a __mode.
  716. */
  717. basic_ofstream(const wchar_t* __s,
  718. ios_base::openmode __mode = ios_base::out|ios_base::trunc)
  719. : __ostream_type(), _M_filebuf()
  720. {
  721. this->init(&_M_filebuf);
  722. this->open(__s, __mode);
  723. }
  724. #endif
  725. #if __cplusplus >= 201103L
  726. /**
  727. * @brief Create an output file stream.
  728. * @param __s std::string specifying the filename.
  729. * @param __mode Open file in specified mode (see std::ios_base).
  730. *
  731. * @c ios_base::out is automatically included in @a __mode.
  732. */
  733. explicit
  734. basic_ofstream(const std::string& __s,
  735. ios_base::openmode __mode = ios_base::out)
  736. : __ostream_type(), _M_filebuf()
  737. {
  738. this->init(&_M_filebuf);
  739. this->open(__s, __mode);
  740. }
  741. #if __cplusplus >= 201703L
  742. /**
  743. * @brief Create an output file stream.
  744. * @param __s filesystem::path specifying the filename.
  745. * @param __mode Open file in specified mode (see std::ios_base).
  746. *
  747. * @c ios_base::out is automatically included in @a __mode.
  748. */
  749. template<typename _Path, typename _Require = _If_fs_path<_Path>>
  750. basic_ofstream(const _Path& __s,
  751. ios_base::openmode __mode = ios_base::out)
  752. : basic_ofstream(__s.c_str(), __mode)
  753. { }
  754. #endif // C++17
  755. basic_ofstream(const basic_ofstream&) = delete;
  756. basic_ofstream(basic_ofstream&& __rhs)
  757. : __ostream_type(std::move(__rhs)),
  758. _M_filebuf(std::move(__rhs._M_filebuf))
  759. { __ostream_type::set_rdbuf(&_M_filebuf); }
  760. #endif
  761. /**
  762. * @brief The destructor does nothing.
  763. *
  764. * The file is closed by the filebuf object, not the formatting
  765. * stream.
  766. */
  767. ~basic_ofstream()
  768. { }
  769. #if __cplusplus >= 201103L
  770. // 27.8.3.2 Assign and swap:
  771. basic_ofstream&
  772. operator=(const basic_ofstream&) = delete;
  773. basic_ofstream&
  774. operator=(basic_ofstream&& __rhs)
  775. {
  776. __ostream_type::operator=(std::move(__rhs));
  777. _M_filebuf = std::move(__rhs._M_filebuf);
  778. return *this;
  779. }
  780. void
  781. swap(basic_ofstream& __rhs)
  782. {
  783. __ostream_type::swap(__rhs);
  784. _M_filebuf.swap(__rhs._M_filebuf);
  785. }
  786. #endif
  787. // Members:
  788. /**
  789. * @brief Accessing the underlying buffer.
  790. * @return The current basic_filebuf buffer.
  791. *
  792. * This hides both signatures of std::basic_ios::rdbuf().
  793. */
  794. __filebuf_type*
  795. rdbuf() const
  796. { return const_cast<__filebuf_type*>(&_M_filebuf); }
  797. /**
  798. * @brief Wrapper to test for an open file.
  799. * @return @c rdbuf()->is_open()
  800. */
  801. bool
  802. is_open()
  803. { return _M_filebuf.is_open(); }
  804. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  805. // 365. Lack of const-qualification in clause 27
  806. bool
  807. is_open() const
  808. { return _M_filebuf.is_open(); }
  809. /**
  810. * @brief Opens an external file.
  811. * @param __s The name of the file.
  812. * @param __mode The open mode flags.
  813. *
  814. * Calls @c std::basic_filebuf::open(__s,__mode|out). If that
  815. * function fails, @c failbit is set in the stream's error state.
  816. */
  817. void
  818. open(const char* __s, ios_base::openmode __mode = ios_base::out)
  819. {
  820. if (!_M_filebuf.open(__s, __mode | ios_base::out))
  821. this->setstate(ios_base::failbit);
  822. else
  823. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  824. // 409. Closing an fstream should clear error state
  825. this->clear();
  826. }
  827. #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
  828. /**
  829. * @brief Opens an external file.
  830. * @param __s The name of the file.
  831. * @param __mode The open mode flags.
  832. *
  833. * Calls @c std::basic_filebuf::open(__s,__mode|out). If that
  834. * function fails, @c failbit is set in the stream's error state.
  835. */
  836. void
  837. open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out)
  838. {
  839. if (!_M_filebuf.open(__s, __mode | ios_base::out))
  840. this->setstate(ios_base::failbit);
  841. else
  842. this->clear();
  843. }
  844. #endif
  845. #if __cplusplus >= 201103L
  846. /**
  847. * @brief Opens an external file.
  848. * @param __s The name of the file.
  849. * @param __mode The open mode flags.
  850. *
  851. * Calls @c std::basic_filebuf::open(s,mode|out). If that
  852. * function fails, @c failbit is set in the stream's error state.
  853. */
  854. void
  855. open(const std::string& __s, ios_base::openmode __mode = ios_base::out)
  856. {
  857. if (!_M_filebuf.open(__s, __mode | ios_base::out))
  858. this->setstate(ios_base::failbit);
  859. else
  860. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  861. // 409. Closing an fstream should clear error state
  862. this->clear();
  863. }
  864. #if __cplusplus >= 201703L
  865. /**
  866. * @brief Opens an external file.
  867. * @param __s The name of the file, as a filesystem::path.
  868. * @param __mode The open mode flags.
  869. *
  870. * Calls @c std::basic_filebuf::open(__s,__mode|out). If that
  871. * function fails, @c failbit is set in the stream's error state.
  872. */
  873. template<typename _Path>
  874. _If_fs_path<_Path, void>
  875. open(const _Path& __s, ios_base::openmode __mode = ios_base::out)
  876. { open(__s.c_str(), __mode); }
  877. #endif // C++17
  878. #endif // C++11
  879. /**
  880. * @brief Close the file.
  881. *
  882. * Calls @c std::basic_filebuf::close(). If that function
  883. * fails, @c failbit is set in the stream's error state.
  884. */
  885. void
  886. close()
  887. {
  888. if (!_M_filebuf.close())
  889. this->setstate(ios_base::failbit);
  890. }
  891. };
  892. // [27.8.1.11] Template class basic_fstream
  893. /**
  894. * @brief Controlling input and output for files.
  895. * @ingroup io
  896. *
  897. * @tparam _CharT Type of character stream.
  898. * @tparam _Traits Traits for character type, defaults to
  899. * char_traits<_CharT>.
  900. *
  901. * This class supports reading from and writing to named files, using
  902. * the inherited functions from std::basic_iostream. To control the
  903. * associated sequence, an instance of std::basic_filebuf is used, which
  904. * this page refers to as @c sb.
  905. */
  906. template<typename _CharT, typename _Traits>
  907. class basic_fstream : public basic_iostream<_CharT, _Traits>
  908. {
  909. public:
  910. // Types:
  911. typedef _CharT char_type;
  912. typedef _Traits traits_type;
  913. typedef typename traits_type::int_type int_type;
  914. typedef typename traits_type::pos_type pos_type;
  915. typedef typename traits_type::off_type off_type;
  916. // Non-standard types:
  917. typedef basic_filebuf<char_type, traits_type> __filebuf_type;
  918. typedef basic_ios<char_type, traits_type> __ios_type;
  919. typedef basic_iostream<char_type, traits_type> __iostream_type;
  920. private:
  921. __filebuf_type _M_filebuf;
  922. public:
  923. // Constructors/destructor:
  924. /**
  925. * @brief Default constructor.
  926. *
  927. * Initializes @c sb using its default constructor, and passes
  928. * @c &sb to the base class initializer. Does not open any files
  929. * (you haven't given it a filename to open).
  930. */
  931. basic_fstream()
  932. : __iostream_type(), _M_filebuf()
  933. { this->init(&_M_filebuf); }
  934. /**
  935. * @brief Create an input/output file stream.
  936. * @param __s Null terminated string specifying the filename.
  937. * @param __mode Open file in specified mode (see std::ios_base).
  938. */
  939. explicit
  940. basic_fstream(const char* __s,
  941. ios_base::openmode __mode = ios_base::in | ios_base::out)
  942. : __iostream_type(0), _M_filebuf()
  943. {
  944. this->init(&_M_filebuf);
  945. this->open(__s, __mode);
  946. }
  947. #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
  948. /**
  949. * @param Create an input/output file stream.
  950. * @param __s Wide string specifying the filename.
  951. * @param __mode Open file in specified mode (see std::ios_base).
  952. */
  953. basic_fstream(const wchar_t* __s,
  954. ios_base::openmode __mode = ios_base::in | ios_base::out)
  955. : __iostream_type(0), _M_filebuf()
  956. {
  957. this->init(&_M_filebuf);
  958. this->open(__s, __mode);
  959. }
  960. #endif
  961. #if __cplusplus >= 201103L
  962. /**
  963. * @brief Create an input/output file stream.
  964. * @param __s Null terminated string specifying the filename.
  965. * @param __mode Open file in specified mode (see std::ios_base).
  966. */
  967. explicit
  968. basic_fstream(const std::string& __s,
  969. ios_base::openmode __mode = ios_base::in | ios_base::out)
  970. : __iostream_type(0), _M_filebuf()
  971. {
  972. this->init(&_M_filebuf);
  973. this->open(__s, __mode);
  974. }
  975. #if __cplusplus >= 201703L
  976. /**
  977. * @brief Create an input/output file stream.
  978. * @param __s filesystem::path specifying the filename.
  979. * @param __mode Open file in specified mode (see std::ios_base).
  980. */
  981. template<typename _Path, typename _Require = _If_fs_path<_Path>>
  982. basic_fstream(const _Path& __s,
  983. ios_base::openmode __mode = ios_base::in | ios_base::out)
  984. : basic_fstream(__s.c_str(), __mode)
  985. { }
  986. #endif // C++17
  987. basic_fstream(const basic_fstream&) = delete;
  988. basic_fstream(basic_fstream&& __rhs)
  989. : __iostream_type(std::move(__rhs)),
  990. _M_filebuf(std::move(__rhs._M_filebuf))
  991. { __iostream_type::set_rdbuf(&_M_filebuf); }
  992. #endif
  993. /**
  994. * @brief The destructor does nothing.
  995. *
  996. * The file is closed by the filebuf object, not the formatting
  997. * stream.
  998. */
  999. ~basic_fstream()
  1000. { }
  1001. #if __cplusplus >= 201103L
  1002. // 27.8.3.2 Assign and swap:
  1003. basic_fstream&
  1004. operator=(const basic_fstream&) = delete;
  1005. basic_fstream&
  1006. operator=(basic_fstream&& __rhs)
  1007. {
  1008. __iostream_type::operator=(std::move(__rhs));
  1009. _M_filebuf = std::move(__rhs._M_filebuf);
  1010. return *this;
  1011. }
  1012. void
  1013. swap(basic_fstream& __rhs)
  1014. {
  1015. __iostream_type::swap(__rhs);
  1016. _M_filebuf.swap(__rhs._M_filebuf);
  1017. }
  1018. #endif
  1019. // Members:
  1020. /**
  1021. * @brief Accessing the underlying buffer.
  1022. * @return The current basic_filebuf buffer.
  1023. *
  1024. * This hides both signatures of std::basic_ios::rdbuf().
  1025. */
  1026. __filebuf_type*
  1027. rdbuf() const
  1028. { return const_cast<__filebuf_type*>(&_M_filebuf); }
  1029. /**
  1030. * @brief Wrapper to test for an open file.
  1031. * @return @c rdbuf()->is_open()
  1032. */
  1033. bool
  1034. is_open()
  1035. { return _M_filebuf.is_open(); }
  1036. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1037. // 365. Lack of const-qualification in clause 27
  1038. bool
  1039. is_open() const
  1040. { return _M_filebuf.is_open(); }
  1041. /**
  1042. * @brief Opens an external file.
  1043. * @param __s The name of the file.
  1044. * @param __mode The open mode flags.
  1045. *
  1046. * Calls @c std::basic_filebuf::open(__s,__mode). If that
  1047. * function fails, @c failbit is set in the stream's error state.
  1048. */
  1049. void
  1050. open(const char* __s,
  1051. ios_base::openmode __mode = ios_base::in | ios_base::out)
  1052. {
  1053. if (!_M_filebuf.open(__s, __mode))
  1054. this->setstate(ios_base::failbit);
  1055. else
  1056. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1057. // 409. Closing an fstream should clear error state
  1058. this->clear();
  1059. }
  1060. #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
  1061. /**
  1062. * @brief Opens an external file.
  1063. * @param __s The name of the file.
  1064. * @param __mode The open mode flags.
  1065. *
  1066. * Calls @c std::basic_filebuf::open(__s,__mode). If that
  1067. * function fails, @c failbit is set in the stream's error state.
  1068. */
  1069. void
  1070. open(const wchar_t* __s,
  1071. ios_base::openmode __mode = ios_base::in | ios_base::out)
  1072. {
  1073. if (!_M_filebuf.open(__s, __mode))
  1074. this->setstate(ios_base::failbit);
  1075. else
  1076. this->clear();
  1077. }
  1078. #endif
  1079. #if __cplusplus >= 201103L
  1080. /**
  1081. * @brief Opens an external file.
  1082. * @param __s The name of the file.
  1083. * @param __mode The open mode flags.
  1084. *
  1085. * Calls @c std::basic_filebuf::open(__s,__mode). If that
  1086. * function fails, @c failbit is set in the stream's error state.
  1087. */
  1088. void
  1089. open(const std::string& __s,
  1090. ios_base::openmode __mode = ios_base::in | ios_base::out)
  1091. {
  1092. if (!_M_filebuf.open(__s, __mode))
  1093. this->setstate(ios_base::failbit);
  1094. else
  1095. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1096. // 409. Closing an fstream should clear error state
  1097. this->clear();
  1098. }
  1099. #if __cplusplus >= 201703L
  1100. /**
  1101. * @brief Opens an external file.
  1102. * @param __s The name of the file, as a filesystem::path.
  1103. * @param __mode The open mode flags.
  1104. *
  1105. * Calls @c std::basic_filebuf::open(__s,__mode). If that
  1106. * function fails, @c failbit is set in the stream's error state.
  1107. */
  1108. template<typename _Path>
  1109. _If_fs_path<_Path, void>
  1110. open(const _Path& __s,
  1111. ios_base::openmode __mode = ios_base::in | ios_base::out)
  1112. { open(__s.c_str(), __mode); }
  1113. #endif // C++17
  1114. #endif // C++11
  1115. /**
  1116. * @brief Close the file.
  1117. *
  1118. * Calls @c std::basic_filebuf::close(). If that function
  1119. * fails, @c failbit is set in the stream's error state.
  1120. */
  1121. void
  1122. close()
  1123. {
  1124. if (!_M_filebuf.close())
  1125. this->setstate(ios_base::failbit);
  1126. }
  1127. };
  1128. #if __cplusplus >= 201103L
  1129. /// Swap specialization for filebufs.
  1130. template <class _CharT, class _Traits>
  1131. inline void
  1132. swap(basic_filebuf<_CharT, _Traits>& __x,
  1133. basic_filebuf<_CharT, _Traits>& __y)
  1134. { __x.swap(__y); }
  1135. /// Swap specialization for ifstreams.
  1136. template <class _CharT, class _Traits>
  1137. inline void
  1138. swap(basic_ifstream<_CharT, _Traits>& __x,
  1139. basic_ifstream<_CharT, _Traits>& __y)
  1140. { __x.swap(__y); }
  1141. /// Swap specialization for ofstreams.
  1142. template <class _CharT, class _Traits>
  1143. inline void
  1144. swap(basic_ofstream<_CharT, _Traits>& __x,
  1145. basic_ofstream<_CharT, _Traits>& __y)
  1146. { __x.swap(__y); }
  1147. /// Swap specialization for fstreams.
  1148. template <class _CharT, class _Traits>
  1149. inline void
  1150. swap(basic_fstream<_CharT, _Traits>& __x,
  1151. basic_fstream<_CharT, _Traits>& __y)
  1152. { __x.swap(__y); }
  1153. #endif
  1154. _GLIBCXX_END_NAMESPACE_VERSION
  1155. } // namespace
  1156. #include <bits/fstream.tcc>
  1157. #endif /* _GLIBCXX_FSTREAM */