]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/fstream.tcc
Move from CPP to CXX.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / fstream.tcc
1 // File based streams -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
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.
11
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.
16
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.
21
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.
30
31 //
32 // ISO C++ 14882: 27.8 File-based streams
33 //
34
35 #ifndef _FSTREAM_TCC
36 #define _FSTREAM_TCC 1
37
38 #pragma GCC system_header
39
40 namespace std
41 {
42 template<typename _CharT, typename _Traits>
43 void
44 basic_filebuf<_CharT, _Traits>::
45 _M_allocate_internal_buffer()
46 {
47 if (!_M_buf_allocated && this->_M_buf_size)
48 {
49 // Allocate internal buffer.
50 this->_M_buf = new char_type[this->_M_buf_size];
51 _M_buf_allocated = true;
52 }
53 }
54
55 // Both close and setbuf need to deallocate internal buffers, if it exists.
56 template<typename _CharT, typename _Traits>
57 void
58 basic_filebuf<_CharT, _Traits>::
59 _M_destroy_internal_buffer() throw()
60 {
61 if (_M_buf_allocated)
62 {
63 delete [] this->_M_buf;
64 this->_M_buf = NULL;
65 _M_buf_allocated = false;
66 }
67 }
68
69 template<typename _CharT, typename _Traits>
70 basic_filebuf<_CharT, _Traits>::
71 basic_filebuf() : __streambuf_type(), _M_file(&_M_lock),
72 _M_mode(ios_base::openmode(0)), _M_state_cur(__state_type()),
73 _M_state_beg(__state_type()), _M_buf(NULL), _M_buf_size(BUFSIZ),
74 _M_buf_allocated(false), _M_reading(false), _M_writing(false),
75 _M_last_overflowed(false), _M_pback_cur_save(0), _M_pback_end_save(0),
76 _M_pback_init(false), _M_codecvt(0)
77 {
78 if (has_facet<__codecvt_type>(this->_M_buf_locale))
79 _M_codecvt = &use_facet<__codecvt_type>(this->_M_buf_locale);
80 }
81
82 template<typename _CharT, typename _Traits>
83 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
84 basic_filebuf<_CharT, _Traits>::
85 open(const char* __s, ios_base::openmode __mode)
86 {
87 __filebuf_type *__ret = NULL;
88 if (!this->is_open())
89 {
90 _M_file.open(__s, __mode);
91 if (this->is_open())
92 {
93 _M_allocate_internal_buffer();
94 this->_M_mode = __mode;
95
96 // Setup initial buffer to 'uncommitted' mode.
97 _M_reading = false;
98 _M_writing = false;
99 _M_set_buffer(-1);
100
101 // 27.8.1.3,4
102 if ((__mode & ios_base::ate)
103 && this->seekoff(0, ios_base::end, __mode) < 0)
104 this->close();
105 else
106 __ret = this;
107 }
108 }
109 return __ret;
110 }
111
112 template<typename _CharT, typename _Traits>
113 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
114 basic_filebuf<_CharT, _Traits>::
115 close() throw()
116 {
117 __filebuf_type* __ret = NULL;
118 if (this->is_open())
119 {
120 bool __testfail = false;
121 try
122 {
123 if (this->pbase() < this->pptr()
124 && traits_type::eq_int_type(this->overflow(),
125 traits_type::eof()))
126 __testfail = true;
127 #if 0
128 // XXX not done
129 if (_M_last_overflowed)
130 {
131 _M_output_unshift();
132 this->overflow();
133 }
134 #endif
135 }
136 catch(...)
137 {
138 __testfail = true;
139 }
140
141 // NB: Do this here so that re-opened filebufs will be cool...
142 this->_M_mode = ios_base::openmode(0);
143 this->_M_pback_init = false;
144 _M_destroy_internal_buffer();
145 _M_reading = false;
146 _M_writing = false;
147 _M_set_buffer(-1);
148
149 if (!_M_file.close())
150 __testfail = true;
151
152 if (!__testfail)
153 __ret = this;
154 }
155 _M_last_overflowed = false;
156 return __ret;
157 }
158
159 template<typename _CharT, typename _Traits>
160 streamsize
161 basic_filebuf<_CharT, _Traits>::
162 showmanyc()
163 {
164 streamsize __ret = -1;
165 const bool __testin = this->_M_mode & ios_base::in;
166
167 if (__testin && this->is_open())
168 {
169 // For a stateful encoding (-1) the pending sequence might be just
170 // shift and unshift prefixes with no actual character.
171 __ret = this->egptr() - this->gptr();
172 if (__check_facet(_M_codecvt).encoding() >= 0)
173 __ret += _M_file.showmanyc() / _M_codecvt->max_length();
174 }
175
176 _M_last_overflowed = false;
177 return __ret;
178 }
179
180 template<typename _CharT, typename _Traits>
181 typename basic_filebuf<_CharT, _Traits>::int_type
182 basic_filebuf<_CharT, _Traits>::
183 underflow()
184 {
185 int_type __ret = traits_type::eof();
186 const bool __testin = this->_M_mode & ios_base::in;
187 const bool __testout = this->_M_mode & ios_base::out;
188
189 if (__testin && !_M_writing)
190 {
191 // Check for pback madness, and if so swich back to the
192 // normal buffers and jet outta here before expensive
193 // fileops happen...
194 _M_destroy_pback();
195
196 if (this->gptr() < this->egptr())
197 return traits_type::to_int_type(*this->gptr());
198
199 // Get and convert input sequence.
200 const size_t __buflen = this->_M_buf_size > 1
201 ? this->_M_buf_size - 1 : 1;
202 streamsize __elen = 0;
203 streamsize __ilen = 0;
204 if (__check_facet(_M_codecvt).always_noconv())
205 {
206 __elen = _M_file.xsgetn(reinterpret_cast<char*>(this->eback()),
207 __buflen);
208 __ilen = __elen;
209 }
210 else
211 {
212 // Worst-case number of external bytes.
213 // XXX Not done encoding() == -1.
214 const int __enc = _M_codecvt->encoding();
215 const streamsize __blen = __enc > 0 ? __buflen * __enc : __buflen;
216 char* __buf = static_cast<char*>(__builtin_alloca(__blen));
217 __elen = _M_file.xsgetn(__buf, __blen);
218
219 const char* __eend;
220 char_type* __iend;
221 codecvt_base::result __r;
222 __r = _M_codecvt->in(_M_state_cur, __buf, __buf + __elen,
223 __eend, this->eback(),
224 this->eback() + __buflen, __iend);
225 if (__r == codecvt_base::ok || __r == codecvt_base::partial)
226 __ilen = __iend - this->eback();
227 else if (__r == codecvt_base::noconv)
228 {
229 traits_type::copy(this->eback(),
230 reinterpret_cast<char_type*>(__buf),
231 __elen);
232 __ilen = __elen;
233 }
234 else
235 {
236 // Unwind.
237 __ilen = 0;
238 _M_file.seekoff(-__elen, ios_base::cur, ios_base::in);
239 }
240 }
241
242 if (__ilen > 0)
243 {
244 _M_set_buffer(__ilen);
245 _M_reading = true;
246 __ret = traits_type::to_int_type(*this->gptr());
247 }
248 else if (__elen == 0)
249 {
250 // If the actual end of file is reached, set 'uncommitted'
251 // mode, thus allowing an immediate write without an
252 // intervening seek.
253 _M_set_buffer(-1);
254 _M_reading = false;
255 }
256 }
257 _M_last_overflowed = false;
258 return __ret;
259 }
260
261 template<typename _CharT, typename _Traits>
262 typename basic_filebuf<_CharT, _Traits>::int_type
263 basic_filebuf<_CharT, _Traits>::
264 pbackfail(int_type __i)
265 {
266 int_type __ret = traits_type::eof();
267 const bool __testin = this->_M_mode & ios_base::in;
268
269 if (__testin && !_M_writing)
270 {
271 // Remember whether the pback buffer is active, otherwise below
272 // we may try to store in it a second char (libstdc++/9761).
273 const bool __testpb = this->_M_pback_init;
274 const bool __testeof = traits_type::eq_int_type(__i, __ret);
275
276 int_type __tmp;
277 if (this->eback() < this->gptr())
278 {
279 this->gbump(-1);
280 __tmp = traits_type::to_int_type(*this->gptr());
281 }
282 else if (this->seekoff(-1, ios_base::cur) >= 0)
283 {
284 __tmp = this->underflow();
285 if (traits_type::eq_int_type(__tmp, __ret))
286 return __ret;
287 }
288 else
289 {
290 // At the beginning of the buffer, need to make a
291 // putback position available. But the seek may fail
292 // (f.i., at the beginning of a file, see
293 // libstdc++/9439) and in that case we return
294 // traits_type::eof().
295 return __ret;
296 }
297
298 // Try to put back __i into input sequence in one of three ways.
299 // Order these tests done in is unspecified by the standard.
300 if (!__testeof && traits_type::eq_int_type(__i, __tmp))
301 __ret = __i;
302 else if (__testeof)
303 __ret = traits_type::not_eof(__i);
304 else if (!__testpb)
305 {
306 _M_create_pback();
307 _M_reading = true;
308 *this->gptr() = traits_type::to_char_type(__i);
309 __ret = __i;
310 }
311 }
312 _M_last_overflowed = false;
313 return __ret;
314 }
315
316 template<typename _CharT, typename _Traits>
317 typename basic_filebuf<_CharT, _Traits>::int_type
318 basic_filebuf<_CharT, _Traits>::
319 overflow(int_type __c)
320 {
321 int_type __ret = traits_type::eof();
322 const bool __testeof = traits_type::eq_int_type(__c, __ret);
323 const bool __testout = this->_M_mode & ios_base::out;
324
325 if (__testout && !_M_reading)
326 {
327 if (this->pbase() < this->pptr())
328 {
329 // If appropriate, append the overflow char.
330 if (!__testeof)
331 {
332 *this->pptr() = traits_type::to_char_type(__c);
333 this->pbump(1);
334 }
335
336 // Convert pending sequence to external representation,
337 // output.
338 if (_M_convert_to_external(this->pbase(),
339 this->pptr() - this->pbase())
340 && (!__testeof || (__testeof && !_M_file.sync())))
341 {
342 _M_set_buffer(0);
343 __ret = traits_type::not_eof(__c);
344 }
345 }
346 else if (this->_M_buf_size > 1)
347 {
348 // Overflow in 'uncommitted' mode: set _M_writing, set
349 // the buffer to the initial 'write' mode, and put __c
350 // into the buffer.
351 _M_set_buffer(0);
352 _M_writing = true;
353 if (!__testeof)
354 {
355 *this->pptr() = traits_type::to_char_type(__c);
356 this->pbump(1);
357 }
358 __ret = traits_type::not_eof(__c);
359 }
360 else
361 {
362 // Unbuffered.
363 char_type __conv = traits_type::to_char_type(__c);
364 if (__testeof || _M_convert_to_external(&__conv, 1))
365 {
366 _M_writing = true;
367 __ret = traits_type::not_eof(__c);
368 }
369 }
370 }
371 _M_last_overflowed = true;
372 return __ret;
373 }
374
375 template<typename _CharT, typename _Traits>
376 bool
377 basic_filebuf<_CharT, _Traits>::
378 _M_convert_to_external(_CharT* __ibuf, streamsize __ilen)
379 {
380 // Sizes of external and pending output.
381 streamsize __elen = 0;
382 streamsize __plen = 0;
383
384 if (__check_facet(_M_codecvt).always_noconv())
385 {
386 __elen += _M_file.xsputn(reinterpret_cast<char*>(__ibuf), __ilen);
387 __plen += __ilen;
388 }
389 else
390 {
391 // Worst-case number of external bytes needed.
392 // XXX Not done encoding() == -1.
393 streamsize __blen = __ilen * _M_codecvt->max_length();
394 char* __buf = static_cast<char*>(__builtin_alloca(__blen));
395
396 char* __bend;
397 const char_type* __iend;
398 codecvt_base::result __r;
399 __r = _M_codecvt->out(_M_state_cur, __ibuf, __ibuf + __ilen,
400 __iend, __buf, __buf + __blen, __bend);
401
402 if (__r == codecvt_base::ok || __r == codecvt_base::partial)
403 __blen = __bend - __buf;
404 else if (__r == codecvt_base::noconv)
405 {
406 // Same as the always_noconv case above.
407 __buf = reinterpret_cast<char*>(__ibuf);
408 __blen = __ilen;
409 }
410 else
411 {
412 // Result == error.
413 __blen = 0;
414 }
415
416 if (__blen)
417 {
418 __elen += _M_file.xsputn(__buf, __blen);
419 __plen += __blen;
420 }
421
422 // Try once more for partial conversions.
423 if (__r == codecvt_base::partial)
424 {
425 const char_type* __iresume = __iend;
426 streamsize __rlen = this->pptr() - __iend;
427 __r = _M_codecvt->out(_M_state_cur, __iresume,
428 __iresume + __rlen, __iend, __buf,
429 __buf + __blen, __bend);
430 if (__r != codecvt_base::error)
431 {
432 __rlen = __bend - __buf;
433 __elen += _M_file.xsputn(__buf, __rlen);
434 __plen += __rlen;
435 }
436 }
437 }
438 return __elen && __elen == __plen;
439 }
440
441 template<typename _CharT, typename _Traits>
442 typename basic_filebuf<_CharT, _Traits>::__streambuf_type*
443 basic_filebuf<_CharT, _Traits>::
444 setbuf(char_type* __s, streamsize __n)
445 {
446 if (!this->is_open() && __s == 0 && __n == 0)
447 this->_M_buf_size = 1;
448 else if (__s && __n > 0)
449 {
450 // This is implementation-defined behavior, and assumes that
451 // an external char_type array of length __n exists and has
452 // been pre-allocated. If this is not the case, things will
453 // quickly blow up. When __n > 1, __n - 1 positions will be
454 // used for the get area, __n - 1 for the put area and 1
455 // position to host the overflow char of a full put area.
456 // When __n == 1, 1 position will be used for the get area
457 // and 0 for the put area, as in the unbuffered case above.
458
459 // Step 1: Destroy the current internal array.
460 _M_destroy_internal_buffer();
461
462 // Step 2: Use the external array.
463 this->_M_buf = __s;
464 this->_M_buf_size = __n;
465 _M_reading = false;
466 _M_writing = false;
467 _M_set_buffer(-1);
468 }
469 _M_last_overflowed = false;
470 return this;
471 }
472
473 template<typename _CharT, typename _Traits>
474 typename basic_filebuf<_CharT, _Traits>::pos_type
475 basic_filebuf<_CharT, _Traits>::
476 seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode)
477 {
478 pos_type __ret = pos_type(off_type(-1));
479 const bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
480 const bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
481
482 int __width = 0;
483 if (_M_codecvt)
484 __width = _M_codecvt->encoding();
485 if (__width < 0)
486 __width = 0;
487
488 const bool __testfail = __off != 0 && __width <= 0;
489 if (this->is_open() && !__testfail && (__testin || __testout))
490 {
491 // Ditch any pback buffers to avoid confusion.
492 _M_destroy_pback();
493
494 off_type __computed_off = __off;
495 if (this->pbase() < this->pptr())
496 {
497 // Part one: update the output sequence.
498 this->sync();
499
500 // Part two: output unshift sequence.
501 _M_output_unshift();
502 }
503 else if (_M_reading && __way == ios_base::cur)
504 __computed_off += this->gptr() - this->egptr();
505
506 // Returns pos_type(off_type(-1)) in case of failure.
507 __ret = _M_file.seekoff(__computed_off * __width, __way, __mode);
508
509 _M_reading = false;
510 _M_writing = false;
511 _M_set_buffer(-1);
512 }
513 _M_last_overflowed = false;
514 return __ret;
515 }
516
517 template<typename _CharT, typename _Traits>
518 typename basic_filebuf<_CharT, _Traits>::pos_type
519 basic_filebuf<_CharT, _Traits>::
520 seekpos(pos_type __pos, ios_base::openmode __mode)
521 {
522 #ifdef _GLIBCXX_RESOLVE_LIB_DEFECTS
523 // 171. Strange seekpos() semantics due to joint position
524 pos_type __ret = pos_type(off_type(-1));
525
526 int __width = 0;
527 if (_M_codecvt)
528 __width = _M_codecvt->encoding();
529 if (__width > 0)
530 __ret = this->seekoff(off_type(__pos) / __width, ios_base::beg, __mode);
531
532 return __ret;
533 #endif
534 }
535
536 template<typename _CharT, typename _Traits>
537 void
538 basic_filebuf<_CharT, _Traits>::
539 _M_output_unshift()
540 { }
541
542 template<typename _CharT, typename _Traits>
543 void
544 basic_filebuf<_CharT, _Traits>::
545 imbue(const locale& __loc)
546 {
547 const bool __testbeg = !this->seekoff(0, ios_base::cur, this->_M_mode);
548 const bool __teststate = __check_facet(_M_codecvt).encoding() == -1;
549
550 if (this->_M_buf_locale != __loc
551 && (!this->is_open() || (__testbeg && !__teststate)))
552 {
553 this->_M_buf_locale = __loc;
554 if (__builtin_expect(has_facet<__codecvt_type>(__loc), true))
555 _M_codecvt = &use_facet<__codecvt_type>(__loc);
556
557 // NB This may require the reconversion of previously
558 // converted chars. This in turn may cause the
559 // reconstruction of the original file. YIKES!! This
560 // implementation interprets this requirement as requiring
561 // the file position be at the beginning, and a stateless
562 // encoding, or that the filebuf be closed. Opinions may differ.
563 }
564 _M_last_overflowed = false;
565 }
566
567 // Inhibit implicit instantiations for required instantiations,
568 // which are defined via explicit instantiations elsewhere.
569 // NB: This syntax is a GNU extension.
570 #if _GLIBCXX_EXTERN_TEMPLATE
571 extern template class basic_filebuf<char>;
572 extern template class basic_ifstream<char>;
573 extern template class basic_ofstream<char>;
574 extern template class basic_fstream<char>;
575
576 #ifdef _GLIBCXX_USE_WCHAR_T
577 extern template class basic_filebuf<wchar_t>;
578 extern template class basic_ifstream<wchar_t>;
579 extern template class basic_ofstream<wchar_t>;
580 extern template class basic_fstream<wchar_t>;
581 #endif
582 #endif
583 } // namespace std
584
585 #endif