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