1 1.1 mrg // File based streams -*- C++ -*- 2 1.1 mrg 3 1.12 mrg // Copyright (C) 1997-2022 Free Software Foundation, Inc. 4 1.1 mrg // 5 1.1 mrg // This file is part of the GNU ISO C++ Library. This library is free 6 1.1 mrg // software; you can redistribute it and/or modify it under the 7 1.1 mrg // terms of the GNU General Public License as published by the 8 1.1 mrg // Free Software Foundation; either version 3, or (at your option) 9 1.1 mrg // any later version. 10 1.1 mrg 11 1.1 mrg // This library is distributed in the hope that it will be useful, 12 1.1 mrg // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 1.1 mrg // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 1.1 mrg // GNU General Public License for more details. 15 1.1 mrg 16 1.1 mrg // Under Section 7 of GPL version 3, you are granted additional 17 1.1 mrg // permissions described in the GCC Runtime Library Exception, version 18 1.1 mrg // 3.1, as published by the Free Software Foundation. 19 1.1 mrg 20 1.1 mrg // You should have received a copy of the GNU General Public License and 21 1.1 mrg // a copy of the GCC Runtime Library Exception along with this program; 22 1.1 mrg // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 1.1 mrg // <http://www.gnu.org/licenses/>. 24 1.1 mrg 25 1.3 mrg /** @file include/fstream 26 1.1 mrg * This is a Standard C++ Library header. 27 1.1 mrg */ 28 1.1 mrg 29 1.1 mrg // 30 1.1 mrg // ISO C++ 14882: 27.8 File-based streams 31 1.1 mrg // 32 1.1 mrg 33 1.1 mrg #ifndef _GLIBCXX_FSTREAM 34 1.1 mrg #define _GLIBCXX_FSTREAM 1 35 1.1 mrg 36 1.1 mrg #pragma GCC system_header 37 1.1 mrg 38 1.1 mrg #include <istream> 39 1.1 mrg #include <ostream> 40 1.1 mrg #include <bits/codecvt.h> 41 1.1 mrg #include <cstdio> // For BUFSIZ 42 1.1 mrg #include <bits/basic_file.h> // For __basic_file, __c_lock 43 1.3 mrg #if __cplusplus >= 201103L 44 1.1 mrg #include <string> // For std::string overloads. 45 1.1 mrg #endif 46 1.1 mrg 47 1.12 mrg // This can be overridden by the target's os_defines.h 48 1.12 mrg #ifndef _GLIBCXX_BUFSIZ 49 1.12 mrg # define _GLIBCXX_BUFSIZ BUFSIZ 50 1.12 mrg #endif 51 1.12 mrg 52 1.3 mrg namespace std _GLIBCXX_VISIBILITY(default) 53 1.3 mrg { 54 1.3 mrg _GLIBCXX_BEGIN_NAMESPACE_VERSION 55 1.1 mrg 56 1.9 mrg #if __cplusplus >= 201703L 57 1.9 mrg // Enable if _Path is a filesystem::path or experimental::filesystem::path 58 1.9 mrg template<typename _Path, typename _Result = _Path, typename _Path2 59 1.9 mrg = decltype(std::declval<_Path&>().make_preferred().filename())> 60 1.9 mrg using _If_fs_path = enable_if_t<is_same_v<_Path, _Path2>, _Result>; 61 1.9 mrg #endif // C++17 62 1.9 mrg 63 1.9 mrg 64 1.1 mrg // [27.8.1.1] template class basic_filebuf 65 1.1 mrg /** 66 1.1 mrg * @brief The actual work of input and output (for files). 67 1.1 mrg * @ingroup io 68 1.1 mrg * 69 1.3 mrg * @tparam _CharT Type of character stream. 70 1.3 mrg * @tparam _Traits Traits for character type, defaults to 71 1.3 mrg * char_traits<_CharT>. 72 1.3 mrg * 73 1.1 mrg * This class associates both its input and output sequence with an 74 1.1 mrg * external disk file, and maintains a joint file position for both 75 1.1 mrg * sequences. Many of its semantics are described in terms of similar 76 1.1 mrg * behavior in the Standard C Library's @c FILE streams. 77 1.3 mrg * 78 1.3 mrg * Requirements on traits_type, specific to this class: 79 1.3 mrg * - traits_type::pos_type must be fpos<traits_type::state_type> 80 1.3 mrg * - traits_type::off_type must be streamoff 81 1.3 mrg * - traits_type::state_type must be Assignable and DefaultConstructible, 82 1.3 mrg * - traits_type::state_type() must be the initial state for codecvt. 83 1.1 mrg */ 84 1.1 mrg template<typename _CharT, typename _Traits> 85 1.1 mrg class basic_filebuf : public basic_streambuf<_CharT, _Traits> 86 1.1 mrg { 87 1.5 mrg #if __cplusplus >= 201103L 88 1.5 mrg template<typename _Tp> 89 1.5 mrg using __chk_state = __and_<is_copy_assignable<_Tp>, 90 1.5 mrg is_copy_constructible<_Tp>, 91 1.5 mrg is_default_constructible<_Tp>>; 92 1.5 mrg 93 1.5 mrg static_assert(__chk_state<typename _Traits::state_type>::value, 94 1.5 mrg "state_type must be CopyAssignable, CopyConstructible" 95 1.5 mrg " and DefaultConstructible"); 96 1.5 mrg 97 1.5 mrg static_assert(is_same<typename _Traits::pos_type, 98 1.5 mrg fpos<typename _Traits::state_type>>::value, 99 1.5 mrg "pos_type must be fpos<state_type>"); 100 1.5 mrg #endif 101 1.1 mrg public: 102 1.1 mrg // Types: 103 1.1 mrg typedef _CharT char_type; 104 1.1 mrg typedef _Traits traits_type; 105 1.1 mrg typedef typename traits_type::int_type int_type; 106 1.1 mrg typedef typename traits_type::pos_type pos_type; 107 1.1 mrg typedef typename traits_type::off_type off_type; 108 1.1 mrg 109 1.1 mrg typedef basic_streambuf<char_type, traits_type> __streambuf_type; 110 1.1 mrg typedef basic_filebuf<char_type, traits_type> __filebuf_type; 111 1.1 mrg typedef __basic_file<char> __file_type; 112 1.1 mrg typedef typename traits_type::state_type __state_type; 113 1.1 mrg typedef codecvt<char_type, char, __state_type> __codecvt_type; 114 1.1 mrg 115 1.1 mrg friend class ios_base; // For sync_with_stdio. 116 1.1 mrg 117 1.1 mrg protected: 118 1.1 mrg // Data Members: 119 1.1 mrg // MT lock inherited from libio or other low-level io library. 120 1.1 mrg __c_lock _M_lock; 121 1.1 mrg 122 1.1 mrg // External buffer. 123 1.1 mrg __file_type _M_file; 124 1.1 mrg 125 1.1 mrg /// Place to stash in || out || in | out settings for current filebuf. 126 1.1 mrg ios_base::openmode _M_mode; 127 1.1 mrg 128 1.1 mrg // Beginning state type for codecvt. 129 1.1 mrg __state_type _M_state_beg; 130 1.1 mrg 131 1.1 mrg // During output, the state that corresponds to pptr(), 132 1.1 mrg // during input, the state that corresponds to egptr() and 133 1.1 mrg // _M_ext_next. 134 1.1 mrg __state_type _M_state_cur; 135 1.1 mrg 136 1.1 mrg // Not used for output. During input, the state that corresponds 137 1.1 mrg // to eback() and _M_ext_buf. 138 1.1 mrg __state_type _M_state_last; 139 1.1 mrg 140 1.1 mrg /// Pointer to the beginning of internal buffer. 141 1.5 mrg char_type* _M_buf; 142 1.1 mrg 143 1.1 mrg /** 144 1.1 mrg * Actual size of internal buffer. This number is equal to the size 145 1.1 mrg * of the put area + 1 position, reserved for the overflow char of 146 1.1 mrg * a full area. 147 1.1 mrg */ 148 1.1 mrg size_t _M_buf_size; 149 1.1 mrg 150 1.1 mrg // Set iff _M_buf is allocated memory from _M_allocate_internal_buffer. 151 1.1 mrg bool _M_buf_allocated; 152 1.1 mrg 153 1.1 mrg /** 154 1.1 mrg * _M_reading == false && _M_writing == false for @b uncommitted mode; 155 1.1 mrg * _M_reading == true for @b read mode; 156 1.1 mrg * _M_writing == true for @b write mode; 157 1.1 mrg * 158 1.1 mrg * NB: _M_reading == true && _M_writing == true is unused. 159 1.1 mrg */ 160 1.1 mrg bool _M_reading; 161 1.1 mrg bool _M_writing; 162 1.1 mrg 163 1.11 mrg ///@{ 164 1.1 mrg /** 165 1.1 mrg * Necessary bits for putback buffer management. 166 1.1 mrg * 167 1.1 mrg * @note pbacks of over one character are not currently supported. 168 1.1 mrg */ 169 1.1 mrg char_type _M_pback; 170 1.1 mrg char_type* _M_pback_cur_save; 171 1.1 mrg char_type* _M_pback_end_save; 172 1.1 mrg bool _M_pback_init; 173 1.11 mrg ///@} 174 1.1 mrg 175 1.1 mrg // Cached codecvt facet. 176 1.1 mrg const __codecvt_type* _M_codecvt; 177 1.1 mrg 178 1.1 mrg /** 179 1.1 mrg * Buffer for external characters. Used for input when 180 1.1 mrg * codecvt::always_noconv() == false. When valid, this corresponds 181 1.1 mrg * to eback(). 182 1.1 mrg */ 183 1.1 mrg char* _M_ext_buf; 184 1.1 mrg 185 1.1 mrg /** 186 1.1 mrg * Size of buffer held by _M_ext_buf. 187 1.1 mrg */ 188 1.1 mrg streamsize _M_ext_buf_size; 189 1.1 mrg 190 1.1 mrg /** 191 1.1 mrg * Pointers into the buffer held by _M_ext_buf that delimit a 192 1.1 mrg * subsequence of bytes that have been read but not yet converted. 193 1.1 mrg * When valid, _M_ext_next corresponds to egptr(). 194 1.1 mrg */ 195 1.1 mrg const char* _M_ext_next; 196 1.1 mrg char* _M_ext_end; 197 1.1 mrg 198 1.1 mrg /** 199 1.1 mrg * Initializes pback buffers, and moves normal buffers to safety. 200 1.1 mrg * Assumptions: 201 1.1 mrg * _M_in_cur has already been moved back 202 1.1 mrg */ 203 1.1 mrg void 204 1.1 mrg _M_create_pback() 205 1.1 mrg { 206 1.1 mrg if (!_M_pback_init) 207 1.1 mrg { 208 1.1 mrg _M_pback_cur_save = this->gptr(); 209 1.1 mrg _M_pback_end_save = this->egptr(); 210 1.1 mrg this->setg(&_M_pback, &_M_pback, &_M_pback + 1); 211 1.1 mrg _M_pback_init = true; 212 1.1 mrg } 213 1.1 mrg } 214 1.1 mrg 215 1.1 mrg /** 216 1.1 mrg * Deactivates pback buffer contents, and restores normal buffer. 217 1.1 mrg * Assumptions: 218 1.1 mrg * The pback buffer has only moved forward. 219 1.1 mrg */ 220 1.1 mrg void 221 1.1 mrg _M_destroy_pback() throw() 222 1.1 mrg { 223 1.1 mrg if (_M_pback_init) 224 1.1 mrg { 225 1.1 mrg // Length _M_in_cur moved in the pback buffer. 226 1.1 mrg _M_pback_cur_save += this->gptr() != this->eback(); 227 1.1 mrg this->setg(_M_buf, _M_pback_cur_save, _M_pback_end_save); 228 1.1 mrg _M_pback_init = false; 229 1.1 mrg } 230 1.1 mrg } 231 1.1 mrg 232 1.1 mrg public: 233 1.1 mrg // Constructors/destructor: 234 1.1 mrg /** 235 1.1 mrg * @brief Does not open any files. 236 1.1 mrg * 237 1.1 mrg * The default constructor initializes the parent class using its 238 1.1 mrg * own default ctor. 239 1.1 mrg */ 240 1.1 mrg basic_filebuf(); 241 1.1 mrg 242 1.5 mrg #if __cplusplus >= 201103L 243 1.5 mrg basic_filebuf(const basic_filebuf&) = delete; 244 1.5 mrg basic_filebuf(basic_filebuf&&); 245 1.5 mrg #endif 246 1.5 mrg 247 1.1 mrg /** 248 1.1 mrg * @brief The destructor closes the file first. 249 1.1 mrg */ 250 1.1 mrg virtual 251 1.1 mrg ~basic_filebuf() 252 1.10 mrg { 253 1.10 mrg __try 254 1.10 mrg { this->close(); } 255 1.10 mrg __catch(...) 256 1.10 mrg { } 257 1.10 mrg } 258 1.1 mrg 259 1.5 mrg #if __cplusplus >= 201103L 260 1.5 mrg basic_filebuf& operator=(const basic_filebuf&) = delete; 261 1.5 mrg basic_filebuf& operator=(basic_filebuf&&); 262 1.5 mrg void swap(basic_filebuf&); 263 1.5 mrg #endif 264 1.5 mrg 265 1.1 mrg // Members: 266 1.1 mrg /** 267 1.1 mrg * @brief Returns true if the external file is open. 268 1.1 mrg */ 269 1.1 mrg bool 270 1.1 mrg is_open() const throw() 271 1.1 mrg { return _M_file.is_open(); } 272 1.1 mrg 273 1.1 mrg /** 274 1.1 mrg * @brief Opens an external file. 275 1.3 mrg * @param __s The name of the file. 276 1.3 mrg * @param __mode The open mode flags. 277 1.1 mrg * @return @c this on success, NULL on failure 278 1.1 mrg * 279 1.1 mrg * If a file is already open, this function immediately fails. 280 1.3 mrg * Otherwise it tries to open the file named @a __s using the flags 281 1.3 mrg * given in @a __mode. 282 1.1 mrg * 283 1.1 mrg * Table 92, adapted here, gives the relation between openmode 284 1.5 mrg * combinations and the equivalent @c fopen() flags. 285 1.1 mrg * (NB: lines app, in|out|app, in|app, binary|app, binary|in|out|app, 286 1.1 mrg * and binary|in|app per DR 596) 287 1.5 mrg * <pre> 288 1.1 mrg * +---------------------------------------------------------+ 289 1.1 mrg * | ios_base Flag combination stdio equivalent | 290 1.1 mrg * |binary in out trunc app | 291 1.1 mrg * +---------------------------------------------------------+ 292 1.1 mrg * | + w | 293 1.1 mrg * | + + a | 294 1.1 mrg * | + a | 295 1.1 mrg * | + + w | 296 1.1 mrg * | + r | 297 1.1 mrg * | + + r+ | 298 1.1 mrg * | + + + w+ | 299 1.1 mrg * | + + + a+ | 300 1.1 mrg * | + + a+ | 301 1.1 mrg * +---------------------------------------------------------+ 302 1.1 mrg * | + + wb | 303 1.1 mrg * | + + + ab | 304 1.1 mrg * | + + ab | 305 1.1 mrg * | + + + wb | 306 1.1 mrg * | + + rb | 307 1.1 mrg * | + + + r+b | 308 1.1 mrg * | + + + + w+b | 309 1.1 mrg * | + + + + a+b | 310 1.1 mrg * | + + + a+b | 311 1.1 mrg * +---------------------------------------------------------+ 312 1.5 mrg * </pre> 313 1.1 mrg */ 314 1.1 mrg __filebuf_type* 315 1.1 mrg open(const char* __s, ios_base::openmode __mode); 316 1.1 mrg 317 1.10 mrg #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T 318 1.10 mrg /** 319 1.10 mrg * @brief Opens an external file. 320 1.10 mrg * @param __s The name of the file, as a wide character string. 321 1.10 mrg * @param __mode The open mode flags. 322 1.10 mrg * @return @c this on success, NULL on failure 323 1.10 mrg */ 324 1.10 mrg __filebuf_type* 325 1.10 mrg open(const wchar_t* __s, ios_base::openmode __mode); 326 1.10 mrg #endif 327 1.10 mrg 328 1.3 mrg #if __cplusplus >= 201103L 329 1.1 mrg /** 330 1.1 mrg * @brief Opens an external file. 331 1.3 mrg * @param __s The name of the file. 332 1.3 mrg * @param __mode The open mode flags. 333 1.1 mrg * @return @c this on success, NULL on failure 334 1.1 mrg */ 335 1.1 mrg __filebuf_type* 336 1.1 mrg open(const std::string& __s, ios_base::openmode __mode) 337 1.1 mrg { return open(__s.c_str(), __mode); } 338 1.9 mrg 339 1.9 mrg #if __cplusplus >= 201703L 340 1.9 mrg /** 341 1.9 mrg * @brief Opens an external file. 342 1.9 mrg * @param __s The name of the file, as a filesystem::path. 343 1.9 mrg * @param __mode The open mode flags. 344 1.9 mrg * @return @c this on success, NULL on failure 345 1.9 mrg */ 346 1.9 mrg template<typename _Path> 347 1.9 mrg _If_fs_path<_Path, __filebuf_type*> 348 1.9 mrg open(const _Path& __s, ios_base::openmode __mode) 349 1.9 mrg { return open(__s.c_str(), __mode); } 350 1.9 mrg #endif // C++17 351 1.9 mrg #endif // C++11 352 1.1 mrg 353 1.1 mrg /** 354 1.1 mrg * @brief Closes the currently associated file. 355 1.1 mrg * @return @c this on success, NULL on failure 356 1.1 mrg * 357 1.1 mrg * If no file is currently open, this function immediately fails. 358 1.1 mrg * 359 1.1 mrg * If a <em>put buffer area</em> exists, @c overflow(eof) is 360 1.1 mrg * called to flush all the characters. The file is then 361 1.1 mrg * closed. 362 1.1 mrg * 363 1.1 mrg * If any operations fail, this function also fails. 364 1.1 mrg */ 365 1.1 mrg __filebuf_type* 366 1.1 mrg close(); 367 1.1 mrg 368 1.1 mrg protected: 369 1.1 mrg void 370 1.1 mrg _M_allocate_internal_buffer(); 371 1.1 mrg 372 1.1 mrg void 373 1.1 mrg _M_destroy_internal_buffer() throw(); 374 1.1 mrg 375 1.1 mrg // [27.8.1.4] overridden virtual functions 376 1.1 mrg virtual streamsize 377 1.1 mrg showmanyc(); 378 1.1 mrg 379 1.1 mrg // Stroustrup, 1998, p. 628 380 1.1 mrg // underflow() and uflow() functions are called to get the next 381 1.1 mrg // character from the real input source when the buffer is empty. 382 1.1 mrg // Buffered input uses underflow() 383 1.1 mrg 384 1.1 mrg virtual int_type 385 1.1 mrg underflow(); 386 1.1 mrg 387 1.1 mrg virtual int_type 388 1.1 mrg pbackfail(int_type __c = _Traits::eof()); 389 1.1 mrg 390 1.1 mrg // Stroustrup, 1998, p 648 391 1.1 mrg // The overflow() function is called to transfer characters to the 392 1.1 mrg // real output destination when the buffer is full. A call to 393 1.1 mrg // overflow(c) outputs the contents of the buffer plus the 394 1.1 mrg // character c. 395 1.1 mrg // 27.5.2.4.5 396 1.1 mrg // Consume some sequence of the characters in the pending sequence. 397 1.1 mrg virtual int_type 398 1.1 mrg overflow(int_type __c = _Traits::eof()); 399 1.1 mrg 400 1.1 mrg // Convert internal byte sequence to external, char-based 401 1.1 mrg // sequence via codecvt. 402 1.1 mrg bool 403 1.1 mrg _M_convert_to_external(char_type*, streamsize); 404 1.1 mrg 405 1.1 mrg /** 406 1.1 mrg * @brief Manipulates the buffer. 407 1.3 mrg * @param __s Pointer to a buffer area. 408 1.3 mrg * @param __n Size of @a __s. 409 1.1 mrg * @return @c this 410 1.1 mrg * 411 1.3 mrg * If no file has been opened, and both @a __s and @a __n are zero, then 412 1.3 mrg * the stream becomes unbuffered. Otherwise, @c __s is used as a 413 1.1 mrg * buffer; see 414 1.5 mrg * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering 415 1.1 mrg * for more. 416 1.1 mrg */ 417 1.1 mrg virtual __streambuf_type* 418 1.1 mrg setbuf(char_type* __s, streamsize __n); 419 1.1 mrg 420 1.1 mrg virtual pos_type 421 1.1 mrg seekoff(off_type __off, ios_base::seekdir __way, 422 1.1 mrg ios_base::openmode __mode = ios_base::in | ios_base::out); 423 1.1 mrg 424 1.1 mrg virtual pos_type 425 1.1 mrg seekpos(pos_type __pos, 426 1.1 mrg ios_base::openmode __mode = ios_base::in | ios_base::out); 427 1.1 mrg 428 1.3 mrg // Common code for seekoff, seekpos, and overflow 429 1.1 mrg pos_type 430 1.1 mrg _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state); 431 1.8 mrg 432 1.3 mrg int 433 1.3 mrg _M_get_ext_pos(__state_type &__state); 434 1.1 mrg 435 1.1 mrg virtual int 436 1.1 mrg sync(); 437 1.1 mrg 438 1.1 mrg virtual void 439 1.1 mrg imbue(const locale& __loc); 440 1.1 mrg 441 1.1 mrg virtual streamsize 442 1.1 mrg xsgetn(char_type* __s, streamsize __n); 443 1.1 mrg 444 1.1 mrg virtual streamsize 445 1.1 mrg xsputn(const char_type* __s, streamsize __n); 446 1.1 mrg 447 1.1 mrg // Flushes output buffer, then writes unshift sequence. 448 1.1 mrg bool 449 1.1 mrg _M_terminate_output(); 450 1.1 mrg 451 1.1 mrg /** 452 1.1 mrg * This function sets the pointers of the internal buffer, both get 453 1.1 mrg * and put areas. Typically: 454 1.1 mrg * 455 1.1 mrg * __off == egptr() - eback() upon underflow/uflow (@b read mode); 456 1.1 mrg * __off == 0 upon overflow (@b write mode); 457 1.1 mrg * __off == -1 upon open, setbuf, seekoff/pos (@b uncommitted mode). 458 1.1 mrg * 459 1.1 mrg * NB: epptr() - pbase() == _M_buf_size - 1, since _M_buf_size 460 1.1 mrg * reflects the actual allocated memory and the last cell is reserved 461 1.1 mrg * for the overflow char of a full put area. 462 1.1 mrg */ 463 1.1 mrg void 464 1.1 mrg _M_set_buffer(streamsize __off) 465 1.1 mrg { 466 1.5 mrg const bool __testin = _M_mode & ios_base::in; 467 1.5 mrg const bool __testout = (_M_mode & ios_base::out 468 1.5 mrg || _M_mode & ios_base::app); 469 1.5 mrg 470 1.1 mrg if (__testin && __off > 0) 471 1.1 mrg this->setg(_M_buf, _M_buf, _M_buf + __off); 472 1.1 mrg else 473 1.1 mrg this->setg(_M_buf, _M_buf, _M_buf); 474 1.1 mrg 475 1.1 mrg if (__testout && __off == 0 && _M_buf_size > 1 ) 476 1.1 mrg this->setp(_M_buf, _M_buf + _M_buf_size - 1); 477 1.1 mrg else 478 1.3 mrg this->setp(0, 0); 479 1.1 mrg } 480 1.1 mrg }; 481 1.1 mrg 482 1.1 mrg // [27.8.1.5] Template class basic_ifstream 483 1.1 mrg /** 484 1.1 mrg * @brief Controlling input for files. 485 1.1 mrg * @ingroup io 486 1.1 mrg * 487 1.3 mrg * @tparam _CharT Type of character stream. 488 1.3 mrg * @tparam _Traits Traits for character type, defaults to 489 1.3 mrg * char_traits<_CharT>. 490 1.3 mrg * 491 1.1 mrg * This class supports reading from named files, using the inherited 492 1.1 mrg * functions from std::basic_istream. To control the associated 493 1.1 mrg * sequence, an instance of std::basic_filebuf is used, which this page 494 1.1 mrg * refers to as @c sb. 495 1.1 mrg */ 496 1.1 mrg template<typename _CharT, typename _Traits> 497 1.1 mrg class basic_ifstream : public basic_istream<_CharT, _Traits> 498 1.1 mrg { 499 1.1 mrg public: 500 1.1 mrg // Types: 501 1.1 mrg typedef _CharT char_type; 502 1.1 mrg typedef _Traits traits_type; 503 1.1 mrg typedef typename traits_type::int_type int_type; 504 1.1 mrg typedef typename traits_type::pos_type pos_type; 505 1.1 mrg typedef typename traits_type::off_type off_type; 506 1.1 mrg 507 1.1 mrg // Non-standard types: 508 1.1 mrg typedef basic_filebuf<char_type, traits_type> __filebuf_type; 509 1.1 mrg typedef basic_istream<char_type, traits_type> __istream_type; 510 1.1 mrg 511 1.1 mrg private: 512 1.1 mrg __filebuf_type _M_filebuf; 513 1.1 mrg 514 1.1 mrg public: 515 1.1 mrg // Constructors/Destructors: 516 1.1 mrg /** 517 1.1 mrg * @brief Default constructor. 518 1.1 mrg * 519 1.1 mrg * Initializes @c sb using its default constructor, and passes 520 1.1 mrg * @c &sb to the base class initializer. Does not open any files 521 1.1 mrg * (you haven't given it a filename to open). 522 1.1 mrg */ 523 1.1 mrg basic_ifstream() : __istream_type(), _M_filebuf() 524 1.1 mrg { this->init(&_M_filebuf); } 525 1.1 mrg 526 1.1 mrg /** 527 1.1 mrg * @brief Create an input file stream. 528 1.3 mrg * @param __s Null terminated string specifying the filename. 529 1.3 mrg * @param __mode Open file in specified mode (see std::ios_base). 530 1.1 mrg * 531 1.3 mrg * @c ios_base::in is automatically included in @a __mode. 532 1.1 mrg */ 533 1.1 mrg explicit 534 1.1 mrg basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in) 535 1.1 mrg : __istream_type(), _M_filebuf() 536 1.1 mrg { 537 1.1 mrg this->init(&_M_filebuf); 538 1.1 mrg this->open(__s, __mode); 539 1.1 mrg } 540 1.1 mrg 541 1.10 mrg #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T 542 1.10 mrg /** 543 1.10 mrg * @param Create an input file stream. 544 1.10 mrg * @param __s Wide string specifying the filename. 545 1.10 mrg * @param __mode Open file in specified mode (see std::ios_base). 546 1.10 mrg * 547 1.10 mrg * @c ios_base::in is automatically included in @a __mode. 548 1.10 mrg */ 549 1.10 mrg basic_ifstream(const wchar_t* __s, 550 1.10 mrg ios_base::openmode __mode = ios_base::in) 551 1.10 mrg : __istream_type(), _M_filebuf() 552 1.10 mrg { 553 1.10 mrg this->init(&_M_filebuf); 554 1.10 mrg this->open(__s, __mode); 555 1.10 mrg } 556 1.10 mrg #endif 557 1.10 mrg 558 1.3 mrg #if __cplusplus >= 201103L 559 1.1 mrg /** 560 1.1 mrg * @brief Create an input file stream. 561 1.3 mrg * @param __s std::string specifying the filename. 562 1.3 mrg * @param __mode Open file in specified mode (see std::ios_base). 563 1.1 mrg * 564 1.3 mrg * @c ios_base::in is automatically included in @a __mode. 565 1.1 mrg */ 566 1.1 mrg explicit 567 1.1 mrg basic_ifstream(const std::string& __s, 568 1.1 mrg ios_base::openmode __mode = ios_base::in) 569 1.1 mrg : __istream_type(), _M_filebuf() 570 1.1 mrg { 571 1.1 mrg this->init(&_M_filebuf); 572 1.1 mrg this->open(__s, __mode); 573 1.1 mrg } 574 1.5 mrg 575 1.9 mrg #if __cplusplus >= 201703L 576 1.9 mrg /** 577 1.11 mrg * @brief Create an input file stream. 578 1.9 mrg * @param __s filesystem::path specifying the filename. 579 1.9 mrg * @param __mode Open file in specified mode (see std::ios_base). 580 1.9 mrg * 581 1.9 mrg * @c ios_base::in is automatically included in @a __mode. 582 1.9 mrg */ 583 1.9 mrg template<typename _Path, typename _Require = _If_fs_path<_Path>> 584 1.9 mrg basic_ifstream(const _Path& __s, 585 1.9 mrg ios_base::openmode __mode = ios_base::in) 586 1.9 mrg : basic_ifstream(__s.c_str(), __mode) 587 1.9 mrg { } 588 1.9 mrg #endif // C++17 589 1.9 mrg 590 1.5 mrg basic_ifstream(const basic_ifstream&) = delete; 591 1.5 mrg 592 1.5 mrg basic_ifstream(basic_ifstream&& __rhs) 593 1.5 mrg : __istream_type(std::move(__rhs)), 594 1.5 mrg _M_filebuf(std::move(__rhs._M_filebuf)) 595 1.5 mrg { __istream_type::set_rdbuf(&_M_filebuf); } 596 1.9 mrg #endif // C++11 597 1.1 mrg 598 1.1 mrg /** 599 1.1 mrg * @brief The destructor does nothing. 600 1.1 mrg * 601 1.1 mrg * The file is closed by the filebuf object, not the formatting 602 1.1 mrg * stream. 603 1.1 mrg */ 604 1.1 mrg ~basic_ifstream() 605 1.1 mrg { } 606 1.1 mrg 607 1.5 mrg #if __cplusplus >= 201103L 608 1.5 mrg // 27.8.3.2 Assign and swap: 609 1.5 mrg 610 1.5 mrg basic_ifstream& 611 1.5 mrg operator=(const basic_ifstream&) = delete; 612 1.5 mrg 613 1.5 mrg basic_ifstream& 614 1.5 mrg operator=(basic_ifstream&& __rhs) 615 1.5 mrg { 616 1.5 mrg __istream_type::operator=(std::move(__rhs)); 617 1.5 mrg _M_filebuf = std::move(__rhs._M_filebuf); 618 1.5 mrg return *this; 619 1.5 mrg } 620 1.5 mrg 621 1.5 mrg void 622 1.5 mrg swap(basic_ifstream& __rhs) 623 1.5 mrg { 624 1.5 mrg __istream_type::swap(__rhs); 625 1.5 mrg _M_filebuf.swap(__rhs._M_filebuf); 626 1.5 mrg } 627 1.5 mrg #endif 628 1.5 mrg 629 1.1 mrg // Members: 630 1.1 mrg /** 631 1.1 mrg * @brief Accessing the underlying buffer. 632 1.1 mrg * @return The current basic_filebuf buffer. 633 1.1 mrg * 634 1.1 mrg * This hides both signatures of std::basic_ios::rdbuf(). 635 1.1 mrg */ 636 1.1 mrg __filebuf_type* 637 1.1 mrg rdbuf() const 638 1.1 mrg { return const_cast<__filebuf_type*>(&_M_filebuf); } 639 1.1 mrg 640 1.1 mrg /** 641 1.1 mrg * @brief Wrapper to test for an open file. 642 1.1 mrg * @return @c rdbuf()->is_open() 643 1.1 mrg */ 644 1.1 mrg bool 645 1.1 mrg is_open() 646 1.1 mrg { return _M_filebuf.is_open(); } 647 1.1 mrg 648 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 649 1.1 mrg // 365. Lack of const-qualification in clause 27 650 1.1 mrg bool 651 1.1 mrg is_open() const 652 1.1 mrg { return _M_filebuf.is_open(); } 653 1.1 mrg 654 1.1 mrg /** 655 1.1 mrg * @brief Opens an external file. 656 1.3 mrg * @param __s The name of the file. 657 1.3 mrg * @param __mode The open mode flags. 658 1.1 mrg * 659 1.3 mrg * Calls @c std::basic_filebuf::open(s,__mode|in). If that function 660 1.1 mrg * fails, @c failbit is set in the stream's error state. 661 1.1 mrg */ 662 1.1 mrg void 663 1.1 mrg open(const char* __s, ios_base::openmode __mode = ios_base::in) 664 1.1 mrg { 665 1.1 mrg if (!_M_filebuf.open(__s, __mode | ios_base::in)) 666 1.1 mrg this->setstate(ios_base::failbit); 667 1.1 mrg else 668 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 669 1.1 mrg // 409. Closing an fstream should clear error state 670 1.1 mrg this->clear(); 671 1.1 mrg } 672 1.1 mrg 673 1.10 mrg #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T 674 1.10 mrg /** 675 1.10 mrg * @brief Opens an external file. 676 1.10 mrg * @param __s The name of the file, as a wide character string. 677 1.10 mrg * @param __mode The open mode flags. 678 1.10 mrg * 679 1.10 mrg * Calls @c std::basic_filebuf::open(__s,__mode|in). If that function 680 1.10 mrg * fails, @c failbit is set in the stream's error state. 681 1.10 mrg */ 682 1.10 mrg void 683 1.10 mrg open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in) 684 1.10 mrg { 685 1.10 mrg if (!_M_filebuf.open(__s, __mode | ios_base::in)) 686 1.10 mrg this->setstate(ios_base::failbit); 687 1.10 mrg else 688 1.10 mrg this->clear(); 689 1.10 mrg } 690 1.10 mrg #endif 691 1.10 mrg 692 1.3 mrg #if __cplusplus >= 201103L 693 1.1 mrg /** 694 1.1 mrg * @brief Opens an external file. 695 1.3 mrg * @param __s The name of the file. 696 1.3 mrg * @param __mode The open mode flags. 697 1.1 mrg * 698 1.3 mrg * Calls @c std::basic_filebuf::open(__s,__mode|in). If that function 699 1.1 mrg * fails, @c failbit is set in the stream's error state. 700 1.1 mrg */ 701 1.1 mrg void 702 1.1 mrg open(const std::string& __s, ios_base::openmode __mode = ios_base::in) 703 1.1 mrg { 704 1.1 mrg if (!_M_filebuf.open(__s, __mode | ios_base::in)) 705 1.1 mrg this->setstate(ios_base::failbit); 706 1.1 mrg else 707 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 708 1.1 mrg // 409. Closing an fstream should clear error state 709 1.1 mrg this->clear(); 710 1.1 mrg } 711 1.9 mrg 712 1.9 mrg #if __cplusplus >= 201703L 713 1.9 mrg /** 714 1.9 mrg * @brief Opens an external file. 715 1.9 mrg * @param __s The name of the file, as a filesystem::path. 716 1.9 mrg * @param __mode The open mode flags. 717 1.9 mrg * 718 1.9 mrg * Calls @c std::basic_filebuf::open(__s,__mode|in). If that function 719 1.9 mrg * fails, @c failbit is set in the stream's error state. 720 1.9 mrg */ 721 1.9 mrg template<typename _Path> 722 1.9 mrg _If_fs_path<_Path, void> 723 1.9 mrg open(const _Path& __s, ios_base::openmode __mode = ios_base::in) 724 1.9 mrg { open(__s.c_str(), __mode); } 725 1.9 mrg #endif // C++17 726 1.9 mrg #endif // C++11 727 1.1 mrg 728 1.1 mrg /** 729 1.1 mrg * @brief Close the file. 730 1.1 mrg * 731 1.1 mrg * Calls @c std::basic_filebuf::close(). If that function 732 1.1 mrg * fails, @c failbit is set in the stream's error state. 733 1.1 mrg */ 734 1.1 mrg void 735 1.1 mrg close() 736 1.1 mrg { 737 1.1 mrg if (!_M_filebuf.close()) 738 1.1 mrg this->setstate(ios_base::failbit); 739 1.1 mrg } 740 1.1 mrg }; 741 1.1 mrg 742 1.1 mrg 743 1.1 mrg // [27.8.1.8] Template class basic_ofstream 744 1.1 mrg /** 745 1.1 mrg * @brief Controlling output for files. 746 1.1 mrg * @ingroup io 747 1.1 mrg * 748 1.3 mrg * @tparam _CharT Type of character stream. 749 1.3 mrg * @tparam _Traits Traits for character type, defaults to 750 1.3 mrg * char_traits<_CharT>. 751 1.3 mrg * 752 1.1 mrg * This class supports reading from named files, using the inherited 753 1.1 mrg * functions from std::basic_ostream. To control the associated 754 1.1 mrg * sequence, an instance of std::basic_filebuf is used, which this page 755 1.1 mrg * refers to as @c sb. 756 1.1 mrg */ 757 1.1 mrg template<typename _CharT, typename _Traits> 758 1.1 mrg class basic_ofstream : public basic_ostream<_CharT,_Traits> 759 1.1 mrg { 760 1.1 mrg public: 761 1.1 mrg // Types: 762 1.1 mrg typedef _CharT char_type; 763 1.1 mrg typedef _Traits traits_type; 764 1.1 mrg typedef typename traits_type::int_type int_type; 765 1.1 mrg typedef typename traits_type::pos_type pos_type; 766 1.1 mrg typedef typename traits_type::off_type off_type; 767 1.1 mrg 768 1.1 mrg // Non-standard types: 769 1.1 mrg typedef basic_filebuf<char_type, traits_type> __filebuf_type; 770 1.1 mrg typedef basic_ostream<char_type, traits_type> __ostream_type; 771 1.1 mrg 772 1.1 mrg private: 773 1.1 mrg __filebuf_type _M_filebuf; 774 1.1 mrg 775 1.1 mrg public: 776 1.1 mrg // Constructors: 777 1.1 mrg /** 778 1.1 mrg * @brief Default constructor. 779 1.1 mrg * 780 1.1 mrg * Initializes @c sb using its default constructor, and passes 781 1.1 mrg * @c &sb to the base class initializer. Does not open any files 782 1.1 mrg * (you haven't given it a filename to open). 783 1.1 mrg */ 784 1.1 mrg basic_ofstream(): __ostream_type(), _M_filebuf() 785 1.1 mrg { this->init(&_M_filebuf); } 786 1.1 mrg 787 1.1 mrg /** 788 1.1 mrg * @brief Create an output file stream. 789 1.3 mrg * @param __s Null terminated string specifying the filename. 790 1.3 mrg * @param __mode Open file in specified mode (see std::ios_base). 791 1.1 mrg * 792 1.9 mrg * @c ios_base::out is automatically included in @a __mode. 793 1.1 mrg */ 794 1.1 mrg explicit 795 1.1 mrg basic_ofstream(const char* __s, 796 1.9 mrg ios_base::openmode __mode = ios_base::out) 797 1.1 mrg : __ostream_type(), _M_filebuf() 798 1.1 mrg { 799 1.1 mrg this->init(&_M_filebuf); 800 1.1 mrg this->open(__s, __mode); 801 1.1 mrg } 802 1.1 mrg 803 1.10 mrg #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T 804 1.10 mrg /** 805 1.10 mrg * @param Create an output file stream. 806 1.10 mrg * @param __s Wide string specifying the filename. 807 1.10 mrg * @param __mode Open file in specified mode (see std::ios_base). 808 1.10 mrg * 809 1.10 mrg * @c ios_base::out | @c ios_base::trunc is automatically included in 810 1.10 mrg * @a __mode. 811 1.10 mrg */ 812 1.10 mrg basic_ofstream(const wchar_t* __s, 813 1.10 mrg ios_base::openmode __mode = ios_base::out|ios_base::trunc) 814 1.10 mrg : __ostream_type(), _M_filebuf() 815 1.10 mrg { 816 1.10 mrg this->init(&_M_filebuf); 817 1.10 mrg this->open(__s, __mode); 818 1.10 mrg } 819 1.10 mrg #endif 820 1.10 mrg 821 1.3 mrg #if __cplusplus >= 201103L 822 1.1 mrg /** 823 1.1 mrg * @brief Create an output file stream. 824 1.3 mrg * @param __s std::string specifying the filename. 825 1.3 mrg * @param __mode Open file in specified mode (see std::ios_base). 826 1.1 mrg * 827 1.9 mrg * @c ios_base::out is automatically included in @a __mode. 828 1.1 mrg */ 829 1.1 mrg explicit 830 1.1 mrg basic_ofstream(const std::string& __s, 831 1.9 mrg ios_base::openmode __mode = ios_base::out) 832 1.1 mrg : __ostream_type(), _M_filebuf() 833 1.1 mrg { 834 1.1 mrg this->init(&_M_filebuf); 835 1.1 mrg this->open(__s, __mode); 836 1.1 mrg } 837 1.5 mrg 838 1.9 mrg #if __cplusplus >= 201703L 839 1.9 mrg /** 840 1.11 mrg * @brief Create an output file stream. 841 1.9 mrg * @param __s filesystem::path specifying the filename. 842 1.9 mrg * @param __mode Open file in specified mode (see std::ios_base). 843 1.9 mrg * 844 1.9 mrg * @c ios_base::out is automatically included in @a __mode. 845 1.9 mrg */ 846 1.9 mrg template<typename _Path, typename _Require = _If_fs_path<_Path>> 847 1.9 mrg basic_ofstream(const _Path& __s, 848 1.9 mrg ios_base::openmode __mode = ios_base::out) 849 1.9 mrg : basic_ofstream(__s.c_str(), __mode) 850 1.9 mrg { } 851 1.9 mrg #endif // C++17 852 1.9 mrg 853 1.5 mrg basic_ofstream(const basic_ofstream&) = delete; 854 1.5 mrg 855 1.5 mrg basic_ofstream(basic_ofstream&& __rhs) 856 1.5 mrg : __ostream_type(std::move(__rhs)), 857 1.5 mrg _M_filebuf(std::move(__rhs._M_filebuf)) 858 1.5 mrg { __ostream_type::set_rdbuf(&_M_filebuf); } 859 1.1 mrg #endif 860 1.1 mrg 861 1.1 mrg /** 862 1.1 mrg * @brief The destructor does nothing. 863 1.1 mrg * 864 1.1 mrg * The file is closed by the filebuf object, not the formatting 865 1.1 mrg * stream. 866 1.1 mrg */ 867 1.1 mrg ~basic_ofstream() 868 1.1 mrg { } 869 1.1 mrg 870 1.5 mrg #if __cplusplus >= 201103L 871 1.5 mrg // 27.8.3.2 Assign and swap: 872 1.5 mrg 873 1.5 mrg basic_ofstream& 874 1.5 mrg operator=(const basic_ofstream&) = delete; 875 1.5 mrg 876 1.5 mrg basic_ofstream& 877 1.5 mrg operator=(basic_ofstream&& __rhs) 878 1.5 mrg { 879 1.5 mrg __ostream_type::operator=(std::move(__rhs)); 880 1.5 mrg _M_filebuf = std::move(__rhs._M_filebuf); 881 1.5 mrg return *this; 882 1.5 mrg } 883 1.5 mrg 884 1.5 mrg void 885 1.5 mrg swap(basic_ofstream& __rhs) 886 1.5 mrg { 887 1.5 mrg __ostream_type::swap(__rhs); 888 1.5 mrg _M_filebuf.swap(__rhs._M_filebuf); 889 1.5 mrg } 890 1.5 mrg #endif 891 1.5 mrg 892 1.1 mrg // Members: 893 1.1 mrg /** 894 1.1 mrg * @brief Accessing the underlying buffer. 895 1.1 mrg * @return The current basic_filebuf buffer. 896 1.1 mrg * 897 1.1 mrg * This hides both signatures of std::basic_ios::rdbuf(). 898 1.1 mrg */ 899 1.1 mrg __filebuf_type* 900 1.1 mrg rdbuf() const 901 1.1 mrg { return const_cast<__filebuf_type*>(&_M_filebuf); } 902 1.1 mrg 903 1.1 mrg /** 904 1.1 mrg * @brief Wrapper to test for an open file. 905 1.1 mrg * @return @c rdbuf()->is_open() 906 1.1 mrg */ 907 1.1 mrg bool 908 1.1 mrg is_open() 909 1.1 mrg { return _M_filebuf.is_open(); } 910 1.1 mrg 911 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 912 1.1 mrg // 365. Lack of const-qualification in clause 27 913 1.1 mrg bool 914 1.1 mrg is_open() const 915 1.1 mrg { return _M_filebuf.is_open(); } 916 1.1 mrg 917 1.1 mrg /** 918 1.1 mrg * @brief Opens an external file. 919 1.3 mrg * @param __s The name of the file. 920 1.3 mrg * @param __mode The open mode flags. 921 1.1 mrg * 922 1.9 mrg * Calls @c std::basic_filebuf::open(__s,__mode|out). If that 923 1.1 mrg * function fails, @c failbit is set in the stream's error state. 924 1.1 mrg */ 925 1.1 mrg void 926 1.9 mrg open(const char* __s, ios_base::openmode __mode = ios_base::out) 927 1.1 mrg { 928 1.1 mrg if (!_M_filebuf.open(__s, __mode | ios_base::out)) 929 1.1 mrg this->setstate(ios_base::failbit); 930 1.1 mrg else 931 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 932 1.1 mrg // 409. Closing an fstream should clear error state 933 1.1 mrg this->clear(); 934 1.1 mrg } 935 1.1 mrg 936 1.10 mrg #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T 937 1.10 mrg /** 938 1.10 mrg * @brief Opens an external file. 939 1.10 mrg * @param __s The name of the file. 940 1.10 mrg * @param __mode The open mode flags. 941 1.10 mrg * 942 1.10 mrg * Calls @c std::basic_filebuf::open(__s,__mode|out). If that 943 1.10 mrg * function fails, @c failbit is set in the stream's error state. 944 1.10 mrg */ 945 1.10 mrg void 946 1.10 mrg open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out) 947 1.10 mrg { 948 1.10 mrg if (!_M_filebuf.open(__s, __mode | ios_base::out)) 949 1.10 mrg this->setstate(ios_base::failbit); 950 1.10 mrg else 951 1.10 mrg this->clear(); 952 1.10 mrg } 953 1.10 mrg #endif 954 1.10 mrg 955 1.3 mrg #if __cplusplus >= 201103L 956 1.1 mrg /** 957 1.1 mrg * @brief Opens an external file. 958 1.3 mrg * @param __s The name of the file. 959 1.3 mrg * @param __mode The open mode flags. 960 1.1 mrg * 961 1.9 mrg * Calls @c std::basic_filebuf::open(s,mode|out). If that 962 1.1 mrg * function fails, @c failbit is set in the stream's error state. 963 1.1 mrg */ 964 1.1 mrg void 965 1.9 mrg open(const std::string& __s, ios_base::openmode __mode = ios_base::out) 966 1.1 mrg { 967 1.1 mrg if (!_M_filebuf.open(__s, __mode | ios_base::out)) 968 1.1 mrg this->setstate(ios_base::failbit); 969 1.1 mrg else 970 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 971 1.1 mrg // 409. Closing an fstream should clear error state 972 1.1 mrg this->clear(); 973 1.1 mrg } 974 1.9 mrg 975 1.9 mrg #if __cplusplus >= 201703L 976 1.9 mrg /** 977 1.9 mrg * @brief Opens an external file. 978 1.9 mrg * @param __s The name of the file, as a filesystem::path. 979 1.9 mrg * @param __mode The open mode flags. 980 1.9 mrg * 981 1.9 mrg * Calls @c std::basic_filebuf::open(__s,__mode|out). If that 982 1.9 mrg * function fails, @c failbit is set in the stream's error state. 983 1.9 mrg */ 984 1.9 mrg template<typename _Path> 985 1.9 mrg _If_fs_path<_Path, void> 986 1.9 mrg open(const _Path& __s, ios_base::openmode __mode = ios_base::out) 987 1.9 mrg { open(__s.c_str(), __mode); } 988 1.9 mrg #endif // C++17 989 1.9 mrg #endif // C++11 990 1.1 mrg 991 1.1 mrg /** 992 1.1 mrg * @brief Close the file. 993 1.1 mrg * 994 1.1 mrg * Calls @c std::basic_filebuf::close(). If that function 995 1.1 mrg * fails, @c failbit is set in the stream's error state. 996 1.1 mrg */ 997 1.1 mrg void 998 1.1 mrg close() 999 1.1 mrg { 1000 1.1 mrg if (!_M_filebuf.close()) 1001 1.1 mrg this->setstate(ios_base::failbit); 1002 1.1 mrg } 1003 1.1 mrg }; 1004 1.1 mrg 1005 1.1 mrg 1006 1.1 mrg // [27.8.1.11] Template class basic_fstream 1007 1.1 mrg /** 1008 1.1 mrg * @brief Controlling input and output for files. 1009 1.1 mrg * @ingroup io 1010 1.1 mrg * 1011 1.3 mrg * @tparam _CharT Type of character stream. 1012 1.3 mrg * @tparam _Traits Traits for character type, defaults to 1013 1.3 mrg * char_traits<_CharT>. 1014 1.3 mrg * 1015 1.1 mrg * This class supports reading from and writing to named files, using 1016 1.1 mrg * the inherited functions from std::basic_iostream. To control the 1017 1.1 mrg * associated sequence, an instance of std::basic_filebuf is used, which 1018 1.1 mrg * this page refers to as @c sb. 1019 1.1 mrg */ 1020 1.1 mrg template<typename _CharT, typename _Traits> 1021 1.1 mrg class basic_fstream : public basic_iostream<_CharT, _Traits> 1022 1.1 mrg { 1023 1.1 mrg public: 1024 1.1 mrg // Types: 1025 1.1 mrg typedef _CharT char_type; 1026 1.1 mrg typedef _Traits traits_type; 1027 1.1 mrg typedef typename traits_type::int_type int_type; 1028 1.1 mrg typedef typename traits_type::pos_type pos_type; 1029 1.1 mrg typedef typename traits_type::off_type off_type; 1030 1.1 mrg 1031 1.1 mrg // Non-standard types: 1032 1.1 mrg typedef basic_filebuf<char_type, traits_type> __filebuf_type; 1033 1.1 mrg typedef basic_ios<char_type, traits_type> __ios_type; 1034 1.1 mrg typedef basic_iostream<char_type, traits_type> __iostream_type; 1035 1.1 mrg 1036 1.1 mrg private: 1037 1.1 mrg __filebuf_type _M_filebuf; 1038 1.1 mrg 1039 1.1 mrg public: 1040 1.1 mrg // Constructors/destructor: 1041 1.1 mrg /** 1042 1.1 mrg * @brief Default constructor. 1043 1.1 mrg * 1044 1.1 mrg * Initializes @c sb using its default constructor, and passes 1045 1.1 mrg * @c &sb to the base class initializer. Does not open any files 1046 1.1 mrg * (you haven't given it a filename to open). 1047 1.1 mrg */ 1048 1.1 mrg basic_fstream() 1049 1.1 mrg : __iostream_type(), _M_filebuf() 1050 1.1 mrg { this->init(&_M_filebuf); } 1051 1.1 mrg 1052 1.1 mrg /** 1053 1.1 mrg * @brief Create an input/output file stream. 1054 1.3 mrg * @param __s Null terminated string specifying the filename. 1055 1.3 mrg * @param __mode Open file in specified mode (see std::ios_base). 1056 1.1 mrg */ 1057 1.1 mrg explicit 1058 1.1 mrg basic_fstream(const char* __s, 1059 1.1 mrg ios_base::openmode __mode = ios_base::in | ios_base::out) 1060 1.3 mrg : __iostream_type(0), _M_filebuf() 1061 1.1 mrg { 1062 1.1 mrg this->init(&_M_filebuf); 1063 1.1 mrg this->open(__s, __mode); 1064 1.1 mrg } 1065 1.1 mrg 1066 1.10 mrg #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T 1067 1.10 mrg /** 1068 1.10 mrg * @param Create an input/output file stream. 1069 1.10 mrg * @param __s Wide string specifying the filename. 1070 1.10 mrg * @param __mode Open file in specified mode (see std::ios_base). 1071 1.10 mrg */ 1072 1.10 mrg basic_fstream(const wchar_t* __s, 1073 1.10 mrg ios_base::openmode __mode = ios_base::in | ios_base::out) 1074 1.10 mrg : __iostream_type(0), _M_filebuf() 1075 1.10 mrg { 1076 1.10 mrg this->init(&_M_filebuf); 1077 1.10 mrg this->open(__s, __mode); 1078 1.10 mrg } 1079 1.10 mrg #endif 1080 1.10 mrg 1081 1.3 mrg #if __cplusplus >= 201103L 1082 1.1 mrg /** 1083 1.1 mrg * @brief Create an input/output file stream. 1084 1.3 mrg * @param __s Null terminated string specifying the filename. 1085 1.3 mrg * @param __mode Open file in specified mode (see std::ios_base). 1086 1.1 mrg */ 1087 1.1 mrg explicit 1088 1.1 mrg basic_fstream(const std::string& __s, 1089 1.1 mrg ios_base::openmode __mode = ios_base::in | ios_base::out) 1090 1.3 mrg : __iostream_type(0), _M_filebuf() 1091 1.1 mrg { 1092 1.1 mrg this->init(&_M_filebuf); 1093 1.1 mrg this->open(__s, __mode); 1094 1.1 mrg } 1095 1.5 mrg 1096 1.9 mrg #if __cplusplus >= 201703L 1097 1.9 mrg /** 1098 1.11 mrg * @brief Create an input/output file stream. 1099 1.9 mrg * @param __s filesystem::path specifying the filename. 1100 1.9 mrg * @param __mode Open file in specified mode (see std::ios_base). 1101 1.9 mrg */ 1102 1.9 mrg template<typename _Path, typename _Require = _If_fs_path<_Path>> 1103 1.9 mrg basic_fstream(const _Path& __s, 1104 1.9 mrg ios_base::openmode __mode = ios_base::in | ios_base::out) 1105 1.9 mrg : basic_fstream(__s.c_str(), __mode) 1106 1.9 mrg { } 1107 1.9 mrg #endif // C++17 1108 1.9 mrg 1109 1.5 mrg basic_fstream(const basic_fstream&) = delete; 1110 1.5 mrg 1111 1.5 mrg basic_fstream(basic_fstream&& __rhs) 1112 1.5 mrg : __iostream_type(std::move(__rhs)), 1113 1.5 mrg _M_filebuf(std::move(__rhs._M_filebuf)) 1114 1.5 mrg { __iostream_type::set_rdbuf(&_M_filebuf); } 1115 1.1 mrg #endif 1116 1.1 mrg 1117 1.1 mrg /** 1118 1.1 mrg * @brief The destructor does nothing. 1119 1.1 mrg * 1120 1.1 mrg * The file is closed by the filebuf object, not the formatting 1121 1.1 mrg * stream. 1122 1.1 mrg */ 1123 1.1 mrg ~basic_fstream() 1124 1.1 mrg { } 1125 1.1 mrg 1126 1.5 mrg #if __cplusplus >= 201103L 1127 1.5 mrg // 27.8.3.2 Assign and swap: 1128 1.5 mrg 1129 1.5 mrg basic_fstream& 1130 1.5 mrg operator=(const basic_fstream&) = delete; 1131 1.5 mrg 1132 1.5 mrg basic_fstream& 1133 1.5 mrg operator=(basic_fstream&& __rhs) 1134 1.5 mrg { 1135 1.5 mrg __iostream_type::operator=(std::move(__rhs)); 1136 1.5 mrg _M_filebuf = std::move(__rhs._M_filebuf); 1137 1.5 mrg return *this; 1138 1.5 mrg } 1139 1.5 mrg 1140 1.5 mrg void 1141 1.5 mrg swap(basic_fstream& __rhs) 1142 1.5 mrg { 1143 1.5 mrg __iostream_type::swap(__rhs); 1144 1.5 mrg _M_filebuf.swap(__rhs._M_filebuf); 1145 1.5 mrg } 1146 1.5 mrg #endif 1147 1.5 mrg 1148 1.1 mrg // Members: 1149 1.1 mrg /** 1150 1.1 mrg * @brief Accessing the underlying buffer. 1151 1.1 mrg * @return The current basic_filebuf buffer. 1152 1.1 mrg * 1153 1.1 mrg * This hides both signatures of std::basic_ios::rdbuf(). 1154 1.1 mrg */ 1155 1.1 mrg __filebuf_type* 1156 1.1 mrg rdbuf() const 1157 1.1 mrg { return const_cast<__filebuf_type*>(&_M_filebuf); } 1158 1.1 mrg 1159 1.1 mrg /** 1160 1.1 mrg * @brief Wrapper to test for an open file. 1161 1.1 mrg * @return @c rdbuf()->is_open() 1162 1.1 mrg */ 1163 1.1 mrg bool 1164 1.1 mrg is_open() 1165 1.1 mrg { return _M_filebuf.is_open(); } 1166 1.1 mrg 1167 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 1168 1.1 mrg // 365. Lack of const-qualification in clause 27 1169 1.1 mrg bool 1170 1.1 mrg is_open() const 1171 1.1 mrg { return _M_filebuf.is_open(); } 1172 1.1 mrg 1173 1.1 mrg /** 1174 1.1 mrg * @brief Opens an external file. 1175 1.3 mrg * @param __s The name of the file. 1176 1.3 mrg * @param __mode The open mode flags. 1177 1.1 mrg * 1178 1.3 mrg * Calls @c std::basic_filebuf::open(__s,__mode). If that 1179 1.1 mrg * function fails, @c failbit is set in the stream's error state. 1180 1.1 mrg */ 1181 1.1 mrg void 1182 1.1 mrg open(const char* __s, 1183 1.1 mrg ios_base::openmode __mode = ios_base::in | ios_base::out) 1184 1.1 mrg { 1185 1.1 mrg if (!_M_filebuf.open(__s, __mode)) 1186 1.1 mrg this->setstate(ios_base::failbit); 1187 1.1 mrg else 1188 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 1189 1.1 mrg // 409. Closing an fstream should clear error state 1190 1.1 mrg this->clear(); 1191 1.1 mrg } 1192 1.1 mrg 1193 1.10 mrg #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T 1194 1.10 mrg /** 1195 1.10 mrg * @brief Opens an external file. 1196 1.10 mrg * @param __s The name of the file. 1197 1.10 mrg * @param __mode The open mode flags. 1198 1.10 mrg * 1199 1.10 mrg * Calls @c std::basic_filebuf::open(__s,__mode). If that 1200 1.10 mrg * function fails, @c failbit is set in the stream's error state. 1201 1.10 mrg */ 1202 1.10 mrg void 1203 1.10 mrg open(const wchar_t* __s, 1204 1.10 mrg ios_base::openmode __mode = ios_base::in | ios_base::out) 1205 1.10 mrg { 1206 1.10 mrg if (!_M_filebuf.open(__s, __mode)) 1207 1.10 mrg this->setstate(ios_base::failbit); 1208 1.10 mrg else 1209 1.10 mrg this->clear(); 1210 1.10 mrg } 1211 1.10 mrg #endif 1212 1.10 mrg 1213 1.3 mrg #if __cplusplus >= 201103L 1214 1.1 mrg /** 1215 1.1 mrg * @brief Opens an external file. 1216 1.3 mrg * @param __s The name of the file. 1217 1.3 mrg * @param __mode The open mode flags. 1218 1.1 mrg * 1219 1.3 mrg * Calls @c std::basic_filebuf::open(__s,__mode). If that 1220 1.1 mrg * function fails, @c failbit is set in the stream's error state. 1221 1.1 mrg */ 1222 1.1 mrg void 1223 1.1 mrg open(const std::string& __s, 1224 1.1 mrg ios_base::openmode __mode = ios_base::in | ios_base::out) 1225 1.1 mrg { 1226 1.1 mrg if (!_M_filebuf.open(__s, __mode)) 1227 1.1 mrg this->setstate(ios_base::failbit); 1228 1.1 mrg else 1229 1.1 mrg // _GLIBCXX_RESOLVE_LIB_DEFECTS 1230 1.1 mrg // 409. Closing an fstream should clear error state 1231 1.1 mrg this->clear(); 1232 1.1 mrg } 1233 1.9 mrg 1234 1.9 mrg #if __cplusplus >= 201703L 1235 1.9 mrg /** 1236 1.9 mrg * @brief Opens an external file. 1237 1.9 mrg * @param __s The name of the file, as a filesystem::path. 1238 1.9 mrg * @param __mode The open mode flags. 1239 1.9 mrg * 1240 1.9 mrg * Calls @c std::basic_filebuf::open(__s,__mode). If that 1241 1.9 mrg * function fails, @c failbit is set in the stream's error state. 1242 1.9 mrg */ 1243 1.9 mrg template<typename _Path> 1244 1.9 mrg _If_fs_path<_Path, void> 1245 1.9 mrg open(const _Path& __s, 1246 1.9 mrg ios_base::openmode __mode = ios_base::in | ios_base::out) 1247 1.9 mrg { open(__s.c_str(), __mode); } 1248 1.9 mrg #endif // C++17 1249 1.9 mrg #endif // C++11 1250 1.1 mrg 1251 1.1 mrg /** 1252 1.1 mrg * @brief Close the file. 1253 1.1 mrg * 1254 1.1 mrg * Calls @c std::basic_filebuf::close(). If that function 1255 1.1 mrg * fails, @c failbit is set in the stream's error state. 1256 1.1 mrg */ 1257 1.1 mrg void 1258 1.1 mrg close() 1259 1.1 mrg { 1260 1.1 mrg if (!_M_filebuf.close()) 1261 1.1 mrg this->setstate(ios_base::failbit); 1262 1.1 mrg } 1263 1.1 mrg }; 1264 1.1 mrg 1265 1.5 mrg #if __cplusplus >= 201103L 1266 1.5 mrg /// Swap specialization for filebufs. 1267 1.5 mrg template <class _CharT, class _Traits> 1268 1.5 mrg inline void 1269 1.5 mrg swap(basic_filebuf<_CharT, _Traits>& __x, 1270 1.5 mrg basic_filebuf<_CharT, _Traits>& __y) 1271 1.5 mrg { __x.swap(__y); } 1272 1.5 mrg 1273 1.5 mrg /// Swap specialization for ifstreams. 1274 1.5 mrg template <class _CharT, class _Traits> 1275 1.5 mrg inline void 1276 1.5 mrg swap(basic_ifstream<_CharT, _Traits>& __x, 1277 1.5 mrg basic_ifstream<_CharT, _Traits>& __y) 1278 1.5 mrg { __x.swap(__y); } 1279 1.5 mrg 1280 1.5 mrg /// Swap specialization for ofstreams. 1281 1.5 mrg template <class _CharT, class _Traits> 1282 1.5 mrg inline void 1283 1.5 mrg swap(basic_ofstream<_CharT, _Traits>& __x, 1284 1.5 mrg basic_ofstream<_CharT, _Traits>& __y) 1285 1.5 mrg { __x.swap(__y); } 1286 1.5 mrg 1287 1.5 mrg /// Swap specialization for fstreams. 1288 1.5 mrg template <class _CharT, class _Traits> 1289 1.5 mrg inline void 1290 1.5 mrg swap(basic_fstream<_CharT, _Traits>& __x, 1291 1.5 mrg basic_fstream<_CharT, _Traits>& __y) 1292 1.5 mrg { __x.swap(__y); } 1293 1.5 mrg #endif 1294 1.5 mrg 1295 1.3 mrg _GLIBCXX_END_NAMESPACE_VERSION 1296 1.3 mrg } // namespace 1297 1.1 mrg 1298 1.3 mrg #include <bits/fstream.tcc> 1299 1.1 mrg 1300 1.1 mrg #endif /* _GLIBCXX_FSTREAM */ 1301