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