00001 // File based streams -*- C++ -*- 00002 00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003 00004 // Free Software Foundation, Inc. 00005 // 00006 // This file is part of the GNU ISO C++ Library. This library is free 00007 // software; you can redistribute it and/or modify it under the 00008 // terms of the GNU General Public License as published by the 00009 // Free Software Foundation; either version 2, or (at your option) 00010 // any later version. 00011 00012 // This library is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 00017 // You should have received a copy of the GNU General Public License along 00018 // with this library; see the file COPYING. If not, write to the Free 00019 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 00020 // USA. 00021 00022 // As a special exception, you may use this file as part of a free software 00023 // library without restriction. Specifically, if other files instantiate 00024 // templates or use macros or inline functions from this file, or you compile 00025 // this file and link it with other files to produce an executable, this 00026 // file does not by itself cause the resulting executable to be covered by 00027 // the GNU General Public License. This exception does not however 00028 // invalidate any other reasons why the executable file might be covered by 00029 // the GNU General Public License. 00030 00031 // 00032 // ISO C++ 14882: 27.8 File-based streams 00033 // 00034 00035 /** @file fstream 00036 * This is a Standard C++ Library header. You should @c #include this header 00037 * in your programs, rather than any of the "st[dl]_*.h" implementation files. 00038 */ 00039 00040 #ifndef _GLIBCXX_FSTREAM 00041 #define _GLIBCXX_FSTREAM 1 00042 00043 #pragma GCC system_header 00044 00045 #include <istream> 00046 #include <ostream> 00047 #include <locale> // For codecvt 00048 #include <cstdio> // For SEEK_SET, SEEK_CUR, SEEK_END, BUFSIZ 00049 #include <bits/basic_file.h> 00050 #include <bits/gthr.h> 00051 00052 namespace std 00053 { 00054 // [27.8.1.1] template class basic_filebuf 00055 /** 00056 * @brief The actual work of input and output (for files). 00057 * 00058 * This class associates both its input and output sequence with an 00059 * external disk file, and maintains a joint file position for both 00060 * sequences. Many of its sematics are described in terms of similar 00061 * behavior in the Standard C Library's @c FILE streams. 00062 */ 00063 // Requirements on traits_type, specific to this class: 00064 // traits_type::pos_type must be fpos<traits_type::state_type> 00065 // traits_type::off_type must be streamoff 00066 // traits_type::state_type must be Assignable and DefaultConstructable, 00067 // and traits_type::state_type() must be the initial state for codecvt. 00068 template<typename _CharT, typename _Traits> 00069 class basic_filebuf : public basic_streambuf<_CharT, _Traits> 00070 { 00071 public: 00072 // Types: 00073 typedef _CharT char_type; 00074 typedef _Traits traits_type; 00075 typedef typename traits_type::int_type int_type; 00076 typedef typename traits_type::pos_type pos_type; 00077 typedef typename traits_type::off_type off_type; 00078 00079 //@{ 00080 /** 00081 * @if maint 00082 * @doctodo 00083 * @endif 00084 */ 00085 typedef basic_streambuf<char_type, traits_type> __streambuf_type; 00086 typedef basic_filebuf<char_type, traits_type> __filebuf_type; 00087 typedef __basic_file<char> __file_type; 00088 typedef typename traits_type::state_type __state_type; 00089 typedef codecvt<char_type, char, __state_type> __codecvt_type; 00090 //@} 00091 00092 friend class ios_base; // For sync_with_stdio. 00093 00094 protected: 00095 // Data Members: 00096 // MT lock inherited from libio or other low-level io library. 00097 /** 00098 * @if maint 00099 * @doctodo 00100 * @endif 00101 */ 00102 __c_lock _M_lock; 00103 00104 // External buffer. 00105 /** 00106 * @if maint 00107 * @doctodo 00108 * @endif 00109 */ 00110 __file_type _M_file; 00111 00112 /** 00113 * @if maint 00114 * Place to stash in || out || in | out settings for current filebuf. 00115 * @endif 00116 */ 00117 ios_base::openmode _M_mode; 00118 00119 // Beginning state type for codecvt. 00120 /** 00121 * @if maint 00122 * @doctodo 00123 * @endif 00124 */ 00125 __state_type _M_state_beg; 00126 00127 // During output, the state that corresponds to pptr(), 00128 // during input, the state that corresponds to egptr() and 00129 // _M_ext_next. 00130 /** 00131 * @if maint 00132 * @doctodo 00133 * @endif 00134 */ 00135 __state_type _M_state_cur; 00136 00137 // Not used for output. During input, the state that corresponds 00138 // to eback() and _M_ext_buf. 00139 /** 00140 * @if maint 00141 * @doctodo 00142 * @endif 00143 */ 00144 __state_type _M_state_last; 00145 00146 /** 00147 * @if maint 00148 * Pointer to the beginning of internal buffer. 00149 * @endif 00150 */ 00151 char_type* _M_buf; 00152 00153 /** 00154 * @if maint 00155 * Actual size of internal buffer. This number is equal to the size 00156 * of the put area + 1 position, reserved for the overflow char of 00157 * a full area. 00158 * @endif 00159 */ 00160 size_t _M_buf_size; 00161 00162 // Set iff _M_buf is allocated memory from _M_allocate_internal_buffer. 00163 /** 00164 * @if maint 00165 * @doctodo 00166 * @endif 00167 */ 00168 bool _M_buf_allocated; 00169 00170 /** 00171 * @if maint 00172 * _M_reading == false && _M_writing == false for 'uncommitted' mode; 00173 * _M_reading == true for 'read' mode; 00174 * _M_writing == true for 'write' mode; 00175 * 00176 * NB: _M_reading == true && _M_writing == true is unused. 00177 * @endif 00178 */ 00179 bool _M_reading; 00180 bool _M_writing; 00181 00182 //@{ 00183 /** 00184 * @if maint 00185 * Necessary bits for putback buffer management. 00186 * 00187 * @note pbacks of over one character are not currently supported. 00188 * @endif 00189 */ 00190 char_type _M_pback; 00191 char_type* _M_pback_cur_save; 00192 char_type* _M_pback_end_save; 00193 bool _M_pback_init; 00194 //@} 00195 00196 // Cached codecvt facet. 00197 const __codecvt_type* _M_codecvt; 00198 00199 /** 00200 * @if maint 00201 * Buffer for external characters. Used for input when 00202 * codecvt::always_noconv() == false. When valid, this corresponds 00203 * to eback(). 00204 * @endif 00205 */ 00206 char* _M_ext_buf; 00207 00208 /** 00209 * @if maint 00210 * Size of buffer held by _M_ext_buf. 00211 * @endif 00212 */ 00213 streamsize _M_ext_buf_size; 00214 00215 /** 00216 * @if maint 00217 * Pointers into the buffer held by _M_ext_buf that delimit a 00218 * subsequence of bytes that have been read but not yet converted. 00219 * When valid, _M_ext_next corresponds to egptr(). 00220 * @endif 00221 */ 00222 const char* _M_ext_next; 00223 char* _M_ext_end; 00224 00225 /** 00226 * @if maint 00227 * Initializes pback buffers, and moves normal buffers to safety. 00228 * Assumptions: 00229 * _M_in_cur has already been moved back 00230 * @endif 00231 */ 00232 void 00233 _M_create_pback() 00234 { 00235 if (!_M_pback_init) 00236 { 00237 _M_pback_cur_save = this->gptr(); 00238 _M_pback_end_save = this->egptr(); 00239 this->setg(&_M_pback, &_M_pback, &_M_pback + 1); 00240 _M_pback_init = true; 00241 } 00242 } 00243 00244 /** 00245 * @if maint 00246 * Deactivates pback buffer contents, and restores normal buffer. 00247 * Assumptions: 00248 * The pback buffer has only moved forward. 00249 * @endif 00250 */ 00251 void 00252 _M_destroy_pback() throw() 00253 { 00254 if (_M_pback_init) 00255 { 00256 // Length _M_in_cur moved in the pback buffer. 00257 _M_pback_cur_save += this->gptr() != this->eback(); 00258 this->setg(this->_M_buf, _M_pback_cur_save, _M_pback_end_save); 00259 _M_pback_init = false; 00260 } 00261 } 00262 00263 public: 00264 // Constructors/destructor: 00265 /** 00266 * @brief Does not open any files. 00267 * 00268 * The default constructor initializes the parent class using its 00269 * own default ctor. 00270 */ 00271 basic_filebuf(); 00272 00273 /** 00274 * @brief The destructor closes the file first. 00275 */ 00276 virtual 00277 ~basic_filebuf() 00278 { this->close(); } 00279 00280 // Members: 00281 /** 00282 * @brief Returns true if the external file is open. 00283 */ 00284 bool 00285 is_open() const throw() { return _M_file.is_open(); } 00286 00287 /** 00288 * @brief Opens an external file. 00289 * @param s The name of the file. 00290 * @param mode The open mode flags. 00291 * @return @c this on success, NULL on failure 00292 * 00293 * If a file is already open, this function immediately fails. 00294 * Otherwise it tries to open the file named @a s using the flags 00295 * given in @a mode. 00296 * 00297 * [Table 92 gives the relation between openmode combinations and the 00298 * equivalent fopen() flags, but the table has not been copied yet.] 00299 */ 00300 __filebuf_type* 00301 open(const char* __s, ios_base::openmode __mode); 00302 00303 /** 00304 * @brief Closes the currently associated file. 00305 * @return @c this on success, NULL on failure 00306 * 00307 * If no file is currently open, this function immediately fails. 00308 * 00309 * If a "put buffer area" exists, @c overflow(eof) is called to flush 00310 * all the characters. The file is then closed. 00311 * 00312 * If any operations fail, this function also fails. 00313 */ 00314 __filebuf_type* 00315 close() throw(); 00316 00317 protected: 00318 /** 00319 * @if maint 00320 * @doctodo 00321 * @endif 00322 */ 00323 void 00324 _M_allocate_internal_buffer(); 00325 00326 /** 00327 * @if maint 00328 * @doctodo 00329 * @endif 00330 */ 00331 void 00332 _M_destroy_internal_buffer() throw(); 00333 00334 // [27.8.1.4] overridden virtual functions 00335 // [documentation is inherited] 00336 virtual streamsize 00337 showmanyc(); 00338 00339 // Stroustrup, 1998, p. 628 00340 // underflow() and uflow() functions are called to get the next 00341 // charater from the real input source when the buffer is empty. 00342 // Buffered input uses underflow() 00343 00344 // [documentation is inherited] 00345 virtual int_type 00346 underflow(); 00347 00348 // [documentation is inherited] 00349 virtual int_type 00350 pbackfail(int_type __c = _Traits::eof()); 00351 00352 // Stroustrup, 1998, p 648 00353 // The overflow() function is called to transfer characters to the 00354 // real output destination when the buffer is full. A call to 00355 // overflow(c) outputs the contents of the buffer plus the 00356 // character c. 00357 // 27.5.2.4.5 00358 // Consume some sequence of the characters in the pending sequence. 00359 /** 00360 * @if maint 00361 * @doctodo 00362 * @endif 00363 */ 00364 virtual int_type 00365 overflow(int_type __c = _Traits::eof()); 00366 00367 // Convert internal byte sequence to external, char-based 00368 // sequence via codecvt. 00369 /** 00370 * @if maint 00371 * @doctodo 00372 * @endif 00373 */ 00374 bool 00375 _M_convert_to_external(char_type*, streamsize); 00376 00377 /** 00378 * @brief Manipulates the buffer. 00379 * @param s Pointer to a buffer area. 00380 * @param n Size of @a s. 00381 * @return @c this 00382 * 00383 * If no file has been opened, and both @a s and @a n are zero, then 00384 * the stream becomes unbuffered. Otherwise, @c s is used as a 00385 * buffer; see 00386 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2 00387 * for more. 00388 */ 00389 virtual __streambuf_type* 00390 setbuf(char_type* __s, streamsize __n); 00391 00392 // [documentation is inherited] 00393 virtual pos_type 00394 seekoff(off_type __off, ios_base::seekdir __way, 00395 ios_base::openmode __mode = ios_base::in | ios_base::out); 00396 00397 // [documentation is inherited] 00398 virtual pos_type 00399 seekpos(pos_type __pos, 00400 ios_base::openmode __mode = ios_base::in | ios_base::out); 00401 00402 // Common code for seekoff and seekpos 00403 /** 00404 * @if maint 00405 * @doctodo 00406 * @endif 00407 */ 00408 pos_type 00409 _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state); 00410 00411 // [documentation is inherited] 00412 virtual int 00413 sync(); 00414 00415 // [documentation is inherited] 00416 virtual void 00417 imbue(const locale& __loc); 00418 00419 // [documentation is inherited] 00420 virtual streamsize 00421 xsgetn(char_type* __s, streamsize __n); 00422 00423 // [documentation is inherited] 00424 virtual streamsize 00425 xsputn(const char_type* __s, streamsize __n); 00426 00427 // Flushes output buffer, then writes unshift sequence. 00428 /** 00429 * @if maint 00430 * @doctodo 00431 * @endif 00432 */ 00433 bool 00434 _M_terminate_output(); 00435 00436 /** 00437 * @if maint 00438 * This function sets the pointers of the internal buffer, both get 00439 * and put areas. Typically: 00440 * 00441 * __off == egptr() - eback() upon underflow/uflow ('read' mode); 00442 * __off == 0 upon overflow ('write' mode); 00443 * __off == -1 upon open, setbuf, seekoff/pos ('uncommitted' mode). 00444 * 00445 * NB: epptr() - pbase() == _M_buf_size - 1, since _M_buf_size 00446 * reflects the actual allocated memory and the last cell is reserved 00447 * for the overflow char of a full put area. 00448 * @endif 00449 */ 00450 void 00451 _M_set_buffer(streamsize __off) 00452 { 00453 const bool __testin = this->_M_mode & ios_base::in; 00454 const bool __testout = this->_M_mode & ios_base::out; 00455 00456 if (__testin && __off > 0) 00457 this->setg(this->_M_buf, this->_M_buf, this->_M_buf + __off); 00458 else 00459 this->setg(this->_M_buf, this->_M_buf, this->_M_buf); 00460 00461 if (__testout && __off == 0 && this->_M_buf_size > 1 ) 00462 this->setp(this->_M_buf, this->_M_buf + this->_M_buf_size - 1); 00463 else 00464 this->setp(NULL, NULL); 00465 } 00466 }; 00467 00468 // [27.8.1.5] Template class basic_ifstream 00469 /** 00470 * @brief Controlling input for files. 00471 * 00472 * This class supports reading from named files, using the inherited 00473 * functions from std::basic_istream. To control the associated 00474 * sequence, an instance of std::basic_filebuf is used, which this page 00475 * refers to as @c sb. 00476 */ 00477 template<typename _CharT, typename _Traits> 00478 class basic_ifstream : public basic_istream<_CharT, _Traits> 00479 { 00480 public: 00481 // Types: 00482 typedef _CharT char_type; 00483 typedef _Traits traits_type; 00484 typedef typename traits_type::int_type int_type; 00485 typedef typename traits_type::pos_type pos_type; 00486 typedef typename traits_type::off_type off_type; 00487 00488 // Non-standard types: 00489 typedef basic_filebuf<char_type, traits_type> __filebuf_type; 00490 typedef basic_istream<char_type, traits_type> __istream_type; 00491 00492 private: 00493 /** 00494 * @if maint 00495 * @doctodo 00496 * @endif 00497 */ 00498 __filebuf_type _M_filebuf; 00499 00500 public: 00501 // Constructors/Destructors: 00502 /** 00503 * @brief Default constructor. 00504 * 00505 * Initializes @c sb using its default constructor, and passes 00506 * @c &sb to the base class initializer. Does not open any files 00507 * (you haven't given it a filename to open). 00508 */ 00509 basic_ifstream() : __istream_type(), _M_filebuf() 00510 { this->init(&_M_filebuf); } 00511 00512 /** 00513 * @brief Create an input file stream. 00514 * @param s Null terminated string specifying the filename. 00515 * @param mode Open file in specified mode (see std::ios_base). 00516 * 00517 * @c ios_base::in is automatically included in @a mode. 00518 * 00519 * Tip: When using std::string to hold the filename, you must use 00520 * .c_str() before passing it to this constructor. 00521 */ 00522 explicit 00523 basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in) 00524 : __istream_type(), _M_filebuf() 00525 { 00526 this->init(&_M_filebuf); 00527 this->open(__s, __mode); 00528 } 00529 00530 /** 00531 * @brief The destructor does nothing. 00532 * 00533 * The file is closed by the filebuf object, not the formatting 00534 * stream. 00535 */ 00536 ~basic_ifstream() 00537 { } 00538 00539 // Members: 00540 /** 00541 * @brief Accessing the underlying buffer. 00542 * @return The current basic_filebuf buffer. 00543 * 00544 * This hides both signatures of std::basic_ios::rdbuf(). 00545 */ 00546 __filebuf_type* 00547 rdbuf() const 00548 { return const_cast<__filebuf_type*>(&_M_filebuf); } 00549 00550 /** 00551 * @brief Wrapper to test for an open file. 00552 * @return @c rdbuf()->is_open() 00553 */ 00554 bool 00555 is_open() { return _M_filebuf.is_open(); } 00556 00557 /** 00558 * @brief Opens an external file. 00559 * @param s The name of the file. 00560 * @param mode The open mode flags. 00561 * 00562 * Calls @c std::basic_filebuf::open(s,mode|in). If that function 00563 * fails, @c failbit is set in the stream's error state. 00564 * 00565 * Tip: When using std::string to hold the filename, you must use 00566 * .c_str() before passing it to this constructor. 00567 */ 00568 void 00569 open(const char* __s, ios_base::openmode __mode = ios_base::in) 00570 { 00571 if (!_M_filebuf.open(__s, __mode | ios_base::in)) 00572 this->setstate(ios_base::failbit); 00573 } 00574 00575 /** 00576 * @brief Close the file. 00577 * 00578 * Calls @c std::basic_filebuf::close(). If that function 00579 * fails, @c failbit is set in the stream's error state. 00580 */ 00581 void 00582 close() 00583 { 00584 if (!_M_filebuf.close()) 00585 this->setstate(ios_base::failbit); 00586 } 00587 }; 00588 00589 00590 // [27.8.1.8] Template class basic_ofstream 00591 /** 00592 * @brief Controlling output for files. 00593 * 00594 * This class supports reading from named files, using the inherited 00595 * functions from std::basic_ostream. To control the associated 00596 * sequence, an instance of std::basic_filebuf is used, which this page 00597 * refers to as @c sb. 00598 */ 00599 template<typename _CharT, typename _Traits> 00600 class basic_ofstream : public basic_ostream<_CharT,_Traits> 00601 { 00602 public: 00603 // Types: 00604 typedef _CharT char_type; 00605 typedef _Traits traits_type; 00606 typedef typename traits_type::int_type int_type; 00607 typedef typename traits_type::pos_type pos_type; 00608 typedef typename traits_type::off_type off_type; 00609 00610 // Non-standard types: 00611 typedef basic_filebuf<char_type, traits_type> __filebuf_type; 00612 typedef basic_ostream<char_type, traits_type> __ostream_type; 00613 00614 private: 00615 /** 00616 * @if maint 00617 * @doctodo 00618 * @endif 00619 */ 00620 __filebuf_type _M_filebuf; 00621 00622 public: 00623 // Constructors: 00624 /** 00625 * @brief Default constructor. 00626 * 00627 * Initializes @c sb using its default constructor, and passes 00628 * @c &sb to the base class initializer. Does not open any files 00629 * (you haven't given it a filename to open). 00630 */ 00631 basic_ofstream(): __ostream_type(), _M_filebuf() 00632 { this->init(&_M_filebuf); } 00633 00634 /** 00635 * @brief Create an output file stream. 00636 * @param s Null terminated string specifying the filename. 00637 * @param mode Open file in specified mode (see std::ios_base). 00638 * 00639 * @c ios_base::out|ios_base::trunc is automatically included in 00640 * @a mode. 00641 * 00642 * Tip: When using std::string to hold the filename, you must use 00643 * .c_str() before passing it to this constructor. 00644 */ 00645 explicit 00646 basic_ofstream(const char* __s, 00647 ios_base::openmode __mode = ios_base::out|ios_base::trunc) 00648 : __ostream_type(), _M_filebuf() 00649 { 00650 this->init(&_M_filebuf); 00651 this->open(__s, __mode); 00652 } 00653 00654 /** 00655 * @brief The destructor does nothing. 00656 * 00657 * The file is closed by the filebuf object, not the formatting 00658 * stream. 00659 */ 00660 ~basic_ofstream() 00661 { } 00662 00663 // Members: 00664 /** 00665 * @brief Accessing the underlying buffer. 00666 * @return The current basic_filebuf buffer. 00667 * 00668 * This hides both signatures of std::basic_ios::rdbuf(). 00669 */ 00670 __filebuf_type* 00671 rdbuf() const 00672 { return const_cast<__filebuf_type*>(&_M_filebuf); } 00673 00674 /** 00675 * @brief Wrapper to test for an open file. 00676 * @return @c rdbuf()->is_open() 00677 */ 00678 bool 00679 is_open() { return _M_filebuf.is_open(); } 00680 00681 /** 00682 * @brief Opens an external file. 00683 * @param s The name of the file. 00684 * @param mode The open mode flags. 00685 * 00686 * Calls @c std::basic_filebuf::open(s,mode|out|trunc). If that 00687 * function fails, @c failbit is set in the stream's error state. 00688 * 00689 * Tip: When using std::string to hold the filename, you must use 00690 * .c_str() before passing it to this constructor. 00691 */ 00692 void 00693 open(const char* __s, 00694 ios_base::openmode __mode = ios_base::out | ios_base::trunc) 00695 { 00696 if (!_M_filebuf.open(__s, __mode | ios_base::out)) 00697 this->setstate(ios_base::failbit); 00698 } 00699 00700 /** 00701 * @brief Close the file. 00702 * 00703 * Calls @c std::basic_filebuf::close(). If that function 00704 * fails, @c failbit is set in the stream's error state. 00705 */ 00706 void 00707 close() 00708 { 00709 if (!_M_filebuf.close()) 00710 this->setstate(ios_base::failbit); 00711 } 00712 }; 00713 00714 00715 // [27.8.1.11] Template class basic_fstream 00716 /** 00717 * @brief Controlling intput and output for files. 00718 * 00719 * This class supports reading from and writing to named files, using 00720 * the inherited functions from std::basic_iostream. To control the 00721 * associated sequence, an instance of std::basic_filebuf is used, which 00722 * this page refers to as @c sb. 00723 */ 00724 template<typename _CharT, typename _Traits> 00725 class basic_fstream : public basic_iostream<_CharT, _Traits> 00726 { 00727 public: 00728 // Types: 00729 typedef _CharT char_type; 00730 typedef _Traits traits_type; 00731 typedef typename traits_type::int_type int_type; 00732 typedef typename traits_type::pos_type pos_type; 00733 typedef typename traits_type::off_type off_type; 00734 00735 // Non-standard types: 00736 typedef basic_filebuf<char_type, traits_type> __filebuf_type; 00737 typedef basic_ios<char_type, traits_type> __ios_type; 00738 typedef basic_iostream<char_type, traits_type> __iostream_type; 00739 00740 private: 00741 /** 00742 * @if maint 00743 * @doctodo 00744 * @endif 00745 */ 00746 __filebuf_type _M_filebuf; 00747 00748 public: 00749 // Constructors/destructor: 00750 /** 00751 * @brief Default constructor. 00752 * 00753 * Initializes @c sb using its default constructor, and passes 00754 * @c &sb to the base class initializer. Does not open any files 00755 * (you haven't given it a filename to open). 00756 */ 00757 basic_fstream() 00758 : __iostream_type(), _M_filebuf() 00759 { this->init(&_M_filebuf); } 00760 00761 /** 00762 * @brief Create an input/output file stream. 00763 * @param s Null terminated string specifying the filename. 00764 * @param mode Open file in specified mode (see std::ios_base). 00765 * 00766 * Tip: When using std::string to hold the filename, you must use 00767 * .c_str() before passing it to this constructor. 00768 */ 00769 explicit 00770 basic_fstream(const char* __s, 00771 ios_base::openmode __mode = ios_base::in | ios_base::out) 00772 : __iostream_type(NULL), _M_filebuf() 00773 { 00774 this->init(&_M_filebuf); 00775 this->open(__s, __mode); 00776 } 00777 00778 /** 00779 * @brief The destructor does nothing. 00780 * 00781 * The file is closed by the filebuf object, not the formatting 00782 * stream. 00783 */ 00784 ~basic_fstream() 00785 { } 00786 00787 // Members: 00788 /** 00789 * @brief Accessing the underlying buffer. 00790 * @return The current basic_filebuf buffer. 00791 * 00792 * This hides both signatures of std::basic_ios::rdbuf(). 00793 */ 00794 __filebuf_type* 00795 rdbuf() const 00796 { return const_cast<__filebuf_type*>(&_M_filebuf); } 00797 00798 /** 00799 * @brief Wrapper to test for an open file. 00800 * @return @c rdbuf()->is_open() 00801 */ 00802 bool 00803 is_open() { return _M_filebuf.is_open(); } 00804 00805 /** 00806 * @brief Opens an external file. 00807 * @param s The name of the file. 00808 * @param mode The open mode flags. 00809 * 00810 * Calls @c std::basic_filebuf::open(s,mode). If that 00811 * function fails, @c failbit is set in the stream's error state. 00812 * 00813 * Tip: When using std::string to hold the filename, you must use 00814 * .c_str() before passing it to this constructor. 00815 */ 00816 void 00817 open(const char* __s, 00818 ios_base::openmode __mode = ios_base::in | ios_base::out) 00819 { 00820 if (!_M_filebuf.open(__s, __mode)) 00821 this->setstate(ios_base::failbit); 00822 } 00823 00824 /** 00825 * @brief Close the file. 00826 * 00827 * Calls @c std::basic_filebuf::close(). If that function 00828 * fails, @c failbit is set in the stream's error state. 00829 */ 00830 void 00831 close() 00832 { 00833 if (!_M_filebuf.close()) 00834 this->setstate(ios_base::failbit); 00835 } 00836 }; 00837 } // namespace std 00838 00839 #ifndef _GLIBCXX_EXPORT_TEMPLATE 00840 # include <bits/fstream.tcc> 00841 #endif 00842 00843 #endif /* _GLIBCXX_FSTREAM */