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