1 // File based streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009
5 // Free Software Foundation, Inc.
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)
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.
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.
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/>.
28 * This is a Standard C++ Library header.
32 // ISO C++ 14882: 27.8 File-based streams
35 #ifndef _GLIBCXX_FSTREAM
36 #define _GLIBCXX_FSTREAM 1
38 #pragma GCC system_header
42 #include <bits/codecvt.h>
43 #include <cstdio> // For BUFSIZ
44 #include <bits/basic_file.h> // For __basic_file, __c_lock
45 #ifdef __GXX_EXPERIMENTAL_CXX0X__
46 #include <string> // For std::string overloads.
49 _GLIBCXX_BEGIN_NAMESPACE(std)
51 // [27.8.1.1] template class basic_filebuf
53 * @brief The actual work of input and output (for files).
56 * This class associates both its input and output sequence with an
57 * external disk file, and maintains a joint file position for both
58 * sequences. Many of its semantics are described in terms of similar
59 * behavior in the Standard C Library's @c FILE streams.
61 // Requirements on traits_type, specific to this class:
62 // traits_type::pos_type must be fpos<traits_type::state_type>
63 // traits_type::off_type must be streamoff
64 // traits_type::state_type must be Assignable and DefaultConstructible,
65 // and traits_type::state_type() must be the initial state for codecvt.
66 template<typename _CharT, typename _Traits>
67 class basic_filebuf : public basic_streambuf<_CharT, _Traits>
71 typedef _CharT char_type;
72 typedef _Traits traits_type;
73 typedef typename traits_type::int_type int_type;
74 typedef typename traits_type::pos_type pos_type;
75 typedef typename traits_type::off_type off_type;
77 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
78 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
79 typedef __basic_file<char> __file_type;
80 typedef typename traits_type::state_type __state_type;
81 typedef codecvt<char_type, char, __state_type> __codecvt_type;
83 friend class ios_base; // For sync_with_stdio.
87 // MT lock inherited from libio or other low-level io library.
93 /// Place to stash in || out || in | out settings for current filebuf.
94 ios_base::openmode _M_mode;
96 // Beginning state type for codecvt.
97 __state_type _M_state_beg;
99 // During output, the state that corresponds to pptr(),
100 // during input, the state that corresponds to egptr() and
102 __state_type _M_state_cur;
104 // Not used for output. During input, the state that corresponds
105 // to eback() and _M_ext_buf.
106 __state_type _M_state_last;
108 /// Pointer to the beginning of internal buffer.
112 * Actual size of internal buffer. This number is equal to the size
113 * of the put area + 1 position, reserved for the overflow char of
118 // Set iff _M_buf is allocated memory from _M_allocate_internal_buffer.
119 bool _M_buf_allocated;
122 * _M_reading == false && _M_writing == false for 'uncommitted' mode;
123 * _M_reading == true for 'read' mode;
124 * _M_writing == true for 'write' mode;
126 * NB: _M_reading == true && _M_writing == true is unused.
133 * Necessary bits for putback buffer management.
135 * @note pbacks of over one character are not currently supported.
138 char_type* _M_pback_cur_save;
139 char_type* _M_pback_end_save;
143 // Cached codecvt facet.
144 const __codecvt_type* _M_codecvt;
147 * Buffer for external characters. Used for input when
148 * codecvt::always_noconv() == false. When valid, this corresponds
154 * Size of buffer held by _M_ext_buf.
156 streamsize _M_ext_buf_size;
159 * Pointers into the buffer held by _M_ext_buf that delimit a
160 * subsequence of bytes that have been read but not yet converted.
161 * When valid, _M_ext_next corresponds to egptr().
163 const char* _M_ext_next;
167 * Initializes pback buffers, and moves normal buffers to safety.
169 * _M_in_cur has already been moved back
176 _M_pback_cur_save = this->gptr();
177 _M_pback_end_save = this->egptr();
178 this->setg(&_M_pback, &_M_pback, &_M_pback + 1);
179 _M_pback_init = true;
184 * Deactivates pback buffer contents, and restores normal buffer.
186 * The pback buffer has only moved forward.
189 _M_destroy_pback() throw()
193 // Length _M_in_cur moved in the pback buffer.
194 _M_pback_cur_save += this->gptr() != this->eback();
195 this->setg(_M_buf, _M_pback_cur_save, _M_pback_end_save);
196 _M_pback_init = false;
201 // Constructors/destructor:
203 * @brief Does not open any files.
205 * The default constructor initializes the parent class using its
211 * @brief The destructor closes the file first.
219 * @brief Returns true if the external file is open.
222 is_open() const throw()
223 { return _M_file.is_open(); }
226 * @brief Opens an external file.
227 * @param s The name of the file.
228 * @param mode The open mode flags.
229 * @return @c this on success, NULL on failure
231 * If a file is already open, this function immediately fails.
232 * Otherwise it tries to open the file named @a s using the flags
235 * Table 92, adapted here, gives the relation between openmode
236 * combinations and the equivalent fopen() flags.
237 * (NB: lines app, in|out|app, in|app, binary|app, binary|in|out|app,
238 * and binary|in|app per DR 596)
239 * +---------------------------------------------------------+
240 * | ios_base Flag combination stdio equivalent |
241 * |binary in out trunc app |
242 * +---------------------------------------------------------+
252 * +---------------------------------------------------------+
262 * +---------------------------------------------------------+
265 open(const char* __s, ios_base::openmode __mode);
267 #ifdef __GXX_EXPERIMENTAL_CXX0X__
269 * @brief Opens an external file.
270 * @param s The name of the file.
271 * @param mode The open mode flags.
272 * @return @c this on success, NULL on failure
275 open(const std::string& __s, ios_base::openmode __mode)
276 { return open(__s.c_str(), __mode); }
280 * @brief Closes the currently associated file.
281 * @return @c this on success, NULL on failure
283 * If no file is currently open, this function immediately fails.
285 * If a "put buffer area" exists, @c overflow(eof) is called to flush
286 * all the characters. The file is then closed.
288 * If any operations fail, this function also fails.
295 _M_allocate_internal_buffer();
298 _M_destroy_internal_buffer() throw();
300 // [27.8.1.4] overridden virtual functions
304 // Stroustrup, 1998, p. 628
305 // underflow() and uflow() functions are called to get the next
306 // character from the real input source when the buffer is empty.
307 // Buffered input uses underflow()
313 pbackfail(int_type __c = _Traits::eof());
315 // Stroustrup, 1998, p 648
316 // The overflow() function is called to transfer characters to the
317 // real output destination when the buffer is full. A call to
318 // overflow(c) outputs the contents of the buffer plus the
321 // Consume some sequence of the characters in the pending sequence.
323 overflow(int_type __c = _Traits::eof());
325 // Convert internal byte sequence to external, char-based
326 // sequence via codecvt.
328 _M_convert_to_external(char_type*, streamsize);
331 * @brief Manipulates the buffer.
332 * @param s Pointer to a buffer area.
333 * @param n Size of @a s.
336 * If no file has been opened, and both @a s and @a n are zero, then
337 * the stream becomes unbuffered. Otherwise, @c s is used as a
339 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html
342 virtual __streambuf_type*
343 setbuf(char_type* __s, streamsize __n);
346 seekoff(off_type __off, ios_base::seekdir __way,
347 ios_base::openmode __mode = ios_base::in | ios_base::out);
350 seekpos(pos_type __pos,
351 ios_base::openmode __mode = ios_base::in | ios_base::out);
353 // Common code for seekoff and seekpos
355 _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state);
361 imbue(const locale& __loc);
364 xsgetn(char_type* __s, streamsize __n);
367 xsputn(const char_type* __s, streamsize __n);
369 // Flushes output buffer, then writes unshift sequence.
371 _M_terminate_output();
374 * This function sets the pointers of the internal buffer, both get
375 * and put areas. Typically:
377 * __off == egptr() - eback() upon underflow/uflow ('read' mode);
378 * __off == 0 upon overflow ('write' mode);
379 * __off == -1 upon open, setbuf, seekoff/pos ('uncommitted' mode).
381 * NB: epptr() - pbase() == _M_buf_size - 1, since _M_buf_size
382 * reflects the actual allocated memory and the last cell is reserved
383 * for the overflow char of a full put area.
386 _M_set_buffer(streamsize __off)
388 const bool __testin = _M_mode & ios_base::in;
389 const bool __testout = _M_mode & ios_base::out;
391 if (__testin && __off > 0)
392 this->setg(_M_buf, _M_buf, _M_buf + __off);
394 this->setg(_M_buf, _M_buf, _M_buf);
396 if (__testout && __off == 0 && _M_buf_size > 1 )
397 this->setp(_M_buf, _M_buf + _M_buf_size - 1);
399 this->setp(NULL, NULL);
403 // [27.8.1.5] Template class basic_ifstream
405 * @brief Controlling input for files.
408 * This class supports reading from named files, using the inherited
409 * functions from std::basic_istream. To control the associated
410 * sequence, an instance of std::basic_filebuf is used, which this page
411 * refers to as @c sb.
413 template<typename _CharT, typename _Traits>
414 class basic_ifstream : public basic_istream<_CharT, _Traits>
418 typedef _CharT char_type;
419 typedef _Traits traits_type;
420 typedef typename traits_type::int_type int_type;
421 typedef typename traits_type::pos_type pos_type;
422 typedef typename traits_type::off_type off_type;
424 // Non-standard types:
425 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
426 typedef basic_istream<char_type, traits_type> __istream_type;
429 __filebuf_type _M_filebuf;
432 // Constructors/Destructors:
434 * @brief Default constructor.
436 * Initializes @c sb using its default constructor, and passes
437 * @c &sb to the base class initializer. Does not open any files
438 * (you haven't given it a filename to open).
440 basic_ifstream() : __istream_type(), _M_filebuf()
441 { this->init(&_M_filebuf); }
444 * @brief Create an input file stream.
445 * @param s Null terminated string specifying the filename.
446 * @param mode Open file in specified mode (see std::ios_base).
448 * @c ios_base::in is automatically included in @a mode.
450 * Tip: When using std::string to hold the filename, you must use
451 * .c_str() before passing it to this constructor.
454 basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in)
455 : __istream_type(), _M_filebuf()
457 this->init(&_M_filebuf);
458 this->open(__s, __mode);
461 #ifdef __GXX_EXPERIMENTAL_CXX0X__
463 * @brief Create an input file stream.
464 * @param s std::string specifying the filename.
465 * @param mode Open file in specified mode (see std::ios_base).
467 * @c ios_base::in is automatically included in @a mode.
470 basic_ifstream(const std::string& __s,
471 ios_base::openmode __mode = ios_base::in)
472 : __istream_type(), _M_filebuf()
474 this->init(&_M_filebuf);
475 this->open(__s, __mode);
480 * @brief The destructor does nothing.
482 * The file is closed by the filebuf object, not the formatting
490 * @brief Accessing the underlying buffer.
491 * @return The current basic_filebuf buffer.
493 * This hides both signatures of std::basic_ios::rdbuf().
497 { return const_cast<__filebuf_type*>(&_M_filebuf); }
500 * @brief Wrapper to test for an open file.
501 * @return @c rdbuf()->is_open()
505 { return _M_filebuf.is_open(); }
507 // _GLIBCXX_RESOLVE_LIB_DEFECTS
508 // 365. Lack of const-qualification in clause 27
511 { return _M_filebuf.is_open(); }
514 * @brief Opens an external file.
515 * @param s The name of the file.
516 * @param mode The open mode flags.
518 * Calls @c std::basic_filebuf::open(s,mode|in). If that function
519 * fails, @c failbit is set in the stream's error state.
521 * Tip: When using std::string to hold the filename, you must use
522 * .c_str() before passing it to this constructor.
525 open(const char* __s, ios_base::openmode __mode = ios_base::in)
527 if (!_M_filebuf.open(__s, __mode | ios_base::in))
528 this->setstate(ios_base::failbit);
530 // _GLIBCXX_RESOLVE_LIB_DEFECTS
531 // 409. Closing an fstream should clear error state
535 #ifdef __GXX_EXPERIMENTAL_CXX0X__
537 * @brief Opens an external file.
538 * @param s The name of the file.
539 * @param mode The open mode flags.
541 * Calls @c std::basic_filebuf::open(s,mode|in). If that function
542 * fails, @c failbit is set in the stream's error state.
545 open(const std::string& __s, ios_base::openmode __mode = ios_base::in)
547 if (!_M_filebuf.open(__s, __mode | ios_base::in))
548 this->setstate(ios_base::failbit);
550 // _GLIBCXX_RESOLVE_LIB_DEFECTS
551 // 409. Closing an fstream should clear error state
557 * @brief Close the file.
559 * Calls @c std::basic_filebuf::close(). If that function
560 * fails, @c failbit is set in the stream's error state.
565 if (!_M_filebuf.close())
566 this->setstate(ios_base::failbit);
571 // [27.8.1.8] Template class basic_ofstream
573 * @brief Controlling output for files.
576 * This class supports reading from named files, using the inherited
577 * functions from std::basic_ostream. To control the associated
578 * sequence, an instance of std::basic_filebuf is used, which this page
579 * refers to as @c sb.
581 template<typename _CharT, typename _Traits>
582 class basic_ofstream : public basic_ostream<_CharT,_Traits>
586 typedef _CharT char_type;
587 typedef _Traits traits_type;
588 typedef typename traits_type::int_type int_type;
589 typedef typename traits_type::pos_type pos_type;
590 typedef typename traits_type::off_type off_type;
592 // Non-standard types:
593 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
594 typedef basic_ostream<char_type, traits_type> __ostream_type;
597 __filebuf_type _M_filebuf;
602 * @brief Default constructor.
604 * Initializes @c sb using its default constructor, and passes
605 * @c &sb to the base class initializer. Does not open any files
606 * (you haven't given it a filename to open).
608 basic_ofstream(): __ostream_type(), _M_filebuf()
609 { this->init(&_M_filebuf); }
612 * @brief Create an output file stream.
613 * @param s Null terminated string specifying the filename.
614 * @param mode Open file in specified mode (see std::ios_base).
616 * @c ios_base::out|ios_base::trunc is automatically included in
619 * Tip: When using std::string to hold the filename, you must use
620 * .c_str() before passing it to this constructor.
623 basic_ofstream(const char* __s,
624 ios_base::openmode __mode = ios_base::out|ios_base::trunc)
625 : __ostream_type(), _M_filebuf()
627 this->init(&_M_filebuf);
628 this->open(__s, __mode);
631 #ifdef __GXX_EXPERIMENTAL_CXX0X__
633 * @brief Create an output file stream.
634 * @param s std::string specifying the filename.
635 * @param mode Open file in specified mode (see std::ios_base).
637 * @c ios_base::out|ios_base::trunc is automatically included in
641 basic_ofstream(const std::string& __s,
642 ios_base::openmode __mode = ios_base::out|ios_base::trunc)
643 : __ostream_type(), _M_filebuf()
645 this->init(&_M_filebuf);
646 this->open(__s, __mode);
651 * @brief The destructor does nothing.
653 * The file is closed by the filebuf object, not the formatting
661 * @brief Accessing the underlying buffer.
662 * @return The current basic_filebuf buffer.
664 * This hides both signatures of std::basic_ios::rdbuf().
668 { return const_cast<__filebuf_type*>(&_M_filebuf); }
671 * @brief Wrapper to test for an open file.
672 * @return @c rdbuf()->is_open()
676 { return _M_filebuf.is_open(); }
678 // _GLIBCXX_RESOLVE_LIB_DEFECTS
679 // 365. Lack of const-qualification in clause 27
682 { return _M_filebuf.is_open(); }
685 * @brief Opens an external file.
686 * @param s The name of the file.
687 * @param mode The open mode flags.
689 * Calls @c std::basic_filebuf::open(s,mode|out|trunc). If that
690 * function fails, @c failbit is set in the stream's error state.
692 * Tip: When using std::string to hold the filename, you must use
693 * .c_str() before passing it to this constructor.
696 open(const char* __s,
697 ios_base::openmode __mode = ios_base::out | ios_base::trunc)
699 if (!_M_filebuf.open(__s, __mode | ios_base::out))
700 this->setstate(ios_base::failbit);
702 // _GLIBCXX_RESOLVE_LIB_DEFECTS
703 // 409. Closing an fstream should clear error state
707 #ifdef __GXX_EXPERIMENTAL_CXX0X__
709 * @brief Opens an external file.
710 * @param s The name of the file.
711 * @param mode The open mode flags.
713 * Calls @c std::basic_filebuf::open(s,mode|out|trunc). If that
714 * function fails, @c failbit is set in the stream's error state.
717 open(const std::string& __s,
718 ios_base::openmode __mode = ios_base::out | ios_base::trunc)
720 if (!_M_filebuf.open(__s, __mode | ios_base::out))
721 this->setstate(ios_base::failbit);
723 // _GLIBCXX_RESOLVE_LIB_DEFECTS
724 // 409. Closing an fstream should clear error state
730 * @brief Close the file.
732 * Calls @c std::basic_filebuf::close(). If that function
733 * fails, @c failbit is set in the stream's error state.
738 if (!_M_filebuf.close())
739 this->setstate(ios_base::failbit);
744 // [27.8.1.11] Template class basic_fstream
746 * @brief Controlling input and output for files.
749 * This class supports reading from and writing to named files, using
750 * the inherited functions from std::basic_iostream. To control the
751 * associated sequence, an instance of std::basic_filebuf is used, which
752 * this page refers to as @c sb.
754 template<typename _CharT, typename _Traits>
755 class basic_fstream : public basic_iostream<_CharT, _Traits>
759 typedef _CharT char_type;
760 typedef _Traits traits_type;
761 typedef typename traits_type::int_type int_type;
762 typedef typename traits_type::pos_type pos_type;
763 typedef typename traits_type::off_type off_type;
765 // Non-standard types:
766 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
767 typedef basic_ios<char_type, traits_type> __ios_type;
768 typedef basic_iostream<char_type, traits_type> __iostream_type;
771 __filebuf_type _M_filebuf;
774 // Constructors/destructor:
776 * @brief Default constructor.
778 * Initializes @c sb using its default constructor, and passes
779 * @c &sb to the base class initializer. Does not open any files
780 * (you haven't given it a filename to open).
783 : __iostream_type(), _M_filebuf()
784 { this->init(&_M_filebuf); }
787 * @brief Create an input/output file stream.
788 * @param s Null terminated string specifying the filename.
789 * @param mode Open file in specified mode (see std::ios_base).
791 * Tip: When using std::string to hold the filename, you must use
792 * .c_str() before passing it to this constructor.
795 basic_fstream(const char* __s,
796 ios_base::openmode __mode = ios_base::in | ios_base::out)
797 : __iostream_type(NULL), _M_filebuf()
799 this->init(&_M_filebuf);
800 this->open(__s, __mode);
803 #ifdef __GXX_EXPERIMENTAL_CXX0X__
805 * @brief Create an input/output file stream.
806 * @param s Null terminated string specifying the filename.
807 * @param mode Open file in specified mode (see std::ios_base).
810 basic_fstream(const std::string& __s,
811 ios_base::openmode __mode = ios_base::in | ios_base::out)
812 : __iostream_type(NULL), _M_filebuf()
814 this->init(&_M_filebuf);
815 this->open(__s, __mode);
820 * @brief The destructor does nothing.
822 * The file is closed by the filebuf object, not the formatting
830 * @brief Accessing the underlying buffer.
831 * @return The current basic_filebuf buffer.
833 * This hides both signatures of std::basic_ios::rdbuf().
837 { return const_cast<__filebuf_type*>(&_M_filebuf); }
840 * @brief Wrapper to test for an open file.
841 * @return @c rdbuf()->is_open()
845 { return _M_filebuf.is_open(); }
847 // _GLIBCXX_RESOLVE_LIB_DEFECTS
848 // 365. Lack of const-qualification in clause 27
851 { return _M_filebuf.is_open(); }
854 * @brief Opens an external file.
855 * @param s The name of the file.
856 * @param mode The open mode flags.
858 * Calls @c std::basic_filebuf::open(s,mode). If that
859 * function fails, @c failbit is set in the stream's error state.
861 * Tip: When using std::string to hold the filename, you must use
862 * .c_str() before passing it to this constructor.
865 open(const char* __s,
866 ios_base::openmode __mode = ios_base::in | ios_base::out)
868 if (!_M_filebuf.open(__s, __mode))
869 this->setstate(ios_base::failbit);
871 // _GLIBCXX_RESOLVE_LIB_DEFECTS
872 // 409. Closing an fstream should clear error state
876 #ifdef __GXX_EXPERIMENTAL_CXX0X__
878 * @brief Opens an external file.
879 * @param s The name of the file.
880 * @param mode The open mode flags.
882 * Calls @c std::basic_filebuf::open(s,mode). If that
883 * function fails, @c failbit is set in the stream's error state.
886 open(const std::string& __s,
887 ios_base::openmode __mode = ios_base::in | ios_base::out)
889 if (!_M_filebuf.open(__s, __mode))
890 this->setstate(ios_base::failbit);
892 // _GLIBCXX_RESOLVE_LIB_DEFECTS
893 // 409. Closing an fstream should clear error state
899 * @brief Close the file.
901 * Calls @c std::basic_filebuf::close(). If that function
902 * fails, @c failbit is set in the stream's error state.
907 if (!_M_filebuf.close())
908 this->setstate(ios_base::failbit);
912 _GLIBCXX_END_NAMESPACE
914 #ifndef _GLIBCXX_EXPORT_TEMPLATE
915 # include <bits/fstream.tcc>
918 #endif /* _GLIBCXX_FSTREAM */