Mark as release
[official-gcc.git] / libstdc++-v3 / include / std / std_fstream.h
blob0c81633f50bf67c2b471125a4a285f7ffcbaafdf
1 // File based streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007
5 // Free Software Foundation, Inc.
6 //
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)
11 // any later version.
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,
21 // USA.
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.
32 /** @file fstream
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
45 #include <istream>
46 #include <ostream>
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
55 /**
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>
71 public:
72 // Types:
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.
87 protected:
88 // Data Members:
89 // MT lock inherited from libio or other low-level io library.
90 __c_lock _M_lock;
92 // External buffer.
93 __file_type _M_file;
95 /**
96 * @if maint
97 * Place to stash in || out || in | out settings for current filebuf.
98 * @endif
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
107 // _M_ext_next.
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;
115 * @if maint
116 * Pointer to the beginning of internal buffer.
117 * @endif
119 char_type* _M_buf;
122 * @if maint
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
125 * a full area.
126 * @endif
128 size_t _M_buf_size;
130 // Set iff _M_buf is allocated memory from _M_allocate_internal_buffer.
131 bool _M_buf_allocated;
134 * @if maint
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.
140 * @endif
142 bool _M_reading;
143 bool _M_writing;
145 //@{
147 * @if maint
148 * Necessary bits for putback buffer management.
150 * @note pbacks of over one character are not currently supported.
151 * @endif
153 char_type _M_pback;
154 char_type* _M_pback_cur_save;
155 char_type* _M_pback_end_save;
156 bool _M_pback_init;
157 //@}
159 // Cached codecvt facet.
160 const __codecvt_type* _M_codecvt;
163 * @if maint
164 * Buffer for external characters. Used for input when
165 * codecvt::always_noconv() == false. When valid, this corresponds
166 * to eback().
167 * @endif
169 char* _M_ext_buf;
172 * @if maint
173 * Size of buffer held by _M_ext_buf.
174 * @endif
176 streamsize _M_ext_buf_size;
179 * @if maint
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().
183 * @endif
185 const char* _M_ext_next;
186 char* _M_ext_end;
189 * @if maint
190 * Initializes pback buffers, and moves normal buffers to safety.
191 * Assumptions:
192 * _M_in_cur has already been moved back
193 * @endif
195 void
196 _M_create_pback()
198 if (!_M_pback_init)
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;
208 * @if maint
209 * Deactivates pback buffer contents, and restores normal buffer.
210 * Assumptions:
211 * The pback buffer has only moved forward.
212 * @endif
214 void
215 _M_destroy_pback() throw()
217 if (_M_pback_init)
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;
226 public:
227 // Constructors/destructor:
229 * @brief Does not open any files.
231 * The default constructor initializes the parent class using its
232 * own default ctor.
234 basic_filebuf();
237 * @brief The destructor closes the file first.
239 virtual
240 ~basic_filebuf()
241 { this->close(); }
243 // Members:
245 * @brief Returns true if the external file is open.
247 bool
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
259 * given in @a mode.
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 * +---------------------------------------------------------+
268 * | + "w" |
269 * | + + "a" |
270 * | + + "w" |
271 * | + "r" |
272 * | + + "r+" |
273 * | + + + "w+" |
274 * | + + + "a+" |
275 * +---------------------------------------------------------+
276 * | + + "wb" |
277 * | + + + "ab" |
278 * | + + + "wb" |
279 * | + + "rb" |
280 * | + + + "r+b" |
281 * | + + + + "w+b" |
282 * | + + + + "a+b" |
283 * +---------------------------------------------------------+
285 __filebuf_type*
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.
299 __filebuf_type*
300 close() throw();
302 protected:
303 void
304 _M_allocate_internal_buffer();
306 void
307 _M_destroy_internal_buffer() throw();
309 // [27.8.1.4] overridden virtual functions
310 virtual streamsize
311 showmanyc();
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()
318 virtual int_type
319 underflow();
321 virtual int_type
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
328 // character c.
329 // 27.5.2.4.5
330 // Consume some sequence of the characters in the pending sequence.
331 virtual int_type
332 overflow(int_type __c = _Traits::eof());
334 // Convert internal byte sequence to external, char-based
335 // sequence via codecvt.
336 bool
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.
343 * @return @c this
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
347 * buffer; see
348 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2
349 * for more.
351 virtual __streambuf_type*
352 setbuf(char_type* __s, streamsize __n);
354 virtual pos_type
355 seekoff(off_type __off, ios_base::seekdir __way,
356 ios_base::openmode __mode = ios_base::in | ios_base::out);
358 virtual pos_type
359 seekpos(pos_type __pos,
360 ios_base::openmode __mode = ios_base::in | ios_base::out);
362 // Common code for seekoff and seekpos
363 pos_type
364 _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state);
366 virtual int
367 sync();
369 virtual void
370 imbue(const locale& __loc);
372 virtual streamsize
373 xsgetn(char_type* __s, streamsize __n);
375 virtual streamsize
376 xsputn(const char_type* __s, streamsize __n);
378 // Flushes output buffer, then writes unshift sequence.
379 bool
380 _M_terminate_output();
383 * @if maint
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.
394 * @endif
396 void
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);
404 else
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);
409 else
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>
426 public:
427 // Types:
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;
438 private:
439 __filebuf_type _M_filebuf;
441 public:
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.
463 explicit
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
475 * stream.
477 ~basic_ifstream()
480 // Members:
482 * @brief Accessing the underlying buffer.
483 * @return The current basic_filebuf buffer.
485 * This hides both signatures of std::basic_ios::rdbuf().
487 __filebuf_type*
488 rdbuf() const
489 { return const_cast<__filebuf_type*>(&_M_filebuf); }
492 * @brief Wrapper to test for an open file.
493 * @return @c rdbuf()->is_open()
495 bool
496 is_open()
497 { return _M_filebuf.is_open(); }
499 // _GLIBCXX_RESOLVE_LIB_DEFECTS
500 // 365. Lack of const-qualification in clause 27
501 bool
502 is_open() const
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.
516 void
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);
521 else
522 // _GLIBCXX_RESOLVE_LIB_DEFECTS
523 // 409. Closing an fstream should clear error state
524 this->clear();
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.
533 void
534 close()
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>
554 public:
555 // Types:
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;
566 private:
567 __filebuf_type _M_filebuf;
569 public:
570 // Constructors:
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
587 * @a mode.
589 * Tip: When using std::string to hold the filename, you must use
590 * .c_str() before passing it to this constructor.
592 explicit
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
605 * stream.
607 ~basic_ofstream()
610 // Members:
612 * @brief Accessing the underlying buffer.
613 * @return The current basic_filebuf buffer.
615 * This hides both signatures of std::basic_ios::rdbuf().
617 __filebuf_type*
618 rdbuf() const
619 { return const_cast<__filebuf_type*>(&_M_filebuf); }
622 * @brief Wrapper to test for an open file.
623 * @return @c rdbuf()->is_open()
625 bool
626 is_open()
627 { return _M_filebuf.is_open(); }
629 // _GLIBCXX_RESOLVE_LIB_DEFECTS
630 // 365. Lack of const-qualification in clause 27
631 bool
632 is_open() const
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.
646 void
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);
652 else
653 // _GLIBCXX_RESOLVE_LIB_DEFECTS
654 // 409. Closing an fstream should clear error state
655 this->clear();
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.
664 void
665 close()
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>
685 public:
686 // Types:
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;
698 private:
699 __filebuf_type _M_filebuf;
701 public:
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).
710 basic_fstream()
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.
722 explicit
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
735 * stream.
737 ~basic_fstream()
740 // Members:
742 * @brief Accessing the underlying buffer.
743 * @return The current basic_filebuf buffer.
745 * This hides both signatures of std::basic_ios::rdbuf().
747 __filebuf_type*
748 rdbuf() const
749 { return const_cast<__filebuf_type*>(&_M_filebuf); }
752 * @brief Wrapper to test for an open file.
753 * @return @c rdbuf()->is_open()
755 bool
756 is_open()
757 { return _M_filebuf.is_open(); }
759 // _GLIBCXX_RESOLVE_LIB_DEFECTS
760 // 365. Lack of const-qualification in clause 27
761 bool
762 is_open() const
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.
776 void
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);
782 else
783 // _GLIBCXX_RESOLVE_LIB_DEFECTS
784 // 409. Closing an fstream should clear error state
785 this->clear();
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.
794 void
795 close()
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>
806 #endif
808 #endif /* _GLIBCXX_FSTREAM */