libstdc++
|
00001 // File based streams -*- C++ -*- 00002 00003 // Copyright (C) 1997-2014 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 @c 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 * <pre> 00245 * +---------------------------------------------------------+ 00246 * | ios_base Flag combination stdio equivalent | 00247 * |binary in out trunc app | 00248 * +---------------------------------------------------------+ 00249 * | + w | 00250 * | + + a | 00251 * | + a | 00252 * | + + w | 00253 * | + r | 00254 * | + + r+ | 00255 * | + + + w+ | 00256 * | + + + a+ | 00257 * | + + a+ | 00258 * +---------------------------------------------------------+ 00259 * | + + wb | 00260 * | + + + ab | 00261 * | + + ab | 00262 * | + + + wb | 00263 * | + + rb | 00264 * | + + + r+b | 00265 * | + + + + w+b | 00266 * | + + + + a+b | 00267 * | + + + a+b | 00268 * +---------------------------------------------------------+ 00269 * </pre> 00270 */ 00271 __filebuf_type* 00272 open(const char* __s, ios_base::openmode __mode); 00273 00274 #if __cplusplus >= 201103L 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 || _M_mode & ios_base::app); 00402 00403 if (__testin && __off > 0) 00404 this->setg(_M_buf, _M_buf, _M_buf + __off); 00405 else 00406 this->setg(_M_buf, _M_buf, _M_buf); 00407 00408 if (__testout && __off == 0 && _M_buf_size > 1 ) 00409 this->setp(_M_buf, _M_buf + _M_buf_size - 1); 00410 else 00411 this->setp(0, 0); 00412 } 00413 }; 00414 00415 // [27.8.1.5] Template class basic_ifstream 00416 /** 00417 * @brief Controlling input for files. 00418 * @ingroup io 00419 * 00420 * @tparam _CharT Type of character stream. 00421 * @tparam _Traits Traits for character type, defaults to 00422 * char_traits<_CharT>. 00423 * 00424 * This class supports reading from named files, using the inherited 00425 * functions from std::basic_istream. To control the associated 00426 * sequence, an instance of std::basic_filebuf is used, which this page 00427 * refers to as @c sb. 00428 */ 00429 template<typename _CharT, typename _Traits> 00430 class basic_ifstream : public basic_istream<_CharT, _Traits> 00431 { 00432 public: 00433 // Types: 00434 typedef _CharT char_type; 00435 typedef _Traits traits_type; 00436 typedef typename traits_type::int_type int_type; 00437 typedef typename traits_type::pos_type pos_type; 00438 typedef typename traits_type::off_type off_type; 00439 00440 // Non-standard types: 00441 typedef basic_filebuf<char_type, traits_type> __filebuf_type; 00442 typedef basic_istream<char_type, traits_type> __istream_type; 00443 00444 private: 00445 __filebuf_type _M_filebuf; 00446 00447 public: 00448 // Constructors/Destructors: 00449 /** 00450 * @brief Default constructor. 00451 * 00452 * Initializes @c sb using its default constructor, and passes 00453 * @c &sb to the base class initializer. Does not open any files 00454 * (you haven't given it a filename to open). 00455 */ 00456 basic_ifstream() : __istream_type(), _M_filebuf() 00457 { this->init(&_M_filebuf); } 00458 00459 /** 00460 * @brief Create an input file stream. 00461 * @param __s Null terminated string specifying the filename. 00462 * @param __mode Open file in specified mode (see std::ios_base). 00463 * 00464 * @c ios_base::in is automatically included in @a __mode. 00465 * 00466 * Tip: When using std::string to hold the filename, you must use 00467 * .c_str() before passing it to this constructor. 00468 */ 00469 explicit 00470 basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in) 00471 : __istream_type(), _M_filebuf() 00472 { 00473 this->init(&_M_filebuf); 00474 this->open(__s, __mode); 00475 } 00476 00477 #if __cplusplus >= 201103L 00478 /** 00479 * @brief Create an input file stream. 00480 * @param __s std::string specifying the filename. 00481 * @param __mode Open file in specified mode (see std::ios_base). 00482 * 00483 * @c ios_base::in is automatically included in @a __mode. 00484 */ 00485 explicit 00486 basic_ifstream(const std::string& __s, 00487 ios_base::openmode __mode = ios_base::in) 00488 : __istream_type(), _M_filebuf() 00489 { 00490 this->init(&_M_filebuf); 00491 this->open(__s, __mode); 00492 } 00493 #endif 00494 00495 /** 00496 * @brief The destructor does nothing. 00497 * 00498 * The file is closed by the filebuf object, not the formatting 00499 * stream. 00500 */ 00501 ~basic_ifstream() 00502 { } 00503 00504 // Members: 00505 /** 00506 * @brief Accessing the underlying buffer. 00507 * @return The current basic_filebuf buffer. 00508 * 00509 * This hides both signatures of std::basic_ios::rdbuf(). 00510 */ 00511 __filebuf_type* 00512 rdbuf() const 00513 { return const_cast<__filebuf_type*>(&_M_filebuf); } 00514 00515 /** 00516 * @brief Wrapper to test for an open file. 00517 * @return @c rdbuf()->is_open() 00518 */ 00519 bool 00520 is_open() 00521 { return _M_filebuf.is_open(); } 00522 00523 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00524 // 365. Lack of const-qualification in clause 27 00525 bool 00526 is_open() const 00527 { return _M_filebuf.is_open(); } 00528 00529 /** 00530 * @brief Opens an external file. 00531 * @param __s The name of the file. 00532 * @param __mode The open mode flags. 00533 * 00534 * Calls @c std::basic_filebuf::open(s,__mode|in). If that function 00535 * fails, @c failbit is set in the stream's error state. 00536 * 00537 * Tip: When using std::string to hold the filename, you must use 00538 * .c_str() before passing it to this constructor. 00539 */ 00540 void 00541 open(const char* __s, ios_base::openmode __mode = ios_base::in) 00542 { 00543 if (!_M_filebuf.open(__s, __mode | ios_base::in)) 00544 this->setstate(ios_base::failbit); 00545 else 00546 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00547 // 409. Closing an fstream should clear error state 00548 this->clear(); 00549 } 00550 00551 #if __cplusplus >= 201103L 00552 /** 00553 * @brief Opens an external file. 00554 * @param __s The name of the file. 00555 * @param __mode The open mode flags. 00556 * 00557 * Calls @c std::basic_filebuf::open(__s,__mode|in). If that function 00558 * fails, @c failbit is set in the stream's error state. 00559 */ 00560 void 00561 open(const std::string& __s, ios_base::openmode __mode = ios_base::in) 00562 { 00563 if (!_M_filebuf.open(__s, __mode | ios_base::in)) 00564 this->setstate(ios_base::failbit); 00565 else 00566 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00567 // 409. Closing an fstream should clear error state 00568 this->clear(); 00569 } 00570 #endif 00571 00572 /** 00573 * @brief Close the file. 00574 * 00575 * Calls @c std::basic_filebuf::close(). If that function 00576 * fails, @c failbit is set in the stream's error state. 00577 */ 00578 void 00579 close() 00580 { 00581 if (!_M_filebuf.close()) 00582 this->setstate(ios_base::failbit); 00583 } 00584 }; 00585 00586 00587 // [27.8.1.8] Template class basic_ofstream 00588 /** 00589 * @brief Controlling output for files. 00590 * @ingroup io 00591 * 00592 * @tparam _CharT Type of character stream. 00593 * @tparam _Traits Traits for character type, defaults to 00594 * char_traits<_CharT>. 00595 * 00596 * This class supports reading from named files, using the inherited 00597 * functions from std::basic_ostream. To control the associated 00598 * sequence, an instance of std::basic_filebuf is used, which this page 00599 * refers to as @c sb. 00600 */ 00601 template<typename _CharT, typename _Traits> 00602 class basic_ofstream : public basic_ostream<_CharT,_Traits> 00603 { 00604 public: 00605 // Types: 00606 typedef _CharT char_type; 00607 typedef _Traits traits_type; 00608 typedef typename traits_type::int_type int_type; 00609 typedef typename traits_type::pos_type pos_type; 00610 typedef typename traits_type::off_type off_type; 00611 00612 // Non-standard types: 00613 typedef basic_filebuf<char_type, traits_type> __filebuf_type; 00614 typedef basic_ostream<char_type, traits_type> __ostream_type; 00615 00616 private: 00617 __filebuf_type _M_filebuf; 00618 00619 public: 00620 // Constructors: 00621 /** 00622 * @brief Default constructor. 00623 * 00624 * Initializes @c sb using its default constructor, and passes 00625 * @c &sb to the base class initializer. Does not open any files 00626 * (you haven't given it a filename to open). 00627 */ 00628 basic_ofstream(): __ostream_type(), _M_filebuf() 00629 { this->init(&_M_filebuf); } 00630 00631 /** 00632 * @brief Create an output file stream. 00633 * @param __s Null terminated string specifying the filename. 00634 * @param __mode Open file in specified mode (see std::ios_base). 00635 * 00636 * @c ios_base::out | @c ios_base::trunc is automatically included in 00637 * @a __mode. 00638 * 00639 * Tip: When using std::string to hold the filename, you must use 00640 * .c_str() before passing it to this constructor. 00641 */ 00642 explicit 00643 basic_ofstream(const char* __s, 00644 ios_base::openmode __mode = ios_base::out|ios_base::trunc) 00645 : __ostream_type(), _M_filebuf() 00646 { 00647 this->init(&_M_filebuf); 00648 this->open(__s, __mode); 00649 } 00650 00651 #if __cplusplus >= 201103L 00652 /** 00653 * @brief Create an output file stream. 00654 * @param __s std::string specifying the filename. 00655 * @param __mode Open file in specified mode (see std::ios_base). 00656 * 00657 * @c ios_base::out | @c ios_base::trunc is automatically included in 00658 * @a __mode. 00659 */ 00660 explicit 00661 basic_ofstream(const std::string& __s, 00662 ios_base::openmode __mode = ios_base::out|ios_base::trunc) 00663 : __ostream_type(), _M_filebuf() 00664 { 00665 this->init(&_M_filebuf); 00666 this->open(__s, __mode); 00667 } 00668 #endif 00669 00670 /** 00671 * @brief The destructor does nothing. 00672 * 00673 * The file is closed by the filebuf object, not the formatting 00674 * stream. 00675 */ 00676 ~basic_ofstream() 00677 { } 00678 00679 // Members: 00680 /** 00681 * @brief Accessing the underlying buffer. 00682 * @return The current basic_filebuf buffer. 00683 * 00684 * This hides both signatures of std::basic_ios::rdbuf(). 00685 */ 00686 __filebuf_type* 00687 rdbuf() const 00688 { return const_cast<__filebuf_type*>(&_M_filebuf); } 00689 00690 /** 00691 * @brief Wrapper to test for an open file. 00692 * @return @c rdbuf()->is_open() 00693 */ 00694 bool 00695 is_open() 00696 { return _M_filebuf.is_open(); } 00697 00698 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00699 // 365. Lack of const-qualification in clause 27 00700 bool 00701 is_open() const 00702 { return _M_filebuf.is_open(); } 00703 00704 /** 00705 * @brief Opens an external file. 00706 * @param __s The name of the file. 00707 * @param __mode The open mode flags. 00708 * 00709 * Calls @c std::basic_filebuf::open(__s,__mode|out|trunc). If that 00710 * function fails, @c failbit is set in the stream's error state. 00711 * 00712 * Tip: When using std::string to hold the filename, you must use 00713 * .c_str() before passing it to this constructor. 00714 */ 00715 void 00716 open(const char* __s, 00717 ios_base::openmode __mode = ios_base::out | ios_base::trunc) 00718 { 00719 if (!_M_filebuf.open(__s, __mode | ios_base::out)) 00720 this->setstate(ios_base::failbit); 00721 else 00722 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00723 // 409. Closing an fstream should clear error state 00724 this->clear(); 00725 } 00726 00727 #if __cplusplus >= 201103L 00728 /** 00729 * @brief Opens an external file. 00730 * @param __s The name of the file. 00731 * @param __mode The open mode flags. 00732 * 00733 * Calls @c std::basic_filebuf::open(s,mode|out|trunc). If that 00734 * function fails, @c failbit is set in the stream's error state. 00735 */ 00736 void 00737 open(const std::string& __s, 00738 ios_base::openmode __mode = ios_base::out | ios_base::trunc) 00739 { 00740 if (!_M_filebuf.open(__s, __mode | ios_base::out)) 00741 this->setstate(ios_base::failbit); 00742 else 00743 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00744 // 409. Closing an fstream should clear error state 00745 this->clear(); 00746 } 00747 #endif 00748 00749 /** 00750 * @brief Close the file. 00751 * 00752 * Calls @c std::basic_filebuf::close(). If that function 00753 * fails, @c failbit is set in the stream's error state. 00754 */ 00755 void 00756 close() 00757 { 00758 if (!_M_filebuf.close()) 00759 this->setstate(ios_base::failbit); 00760 } 00761 }; 00762 00763 00764 // [27.8.1.11] Template class basic_fstream 00765 /** 00766 * @brief Controlling input and output for files. 00767 * @ingroup io 00768 * 00769 * @tparam _CharT Type of character stream. 00770 * @tparam _Traits Traits for character type, defaults to 00771 * char_traits<_CharT>. 00772 * 00773 * This class supports reading from and writing to named files, using 00774 * the inherited functions from std::basic_iostream. To control the 00775 * associated sequence, an instance of std::basic_filebuf is used, which 00776 * this page refers to as @c sb. 00777 */ 00778 template<typename _CharT, typename _Traits> 00779 class basic_fstream : public basic_iostream<_CharT, _Traits> 00780 { 00781 public: 00782 // Types: 00783 typedef _CharT char_type; 00784 typedef _Traits traits_type; 00785 typedef typename traits_type::int_type int_type; 00786 typedef typename traits_type::pos_type pos_type; 00787 typedef typename traits_type::off_type off_type; 00788 00789 // Non-standard types: 00790 typedef basic_filebuf<char_type, traits_type> __filebuf_type; 00791 typedef basic_ios<char_type, traits_type> __ios_type; 00792 typedef basic_iostream<char_type, traits_type> __iostream_type; 00793 00794 private: 00795 __filebuf_type _M_filebuf; 00796 00797 public: 00798 // Constructors/destructor: 00799 /** 00800 * @brief Default constructor. 00801 * 00802 * Initializes @c sb using its default constructor, and passes 00803 * @c &sb to the base class initializer. Does not open any files 00804 * (you haven't given it a filename to open). 00805 */ 00806 basic_fstream() 00807 : __iostream_type(), _M_filebuf() 00808 { this->init(&_M_filebuf); } 00809 00810 /** 00811 * @brief Create an input/output file stream. 00812 * @param __s Null terminated string specifying the filename. 00813 * @param __mode Open file in specified mode (see std::ios_base). 00814 * 00815 * Tip: When using std::string to hold the filename, you must use 00816 * .c_str() before passing it to this constructor. 00817 */ 00818 explicit 00819 basic_fstream(const char* __s, 00820 ios_base::openmode __mode = ios_base::in | ios_base::out) 00821 : __iostream_type(0), _M_filebuf() 00822 { 00823 this->init(&_M_filebuf); 00824 this->open(__s, __mode); 00825 } 00826 00827 #if __cplusplus >= 201103L 00828 /** 00829 * @brief Create an input/output file stream. 00830 * @param __s Null terminated string specifying the filename. 00831 * @param __mode Open file in specified mode (see std::ios_base). 00832 */ 00833 explicit 00834 basic_fstream(const std::string& __s, 00835 ios_base::openmode __mode = ios_base::in | ios_base::out) 00836 : __iostream_type(0), _M_filebuf() 00837 { 00838 this->init(&_M_filebuf); 00839 this->open(__s, __mode); 00840 } 00841 #endif 00842 00843 /** 00844 * @brief The destructor does nothing. 00845 * 00846 * The file is closed by the filebuf object, not the formatting 00847 * stream. 00848 */ 00849 ~basic_fstream() 00850 { } 00851 00852 // Members: 00853 /** 00854 * @brief Accessing the underlying buffer. 00855 * @return The current basic_filebuf buffer. 00856 * 00857 * This hides both signatures of std::basic_ios::rdbuf(). 00858 */ 00859 __filebuf_type* 00860 rdbuf() const 00861 { return const_cast<__filebuf_type*>(&_M_filebuf); } 00862 00863 /** 00864 * @brief Wrapper to test for an open file. 00865 * @return @c rdbuf()->is_open() 00866 */ 00867 bool 00868 is_open() 00869 { return _M_filebuf.is_open(); } 00870 00871 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00872 // 365. Lack of const-qualification in clause 27 00873 bool 00874 is_open() const 00875 { return _M_filebuf.is_open(); } 00876 00877 /** 00878 * @brief Opens an external file. 00879 * @param __s The name of the file. 00880 * @param __mode The open mode flags. 00881 * 00882 * Calls @c std::basic_filebuf::open(__s,__mode). If that 00883 * function fails, @c failbit is set in the stream's error state. 00884 * 00885 * Tip: When using std::string to hold the filename, you must use 00886 * .c_str() before passing it to this constructor. 00887 */ 00888 void 00889 open(const char* __s, 00890 ios_base::openmode __mode = ios_base::in | ios_base::out) 00891 { 00892 if (!_M_filebuf.open(__s, __mode)) 00893 this->setstate(ios_base::failbit); 00894 else 00895 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00896 // 409. Closing an fstream should clear error state 00897 this->clear(); 00898 } 00899 00900 #if __cplusplus >= 201103L 00901 /** 00902 * @brief Opens an external file. 00903 * @param __s The name of the file. 00904 * @param __mode The open mode flags. 00905 * 00906 * Calls @c std::basic_filebuf::open(__s,__mode). If that 00907 * function fails, @c failbit is set in the stream's error state. 00908 */ 00909 void 00910 open(const std::string& __s, 00911 ios_base::openmode __mode = ios_base::in | ios_base::out) 00912 { 00913 if (!_M_filebuf.open(__s, __mode)) 00914 this->setstate(ios_base::failbit); 00915 else 00916 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00917 // 409. Closing an fstream should clear error state 00918 this->clear(); 00919 } 00920 #endif 00921 00922 /** 00923 * @brief Close the file. 00924 * 00925 * Calls @c std::basic_filebuf::close(). If that function 00926 * fails, @c failbit is set in the stream's error state. 00927 */ 00928 void 00929 close() 00930 { 00931 if (!_M_filebuf.close()) 00932 this->setstate(ios_base::failbit); 00933 } 00934 }; 00935 00936 _GLIBCXX_END_NAMESPACE_VERSION 00937 } // namespace 00938 00939 #include <bits/fstream.tcc> 00940 00941 #endif /* _GLIBCXX_FSTREAM */