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