fstream 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. // File based streams -*- C++ -*-
  2. // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  3. // 2006, 2007, 2008, 2009
  4. // Free Software Foundation, Inc.
  5. //
  6. // This file is part of the GNU ISO C++ Library. This library is free
  7. // software; you can redistribute it and/or modify it under the
  8. // terms of the GNU General Public License as published by the
  9. // Free Software Foundation; either version 3, or (at your option)
  10. // any later version.
  11. // This library is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. // Under Section 7 of GPL version 3, you are granted additional
  16. // permissions described in the GCC Runtime Library Exception, version
  17. // 3.1, as published by the Free Software Foundation.
  18. // You should have received a copy of the GNU General Public License and
  19. // a copy of the GCC Runtime Library Exception along with this program;
  20. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  21. // <http://www.gnu.org/licenses/>.
  22. /** @file fstream
  23. * This is a Standard C++ Library header.
  24. */
  25. //
  26. // ISO C++ 14882: 27.8 File-based streams
  27. //
  28. #ifndef _GLIBCXX_FSTREAM
  29. #define _GLIBCXX_FSTREAM 1
  30. #pragma GCC system_header
  31. #include <istream>
  32. #include <ostream>
  33. #include <bits/codecvt.h>
  34. #include <cstdio> // For BUFSIZ
  35. #include <bits/basic_file.h> // For __basic_file, __c_lock
  36. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  37. #include <string> // For std::string overloads.
  38. #endif
  39. _GLIBCXX_BEGIN_NAMESPACE(std)
  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. * This class associates both its input and output sequence with an
  46. * external disk file, and maintains a joint file position for both
  47. * sequences. Many of its semantics are described in terms of similar
  48. * behavior in the Standard C Library's @c FILE streams.
  49. */
  50. // Requirements on traits_type, specific to this class:
  51. // traits_type::pos_type must be fpos<traits_type::state_type>
  52. // traits_type::off_type must be streamoff
  53. // traits_type::state_type must be Assignable and DefaultConstructible,
  54. // and traits_type::state_type() must be the initial state for codecvt.
  55. template<typename _CharT, typename _Traits>
  56. class basic_filebuf : public basic_streambuf<_CharT, _Traits>
  57. {
  58. public:
  59. // Types:
  60. typedef _CharT char_type;
  61. typedef _Traits traits_type;
  62. typedef typename traits_type::int_type int_type;
  63. typedef typename traits_type::pos_type pos_type;
  64. typedef typename traits_type::off_type off_type;
  65. typedef basic_streambuf<char_type, traits_type> __streambuf_type;
  66. typedef basic_filebuf<char_type, traits_type> __filebuf_type;
  67. typedef __basic_file<char> __file_type;
  68. typedef typename traits_type::state_type __state_type;
  69. typedef codecvt<char_type, char, __state_type> __codecvt_type;
  70. friend class ios_base; // For sync_with_stdio.
  71. protected:
  72. // Data Members:
  73. // MT lock inherited from libio or other low-level io library.
  74. __c_lock _M_lock;
  75. // External buffer.
  76. __file_type _M_file;
  77. /// Place to stash in || out || in | out settings for current filebuf.
  78. ios_base::openmode _M_mode;
  79. // Beginning state type for codecvt.
  80. __state_type _M_state_beg;
  81. // During output, the state that corresponds to pptr(),
  82. // during input, the state that corresponds to egptr() and
  83. // _M_ext_next.
  84. __state_type _M_state_cur;
  85. // Not used for output. During input, the state that corresponds
  86. // to eback() and _M_ext_buf.
  87. __state_type _M_state_last;
  88. /// Pointer to the beginning of internal buffer.
  89. char_type* _M_buf;
  90. /**
  91. * Actual size of internal buffer. This number is equal to the size
  92. * of the put area + 1 position, reserved for the overflow char of
  93. * a full area.
  94. */
  95. size_t _M_buf_size;
  96. // Set iff _M_buf is allocated memory from _M_allocate_internal_buffer.
  97. bool _M_buf_allocated;
  98. /**
  99. * _M_reading == false && _M_writing == false for 'uncommitted' mode;
  100. * _M_reading == true for 'read' mode;
  101. * _M_writing == true for 'write' mode;
  102. *
  103. * NB: _M_reading == true && _M_writing == true is unused.
  104. */
  105. bool _M_reading;
  106. bool _M_writing;
  107. //@{
  108. /**
  109. * Necessary bits for putback buffer management.
  110. *
  111. * @note pbacks of over one character are not currently supported.
  112. */
  113. char_type _M_pback;
  114. char_type* _M_pback_cur_save;
  115. char_type* _M_pback_end_save;
  116. bool _M_pback_init;
  117. //@}
  118. // Cached codecvt facet.
  119. const __codecvt_type* _M_codecvt;
  120. /**
  121. * Buffer for external characters. Used for input when
  122. * codecvt::always_noconv() == false. When valid, this corresponds
  123. * to eback().
  124. */
  125. char* _M_ext_buf;
  126. /**
  127. * Size of buffer held by _M_ext_buf.
  128. */
  129. streamsize _M_ext_buf_size;
  130. /**
  131. * Pointers into the buffer held by _M_ext_buf that delimit a
  132. * subsequence of bytes that have been read but not yet converted.
  133. * When valid, _M_ext_next corresponds to egptr().
  134. */
  135. const char* _M_ext_next;
  136. char* _M_ext_end;
  137. /**
  138. * Initializes pback buffers, and moves normal buffers to safety.
  139. * Assumptions:
  140. * _M_in_cur has already been moved back
  141. */
  142. void
  143. _M_create_pback()
  144. {
  145. if (!_M_pback_init)
  146. {
  147. _M_pback_cur_save = this->gptr();
  148. _M_pback_end_save = this->egptr();
  149. this->setg(&_M_pback, &_M_pback, &_M_pback + 1);
  150. _M_pback_init = true;
  151. }
  152. }
  153. /**
  154. * Deactivates pback buffer contents, and restores normal buffer.
  155. * Assumptions:
  156. * The pback buffer has only moved forward.
  157. */
  158. void
  159. _M_destroy_pback() throw()
  160. {
  161. if (_M_pback_init)
  162. {
  163. // Length _M_in_cur moved in the pback buffer.
  164. _M_pback_cur_save += this->gptr() != this->eback();
  165. this->setg(_M_buf, _M_pback_cur_save, _M_pback_end_save);
  166. _M_pback_init = false;
  167. }
  168. }
  169. public:
  170. // Constructors/destructor:
  171. /**
  172. * @brief Does not open any files.
  173. *
  174. * The default constructor initializes the parent class using its
  175. * own default ctor.
  176. */
  177. basic_filebuf();
  178. /**
  179. * @brief The destructor closes the file first.
  180. */
  181. virtual
  182. ~basic_filebuf()
  183. { this->close(); }
  184. // Members:
  185. /**
  186. * @brief Returns true if the external file is open.
  187. */
  188. bool
  189. is_open() const throw()
  190. { return _M_file.is_open(); }
  191. /**
  192. * @brief Opens an external file.
  193. * @param s The name of the file.
  194. * @param mode The open mode flags.
  195. * @return @c this on success, NULL on failure
  196. *
  197. * If a file is already open, this function immediately fails.
  198. * Otherwise it tries to open the file named @a s using the flags
  199. * given in @a mode.
  200. *
  201. * Table 92, adapted here, gives the relation between openmode
  202. * combinations and the equivalent fopen() flags.
  203. * (NB: lines app, in|out|app, in|app, binary|app, binary|in|out|app,
  204. * and binary|in|app per DR 596)
  205. * +---------------------------------------------------------+
  206. * | ios_base Flag combination stdio equivalent |
  207. * |binary in out trunc app |
  208. * +---------------------------------------------------------+
  209. * | + "w" |
  210. * | + + "a" |
  211. * | + "a" |
  212. * | + + "w" |
  213. * | + "r" |
  214. * | + + "r+" |
  215. * | + + + "w+" |
  216. * | + + + "a+" |
  217. * | + + "a+" |
  218. * +---------------------------------------------------------+
  219. * | + + "wb" |
  220. * | + + + "ab" |
  221. * | + + "ab" |
  222. * | + + + "wb" |
  223. * | + + "rb" |
  224. * | + + + "r+b" |
  225. * | + + + + "w+b" |
  226. * | + + + + "a+b" |
  227. * | + + + "a+b" |
  228. * +---------------------------------------------------------+
  229. */
  230. __filebuf_type*
  231. open(const char* __s, ios_base::openmode __mode);
  232. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  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. __filebuf_type*
  240. open(const std::string& __s, ios_base::openmode __mode)
  241. { return open(__s.c_str(), __mode); }
  242. #endif
  243. /**
  244. * @brief Closes the currently associated file.
  245. * @return @c this on success, NULL on failure
  246. *
  247. * If no file is currently open, this function immediately fails.
  248. *
  249. * If a "put buffer area" exists, @c overflow(eof) is called to flush
  250. * all the characters. The file is then closed.
  251. *
  252. * If any operations fail, this function also fails.
  253. */
  254. __filebuf_type*
  255. close();
  256. protected:
  257. void
  258. _M_allocate_internal_buffer();
  259. void
  260. _M_destroy_internal_buffer() throw();
  261. // [27.8.1.4] overridden virtual functions
  262. virtual streamsize
  263. showmanyc();
  264. // Stroustrup, 1998, p. 628
  265. // underflow() and uflow() functions are called to get the next
  266. // character from the real input source when the buffer is empty.
  267. // Buffered input uses underflow()
  268. virtual int_type
  269. underflow();
  270. virtual int_type
  271. pbackfail(int_type __c = _Traits::eof());
  272. // Stroustrup, 1998, p 648
  273. // The overflow() function is called to transfer characters to the
  274. // real output destination when the buffer is full. A call to
  275. // overflow(c) outputs the contents of the buffer plus the
  276. // character c.
  277. // 27.5.2.4.5
  278. // Consume some sequence of the characters in the pending sequence.
  279. virtual int_type
  280. overflow(int_type __c = _Traits::eof());
  281. // Convert internal byte sequence to external, char-based
  282. // sequence via codecvt.
  283. bool
  284. _M_convert_to_external(char_type*, streamsize);
  285. /**
  286. * @brief Manipulates the buffer.
  287. * @param s Pointer to a buffer area.
  288. * @param n Size of @a s.
  289. * @return @c this
  290. *
  291. * If no file has been opened, and both @a s and @a n are zero, then
  292. * the stream becomes unbuffered. Otherwise, @c s is used as a
  293. * buffer; see
  294. * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html
  295. * for more.
  296. */
  297. virtual __streambuf_type*
  298. setbuf(char_type* __s, streamsize __n);
  299. virtual pos_type
  300. seekoff(off_type __off, ios_base::seekdir __way,
  301. ios_base::openmode __mode = ios_base::in | ios_base::out);
  302. virtual pos_type
  303. seekpos(pos_type __pos,
  304. ios_base::openmode __mode = ios_base::in | ios_base::out);
  305. // Common code for seekoff and seekpos
  306. pos_type
  307. _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state);
  308. virtual int
  309. sync();
  310. virtual void
  311. imbue(const locale& __loc);
  312. virtual streamsize
  313. xsgetn(char_type* __s, streamsize __n);
  314. virtual streamsize
  315. xsputn(const char_type* __s, streamsize __n);
  316. // Flushes output buffer, then writes unshift sequence.
  317. bool
  318. _M_terminate_output();
  319. /**
  320. * This function sets the pointers of the internal buffer, both get
  321. * and put areas. Typically:
  322. *
  323. * __off == egptr() - eback() upon underflow/uflow ('read' mode);
  324. * __off == 0 upon overflow ('write' mode);
  325. * __off == -1 upon open, setbuf, seekoff/pos ('uncommitted' mode).
  326. *
  327. * NB: epptr() - pbase() == _M_buf_size - 1, since _M_buf_size
  328. * reflects the actual allocated memory and the last cell is reserved
  329. * for the overflow char of a full put area.
  330. */
  331. void
  332. _M_set_buffer(streamsize __off)
  333. {
  334. const bool __testin = _M_mode & ios_base::in;
  335. const bool __testout = _M_mode & ios_base::out;
  336. if (__testin && __off > 0)
  337. this->setg(_M_buf, _M_buf, _M_buf + __off);
  338. else
  339. this->setg(_M_buf, _M_buf, _M_buf);
  340. if (__testout && __off == 0 && _M_buf_size > 1 )
  341. this->setp(_M_buf, _M_buf + _M_buf_size - 1);
  342. else
  343. this->setp(NULL, NULL);
  344. }
  345. };
  346. // [27.8.1.5] Template class basic_ifstream
  347. /**
  348. * @brief Controlling input for files.
  349. * @ingroup io
  350. *
  351. * This class supports reading from named files, using the inherited
  352. * functions from std::basic_istream. To control the associated
  353. * sequence, an instance of std::basic_filebuf is used, which this page
  354. * refers to as @c sb.
  355. */
  356. template<typename _CharT, typename _Traits>
  357. class basic_ifstream : public basic_istream<_CharT, _Traits>
  358. {
  359. public:
  360. // Types:
  361. typedef _CharT char_type;
  362. typedef _Traits traits_type;
  363. typedef typename traits_type::int_type int_type;
  364. typedef typename traits_type::pos_type pos_type;
  365. typedef typename traits_type::off_type off_type;
  366. // Non-standard types:
  367. typedef basic_filebuf<char_type, traits_type> __filebuf_type;
  368. typedef basic_istream<char_type, traits_type> __istream_type;
  369. private:
  370. __filebuf_type _M_filebuf;
  371. public:
  372. // Constructors/Destructors:
  373. /**
  374. * @brief Default constructor.
  375. *
  376. * Initializes @c sb using its default constructor, and passes
  377. * @c &sb to the base class initializer. Does not open any files
  378. * (you haven't given it a filename to open).
  379. */
  380. basic_ifstream() : __istream_type(), _M_filebuf()
  381. { this->init(&_M_filebuf); }
  382. /**
  383. * @brief Create an input file stream.
  384. * @param s Null terminated string specifying the filename.
  385. * @param mode Open file in specified mode (see std::ios_base).
  386. *
  387. * @c ios_base::in is automatically included in @a mode.
  388. *
  389. * Tip: When using std::string to hold the filename, you must use
  390. * .c_str() before passing it to this constructor.
  391. */
  392. explicit
  393. basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in)
  394. : __istream_type(), _M_filebuf()
  395. {
  396. this->init(&_M_filebuf);
  397. this->open(__s, __mode);
  398. }
  399. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  400. /**
  401. * @brief Create an input file stream.
  402. * @param s std::string specifying the filename.
  403. * @param mode Open file in specified mode (see std::ios_base).
  404. *
  405. * @c ios_base::in is automatically included in @a mode.
  406. */
  407. explicit
  408. basic_ifstream(const std::string& __s,
  409. ios_base::openmode __mode = ios_base::in)
  410. : __istream_type(), _M_filebuf()
  411. {
  412. this->init(&_M_filebuf);
  413. this->open(__s, __mode);
  414. }
  415. #endif
  416. /**
  417. * @brief The destructor does nothing.
  418. *
  419. * The file is closed by the filebuf object, not the formatting
  420. * stream.
  421. */
  422. ~basic_ifstream()
  423. { }
  424. // Members:
  425. /**
  426. * @brief Accessing the underlying buffer.
  427. * @return The current basic_filebuf buffer.
  428. *
  429. * This hides both signatures of std::basic_ios::rdbuf().
  430. */
  431. __filebuf_type*
  432. rdbuf() const
  433. { return const_cast<__filebuf_type*>(&_M_filebuf); }
  434. /**
  435. * @brief Wrapper to test for an open file.
  436. * @return @c rdbuf()->is_open()
  437. */
  438. bool
  439. is_open()
  440. { return _M_filebuf.is_open(); }
  441. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  442. // 365. Lack of const-qualification in clause 27
  443. bool
  444. is_open() const
  445. { return _M_filebuf.is_open(); }
  446. /**
  447. * @brief Opens an external file.
  448. * @param s The name of the file.
  449. * @param mode The open mode flags.
  450. *
  451. * Calls @c std::basic_filebuf::open(s,mode|in). If that function
  452. * fails, @c failbit is set in the stream's error state.
  453. *
  454. * Tip: When using std::string to hold the filename, you must use
  455. * .c_str() before passing it to this constructor.
  456. */
  457. void
  458. open(const char* __s, ios_base::openmode __mode = ios_base::in)
  459. {
  460. if (!_M_filebuf.open(__s, __mode | ios_base::in))
  461. this->setstate(ios_base::failbit);
  462. else
  463. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  464. // 409. Closing an fstream should clear error state
  465. this->clear();
  466. }
  467. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  468. /**
  469. * @brief Opens an external file.
  470. * @param s The name of the file.
  471. * @param mode The open mode flags.
  472. *
  473. * Calls @c std::basic_filebuf::open(s,mode|in). If that function
  474. * fails, @c failbit is set in the stream's error state.
  475. */
  476. void
  477. open(const std::string& __s, ios_base::openmode __mode = ios_base::in)
  478. {
  479. if (!_M_filebuf.open(__s, __mode | ios_base::in))
  480. this->setstate(ios_base::failbit);
  481. else
  482. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  483. // 409. Closing an fstream should clear error state
  484. this->clear();
  485. }
  486. #endif
  487. /**
  488. * @brief Close the file.
  489. *
  490. * Calls @c std::basic_filebuf::close(). If that function
  491. * fails, @c failbit is set in the stream's error state.
  492. */
  493. void
  494. close()
  495. {
  496. if (!_M_filebuf.close())
  497. this->setstate(ios_base::failbit);
  498. }
  499. };
  500. // [27.8.1.8] Template class basic_ofstream
  501. /**
  502. * @brief Controlling output for files.
  503. * @ingroup io
  504. *
  505. * This class supports reading from named files, using the inherited
  506. * functions from std::basic_ostream. To control the associated
  507. * sequence, an instance of std::basic_filebuf is used, which this page
  508. * refers to as @c sb.
  509. */
  510. template<typename _CharT, typename _Traits>
  511. class basic_ofstream : public basic_ostream<_CharT,_Traits>
  512. {
  513. public:
  514. // Types:
  515. typedef _CharT char_type;
  516. typedef _Traits traits_type;
  517. typedef typename traits_type::int_type int_type;
  518. typedef typename traits_type::pos_type pos_type;
  519. typedef typename traits_type::off_type off_type;
  520. // Non-standard types:
  521. typedef basic_filebuf<char_type, traits_type> __filebuf_type;
  522. typedef basic_ostream<char_type, traits_type> __ostream_type;
  523. private:
  524. __filebuf_type _M_filebuf;
  525. public:
  526. // Constructors:
  527. /**
  528. * @brief Default constructor.
  529. *
  530. * Initializes @c sb using its default constructor, and passes
  531. * @c &sb to the base class initializer. Does not open any files
  532. * (you haven't given it a filename to open).
  533. */
  534. basic_ofstream(): __ostream_type(), _M_filebuf()
  535. { this->init(&_M_filebuf); }
  536. /**
  537. * @brief Create an output file stream.
  538. * @param s Null terminated string specifying the filename.
  539. * @param mode Open file in specified mode (see std::ios_base).
  540. *
  541. * @c ios_base::out|ios_base::trunc is automatically included in
  542. * @a mode.
  543. *
  544. * Tip: When using std::string to hold the filename, you must use
  545. * .c_str() before passing it to this constructor.
  546. */
  547. explicit
  548. basic_ofstream(const char* __s,
  549. ios_base::openmode __mode = ios_base::out|ios_base::trunc)
  550. : __ostream_type(), _M_filebuf()
  551. {
  552. this->init(&_M_filebuf);
  553. this->open(__s, __mode);
  554. }
  555. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  556. /**
  557. * @brief Create an output file stream.
  558. * @param s std::string specifying the filename.
  559. * @param mode Open file in specified mode (see std::ios_base).
  560. *
  561. * @c ios_base::out|ios_base::trunc is automatically included in
  562. * @a mode.
  563. */
  564. explicit
  565. basic_ofstream(const std::string& __s,
  566. ios_base::openmode __mode = ios_base::out|ios_base::trunc)
  567. : __ostream_type(), _M_filebuf()
  568. {
  569. this->init(&_M_filebuf);
  570. this->open(__s, __mode);
  571. }
  572. #endif
  573. /**
  574. * @brief The destructor does nothing.
  575. *
  576. * The file is closed by the filebuf object, not the formatting
  577. * stream.
  578. */
  579. ~basic_ofstream()
  580. { }
  581. // Members:
  582. /**
  583. * @brief Accessing the underlying buffer.
  584. * @return The current basic_filebuf buffer.
  585. *
  586. * This hides both signatures of std::basic_ios::rdbuf().
  587. */
  588. __filebuf_type*
  589. rdbuf() const
  590. { return const_cast<__filebuf_type*>(&_M_filebuf); }
  591. /**
  592. * @brief Wrapper to test for an open file.
  593. * @return @c rdbuf()->is_open()
  594. */
  595. bool
  596. is_open()
  597. { return _M_filebuf.is_open(); }
  598. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  599. // 365. Lack of const-qualification in clause 27
  600. bool
  601. is_open() const
  602. { return _M_filebuf.is_open(); }
  603. /**
  604. * @brief Opens an external file.
  605. * @param s The name of the file.
  606. * @param mode The open mode flags.
  607. *
  608. * Calls @c std::basic_filebuf::open(s,mode|out|trunc). If that
  609. * function fails, @c failbit is set in the stream's error state.
  610. *
  611. * Tip: When using std::string to hold the filename, you must use
  612. * .c_str() before passing it to this constructor.
  613. */
  614. void
  615. open(const char* __s,
  616. ios_base::openmode __mode = ios_base::out | ios_base::trunc)
  617. {
  618. if (!_M_filebuf.open(__s, __mode | ios_base::out))
  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. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  626. /**
  627. * @brief Opens an external file.
  628. * @param s The name of the file.
  629. * @param mode The open mode flags.
  630. *
  631. * Calls @c std::basic_filebuf::open(s,mode|out|trunc). If that
  632. * function fails, @c failbit is set in the stream's error state.
  633. */
  634. void
  635. open(const std::string& __s,
  636. ios_base::openmode __mode = ios_base::out | ios_base::trunc)
  637. {
  638. if (!_M_filebuf.open(__s, __mode | ios_base::out))
  639. this->setstate(ios_base::failbit);
  640. else
  641. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  642. // 409. Closing an fstream should clear error state
  643. this->clear();
  644. }
  645. #endif
  646. /**
  647. * @brief Close the file.
  648. *
  649. * Calls @c std::basic_filebuf::close(). If that function
  650. * fails, @c failbit is set in the stream's error state.
  651. */
  652. void
  653. close()
  654. {
  655. if (!_M_filebuf.close())
  656. this->setstate(ios_base::failbit);
  657. }
  658. };
  659. // [27.8.1.11] Template class basic_fstream
  660. /**
  661. * @brief Controlling input and output for files.
  662. * @ingroup io
  663. *
  664. * This class supports reading from and writing to named files, using
  665. * the inherited functions from std::basic_iostream. To control the
  666. * associated sequence, an instance of std::basic_filebuf is used, which
  667. * this page refers to as @c sb.
  668. */
  669. template<typename _CharT, typename _Traits>
  670. class basic_fstream : public basic_iostream<_CharT, _Traits>
  671. {
  672. public:
  673. // Types:
  674. typedef _CharT char_type;
  675. typedef _Traits traits_type;
  676. typedef typename traits_type::int_type int_type;
  677. typedef typename traits_type::pos_type pos_type;
  678. typedef typename traits_type::off_type off_type;
  679. // Non-standard types:
  680. typedef basic_filebuf<char_type, traits_type> __filebuf_type;
  681. typedef basic_ios<char_type, traits_type> __ios_type;
  682. typedef basic_iostream<char_type, traits_type> __iostream_type;
  683. private:
  684. __filebuf_type _M_filebuf;
  685. public:
  686. // Constructors/destructor:
  687. /**
  688. * @brief Default constructor.
  689. *
  690. * Initializes @c sb using its default constructor, and passes
  691. * @c &sb to the base class initializer. Does not open any files
  692. * (you haven't given it a filename to open).
  693. */
  694. basic_fstream()
  695. : __iostream_type(), _M_filebuf()
  696. { this->init(&_M_filebuf); }
  697. /**
  698. * @brief Create an input/output file stream.
  699. * @param s Null terminated string specifying the filename.
  700. * @param mode Open file in specified mode (see std::ios_base).
  701. *
  702. * Tip: When using std::string to hold the filename, you must use
  703. * .c_str() before passing it to this constructor.
  704. */
  705. explicit
  706. basic_fstream(const char* __s,
  707. ios_base::openmode __mode = ios_base::in | ios_base::out)
  708. : __iostream_type(NULL), _M_filebuf()
  709. {
  710. this->init(&_M_filebuf);
  711. this->open(__s, __mode);
  712. }
  713. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  714. /**
  715. * @brief Create an input/output file stream.
  716. * @param s Null terminated string specifying the filename.
  717. * @param mode Open file in specified mode (see std::ios_base).
  718. */
  719. explicit
  720. basic_fstream(const std::string& __s,
  721. ios_base::openmode __mode = ios_base::in | ios_base::out)
  722. : __iostream_type(NULL), _M_filebuf()
  723. {
  724. this->init(&_M_filebuf);
  725. this->open(__s, __mode);
  726. }
  727. #endif
  728. /**
  729. * @brief The destructor does nothing.
  730. *
  731. * The file is closed by the filebuf object, not the formatting
  732. * stream.
  733. */
  734. ~basic_fstream()
  735. { }
  736. // Members:
  737. /**
  738. * @brief Accessing the underlying buffer.
  739. * @return The current basic_filebuf buffer.
  740. *
  741. * This hides both signatures of std::basic_ios::rdbuf().
  742. */
  743. __filebuf_type*
  744. rdbuf() const
  745. { return const_cast<__filebuf_type*>(&_M_filebuf); }
  746. /**
  747. * @brief Wrapper to test for an open file.
  748. * @return @c rdbuf()->is_open()
  749. */
  750. bool
  751. is_open()
  752. { return _M_filebuf.is_open(); }
  753. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  754. // 365. Lack of const-qualification in clause 27
  755. bool
  756. is_open() const
  757. { return _M_filebuf.is_open(); }
  758. /**
  759. * @brief Opens an external file.
  760. * @param s The name of the file.
  761. * @param mode The open mode flags.
  762. *
  763. * Calls @c std::basic_filebuf::open(s,mode). If that
  764. * function fails, @c failbit is set in the stream's error state.
  765. *
  766. * Tip: When using std::string to hold the filename, you must use
  767. * .c_str() before passing it to this constructor.
  768. */
  769. void
  770. open(const char* __s,
  771. ios_base::openmode __mode = ios_base::in | ios_base::out)
  772. {
  773. if (!_M_filebuf.open(__s, __mode))
  774. this->setstate(ios_base::failbit);
  775. else
  776. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  777. // 409. Closing an fstream should clear error state
  778. this->clear();
  779. }
  780. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  781. /**
  782. * @brief Opens an external file.
  783. * @param s The name of the file.
  784. * @param mode The open mode flags.
  785. *
  786. * Calls @c std::basic_filebuf::open(s,mode). If that
  787. * function fails, @c failbit is set in the stream's error state.
  788. */
  789. void
  790. open(const std::string& __s,
  791. ios_base::openmode __mode = ios_base::in | ios_base::out)
  792. {
  793. if (!_M_filebuf.open(__s, __mode))
  794. this->setstate(ios_base::failbit);
  795. else
  796. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  797. // 409. Closing an fstream should clear error state
  798. this->clear();
  799. }
  800. #endif
  801. /**
  802. * @brief Close the file.
  803. *
  804. * Calls @c std::basic_filebuf::close(). If that function
  805. * fails, @c failbit is set in the stream's error state.
  806. */
  807. void
  808. close()
  809. {
  810. if (!_M_filebuf.close())
  811. this->setstate(ios_base::failbit);
  812. }
  813. };
  814. _GLIBCXX_END_NAMESPACE
  815. #ifndef _GLIBCXX_EXPORT_TEMPLATE
  816. # include <bits/fstream.tcc>
  817. #endif
  818. #endif /* _GLIBCXX_FSTREAM */