1 // File based streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
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.
41 #define _CPP_FSTREAM 1
43 #pragma GCC system_header
47 #include <locale> // For codecvt
48 #include <bits/basic_file.h>
49 #include <bits/gthr.h>
53 // [27.8.1.1] template class basic_filebuf
55 * @brief The actual work of input and output (for files).
57 * This class associates both its input and output sequence with an
58 * external disk file, and maintains a joint file position for both
59 * sequences. Many of its sematics are described in terms of similar
60 * behavior in the Standard C Library's @c FILE streams.
62 template<typename _CharT, typename _Traits>
63 class basic_filebuf : public basic_streambuf<_CharT, _Traits>
67 typedef _CharT char_type;
68 typedef _Traits traits_type;
69 typedef typename traits_type::int_type int_type;
70 typedef typename traits_type::pos_type pos_type;
71 typedef typename traits_type::off_type off_type;
79 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
80 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
81 typedef __basic_file<char> __file_type;
82 typedef typename traits_type::state_type __state_type;
83 typedef codecvt<char_type, char, __state_type> __codecvt_type;
84 typedef typename __codecvt_type::result __res_type;
85 typedef ctype<char_type> __ctype_type;
88 friend class ios_base; // For sync_with_stdio.
92 // MT lock inherited from libio or other low-level io library.
108 // Current and beginning state type for codecvt.
114 __state_type _M_state_cur;
115 __state_type _M_state_beg;
117 // Set iff _M_buf is allocated memory from _M_allocate_internal_buffer.
123 bool _M_buf_allocated;
126 bool _M_last_overflowed;
128 // The position in the buffer corresponding to the external file
135 char_type* _M_filepos;
138 // Constructors/destructor:
140 * @brief Does not open any files.
142 * The default constructor initializes the parent class using its
148 * @brief The destructor closes the file first.
154 _M_last_overflowed = false;
159 * @brief Returns true if the external file is open.
162 is_open() const { return _M_file.is_open(); }
165 * @brief Opens an external file.
166 * @param s The name of the file.
167 * @param mode The open mode flags.
168 * @return @c this on success, NULL on failure
170 * If a file is already open, this function immediately fails.
171 * Otherwise it tries to open the file named @a s using the flags
174 * [Table 92 gives the relation between openmode combinations and the
175 * equivalent fopen() flags, but the table has not been copied yet.]
178 open(const char* __s, ios_base::openmode __mode);
181 * @brief Closes the currently associated file.
182 * @return @c this on success, NULL on failure
184 * If no file is currently open, this function immediately fails.
186 * If a "put buffer area" exists, @c overflow(eof) is called to flush
187 * all the characters. The file is then closed.
189 * If any operations fail, this function also fails.
201 _M_allocate_internal_buffer();
209 _M_destroy_internal_buffer();
211 // [27.8.1.4] overridden virtual functions
212 // [documentation is inherited]
216 // Stroustrup, 1998, p. 628
217 // underflow() and uflow() functions are called to get the next
218 // charater from the real input source when the buffer is empty.
219 // Buffered input uses underflow()
221 // The only difference between underflow() and uflow() is that the
222 // latter bumps _M_in_cur after the read. In the sync_with_stdio
223 // case, this is important, as we need to unget the read character in
224 // the underflow() case in order to maintain synchronization. So
225 // instead of calling underflow() from uflow(), we create a common
226 // subroutine to do the real work.
233 _M_underflow_common(bool __bump);
235 // [documentation is inherited]
239 // [documentation is inherited]
243 // [documentation is inherited]
245 pbackfail(int_type __c = _Traits::eof());
247 // NB: For what the standard expects of the overflow function,
248 // see _M_really_overflow(), below. Because basic_streambuf's
249 // sputc/sputn call overflow directly, and the complications of
250 // this implementation's setting of the initial pointers all
251 // equal to _M_buf when initializing, it seems essential to have
252 // this in actuality be a helper function that checks for the
253 // eccentricities of this implementation, and then call
254 // overflow() if indeed the buffer is full.
256 // [documentation is inherited]
258 overflow(int_type __c = _Traits::eof());
260 // Stroustrup, 1998, p 648
261 // The overflow() function is called to transfer characters to the
262 // real output destination when the buffer is full. A call to
263 // overflow(c) outputs the contents of the buffer plus the
266 // Consume some sequence of the characters in the pending sequence.
273 _M_really_overflow(int_type __c = _Traits::eof());
275 // Convert internal byte sequence to external, char-based
276 // sequence via codecvt.
283 _M_convert_to_external(char_type*, streamsize, streamsize&, streamsize&);
286 * @brief Manipulates the buffer.
287 * @param s Pointer to a buffer area.
288 * @param n Size of @a s.
291 * If no file has been opened, and both @a s and @a n are zero, then
292 * the stream becomes unbuffered. Otherwise, @c s is used as a
294 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2
297 virtual __streambuf_type*
298 setbuf(char_type* __s, streamsize __n);
300 // [documentation is inherited]
302 seekoff(off_type __off, ios_base::seekdir __way,
303 ios_base::openmode __mode = ios_base::in | ios_base::out);
305 // [documentation is inherited]
307 seekpos(pos_type __pos,
308 ios_base::openmode __mode = ios_base::in | ios_base::out);
310 // [documentation is inherited]
315 bool __testput = this->_M_out_cur
316 && this->_M_out_beg < this->_M_out_lim;
318 bool __sync = this->_M_buf_size <= 1;
320 // Make sure that the internal buffer resyncs its idea of
321 // the file position with the external file.
324 // Need to restore current position after the write.
325 off_type __off = this->_M_out_cur - this->_M_out_lim;
327 // _M_file.sync() will be called within
328 if (traits_type::eq_int_type(_M_really_overflow(),
332 _M_file.seekoff(__off, ios_base::cur, __sync);
337 _M_last_overflowed = false;
341 // [documentation is inherited]
343 imbue(const locale& __loc);
345 // [documentation is inherited]
347 xsgetn(char_type* __s, streamsize __n)
349 streamsize __ret = 0;
350 // Clear out pback buffer before going on to the real deal...
351 if (this->_M_pback_init)
353 while (__ret < __n && this->_M_in_cur < this->_M_in_end)
355 *__s = *this->_M_in_cur;
363 __ret += __streambuf_type::xsgetn(__s, __n - __ret);
367 // [documentation is inherited]
369 xsputn(const char_type* __s, streamsize __n)
372 return __streambuf_type::xsputn(__s, __n);
383 // These three functions are used to clarify internal buffer
384 // maintenance. After an overflow, or after a seekoff call that
385 // started at beg or end, or possibly when the stream becomes
386 // unbuffered, and a myrid other obscure corner cases, the
387 // internal buffer does not truly reflect the contents of the
388 // external buffer. At this point, for whatever reason, it is in
389 // an indeterminate state.
396 _M_set_indeterminate(void)
397 { _M_set_determinate(off_type(0)); }
405 _M_set_determinate(off_type __off)
407 bool __testin = this->_M_mode & ios_base::in;
408 bool __testout = this->_M_mode & ios_base::out;
410 this->setg(this->_M_buf, this->_M_buf, this->_M_buf + __off);
413 this->setp(this->_M_buf, this->_M_buf + this->_M_buf_size);
414 this->_M_out_lim = this->_M_buf + __off;
416 _M_filepos = this->_M_buf + __off;
425 _M_is_indeterminate(void)
427 bool __testin = this->_M_mode & ios_base::in;
428 bool __testout = this->_M_mode & ios_base::out;
430 // Don't return true if unbuffered.
434 __ret = this->_M_in_beg == this->_M_in_cur
435 && this->_M_in_cur == this->_M_in_end;
437 __ret = this->_M_out_beg == this->_M_out_cur
438 && this->_M_out_cur == this->_M_out_lim;
444 // Explicit specialization declarations, defined in src/fstream.cc.
446 basic_filebuf<char>::int_type
447 basic_filebuf<char>::_M_underflow_common(bool __bump);
450 basic_filebuf<char>::int_type
451 basic_filebuf<char>::underflow();
454 basic_filebuf<char>::int_type
455 basic_filebuf<char>::uflow();
457 #ifdef _GLIBCPP_USE_WCHAR_T
459 basic_filebuf<wchar_t>::int_type
460 basic_filebuf<wchar_t>::_M_underflow_common(bool __bump);
463 basic_filebuf<wchar_t>::int_type
464 basic_filebuf<wchar_t>::underflow();
467 basic_filebuf<wchar_t>::int_type
468 basic_filebuf<wchar_t>::uflow();
471 // Generic definitions do nothing.
472 template <typename _CharT, typename _Traits>
473 typename basic_filebuf<_CharT, _Traits>::int_type
474 basic_filebuf<_CharT, _Traits>::underflow()
475 { return int_type(); }
477 template <typename _CharT, typename _Traits>
478 typename basic_filebuf<_CharT, _Traits>::int_type
479 basic_filebuf<_CharT, _Traits>::uflow()
480 { return int_type(); }
482 // [27.8.1.5] Template class basic_ifstream
484 * @brief Controlling input for files.
486 * This class supports reading from named files, using the inherited
487 * functions from std::basic_istream. To control the associated
488 * sequence, an instance of std::basic_filebuf is used, which this page
489 * refers to as @c sb.
491 template<typename _CharT, typename _Traits>
492 class basic_ifstream : public basic_istream<_CharT, _Traits>
496 typedef _CharT char_type;
497 typedef _Traits traits_type;
498 typedef typename traits_type::int_type int_type;
499 typedef typename traits_type::pos_type pos_type;
500 typedef typename traits_type::off_type off_type;
502 // Non-standard types:
503 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
504 typedef basic_istream<char_type, traits_type> __istream_type;
512 __filebuf_type _M_filebuf;
515 // Constructors/Destructors:
517 * @brief Default constructor.
519 * Initializes @c sb using its default constructor, and passes
520 * @c &sb to the base class initializer. Does not open any files
521 * (you haven't given it a filename to open).
523 basic_ifstream() : __istream_type(), _M_filebuf()
524 { this->init(&_M_filebuf); }
527 * @brief Create an input file stream.
528 * @param s Null terminated string specifying the filename.
529 * @param mode Open file in specified mode (see std::ios_base).
531 * @c ios_base::in is automatically included in @a mode.
533 * Tip: When using std::string to hold the filename, you must use
534 * .c_str() before passing it to this constructor.
537 basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in)
538 : __istream_type(), _M_filebuf()
540 this->init(&_M_filebuf);
541 this->open(__s, __mode);
545 * @brief The destructor does nothing.
547 * The file is closed by the filebuf object, not the formatting
555 * @brief Accessing the underlying buffer.
556 * @return The current basic_filebuf buffer.
558 * This hides both signatures of std::basic_ios::rdbuf().
562 { return const_cast<__filebuf_type*>(&_M_filebuf); }
565 * @brief Wrapper to test for an open file.
566 * @return @c rdbuf()->is_open()
569 is_open() { return _M_filebuf.is_open(); }
572 * @brief Opens an external file.
573 * @param s The name of the file.
574 * @param mode The open mode flags.
576 * Calls @c std::basic_filebuf::open(s,mode|in). If that function
577 * fails, @c failbit is set in the stream's error state.
579 * Tip: When using std::string to hold the filename, you must use
580 * .c_str() before passing it to this constructor.
583 open(const char* __s, ios_base::openmode __mode = ios_base::in)
585 if (!_M_filebuf.open(__s, __mode | ios_base::in))
586 this->setstate(ios_base::failbit);
590 * @brief Close the file.
592 * Calls @c std::basic_filebuf::close(). If that function
593 * fails, @c failbit is set in the stream's error state.
598 if (!_M_filebuf.close())
599 this->setstate(ios_base::failbit);
604 // [27.8.1.8] Template class basic_ofstream
606 * @brief Controlling output for files.
608 * This class supports reading from named files, using the inherited
609 * functions from std::basic_ostream. To control the associated
610 * sequence, an instance of std::basic_filebuf is used, which this page
611 * refers to as @c sb.
613 template<typename _CharT, typename _Traits>
614 class basic_ofstream : public basic_ostream<_CharT,_Traits>
618 typedef _CharT char_type;
619 typedef _Traits traits_type;
620 typedef typename traits_type::int_type int_type;
621 typedef typename traits_type::pos_type pos_type;
622 typedef typename traits_type::off_type off_type;
624 // Non-standard types:
625 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
626 typedef basic_ostream<char_type, traits_type> __ostream_type;
634 __filebuf_type _M_filebuf;
639 * @brief Default constructor.
641 * Initializes @c sb using its default constructor, and passes
642 * @c &sb to the base class initializer. Does not open any files
643 * (you haven't given it a filename to open).
645 basic_ofstream(): __ostream_type(), _M_filebuf()
646 { this->init(&_M_filebuf); }
649 * @brief Create an output file stream.
650 * @param s Null terminated string specifying the filename.
651 * @param mode Open file in specified mode (see std::ios_base).
653 * @c ios_base::out|ios_base::trunc is automatically included in
656 * Tip: When using std::string to hold the filename, you must use
657 * .c_str() before passing it to this constructor.
660 basic_ofstream(const char* __s,
661 ios_base::openmode __mode = ios_base::out|ios_base::trunc)
662 : __ostream_type(), _M_filebuf()
664 this->init(&_M_filebuf);
665 this->open(__s, __mode);
669 * @brief The destructor does nothing.
671 * The file is closed by the filebuf object, not the formatting
679 * @brief Accessing the underlying buffer.
680 * @return The current basic_filebuf buffer.
682 * This hides both signatures of std::basic_ios::rdbuf().
686 { return const_cast<__filebuf_type*>(&_M_filebuf); }
689 * @brief Wrapper to test for an open file.
690 * @return @c rdbuf()->is_open()
693 is_open() { return _M_filebuf.is_open(); }
696 * @brief Opens an external file.
697 * @param s The name of the file.
698 * @param mode The open mode flags.
700 * Calls @c std::basic_filebuf::open(s,mode|out|trunc). If that
701 * function fails, @c failbit is set in the stream's error state.
703 * Tip: When using std::string to hold the filename, you must use
704 * .c_str() before passing it to this constructor.
707 open(const char* __s,
708 ios_base::openmode __mode = ios_base::out | ios_base::trunc)
710 if (!_M_filebuf.open(__s, __mode | ios_base::out))
711 this->setstate(ios_base::failbit);
715 * @brief Close the file.
717 * Calls @c std::basic_filebuf::close(). If that function
718 * fails, @c failbit is set in the stream's error state.
723 if (!_M_filebuf.close())
724 this->setstate(ios_base::failbit);
729 // [27.8.1.11] Template class basic_fstream
731 * @brief Controlling intput and output for files.
733 * This class supports reading from and writing to named files, using
734 * the inherited functions from std::basic_iostream. To control the
735 * associated sequence, an instance of std::basic_filebuf is used, which
736 * this page refers to as @c sb.
738 template<typename _CharT, typename _Traits>
739 class basic_fstream : public basic_iostream<_CharT, _Traits>
743 typedef _CharT char_type;
744 typedef _Traits traits_type;
745 typedef typename traits_type::int_type int_type;
746 typedef typename traits_type::pos_type pos_type;
747 typedef typename traits_type::off_type off_type;
749 // Non-standard types:
750 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
751 typedef basic_ios<char_type, traits_type> __ios_type;
752 typedef basic_iostream<char_type, traits_type> __iostream_type;
760 __filebuf_type _M_filebuf;
763 // Constructors/destructor:
765 * @brief Default constructor.
767 * Initializes @c sb using its default constructor, and passes
768 * @c &sb to the base class initializer. Does not open any files
769 * (you haven't given it a filename to open).
772 : __iostream_type(), _M_filebuf()
773 { this->init(&_M_filebuf); }
776 * @brief Create an input/output file stream.
777 * @param s Null terminated string specifying the filename.
778 * @param mode Open file in specified mode (see std::ios_base).
780 * Tip: When using std::string to hold the filename, you must use
781 * .c_str() before passing it to this constructor.
784 basic_fstream(const char* __s,
785 ios_base::openmode __mode = ios_base::in | ios_base::out)
786 : __iostream_type(NULL), _M_filebuf()
788 this->init(&_M_filebuf);
789 this->open(__s, __mode);
793 * @brief The destructor does nothing.
795 * The file is closed by the filebuf object, not the formatting
803 * @brief Accessing the underlying buffer.
804 * @return The current basic_filebuf buffer.
806 * This hides both signatures of std::basic_ios::rdbuf().
810 { return const_cast<__filebuf_type*>(&_M_filebuf); }
813 * @brief Wrapper to test for an open file.
814 * @return @c rdbuf()->is_open()
817 is_open() { return _M_filebuf.is_open(); }
820 * @brief Opens an external file.
821 * @param s The name of the file.
822 * @param mode The open mode flags.
824 * Calls @c std::basic_filebuf::open(s,mode). If that
825 * function fails, @c failbit is set in the stream's error state.
827 * Tip: When using std::string to hold the filename, you must use
828 * .c_str() before passing it to this constructor.
831 open(const char* __s,
832 ios_base::openmode __mode = ios_base::in | ios_base::out)
834 if (!_M_filebuf.open(__s, __mode))
835 this->setstate(ios_base::failbit);
839 * @brief Close the file.
841 * Calls @c std::basic_filebuf::close(). If that function
842 * fails, @c failbit is set in the stream's error state.
847 if (!_M_filebuf.close())
848 this->setstate(ios_base::failbit);
853 #ifdef _GLIBCPP_NO_TEMPLATE_EXPORT
856 #ifdef _GLIBCPP_FULLY_COMPLIANT_HEADERS
857 # include <bits/fstream.tcc>