fstream 33 KB

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