1 // File based streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
32 // ISO C++ 14882: 27.8 File-based streams
36 * This is a Standard C++ Library header. You should @c #include this header
37 * in your programs, rather than any of the "st[dl]_*.h" implementation files.
40 #ifndef _GLIBCXX_FSTREAM
41 #define _GLIBCXX_FSTREAM 1
43 #pragma GCC system_header
47 #include <locale> // For codecvt
48 #include <cstdio> // For SEEK_SET, SEEK_CUR, SEEK_END, BUFSIZ
49 #include <bits/basic_file.h>
50 #include <bits/gthr.h>
54 // [27.8.1.1] template class basic_filebuf
56 * @brief The actual work of input and output (for files).
58 * This class associates both its input and output sequence with an
59 * external disk file, and maintains a joint file position for both
60 * sequences. Many of its sematics are described in terms of similar
61 * behavior in the Standard C Library's @c FILE streams.
63 // Requirements on traits_type, specific to this class:
64 // traits_type::pos_type must be fpos<traits_type::state_type>
65 // traits_type::off_type must be streamoff
66 // traits_type::state_type must be Assignable and DefaultConstructable,
67 // and traits_type::state_type() must be the initial state for codecvt.
68 template<typename _CharT
, typename _Traits
>
69 class basic_filebuf
: public basic_streambuf
<_CharT
, _Traits
>
73 typedef _CharT char_type
;
74 typedef _Traits traits_type
;
75 typedef typename
traits_type::int_type int_type
;
76 typedef typename
traits_type::pos_type pos_type
;
77 typedef typename
traits_type::off_type off_type
;
85 typedef basic_streambuf
<char_type
, traits_type
> __streambuf_type
;
86 typedef basic_filebuf
<char_type
, traits_type
> __filebuf_type
;
87 typedef __basic_file
<char> __file_type
;
88 typedef typename
traits_type::state_type __state_type
;
89 typedef codecvt
<char_type
, char, __state_type
> __codecvt_type
;
92 friend class ios_base
; // For sync_with_stdio.
96 // MT lock inherited from libio or other low-level io library.
114 * Place to stash in || out || in | out settings for current filebuf.
117 ios_base::openmode _M_mode
;
119 // Beginning state type for codecvt.
125 __state_type _M_state_beg
;
127 // During output, the state that corresponds to pptr(),
128 // during input, the state that corresponds to egptr() and
135 __state_type _M_state_cur
;
137 // Not used for output. During input, the state that corresponds
138 // to eback() and _M_ext_buf.
144 __state_type _M_state_last
;
148 * Pointer to the beginning of internal buffer.
155 * Actual size of internal buffer. This number is equal to the size
156 * of the put area + 1 position, reserved for the overflow char of
162 // Set iff _M_buf is allocated memory from _M_allocate_internal_buffer.
168 bool _M_buf_allocated
;
172 * _M_reading == false && _M_writing == false for 'uncommitted' mode;
173 * _M_reading == true for 'read' mode;
174 * _M_writing == true for 'write' mode;
176 * NB: _M_reading == true && _M_writing == true is unused.
185 * Necessary bits for putback buffer management.
187 * @note pbacks of over one character are not currently supported.
191 char_type
* _M_pback_cur_save
;
192 char_type
* _M_pback_end_save
;
196 // Cached codecvt facet.
197 const __codecvt_type
* _M_codecvt
;
201 * Buffer for external characters. Used for input when
202 * codecvt::always_noconv() == false. When valid, this corresponds
210 * Size of buffer held by _M_ext_buf.
213 streamsize _M_ext_buf_size
;
217 * Pointers into the buffer held by _M_ext_buf that delimit a
218 * subsequence of bytes that have been read but not yet converted.
219 * When valid, _M_ext_next corresponds to egptr().
222 const char* _M_ext_next
;
227 * Initializes pback buffers, and moves normal buffers to safety.
229 * _M_in_cur has already been moved back
237 _M_pback_cur_save
= this->gptr();
238 _M_pback_end_save
= this->egptr();
239 this->setg(&_M_pback
, &_M_pback
, &_M_pback
+ 1);
240 _M_pback_init
= true;
246 * Deactivates pback buffer contents, and restores normal buffer.
248 * The pback buffer has only moved forward.
252 _M_destroy_pback() throw()
256 // Length _M_in_cur moved in the pback buffer.
257 _M_pback_cur_save
+= this->gptr() != this->eback();
258 this->setg(this->_M_buf
, _M_pback_cur_save
, _M_pback_end_save
);
259 _M_pback_init
= false;
264 // Constructors/destructor:
266 * @brief Does not open any files.
268 * The default constructor initializes the parent class using its
274 * @brief The destructor closes the file first.
282 * @brief Returns true if the external file is open.
285 is_open() const throw()
286 { return _M_file
.is_open(); }
289 * @brief Opens an external file.
290 * @param s The name of the file.
291 * @param mode The open mode flags.
292 * @return @c this on success, NULL on failure
294 * If a file is already open, this function immediately fails.
295 * Otherwise it tries to open the file named @a s using the flags
298 * [Table 92 gives the relation between openmode combinations and the
299 * equivalent fopen() flags, but the table has not been copied yet.]
302 open(const char* __s
, ios_base::openmode __mode
);
305 * @brief Closes the currently associated file.
306 * @return @c this on success, NULL on failure
308 * If no file is currently open, this function immediately fails.
310 * If a "put buffer area" exists, @c overflow(eof) is called to flush
311 * all the characters. The file is then closed.
313 * If any operations fail, this function also fails.
325 _M_allocate_internal_buffer();
333 _M_destroy_internal_buffer() throw();
335 // [27.8.1.4] overridden virtual functions
336 // [documentation is inherited]
340 // Stroustrup, 1998, p. 628
341 // underflow() and uflow() functions are called to get the next
342 // charater from the real input source when the buffer is empty.
343 // Buffered input uses underflow()
345 // [documentation is inherited]
349 // [documentation is inherited]
351 pbackfail(int_type __c
= _Traits::eof());
353 // Stroustrup, 1998, p 648
354 // The overflow() function is called to transfer characters to the
355 // real output destination when the buffer is full. A call to
356 // overflow(c) outputs the contents of the buffer plus the
359 // Consume some sequence of the characters in the pending sequence.
366 overflow(int_type __c
= _Traits::eof());
368 // Convert internal byte sequence to external, char-based
369 // sequence via codecvt.
376 _M_convert_to_external(char_type
*, streamsize
);
379 * @brief Manipulates the buffer.
380 * @param s Pointer to a buffer area.
381 * @param n Size of @a s.
384 * If no file has been opened, and both @a s and @a n are zero, then
385 * the stream becomes unbuffered. Otherwise, @c s is used as a
387 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2
390 virtual __streambuf_type
*
391 setbuf(char_type
* __s
, streamsize __n
);
393 // [documentation is inherited]
395 seekoff(off_type __off
, ios_base::seekdir __way
,
396 ios_base::openmode __mode
= ios_base::in
| ios_base::out
);
398 // [documentation is inherited]
400 seekpos(pos_type __pos
,
401 ios_base::openmode __mode
= ios_base::in
| ios_base::out
);
403 // Common code for seekoff and seekpos
410 _M_seek(off_type __off
, ios_base::seekdir __way
, __state_type __state
);
412 // [documentation is inherited]
416 // [documentation is inherited]
418 imbue(const locale
& __loc
);
420 // [documentation is inherited]
422 xsgetn(char_type
* __s
, streamsize __n
);
424 // [documentation is inherited]
426 xsputn(const char_type
* __s
, streamsize __n
);
428 // Flushes output buffer, then writes unshift sequence.
435 _M_terminate_output();
439 * This function sets the pointers of the internal buffer, both get
440 * and put areas. Typically:
442 * __off == egptr() - eback() upon underflow/uflow ('read' mode);
443 * __off == 0 upon overflow ('write' mode);
444 * __off == -1 upon open, setbuf, seekoff/pos ('uncommitted' mode).
446 * NB: epptr() - pbase() == _M_buf_size - 1, since _M_buf_size
447 * reflects the actual allocated memory and the last cell is reserved
448 * for the overflow char of a full put area.
452 _M_set_buffer(streamsize __off
)
454 const bool __testin
= this->_M_mode
& ios_base::in
;
455 const bool __testout
= this->_M_mode
& ios_base::out
;
457 if (__testin
&& __off
> 0)
458 this->setg(this->_M_buf
, this->_M_buf
, this->_M_buf
+ __off
);
460 this->setg(this->_M_buf
, this->_M_buf
, this->_M_buf
);
462 if (__testout
&& __off
== 0 && this->_M_buf_size
> 1 )
463 this->setp(this->_M_buf
, this->_M_buf
+ this->_M_buf_size
- 1);
465 this->setp(NULL
, NULL
);
469 // [27.8.1.5] Template class basic_ifstream
471 * @brief Controlling input for files.
473 * This class supports reading from named files, using the inherited
474 * functions from std::basic_istream. To control the associated
475 * sequence, an instance of std::basic_filebuf is used, which this page
476 * refers to as @c sb.
478 template<typename _CharT
, typename _Traits
>
479 class basic_ifstream
: public basic_istream
<_CharT
, _Traits
>
483 typedef _CharT char_type
;
484 typedef _Traits traits_type
;
485 typedef typename
traits_type::int_type int_type
;
486 typedef typename
traits_type::pos_type pos_type
;
487 typedef typename
traits_type::off_type off_type
;
489 // Non-standard types:
490 typedef basic_filebuf
<char_type
, traits_type
> __filebuf_type
;
491 typedef basic_istream
<char_type
, traits_type
> __istream_type
;
499 __filebuf_type _M_filebuf
;
502 // Constructors/Destructors:
504 * @brief Default constructor.
506 * Initializes @c sb using its default constructor, and passes
507 * @c &sb to the base class initializer. Does not open any files
508 * (you haven't given it a filename to open).
510 basic_ifstream() : __istream_type(), _M_filebuf()
511 { this->init(&_M_filebuf
); }
514 * @brief Create an input file stream.
515 * @param s Null terminated string specifying the filename.
516 * @param mode Open file in specified mode (see std::ios_base).
518 * @c ios_base::in is automatically included in @a mode.
520 * Tip: When using std::string to hold the filename, you must use
521 * .c_str() before passing it to this constructor.
524 basic_ifstream(const char* __s
, ios_base::openmode __mode
= ios_base::in
)
525 : __istream_type(), _M_filebuf()
527 this->init(&_M_filebuf
);
528 this->open(__s
, __mode
);
532 * @brief The destructor does nothing.
534 * The file is closed by the filebuf object, not the formatting
542 * @brief Accessing the underlying buffer.
543 * @return The current basic_filebuf buffer.
545 * This hides both signatures of std::basic_ios::rdbuf().
549 { return const_cast<__filebuf_type
*>(&_M_filebuf
); }
552 * @brief Wrapper to test for an open file.
553 * @return @c rdbuf()->is_open()
557 { return _M_filebuf
.is_open(); }
559 // _GLIBCXX_RESOLVE_LIB_DEFECTS
560 // 365. Lack of const-qualification in clause 27
563 { return _M_filebuf
.is_open(); }
566 * @brief Opens an external file.
567 * @param s The name of the file.
568 * @param mode The open mode flags.
570 * Calls @c std::basic_filebuf::open(s,mode|in). If that function
571 * fails, @c failbit is set in the stream's error state.
573 * Tip: When using std::string to hold the filename, you must use
574 * .c_str() before passing it to this constructor.
577 open(const char* __s
, ios_base::openmode __mode
= ios_base::in
)
579 if (!_M_filebuf
.open(__s
, __mode
| ios_base::in
))
580 this->setstate(ios_base::failbit
);
584 * @brief Close the file.
586 * Calls @c std::basic_filebuf::close(). If that function
587 * fails, @c failbit is set in the stream's error state.
592 if (!_M_filebuf
.close())
593 this->setstate(ios_base::failbit
);
598 // [27.8.1.8] Template class basic_ofstream
600 * @brief Controlling output for files.
602 * This class supports reading from named files, using the inherited
603 * functions from std::basic_ostream. To control the associated
604 * sequence, an instance of std::basic_filebuf is used, which this page
605 * refers to as @c sb.
607 template<typename _CharT
, typename _Traits
>
608 class basic_ofstream
: public basic_ostream
<_CharT
,_Traits
>
612 typedef _CharT char_type
;
613 typedef _Traits traits_type
;
614 typedef typename
traits_type::int_type int_type
;
615 typedef typename
traits_type::pos_type pos_type
;
616 typedef typename
traits_type::off_type off_type
;
618 // Non-standard types:
619 typedef basic_filebuf
<char_type
, traits_type
> __filebuf_type
;
620 typedef basic_ostream
<char_type
, traits_type
> __ostream_type
;
628 __filebuf_type _M_filebuf
;
633 * @brief Default constructor.
635 * Initializes @c sb using its default constructor, and passes
636 * @c &sb to the base class initializer. Does not open any files
637 * (you haven't given it a filename to open).
639 basic_ofstream(): __ostream_type(), _M_filebuf()
640 { this->init(&_M_filebuf
); }
643 * @brief Create an output file stream.
644 * @param s Null terminated string specifying the filename.
645 * @param mode Open file in specified mode (see std::ios_base).
647 * @c ios_base::out|ios_base::trunc is automatically included in
650 * Tip: When using std::string to hold the filename, you must use
651 * .c_str() before passing it to this constructor.
654 basic_ofstream(const char* __s
,
655 ios_base::openmode __mode
= ios_base::out
|ios_base::trunc
)
656 : __ostream_type(), _M_filebuf()
658 this->init(&_M_filebuf
);
659 this->open(__s
, __mode
);
663 * @brief The destructor does nothing.
665 * The file is closed by the filebuf object, not the formatting
673 * @brief Accessing the underlying buffer.
674 * @return The current basic_filebuf buffer.
676 * This hides both signatures of std::basic_ios::rdbuf().
680 { return const_cast<__filebuf_type
*>(&_M_filebuf
); }
683 * @brief Wrapper to test for an open file.
684 * @return @c rdbuf()->is_open()
688 { return _M_filebuf
.is_open(); }
690 // _GLIBCXX_RESOLVE_LIB_DEFECTS
691 // 365. Lack of const-qualification in clause 27
694 { return _M_filebuf
.is_open(); }
697 * @brief Opens an external file.
698 * @param s The name of the file.
699 * @param mode The open mode flags.
701 * Calls @c std::basic_filebuf::open(s,mode|out|trunc). If that
702 * function fails, @c failbit is set in the stream's error state.
704 * Tip: When using std::string to hold the filename, you must use
705 * .c_str() before passing it to this constructor.
708 open(const char* __s
,
709 ios_base::openmode __mode
= ios_base::out
| ios_base::trunc
)
711 if (!_M_filebuf
.open(__s
, __mode
| ios_base::out
))
712 this->setstate(ios_base::failbit
);
716 * @brief Close the file.
718 * Calls @c std::basic_filebuf::close(). If that function
719 * fails, @c failbit is set in the stream's error state.
724 if (!_M_filebuf
.close())
725 this->setstate(ios_base::failbit
);
730 // [27.8.1.11] Template class basic_fstream
732 * @brief Controlling intput and output for files.
734 * This class supports reading from and writing to named files, using
735 * the inherited functions from std::basic_iostream. To control the
736 * associated sequence, an instance of std::basic_filebuf is used, which
737 * this page refers to as @c sb.
739 template<typename _CharT
, typename _Traits
>
740 class basic_fstream
: public basic_iostream
<_CharT
, _Traits
>
744 typedef _CharT char_type
;
745 typedef _Traits traits_type
;
746 typedef typename
traits_type::int_type int_type
;
747 typedef typename
traits_type::pos_type pos_type
;
748 typedef typename
traits_type::off_type off_type
;
750 // Non-standard types:
751 typedef basic_filebuf
<char_type
, traits_type
> __filebuf_type
;
752 typedef basic_ios
<char_type
, traits_type
> __ios_type
;
753 typedef basic_iostream
<char_type
, traits_type
> __iostream_type
;
761 __filebuf_type _M_filebuf
;
764 // Constructors/destructor:
766 * @brief Default constructor.
768 * Initializes @c sb using its default constructor, and passes
769 * @c &sb to the base class initializer. Does not open any files
770 * (you haven't given it a filename to open).
773 : __iostream_type(), _M_filebuf()
774 { this->init(&_M_filebuf
); }
777 * @brief Create an input/output file stream.
778 * @param s Null terminated string specifying the filename.
779 * @param mode Open file in specified mode (see std::ios_base).
781 * Tip: When using std::string to hold the filename, you must use
782 * .c_str() before passing it to this constructor.
785 basic_fstream(const char* __s
,
786 ios_base::openmode __mode
= ios_base::in
| ios_base::out
)
787 : __iostream_type(NULL
), _M_filebuf()
789 this->init(&_M_filebuf
);
790 this->open(__s
, __mode
);
794 * @brief The destructor does nothing.
796 * The file is closed by the filebuf object, not the formatting
804 * @brief Accessing the underlying buffer.
805 * @return The current basic_filebuf buffer.
807 * This hides both signatures of std::basic_ios::rdbuf().
811 { return const_cast<__filebuf_type
*>(&_M_filebuf
); }
814 * @brief Wrapper to test for an open file.
815 * @return @c rdbuf()->is_open()
819 { return _M_filebuf
.is_open(); }
821 // _GLIBCXX_RESOLVE_LIB_DEFECTS
822 // 365. Lack of const-qualification in clause 27
825 { return _M_filebuf
.is_open(); }
828 * @brief Opens an external file.
829 * @param s The name of the file.
830 * @param mode The open mode flags.
832 * Calls @c std::basic_filebuf::open(s,mode). If that
833 * function fails, @c failbit is set in the stream's error state.
835 * Tip: When using std::string to hold the filename, you must use
836 * .c_str() before passing it to this constructor.
839 open(const char* __s
,
840 ios_base::openmode __mode
= ios_base::in
| ios_base::out
)
842 if (!_M_filebuf
.open(__s
, __mode
))
843 this->setstate(ios_base::failbit
);
847 * @brief Close the file.
849 * Calls @c std::basic_filebuf::close(). If that function
850 * fails, @c failbit is set in the stream's error state.
855 if (!_M_filebuf
.close())
856 this->setstate(ios_base::failbit
);
861 #ifndef _GLIBCXX_EXPORT_TEMPLATE
862 # include <bits/fstream.tcc>
865 #endif /* _GLIBCXX_FSTREAM */