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