1 // File based streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 // ISO C++ 14882: 27.8 File-based streams
34 /** @file std_fstream.h
35 * This is an internal header file, included by other library headers.
36 * You should not attempt to use it directly.
40 #define _CPP_FSTREAM 1
42 #pragma GCC system_header
46 #include <bits/basic_file.h>
47 #include <locale> // For codecvt
48 #include <bits/gthr.h>
52 template<typename _CharT, typename _Traits>
53 class basic_filebuf : public basic_streambuf<_CharT, _Traits>
57 typedef _CharT char_type;
58 typedef _Traits traits_type;
59 typedef typename traits_type::int_type int_type;
60 typedef typename traits_type::pos_type pos_type;
61 typedef typename traits_type::off_type off_type;
63 // Non-standard Types:
64 typedef basic_streambuf<char_type, traits_type> __streambuf_type;
65 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
66 typedef __basic_file<char_type> __file_type;
67 typedef typename traits_type::state_type __state_type;
68 typedef codecvt<char_type, char, __state_type> __codecvt_type;
69 typedef typename __codecvt_type::result __res_type;
70 typedef ctype<char_type> __ctype_type;
72 friend class ios_base; // For sync_with_stdio.
79 // Current and beginning state type for codecvt.
80 __state_type _M_state_cur;
81 __state_type _M_state_beg;
83 // MT lock inherited from libio or other low-level io library.
86 // Set iff _M_buf is allocated memory from _M_allocate_internal_buffer..
87 bool _M_buf_allocated;
90 bool _M_last_overflowed;
93 // Constructors/destructor:
97 basic_filebuf(__c_file_type* __f, ios_base::openmode __mode,
98 int_type __s = static_cast<int_type>(BUFSIZ));
100 // Non-standard member:
108 _M_last_overflowed = false;
113 is_open(void) const { return _M_file ? _M_file->is_open() : false; }
116 open(const char* __s, ios_base::openmode __mode);
123 _M_allocate_internal_buffer();
126 _M_destroy_internal_buffer();
129 _M_allocate_pback_buffer();
131 // Create __file_type object and initialize it properly.
135 // Overridden virtual functions:
139 // Stroustrup, 1998, p. 628
140 // underflow() and uflow() functions are called to get the next
141 // charater from the real input source when the buffer is empty.
142 // Buffered input uses underflow()
147 pbackfail(int_type __c = _Traits::eof());
149 // NB: For what the standard expects of the overflow function,
150 // see _M_really_overflow(), below. Because basic_streambuf's
151 // sputc/sputn call overflow directly, and the complications of
152 // this implementation's setting of the initial pointers all
153 // equal to _M_buf when initializing, it seems essential to have
154 // this in actuality be a helper function that checks for the
155 // eccentricities of this implementation, and then call
156 // overflow() if indeed the buffer is full.
158 overflow(int_type __c = _Traits::eof());
160 // Stroustrup, 1998, p 648
161 // The overflow() function is called to transfer characters to the
162 // real output destination when the buffer is full. A call to
163 // overflow(c) outputs the contents of the buffer plus the
166 // Consume some sequence of the characters in the pending sequence.
168 _M_really_overflow(int_type __c = _Traits::eof());
170 virtual __streambuf_type*
171 setbuf(char_type* __s, streamsize __n);
174 seekoff(off_type __off, ios_base::seekdir __way,
175 ios_base::openmode __mode = ios_base::in | ios_base::out);
178 seekpos(pos_type __pos,
179 ios_base::openmode __mode = ios_base::in | ios_base::out);
184 bool __testput = _M_out_cur && _M_out_beg < _M_out_end;
186 // Make sure that the internal buffer resyncs its idea of
187 // the file position with the external file.
188 if (__testput && !_M_file->sync())
190 // Need to restore current position. This interpreted as
191 // the position of the external byte sequence (_M_file)
192 // plus the offset in the current internal buffer
193 // (_M_out_beg - _M_out_cur)
194 streamoff __cur = _M_file->seekoff(0, ios_base::cur);
195 off_type __off = _M_out_cur - _M_out_beg;
196 _M_really_overflow();
197 _M_file->seekpos(__cur + __off);
199 _M_last_overflowed = false;
204 imbue(const locale& __loc);
207 xsgetn(char_type* __s, streamsize __n)
209 streamsize __ret = 0;
210 // Clear out pback buffer before going on to the real deal...
213 while (__ret < __n && _M_in_cur < _M_in_end)
223 __ret += __streambuf_type::xsgetn(__s, __n - __ret);
228 xsputn(const char_type* __s, streamsize __n)
231 return __streambuf_type::xsputn(__s, __n);
239 // 27.8.1.5 Template class basic_ifstream
240 template<typename _CharT, typename _Traits>
241 class basic_ifstream : public basic_istream<_CharT, _Traits>
245 typedef _CharT char_type;
246 typedef _Traits traits_type;
247 typedef typename traits_type::int_type int_type;
248 typedef typename traits_type::pos_type pos_type;
249 typedef typename traits_type::off_type off_type;
251 // Non-standard types:
252 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
253 typedef basic_istream<char_type, traits_type> __istream_type;
256 __filebuf_type _M_filebuf;
259 // Constructors/Destructors:
261 : __istream_type(NULL), _M_filebuf()
262 { this->init(&_M_filebuf); }
265 basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in)
266 : __istream_type(NULL), _M_filebuf()
268 this->init(&_M_filebuf);
269 this->open(__s, __mode);
278 { return const_cast<__filebuf_type*>(&_M_filebuf); }
281 is_open(void) { return _M_filebuf.is_open(); }
284 open(const char* __s, ios_base::openmode __mode = ios_base::in)
286 if (_M_filebuf.open(__s, __mode | ios_base::in) == NULL)
287 this->setstate(ios_base::failbit);
293 if (!_M_filebuf.close())
294 this->setstate(ios_base::failbit);
299 // 27.8.1.8 Template class basic_ofstream
300 template<typename _CharT, typename _Traits>
301 class basic_ofstream : public basic_ostream<_CharT,_Traits>
305 typedef _CharT char_type;
306 typedef _Traits traits_type;
307 typedef typename traits_type::int_type int_type;
308 typedef typename traits_type::pos_type pos_type;
309 typedef typename traits_type::off_type off_type;
311 // Non-standard types:
312 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
313 typedef basic_ostream<char_type, traits_type> __ostream_type;
316 __filebuf_type _M_filebuf;
321 : __ostream_type(NULL), _M_filebuf()
322 { this->init(&_M_filebuf); }
325 basic_ofstream(const char* __s,
326 ios_base::openmode __mode = ios_base::out|ios_base::trunc)
327 : __ostream_type(NULL), _M_filebuf()
329 this->init(&_M_filebuf);
330 this->open(__s, __mode);
339 { return const_cast<__filebuf_type*>(&_M_filebuf); }
342 is_open(void) { return _M_filebuf.is_open(); }
345 open(const char* __s,
346 ios_base::openmode __mode = ios_base::out | ios_base::trunc)
348 if (!_M_filebuf.open(__s, __mode | ios_base::out))
349 this->setstate(ios_base::failbit);
355 if (!_M_filebuf.close())
356 this->setstate(ios_base::failbit);
361 // 27.8.1.11 Template class basic_fstream
362 template<typename _CharT, typename _Traits>
363 class basic_fstream : public basic_iostream<_CharT, _Traits>
367 typedef _CharT char_type;
368 typedef _Traits traits_type;
369 typedef typename traits_type::int_type int_type;
370 typedef typename traits_type::pos_type pos_type;
371 typedef typename traits_type::off_type off_type;
373 // Non-standard types:
374 typedef basic_filebuf<char_type, traits_type> __filebuf_type;
375 typedef basic_ios<char_type, traits_type> __ios_type;
376 typedef basic_iostream<char_type, traits_type> __iostream_type;
379 __filebuf_type _M_filebuf;
382 // Constructors/destructor:
384 : __iostream_type(NULL), _M_filebuf()
385 { this->init(&_M_filebuf); }
388 basic_fstream(const char* __s,
389 ios_base::openmode __mode = ios_base::in | ios_base::out)
390 : __iostream_type(NULL), _M_filebuf()
392 this->init(&_M_filebuf);
393 this->open(__s, __mode);
402 { return const_cast<__filebuf_type*>(&_M_filebuf); }
405 is_open(void) { return _M_filebuf.is_open(); }
408 open(const char* __s,
409 ios_base::openmode __mode = ios_base::in | ios_base::out)
411 if (!_M_filebuf.open(__s, __mode))
412 setstate(ios_base::failbit);
418 if (!_M_filebuf.close())
419 setstate(ios_base::failbit);
425 #ifdef _GLIBCPP_NO_TEMPLATE_EXPORT
427 #ifdef _GLIBCPP_FULLY_COMPLIANT_HEADERS
428 # include <bits/fstream.tcc>