1 // File based streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
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]
237 underflow() { return _M_underflow_common(false); }
239 // [documentation is inherited]
241 uflow() { return _M_underflow_common(true); }
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]
314 bool __testput = _M_out_cur && _M_out_beg < _M_out_end;
316 // Make sure that the internal buffer resyncs its idea of
317 // the file position with the external file.
320 // Need to restore current position after the write.
321 off_type __off = _M_out_cur - _M_out_end;
322 _M_really_overflow(); // _M_file.sync() will be called within
324 _M_file.seekoff(__off, ios_base::cur);
328 _M_last_overflowed = false;
332 // [documentation is inherited]
334 imbue(const locale& __loc);
336 // [documentation is inherited]
338 xsgetn(char_type* __s, streamsize __n)
340 streamsize __ret = 0;
341 // Clear out pback buffer before going on to the real deal...
344 while (__ret < __n && _M_in_cur < _M_in_end)
354 __ret += __streambuf_type::xsgetn(__s, __n - __ret);
358 // [documentation is inherited]
360 xsputn(const char_type* __s, streamsize __n)
363 return __streambuf_type::xsputn(__s, __n);
374 // These three functions are used to clarify internal buffer
375 // maintenance. After an overflow, or after a seekoff call that
376 // started at beg or end, or possibly when the stream becomes
377 // unbuffered, and a myrid other obscure corner cases, the
378 // internal buffer does not truly reflect the contents of the
379 // external buffer. At this point, for whatever reason, it is in
380 // an indeterminate state.
387 _M_set_indeterminate(void)
389 if (_M_mode & ios_base::in)
390 this->setg(_M_buf, _M_buf, _M_buf);
391 if (_M_mode & ios_base::out)
392 this->setp(_M_buf, _M_buf);
402 _M_set_determinate(off_type __off)
404 bool __testin = _M_mode & ios_base::in;
405 bool __testout = _M_mode & ios_base::out;
407 this->setg(_M_buf, _M_buf, _M_buf + __off);
409 this->setp(_M_buf, _M_buf + __off);
410 _M_filepos = _M_buf + __off;
419 _M_is_indeterminate(void)
422 // Don't return true if unbuffered.
425 if (_M_mode & ios_base::in)
426 __ret = _M_in_beg == _M_in_cur && _M_in_cur == _M_in_end;
427 if (_M_mode & ios_base::out)
428 __ret = _M_out_beg == _M_out_cur && _M_out_cur == _M_out_end;
434 // Explicit specializations, defined in src/fstream.cc.
436 basic_filebuf<char>::int_type
437 basic_filebuf<char>::_M_underflow_common(bool __bump);
439 #ifdef _GLIBCPP_USE_WCHAR_T
441 basic_filebuf<wchar_t>::int_type
442 basic_filebuf<wchar_t>::_M_underflow_common(bool __bump);
445 // [27.8.1.5] Template class basic_ifstream
447 * @brief Controlling input for files.
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.
454 template<typename _CharT, typename _Traits>
455 class basic_ifstream : public basic_istream<_CharT, _Traits>
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;
465 // Non-standard types:
466 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
467 typedef basic_istream<char_type, traits_type> __istream_type;
475 __filebuf_type _M_filebuf;
478 // Constructors/Destructors:
480 * @brief Default constructor.
482 * Initializes @c sb using its default constructor, and passes
483 * @c &sb to the base class initializer. Does not open any files
484 * (you haven't given it a filename to open).
487 : __istream_type(NULL), _M_filebuf()
488 { this->init(&_M_filebuf); }
491 * @brief Create an input file stream.
492 * @param s Null terminated string specifying the filename.
493 * @param mode Open file in specified mode (see std::ios_base).
495 * @c ios_base::in is automatically included in @a mode.
497 * Tip: When using std::string to hold the filename, you must use
498 * .c_str() before passing it to this constructor.
501 basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in)
502 : __istream_type(NULL), _M_filebuf()
504 this->init(&_M_filebuf);
505 this->open(__s, __mode);
509 * @brief The destructor does nothing.
511 * The file is closed by the filebuf object, not the formatting
519 * @brief Accessing the underlying buffer.
520 * @return The current basic_filebuf buffer.
522 * This hides both signatures of std::basic_ios::rdbuf().
526 { return const_cast<__filebuf_type*>(&_M_filebuf); }
529 * @brief Wrapper to test for an open file.
530 * @return @c rdbuf()->is_open()
533 is_open() { return _M_filebuf.is_open(); }
536 * @brief Opens an external file.
537 * @param s The name of the file.
538 * @param mode The open mode flags.
540 * Calls @c std::basic_filebuf::open(s,mode|in). If that function
541 * fails, @c failbit is set in the stream's error state.
543 * Tip: When using std::string to hold the filename, you must use
544 * .c_str() before passing it to this constructor.
547 open(const char* __s, ios_base::openmode __mode = ios_base::in)
549 if (!_M_filebuf.open(__s, __mode | ios_base::in))
550 this->setstate(ios_base::failbit);
554 * @brief Close the file.
556 * Calls @c std::basic_filebuf::close(). If that function
557 * fails, @c failbit is set in the stream's error state.
562 if (!_M_filebuf.close())
563 this->setstate(ios_base::failbit);
568 // [27.8.1.8] Template class basic_ofstream
570 * @brief Controlling output for files.
572 * This class supports reading from named files, using the inherited
573 * functions from std::basic_ostream. To control the associated
574 * sequence, an instance of std::basic_filebuf is used, which this page
575 * refers to as @c sb.
577 template<typename _CharT, typename _Traits>
578 class basic_ofstream : public basic_ostream<_CharT,_Traits>
582 typedef _CharT char_type;
583 typedef _Traits traits_type;
584 typedef typename traits_type::int_type int_type;
585 typedef typename traits_type::pos_type pos_type;
586 typedef typename traits_type::off_type off_type;
588 // Non-standard types:
589 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
590 typedef basic_ostream<char_type, traits_type> __ostream_type;
598 __filebuf_type _M_filebuf;
603 * @brief Default constructor.
605 * Initializes @c sb using its default constructor, and passes
606 * @c &sb to the base class initializer. Does not open any files
607 * (you haven't given it a filename to open).
610 : __ostream_type(NULL), _M_filebuf()
611 { this->init(&_M_filebuf); }
614 * @brief Create an output file stream.
615 * @param s Null terminated string specifying the filename.
616 * @param mode Open file in specified mode (see std::ios_base).
618 * @c ios_base::out|ios_base::trunc is automatically included in
621 * Tip: When using std::string to hold the filename, you must use
622 * .c_str() before passing it to this constructor.
625 basic_ofstream(const char* __s,
626 ios_base::openmode __mode = ios_base::out|ios_base::trunc)
627 : __ostream_type(NULL), _M_filebuf()
629 this->init(&_M_filebuf);
630 this->open(__s, __mode);
634 * @brief The destructor does nothing.
636 * The file is closed by the filebuf object, not the formatting
644 * @brief Accessing the underlying buffer.
645 * @return The current basic_filebuf buffer.
647 * This hides both signatures of std::basic_ios::rdbuf().
651 { return const_cast<__filebuf_type*>(&_M_filebuf); }
654 * @brief Wrapper to test for an open file.
655 * @return @c rdbuf()->is_open()
658 is_open() { return _M_filebuf.is_open(); }
661 * @brief Opens an external file.
662 * @param s The name of the file.
663 * @param mode The open mode flags.
665 * Calls @c std::basic_filebuf::open(s,mode|out|trunc). If that
666 * function fails, @c failbit is set in the stream's error state.
668 * Tip: When using std::string to hold the filename, you must use
669 * .c_str() before passing it to this constructor.
672 open(const char* __s,
673 ios_base::openmode __mode = ios_base::out | ios_base::trunc)
675 if (!_M_filebuf.open(__s, __mode | ios_base::out))
676 this->setstate(ios_base::failbit);
680 * @brief Close the file.
682 * Calls @c std::basic_filebuf::close(). If that function
683 * fails, @c failbit is set in the stream's error state.
688 if (!_M_filebuf.close())
689 this->setstate(ios_base::failbit);
694 // [27.8.1.11] Template class basic_fstream
696 * @brief Controlling intput and output for files.
698 * This class supports reading from and writing to named files, using
699 * the inherited functions from std::basic_iostream. To control the
700 * associated sequence, an instance of std::basic_filebuf is used, which
701 * this page refers to as @c sb.
703 template<typename _CharT, typename _Traits>
704 class basic_fstream : public basic_iostream<_CharT, _Traits>
708 typedef _CharT char_type;
709 typedef _Traits traits_type;
710 typedef typename traits_type::int_type int_type;
711 typedef typename traits_type::pos_type pos_type;
712 typedef typename traits_type::off_type off_type;
714 // Non-standard types:
715 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
716 typedef basic_ios<char_type, traits_type> __ios_type;
717 typedef basic_iostream<char_type, traits_type> __iostream_type;
725 __filebuf_type _M_filebuf;
728 // Constructors/destructor:
730 * @brief Default constructor.
732 * Initializes @c sb using its default constructor, and passes
733 * @c &sb to the base class initializer. Does not open any files
734 * (you haven't given it a filename to open).
737 : __iostream_type(NULL), _M_filebuf()
738 { this->init(&_M_filebuf); }
741 * @brief Create an input/output file stream.
742 * @param s Null terminated string specifying the filename.
743 * @param mode Open file in specified mode (see std::ios_base).
745 * Tip: When using std::string to hold the filename, you must use
746 * .c_str() before passing it to this constructor.
749 basic_fstream(const char* __s,
750 ios_base::openmode __mode = ios_base::in | ios_base::out)
751 : __iostream_type(NULL), _M_filebuf()
753 this->init(&_M_filebuf);
754 this->open(__s, __mode);
758 * @brief The destructor does nothing.
760 * The file is closed by the filebuf object, not the formatting
768 * @brief Accessing the underlying buffer.
769 * @return The current basic_filebuf buffer.
771 * This hides both signatures of std::basic_ios::rdbuf().
775 { return const_cast<__filebuf_type*>(&_M_filebuf); }
778 * @brief Wrapper to test for an open file.
779 * @return @c rdbuf()->is_open()
782 is_open() { return _M_filebuf.is_open(); }
785 * @brief Opens an external file.
786 * @param s The name of the file.
787 * @param mode The open mode flags.
789 * Calls @c std::basic_filebuf::open(s,mode). If that
790 * function fails, @c failbit is set in the stream's error state.
792 * Tip: When using std::string to hold the filename, you must use
793 * .c_str() before passing it to this constructor.
796 open(const char* __s,
797 ios_base::openmode __mode = ios_base::in | ios_base::out)
799 if (!_M_filebuf.open(__s, __mode))
800 setstate(ios_base::failbit);
804 * @brief Close the file.
806 * Calls @c std::basic_filebuf::close(). If that function
807 * fails, @c failbit is set in the stream's error state.
812 if (!_M_filebuf.close())
813 setstate(ios_base::failbit);
818 #ifdef _GLIBCPP_NO_TEMPLATE_EXPORT
821 #ifdef _GLIBCPP_FULLY_COMPLIANT_HEADERS
822 # include <bits/fstream.tcc>