2005-01-06 Benjamin Kosnik <bkoz@redhat.com>
[official-gcc.git] / libstdc++-v3 / include / std / std_fstream.h
blobed119d4c8eab915fcb996267ea20fd0c75b55a42
1 // File based streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 // Free Software Foundation, Inc.
5 //
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)
10 // any later version.
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,
20 // USA.
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
35 /** @file fstream
36 * This is a Standard C++ Library header.
39 #ifndef _GLIBCXX_FSTREAM
40 #define _GLIBCXX_FSTREAM 1
42 #pragma GCC system_header
44 #include <istream>
45 #include <ostream>
46 #include <locale> // For codecvt
47 #include <cstdio> // For SEEK_SET, SEEK_CUR, SEEK_END, BUFSIZ
48 #include <bits/basic_file.h>
49 #include <bits/gthr.h>
51 namespace std
53 // [27.8.1.1] template class basic_filebuf
54 /**
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 // Requirements on traits_type, specific to this class:
63 // traits_type::pos_type must be fpos<traits_type::state_type>
64 // traits_type::off_type must be streamoff
65 // traits_type::state_type must be Assignable and DefaultConstructable,
66 // and traits_type::state_type() must be the initial state for codecvt.
67 template<typename _CharT, typename _Traits>
68 class basic_filebuf : public basic_streambuf<_CharT, _Traits>
70 public:
71 // Types:
72 typedef _CharT char_type;
73 typedef _Traits traits_type;
74 typedef typename traits_type::int_type int_type;
75 typedef typename traits_type::pos_type pos_type;
76 typedef typename traits_type::off_type off_type;
78 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
79 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
80 typedef __basic_file<char> __file_type;
81 typedef typename traits_type::state_type __state_type;
82 typedef codecvt<char_type, char, __state_type> __codecvt_type;
84 friend class ios_base; // For sync_with_stdio.
86 protected:
87 // Data Members:
88 // MT lock inherited from libio or other low-level io library.
89 __c_lock _M_lock;
91 // External buffer.
92 __file_type _M_file;
94 /**
95 * @if maint
96 * Place to stash in || out || in | out settings for current filebuf.
97 * @endif
99 ios_base::openmode _M_mode;
101 // Beginning state type for codecvt.
102 __state_type _M_state_beg;
104 // During output, the state that corresponds to pptr(),
105 // during input, the state that corresponds to egptr() and
106 // _M_ext_next.
107 __state_type _M_state_cur;
109 // Not used for output. During input, the state that corresponds
110 // to eback() and _M_ext_buf.
111 __state_type _M_state_last;
114 * @if maint
115 * Pointer to the beginning of internal buffer.
116 * @endif
118 char_type* _M_buf;
121 * @if maint
122 * Actual size of internal buffer. This number is equal to the size
123 * of the put area + 1 position, reserved for the overflow char of
124 * a full area.
125 * @endif
127 size_t _M_buf_size;
129 // Set iff _M_buf is allocated memory from _M_allocate_internal_buffer.
130 bool _M_buf_allocated;
133 * @if maint
134 * _M_reading == false && _M_writing == false for 'uncommitted' mode;
135 * _M_reading == true for 'read' mode;
136 * _M_writing == true for 'write' mode;
138 * NB: _M_reading == true && _M_writing == true is unused.
139 * @endif
141 bool _M_reading;
142 bool _M_writing;
144 //@{
146 * @if maint
147 * Necessary bits for putback buffer management.
149 * @note pbacks of over one character are not currently supported.
150 * @endif
152 char_type _M_pback;
153 char_type* _M_pback_cur_save;
154 char_type* _M_pback_end_save;
155 bool _M_pback_init;
156 //@}
158 // Cached codecvt facet.
159 const __codecvt_type* _M_codecvt;
162 * @if maint
163 * Buffer for external characters. Used for input when
164 * codecvt::always_noconv() == false. When valid, this corresponds
165 * to eback().
166 * @endif
168 char* _M_ext_buf;
171 * @if maint
172 * Size of buffer held by _M_ext_buf.
173 * @endif
175 streamsize _M_ext_buf_size;
178 * @if maint
179 * Pointers into the buffer held by _M_ext_buf that delimit a
180 * subsequence of bytes that have been read but not yet converted.
181 * When valid, _M_ext_next corresponds to egptr().
182 * @endif
184 const char* _M_ext_next;
185 char* _M_ext_end;
188 * @if maint
189 * Initializes pback buffers, and moves normal buffers to safety.
190 * Assumptions:
191 * _M_in_cur has already been moved back
192 * @endif
194 void
195 _M_create_pback()
197 if (!_M_pback_init)
199 _M_pback_cur_save = this->gptr();
200 _M_pback_end_save = this->egptr();
201 this->setg(&_M_pback, &_M_pback, &_M_pback + 1);
202 _M_pback_init = true;
207 * @if maint
208 * Deactivates pback buffer contents, and restores normal buffer.
209 * Assumptions:
210 * The pback buffer has only moved forward.
211 * @endif
213 void
214 _M_destroy_pback() throw()
216 if (_M_pback_init)
218 // Length _M_in_cur moved in the pback buffer.
219 _M_pback_cur_save += this->gptr() != this->eback();
220 this->setg(_M_buf, _M_pback_cur_save, _M_pback_end_save);
221 _M_pback_init = false;
225 public:
226 // Constructors/destructor:
228 * @brief Does not open any files.
230 * The default constructor initializes the parent class using its
231 * own default ctor.
233 basic_filebuf();
236 * @brief The destructor closes the file first.
238 virtual
239 ~basic_filebuf()
240 { this->close(); }
242 // Members:
244 * @brief Returns true if the external file is open.
246 bool
247 is_open() const throw()
248 { return _M_file.is_open(); }
251 * @brief Opens an external file.
252 * @param s The name of the file.
253 * @param mode The open mode flags.
254 * @return @c this on success, NULL on failure
256 * If a file is already open, this function immediately fails.
257 * Otherwise it tries to open the file named @a s using the flags
258 * given in @a mode.
260 * [Table 92 gives the relation between openmode combinations and the
261 * equivalent fopen() flags, but the table has not been copied yet.]
263 __filebuf_type*
264 open(const char* __s, ios_base::openmode __mode);
267 * @brief Closes the currently associated file.
268 * @return @c this on success, NULL on failure
270 * If no file is currently open, this function immediately fails.
272 * If a "put buffer area" exists, @c overflow(eof) is called to flush
273 * all the characters. The file is then closed.
275 * If any operations fail, this function also fails.
277 __filebuf_type*
278 close() throw();
280 protected:
281 void
282 _M_allocate_internal_buffer();
284 void
285 _M_destroy_internal_buffer() throw();
287 // [27.8.1.4] overridden virtual functions
288 virtual streamsize
289 showmanyc();
291 // Stroustrup, 1998, p. 628
292 // underflow() and uflow() functions are called to get the next
293 // charater from the real input source when the buffer is empty.
294 // Buffered input uses underflow()
296 virtual int_type
297 underflow();
299 virtual int_type
300 pbackfail(int_type __c = _Traits::eof());
302 // Stroustrup, 1998, p 648
303 // The overflow() function is called to transfer characters to the
304 // real output destination when the buffer is full. A call to
305 // overflow(c) outputs the contents of the buffer plus the
306 // character c.
307 // 27.5.2.4.5
308 // Consume some sequence of the characters in the pending sequence.
309 virtual int_type
310 overflow(int_type __c = _Traits::eof());
312 // Convert internal byte sequence to external, char-based
313 // sequence via codecvt.
314 bool
315 _M_convert_to_external(char_type*, streamsize);
318 * @brief Manipulates the buffer.
319 * @param s Pointer to a buffer area.
320 * @param n Size of @a s.
321 * @return @c this
323 * If no file has been opened, and both @a s and @a n are zero, then
324 * the stream becomes unbuffered. Otherwise, @c s is used as a
325 * buffer; see
326 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2
327 * for more.
329 virtual __streambuf_type*
330 setbuf(char_type* __s, streamsize __n);
332 virtual pos_type
333 seekoff(off_type __off, ios_base::seekdir __way,
334 ios_base::openmode __mode = ios_base::in | ios_base::out);
336 virtual pos_type
337 seekpos(pos_type __pos,
338 ios_base::openmode __mode = ios_base::in | ios_base::out);
340 // Common code for seekoff and seekpos
341 pos_type
342 _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state);
344 virtual int
345 sync();
347 virtual void
348 imbue(const locale& __loc);
350 virtual streamsize
351 xsgetn(char_type* __s, streamsize __n);
353 virtual streamsize
354 xsputn(const char_type* __s, streamsize __n);
356 // Flushes output buffer, then writes unshift sequence.
357 bool
358 _M_terminate_output();
361 * @if maint
362 * This function sets the pointers of the internal buffer, both get
363 * and put areas. Typically:
365 * __off == egptr() - eback() upon underflow/uflow ('read' mode);
366 * __off == 0 upon overflow ('write' mode);
367 * __off == -1 upon open, setbuf, seekoff/pos ('uncommitted' mode).
369 * NB: epptr() - pbase() == _M_buf_size - 1, since _M_buf_size
370 * reflects the actual allocated memory and the last cell is reserved
371 * for the overflow char of a full put area.
372 * @endif
374 void
375 _M_set_buffer(streamsize __off)
377 const bool __testin = _M_mode & ios_base::in;
378 const bool __testout = _M_mode & ios_base::out;
380 if (__testin && __off > 0)
381 this->setg(_M_buf, _M_buf, _M_buf + __off);
382 else
383 this->setg(_M_buf, _M_buf, _M_buf);
385 if (__testout && __off == 0 && _M_buf_size > 1 )
386 this->setp(_M_buf, _M_buf + _M_buf_size - 1);
387 else
388 this->setp(NULL, NULL);
392 // [27.8.1.5] Template class basic_ifstream
394 * @brief Controlling input for files.
396 * This class supports reading from named files, using the inherited
397 * functions from std::basic_istream. To control the associated
398 * sequence, an instance of std::basic_filebuf is used, which this page
399 * refers to as @c sb.
401 template<typename _CharT, typename _Traits>
402 class basic_ifstream : public basic_istream<_CharT, _Traits>
404 public:
405 // Types:
406 typedef _CharT char_type;
407 typedef _Traits traits_type;
408 typedef typename traits_type::int_type int_type;
409 typedef typename traits_type::pos_type pos_type;
410 typedef typename traits_type::off_type off_type;
412 // Non-standard types:
413 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
414 typedef basic_istream<char_type, traits_type> __istream_type;
416 private:
417 __filebuf_type _M_filebuf;
419 public:
420 // Constructors/Destructors:
422 * @brief Default constructor.
424 * Initializes @c sb using its default constructor, and passes
425 * @c &sb to the base class initializer. Does not open any files
426 * (you haven't given it a filename to open).
428 basic_ifstream() : __istream_type(), _M_filebuf()
429 { this->init(&_M_filebuf); }
432 * @brief Create an input file stream.
433 * @param s Null terminated string specifying the filename.
434 * @param mode Open file in specified mode (see std::ios_base).
436 * @c ios_base::in is automatically included in @a mode.
438 * Tip: When using std::string to hold the filename, you must use
439 * .c_str() before passing it to this constructor.
441 explicit
442 basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in)
443 : __istream_type(), _M_filebuf()
445 this->init(&_M_filebuf);
446 this->open(__s, __mode);
450 * @brief The destructor does nothing.
452 * The file is closed by the filebuf object, not the formatting
453 * stream.
455 ~basic_ifstream()
458 // Members:
460 * @brief Accessing the underlying buffer.
461 * @return The current basic_filebuf buffer.
463 * This hides both signatures of std::basic_ios::rdbuf().
465 __filebuf_type*
466 rdbuf() const
467 { return const_cast<__filebuf_type*>(&_M_filebuf); }
470 * @brief Wrapper to test for an open file.
471 * @return @c rdbuf()->is_open()
473 bool
474 is_open()
475 { return _M_filebuf.is_open(); }
477 // _GLIBCXX_RESOLVE_LIB_DEFECTS
478 // 365. Lack of const-qualification in clause 27
479 bool
480 is_open() const
481 { return _M_filebuf.is_open(); }
484 * @brief Opens an external file.
485 * @param s The name of the file.
486 * @param mode The open mode flags.
488 * Calls @c std::basic_filebuf::open(s,mode|in). If that function
489 * fails, @c failbit is set in the stream's error state.
491 * Tip: When using std::string to hold the filename, you must use
492 * .c_str() before passing it to this constructor.
494 void
495 open(const char* __s, ios_base::openmode __mode = ios_base::in)
497 if (!_M_filebuf.open(__s, __mode | ios_base::in))
498 this->setstate(ios_base::failbit);
502 * @brief Close the file.
504 * Calls @c std::basic_filebuf::close(). If that function
505 * fails, @c failbit is set in the stream's error state.
507 void
508 close()
510 if (!_M_filebuf.close())
511 this->setstate(ios_base::failbit);
516 // [27.8.1.8] Template class basic_ofstream
518 * @brief Controlling output for files.
520 * This class supports reading from named files, using the inherited
521 * functions from std::basic_ostream. To control the associated
522 * sequence, an instance of std::basic_filebuf is used, which this page
523 * refers to as @c sb.
525 template<typename _CharT, typename _Traits>
526 class basic_ofstream : public basic_ostream<_CharT,_Traits>
528 public:
529 // Types:
530 typedef _CharT char_type;
531 typedef _Traits traits_type;
532 typedef typename traits_type::int_type int_type;
533 typedef typename traits_type::pos_type pos_type;
534 typedef typename traits_type::off_type off_type;
536 // Non-standard types:
537 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
538 typedef basic_ostream<char_type, traits_type> __ostream_type;
540 private:
541 __filebuf_type _M_filebuf;
543 public:
544 // Constructors:
546 * @brief Default constructor.
548 * Initializes @c sb using its default constructor, and passes
549 * @c &sb to the base class initializer. Does not open any files
550 * (you haven't given it a filename to open).
552 basic_ofstream(): __ostream_type(), _M_filebuf()
553 { this->init(&_M_filebuf); }
556 * @brief Create an output file stream.
557 * @param s Null terminated string specifying the filename.
558 * @param mode Open file in specified mode (see std::ios_base).
560 * @c ios_base::out|ios_base::trunc is automatically included in
561 * @a mode.
563 * Tip: When using std::string to hold the filename, you must use
564 * .c_str() before passing it to this constructor.
566 explicit
567 basic_ofstream(const char* __s,
568 ios_base::openmode __mode = ios_base::out|ios_base::trunc)
569 : __ostream_type(), _M_filebuf()
571 this->init(&_M_filebuf);
572 this->open(__s, __mode);
576 * @brief The destructor does nothing.
578 * The file is closed by the filebuf object, not the formatting
579 * stream.
581 ~basic_ofstream()
584 // Members:
586 * @brief Accessing the underlying buffer.
587 * @return The current basic_filebuf buffer.
589 * This hides both signatures of std::basic_ios::rdbuf().
591 __filebuf_type*
592 rdbuf() const
593 { return const_cast<__filebuf_type*>(&_M_filebuf); }
596 * @brief Wrapper to test for an open file.
597 * @return @c rdbuf()->is_open()
599 bool
600 is_open()
601 { return _M_filebuf.is_open(); }
603 // _GLIBCXX_RESOLVE_LIB_DEFECTS
604 // 365. Lack of const-qualification in clause 27
605 bool
606 is_open() const
607 { return _M_filebuf.is_open(); }
610 * @brief Opens an external file.
611 * @param s The name of the file.
612 * @param mode The open mode flags.
614 * Calls @c std::basic_filebuf::open(s,mode|out|trunc). If that
615 * function fails, @c failbit is set in the stream's error state.
617 * Tip: When using std::string to hold the filename, you must use
618 * .c_str() before passing it to this constructor.
620 void
621 open(const char* __s,
622 ios_base::openmode __mode = ios_base::out | ios_base::trunc)
624 if (!_M_filebuf.open(__s, __mode | ios_base::out))
625 this->setstate(ios_base::failbit);
629 * @brief Close the file.
631 * Calls @c std::basic_filebuf::close(). If that function
632 * fails, @c failbit is set in the stream's error state.
634 void
635 close()
637 if (!_M_filebuf.close())
638 this->setstate(ios_base::failbit);
643 // [27.8.1.11] Template class basic_fstream
645 * @brief Controlling intput and output for files.
647 * This class supports reading from and writing to named files, using
648 * the inherited functions from std::basic_iostream. To control the
649 * associated sequence, an instance of std::basic_filebuf is used, which
650 * this page refers to as @c sb.
652 template<typename _CharT, typename _Traits>
653 class basic_fstream : public basic_iostream<_CharT, _Traits>
655 public:
656 // Types:
657 typedef _CharT char_type;
658 typedef _Traits traits_type;
659 typedef typename traits_type::int_type int_type;
660 typedef typename traits_type::pos_type pos_type;
661 typedef typename traits_type::off_type off_type;
663 // Non-standard types:
664 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
665 typedef basic_ios<char_type, traits_type> __ios_type;
666 typedef basic_iostream<char_type, traits_type> __iostream_type;
668 private:
669 __filebuf_type _M_filebuf;
671 public:
672 // Constructors/destructor:
674 * @brief Default constructor.
676 * Initializes @c sb using its default constructor, and passes
677 * @c &sb to the base class initializer. Does not open any files
678 * (you haven't given it a filename to open).
680 basic_fstream()
681 : __iostream_type(), _M_filebuf()
682 { this->init(&_M_filebuf); }
685 * @brief Create an input/output file stream.
686 * @param s Null terminated string specifying the filename.
687 * @param mode Open file in specified mode (see std::ios_base).
689 * Tip: When using std::string to hold the filename, you must use
690 * .c_str() before passing it to this constructor.
692 explicit
693 basic_fstream(const char* __s,
694 ios_base::openmode __mode = ios_base::in | ios_base::out)
695 : __iostream_type(NULL), _M_filebuf()
697 this->init(&_M_filebuf);
698 this->open(__s, __mode);
702 * @brief The destructor does nothing.
704 * The file is closed by the filebuf object, not the formatting
705 * stream.
707 ~basic_fstream()
710 // Members:
712 * @brief Accessing the underlying buffer.
713 * @return The current basic_filebuf buffer.
715 * This hides both signatures of std::basic_ios::rdbuf().
717 __filebuf_type*
718 rdbuf() const
719 { return const_cast<__filebuf_type*>(&_M_filebuf); }
722 * @brief Wrapper to test for an open file.
723 * @return @c rdbuf()->is_open()
725 bool
726 is_open()
727 { return _M_filebuf.is_open(); }
729 // _GLIBCXX_RESOLVE_LIB_DEFECTS
730 // 365. Lack of const-qualification in clause 27
731 bool
732 is_open() const
733 { return _M_filebuf.is_open(); }
736 * @brief Opens an external file.
737 * @param s The name of the file.
738 * @param mode The open mode flags.
740 * Calls @c std::basic_filebuf::open(s,mode). If that
741 * function fails, @c failbit is set in the stream's error state.
743 * Tip: When using std::string to hold the filename, you must use
744 * .c_str() before passing it to this constructor.
746 void
747 open(const char* __s,
748 ios_base::openmode __mode = ios_base::in | ios_base::out)
750 if (!_M_filebuf.open(__s, __mode))
751 this->setstate(ios_base::failbit);
755 * @brief Close the file.
757 * Calls @c std::basic_filebuf::close(). If that function
758 * fails, @c failbit is set in the stream's error state.
760 void
761 close()
763 if (!_M_filebuf.close())
764 this->setstate(ios_base::failbit);
767 } // namespace std
769 #ifndef _GLIBCXX_EXPORT_TEMPLATE
770 # include <bits/fstream.tcc>
771 #endif
773 #endif /* _GLIBCXX_FSTREAM */