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