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