1 // File based streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
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 2, 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 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING. If not, write to the Free
20 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction. Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License. This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
33 * This is a Standard C++ Library header.
37 // ISO C++ 14882: 27.8 File-based streams
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>
52 _GLIBCXX_BEGIN_NAMESPACE(std
)
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
;
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
;
85 friend class ios_base
; // For sync_with_stdio.
89 // MT lock inherited from libio or other low-level io library.
97 * Place to stash in || out || in | out settings for current filebuf.
100 ios_base::openmode _M_mode
;
102 // Beginning state type for codecvt.
103 __state_type _M_state_beg
;
105 // During output, the state that corresponds to pptr(),
106 // during input, the state that corresponds to egptr() and
108 __state_type _M_state_cur
;
110 // Not used for output. During input, the state that corresponds
111 // to eback() and _M_ext_buf.
112 __state_type _M_state_last
;
116 * Pointer to the beginning of internal buffer.
123 * Actual size of internal buffer. This number is equal to the size
124 * of the put area + 1 position, reserved for the overflow char of
130 // Set iff _M_buf is allocated memory from _M_allocate_internal_buffer.
131 bool _M_buf_allocated
;
135 * _M_reading == false && _M_writing == false for 'uncommitted' mode;
136 * _M_reading == true for 'read' mode;
137 * _M_writing == true for 'write' mode;
139 * NB: _M_reading == true && _M_writing == true is unused.
148 * Necessary bits for putback buffer management.
150 * @note pbacks of over one character are not currently supported.
154 char_type
* _M_pback_cur_save
;
155 char_type
* _M_pback_end_save
;
159 // Cached codecvt facet.
160 const __codecvt_type
* _M_codecvt
;
164 * Buffer for external characters. Used for input when
165 * codecvt::always_noconv() == false. When valid, this corresponds
173 * Size of buffer held by _M_ext_buf.
176 streamsize _M_ext_buf_size
;
180 * Pointers into the buffer held by _M_ext_buf that delimit a
181 * subsequence of bytes that have been read but not yet converted.
182 * When valid, _M_ext_next corresponds to egptr().
185 const char* _M_ext_next
;
190 * Initializes pback buffers, and moves normal buffers to safety.
192 * _M_in_cur has already been moved back
200 _M_pback_cur_save
= this->gptr();
201 _M_pback_end_save
= this->egptr();
202 this->setg(&_M_pback
, &_M_pback
, &_M_pback
+ 1);
203 _M_pback_init
= true;
209 * Deactivates pback buffer contents, and restores normal buffer.
211 * The pback buffer has only moved forward.
215 _M_destroy_pback() throw()
219 // Length _M_in_cur moved in the pback buffer.
220 _M_pback_cur_save
+= this->gptr() != this->eback();
221 this->setg(_M_buf
, _M_pback_cur_save
, _M_pback_end_save
);
222 _M_pback_init
= false;
227 // Constructors/destructor:
229 * @brief Does not open any files.
231 * The default constructor initializes the parent class using its
237 * @brief The destructor closes the file first.
245 * @brief Returns true if the external file is open.
248 is_open() const throw()
249 { return _M_file
.is_open(); }
252 * @brief Opens an external file.
253 * @param s The name of the file.
254 * @param mode The open mode flags.
255 * @return @c this on success, NULL on failure
257 * If a file is already open, this function immediately fails.
258 * Otherwise it tries to open the file named @a s using the flags
261 * Table 92, adapted here, gives the relation between openmode
262 * combinations and the equivalent fopen() flags.
263 * (NB: lines in|out|app and binary|in|out|app per DR 596)
264 * +---------------------------------------------------------+
265 * | ios_base Flag combination stdio equivalent |
266 * |binary in out trunc app |
267 * +---------------------------------------------------------+
275 * +---------------------------------------------------------+
283 * +---------------------------------------------------------+
286 open(const char* __s
, ios_base::openmode __mode
);
289 * @brief Closes the currently associated file.
290 * @return @c this on success, NULL on failure
292 * If no file is currently open, this function immediately fails.
294 * If a "put buffer area" exists, @c overflow(eof) is called to flush
295 * all the characters. The file is then closed.
297 * If any operations fail, this function also fails.
304 _M_allocate_internal_buffer();
307 _M_destroy_internal_buffer() throw();
309 // [27.8.1.4] overridden virtual functions
313 // Stroustrup, 1998, p. 628
314 // underflow() and uflow() functions are called to get the next
315 // charater from the real input source when the buffer is empty.
316 // Buffered input uses underflow()
322 pbackfail(int_type __c
= _Traits::eof());
324 // Stroustrup, 1998, p 648
325 // The overflow() function is called to transfer characters to the
326 // real output destination when the buffer is full. A call to
327 // overflow(c) outputs the contents of the buffer plus the
330 // Consume some sequence of the characters in the pending sequence.
332 overflow(int_type __c
= _Traits::eof());
334 // Convert internal byte sequence to external, char-based
335 // sequence via codecvt.
337 _M_convert_to_external(char_type
*, streamsize
);
340 * @brief Manipulates the buffer.
341 * @param s Pointer to a buffer area.
342 * @param n Size of @a s.
345 * If no file has been opened, and both @a s and @a n are zero, then
346 * the stream becomes unbuffered. Otherwise, @c s is used as a
348 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2
351 virtual __streambuf_type
*
352 setbuf(char_type
* __s
, streamsize __n
);
355 seekoff(off_type __off
, ios_base::seekdir __way
,
356 ios_base::openmode __mode
= ios_base::in
| ios_base::out
);
359 seekpos(pos_type __pos
,
360 ios_base::openmode __mode
= ios_base::in
| ios_base::out
);
362 // Common code for seekoff and seekpos
364 _M_seek(off_type __off
, ios_base::seekdir __way
, __state_type __state
);
370 imbue(const locale
& __loc
);
373 xsgetn(char_type
* __s
, streamsize __n
);
376 xsputn(const char_type
* __s
, streamsize __n
);
378 // Flushes output buffer, then writes unshift sequence.
380 _M_terminate_output();
384 * This function sets the pointers of the internal buffer, both get
385 * and put areas. Typically:
387 * __off == egptr() - eback() upon underflow/uflow ('read' mode);
388 * __off == 0 upon overflow ('write' mode);
389 * __off == -1 upon open, setbuf, seekoff/pos ('uncommitted' mode).
391 * NB: epptr() - pbase() == _M_buf_size - 1, since _M_buf_size
392 * reflects the actual allocated memory and the last cell is reserved
393 * for the overflow char of a full put area.
397 _M_set_buffer(streamsize __off
)
399 const bool __testin
= _M_mode
& ios_base::in
;
400 const bool __testout
= _M_mode
& ios_base::out
;
402 if (__testin
&& __off
> 0)
403 this->setg(_M_buf
, _M_buf
, _M_buf
+ __off
);
405 this->setg(_M_buf
, _M_buf
, _M_buf
);
407 if (__testout
&& __off
== 0 && _M_buf_size
> 1 )
408 this->setp(_M_buf
, _M_buf
+ _M_buf_size
- 1);
410 this->setp(NULL
, NULL
);
414 // [27.8.1.5] Template class basic_ifstream
416 * @brief Controlling input for files.
418 * This class supports reading from named files, using the inherited
419 * functions from std::basic_istream. To control the associated
420 * sequence, an instance of std::basic_filebuf is used, which this page
421 * refers to as @c sb.
423 template<typename _CharT
, typename _Traits
>
424 class basic_ifstream
: public basic_istream
<_CharT
, _Traits
>
428 typedef _CharT char_type
;
429 typedef _Traits traits_type
;
430 typedef typename
traits_type::int_type int_type
;
431 typedef typename
traits_type::pos_type pos_type
;
432 typedef typename
traits_type::off_type off_type
;
434 // Non-standard types:
435 typedef basic_filebuf
<char_type
, traits_type
> __filebuf_type
;
436 typedef basic_istream
<char_type
, traits_type
> __istream_type
;
439 __filebuf_type _M_filebuf
;
442 // Constructors/Destructors:
444 * @brief Default constructor.
446 * Initializes @c sb using its default constructor, and passes
447 * @c &sb to the base class initializer. Does not open any files
448 * (you haven't given it a filename to open).
450 basic_ifstream() : __istream_type(), _M_filebuf()
451 { this->init(&_M_filebuf
); }
454 * @brief Create an input file stream.
455 * @param s Null terminated string specifying the filename.
456 * @param mode Open file in specified mode (see std::ios_base).
458 * @c ios_base::in is automatically included in @a mode.
460 * Tip: When using std::string to hold the filename, you must use
461 * .c_str() before passing it to this constructor.
464 basic_ifstream(const char* __s
, ios_base::openmode __mode
= ios_base::in
)
465 : __istream_type(), _M_filebuf()
467 this->init(&_M_filebuf
);
468 this->open(__s
, __mode
);
472 * @brief The destructor does nothing.
474 * The file is closed by the filebuf object, not the formatting
482 * @brief Accessing the underlying buffer.
483 * @return The current basic_filebuf buffer.
485 * This hides both signatures of std::basic_ios::rdbuf().
489 { return const_cast<__filebuf_type
*>(&_M_filebuf
); }
492 * @brief Wrapper to test for an open file.
493 * @return @c rdbuf()->is_open()
497 { return _M_filebuf
.is_open(); }
499 // _GLIBCXX_RESOLVE_LIB_DEFECTS
500 // 365. Lack of const-qualification in clause 27
503 { return _M_filebuf
.is_open(); }
506 * @brief Opens an external file.
507 * @param s The name of the file.
508 * @param mode The open mode flags.
510 * Calls @c std::basic_filebuf::open(s,mode|in). If that function
511 * fails, @c failbit is set in the stream's error state.
513 * Tip: When using std::string to hold the filename, you must use
514 * .c_str() before passing it to this constructor.
517 open(const char* __s
, ios_base::openmode __mode
= ios_base::in
)
519 if (!_M_filebuf
.open(__s
, __mode
| ios_base::in
))
520 this->setstate(ios_base::failbit
);
522 // _GLIBCXX_RESOLVE_LIB_DEFECTS
523 // 409. Closing an fstream should clear error state
528 * @brief Close the file.
530 * Calls @c std::basic_filebuf::close(). If that function
531 * fails, @c failbit is set in the stream's error state.
536 if (!_M_filebuf
.close())
537 this->setstate(ios_base::failbit
);
542 // [27.8.1.8] Template class basic_ofstream
544 * @brief Controlling output for files.
546 * This class supports reading from named files, using the inherited
547 * functions from std::basic_ostream. To control the associated
548 * sequence, an instance of std::basic_filebuf is used, which this page
549 * refers to as @c sb.
551 template<typename _CharT
, typename _Traits
>
552 class basic_ofstream
: public basic_ostream
<_CharT
,_Traits
>
556 typedef _CharT char_type
;
557 typedef _Traits traits_type
;
558 typedef typename
traits_type::int_type int_type
;
559 typedef typename
traits_type::pos_type pos_type
;
560 typedef typename
traits_type::off_type off_type
;
562 // Non-standard types:
563 typedef basic_filebuf
<char_type
, traits_type
> __filebuf_type
;
564 typedef basic_ostream
<char_type
, traits_type
> __ostream_type
;
567 __filebuf_type _M_filebuf
;
572 * @brief Default constructor.
574 * Initializes @c sb using its default constructor, and passes
575 * @c &sb to the base class initializer. Does not open any files
576 * (you haven't given it a filename to open).
578 basic_ofstream(): __ostream_type(), _M_filebuf()
579 { this->init(&_M_filebuf
); }
582 * @brief Create an output file stream.
583 * @param s Null terminated string specifying the filename.
584 * @param mode Open file in specified mode (see std::ios_base).
586 * @c ios_base::out|ios_base::trunc is automatically included in
589 * Tip: When using std::string to hold the filename, you must use
590 * .c_str() before passing it to this constructor.
593 basic_ofstream(const char* __s
,
594 ios_base::openmode __mode
= ios_base::out
|ios_base::trunc
)
595 : __ostream_type(), _M_filebuf()
597 this->init(&_M_filebuf
);
598 this->open(__s
, __mode
);
602 * @brief The destructor does nothing.
604 * The file is closed by the filebuf object, not the formatting
612 * @brief Accessing the underlying buffer.
613 * @return The current basic_filebuf buffer.
615 * This hides both signatures of std::basic_ios::rdbuf().
619 { return const_cast<__filebuf_type
*>(&_M_filebuf
); }
622 * @brief Wrapper to test for an open file.
623 * @return @c rdbuf()->is_open()
627 { return _M_filebuf
.is_open(); }
629 // _GLIBCXX_RESOLVE_LIB_DEFECTS
630 // 365. Lack of const-qualification in clause 27
633 { return _M_filebuf
.is_open(); }
636 * @brief Opens an external file.
637 * @param s The name of the file.
638 * @param mode The open mode flags.
640 * Calls @c std::basic_filebuf::open(s,mode|out|trunc). If that
641 * function fails, @c failbit is set in the stream's error state.
643 * Tip: When using std::string to hold the filename, you must use
644 * .c_str() before passing it to this constructor.
647 open(const char* __s
,
648 ios_base::openmode __mode
= ios_base::out
| ios_base::trunc
)
650 if (!_M_filebuf
.open(__s
, __mode
| ios_base::out
))
651 this->setstate(ios_base::failbit
);
653 // _GLIBCXX_RESOLVE_LIB_DEFECTS
654 // 409. Closing an fstream should clear error state
659 * @brief Close the file.
661 * Calls @c std::basic_filebuf::close(). If that function
662 * fails, @c failbit is set in the stream's error state.
667 if (!_M_filebuf
.close())
668 this->setstate(ios_base::failbit
);
673 // [27.8.1.11] Template class basic_fstream
675 * @brief Controlling intput and output for files.
677 * This class supports reading from and writing to named files, using
678 * the inherited functions from std::basic_iostream. To control the
679 * associated sequence, an instance of std::basic_filebuf is used, which
680 * this page refers to as @c sb.
682 template<typename _CharT
, typename _Traits
>
683 class basic_fstream
: public basic_iostream
<_CharT
, _Traits
>
687 typedef _CharT char_type
;
688 typedef _Traits traits_type
;
689 typedef typename
traits_type::int_type int_type
;
690 typedef typename
traits_type::pos_type pos_type
;
691 typedef typename
traits_type::off_type off_type
;
693 // Non-standard types:
694 typedef basic_filebuf
<char_type
, traits_type
> __filebuf_type
;
695 typedef basic_ios
<char_type
, traits_type
> __ios_type
;
696 typedef basic_iostream
<char_type
, traits_type
> __iostream_type
;
699 __filebuf_type _M_filebuf
;
702 // Constructors/destructor:
704 * @brief Default constructor.
706 * Initializes @c sb using its default constructor, and passes
707 * @c &sb to the base class initializer. Does not open any files
708 * (you haven't given it a filename to open).
711 : __iostream_type(), _M_filebuf()
712 { this->init(&_M_filebuf
); }
715 * @brief Create an input/output file stream.
716 * @param s Null terminated string specifying the filename.
717 * @param mode Open file in specified mode (see std::ios_base).
719 * Tip: When using std::string to hold the filename, you must use
720 * .c_str() before passing it to this constructor.
723 basic_fstream(const char* __s
,
724 ios_base::openmode __mode
= ios_base::in
| ios_base::out
)
725 : __iostream_type(NULL
), _M_filebuf()
727 this->init(&_M_filebuf
);
728 this->open(__s
, __mode
);
732 * @brief The destructor does nothing.
734 * The file is closed by the filebuf object, not the formatting
742 * @brief Accessing the underlying buffer.
743 * @return The current basic_filebuf buffer.
745 * This hides both signatures of std::basic_ios::rdbuf().
749 { return const_cast<__filebuf_type
*>(&_M_filebuf
); }
752 * @brief Wrapper to test for an open file.
753 * @return @c rdbuf()->is_open()
757 { return _M_filebuf
.is_open(); }
759 // _GLIBCXX_RESOLVE_LIB_DEFECTS
760 // 365. Lack of const-qualification in clause 27
763 { return _M_filebuf
.is_open(); }
766 * @brief Opens an external file.
767 * @param s The name of the file.
768 * @param mode The open mode flags.
770 * Calls @c std::basic_filebuf::open(s,mode). If that
771 * function fails, @c failbit is set in the stream's error state.
773 * Tip: When using std::string to hold the filename, you must use
774 * .c_str() before passing it to this constructor.
777 open(const char* __s
,
778 ios_base::openmode __mode
= ios_base::in
| ios_base::out
)
780 if (!_M_filebuf
.open(__s
, __mode
))
781 this->setstate(ios_base::failbit
);
783 // _GLIBCXX_RESOLVE_LIB_DEFECTS
784 // 409. Closing an fstream should clear error state
789 * @brief Close the file.
791 * Calls @c std::basic_filebuf::close(). If that function
792 * fails, @c failbit is set in the stream's error state.
797 if (!_M_filebuf
.close())
798 this->setstate(ios_base::failbit
);
802 _GLIBCXX_END_NAMESPACE
804 #ifndef _GLIBCXX_EXPORT_TEMPLATE
805 # include <bits/fstream.tcc>
808 #endif /* _GLIBCXX_FSTREAM */