1 // Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 2, or (at your option)
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING. If not, write to the Free
16 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // As a special exception, you may use this file as part of a free software
20 // library without restriction. Specifically, if other files instantiate
21 // templates or use macros or inline functions from this file, or you compile
22 // this file and link it with other files to produce an executable, this
23 // file does not by itself cause the resulting executable to be covered by
24 // the GNU General Public License. This exception does not however
25 // invalidate any other reasons why the executable file might be covered by
26 // the GNU General Public License.
29 // ISO C++ 14882: 27.6.2 Output streams
32 #include <bits/std_locale.h>
36 template<typename _CharT, typename _Traits>
37 basic_ostream<_CharT, _Traits>::sentry::
38 sentry(basic_ostream<_CharT,_Traits>& __os)
39 : _M_ok(__os.good()), _M_os(__os)
42 if (_M_ok && __os.tie())
46 template<typename _CharT, typename _Traits>
47 basic_ostream<_CharT, _Traits>&
48 basic_ostream<_CharT, _Traits>::
49 operator<<(__ostream_type& (*__pf)(__ostream_type&))
56 catch(exception& __fail)
58 // 27.6.2.5.1 Common requirements.
59 // Turn this on without causing an ios::failure to be thrown.
60 this->setstate(ios_base::badbit);
61 if ((this->exceptions() & ios_base::badbit) != 0)
62 __throw_exception_again;
68 template<typename _CharT, typename _Traits>
69 basic_ostream<_CharT, _Traits>&
70 basic_ostream<_CharT, _Traits>::
71 operator<<(__ios_type& (*__pf)(__ios_type&))
78 catch(exception& __fail)
80 // 27.6.2.5.1 Common requirements.
81 // Turn this on without causing an ios::failure to be thrown.
82 this->setstate(ios_base::badbit);
83 if ((this->exceptions() & ios_base::badbit) != 0)
84 __throw_exception_again;
90 template<typename _CharT, typename _Traits>
91 basic_ostream<_CharT, _Traits>&
92 basic_ostream<_CharT, _Traits>::
93 operator<<(ios_base& (*__pf)(ios_base&))
100 catch(exception& __fail)
102 // 27.6.2.5.1 Common requirements.
103 // Turn this on without causing an ios::failure to be thrown.
104 this->setstate(ios_base::badbit);
105 if ((this->exceptions() & ios_base::badbit) != 0)
106 __throw_exception_again;
112 template<typename _CharT, typename _Traits>
113 basic_ostream<_CharT, _Traits>&
114 basic_ostream<_CharT, _Traits>::operator<<(bool __n)
116 sentry __cerb(*this);
121 if (_M_check_facet(_M_fnumput))
122 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
123 this->setstate(ios_base::badbit);
125 catch(exception& __fail)
127 // 27.6.1.2.1 Common requirements.
128 // Turn this on without causing an ios::failure to be thrown.
129 this->setstate(ios_base::badbit);
130 if ((this->exceptions() & ios_base::badbit) != 0)
131 __throw_exception_again;
137 template<typename _CharT, typename _Traits>
138 basic_ostream<_CharT, _Traits>&
139 basic_ostream<_CharT, _Traits>::operator<<(long __n)
141 sentry __cerb(*this);
146 char_type __c = this->fill();
147 ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
148 if (_M_check_facet(_M_fnumput))
151 if (__fmt & ios_base::oct || __fmt & ios_base::hex)
153 unsigned long __l = static_cast<unsigned long>(__n);
154 __b = _M_fnumput->put(*this, *this, __c, __l).failed();
157 __b = _M_fnumput->put(*this, *this, __c, __n).failed();
159 this->setstate(ios_base::badbit);
162 catch(exception& __fail)
164 // 27.6.1.2.1 Common requirements.
165 // Turn this on without causing an ios::failure to be thrown.
166 this->setstate(ios_base::badbit);
167 if ((this->exceptions() & ios_base::badbit) != 0)
168 __throw_exception_again;
174 template<typename _CharT, typename _Traits>
175 basic_ostream<_CharT, _Traits>&
176 basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n)
178 sentry __cerb(*this);
183 if (_M_check_facet(_M_fnumput))
184 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
185 this->setstate(ios_base::badbit);
187 catch(exception& __fail)
189 // 27.6.1.2.1 Common requirements.
190 // Turn this on without causing an ios::failure to be thrown.
191 this->setstate(ios_base::badbit);
192 if ((this->exceptions() & ios_base::badbit) != 0)
193 __throw_exception_again;
199 #ifdef _GLIBCPP_USE_LONG_LONG
200 template<typename _CharT, typename _Traits>
201 basic_ostream<_CharT, _Traits>&
202 basic_ostream<_CharT, _Traits>::operator<<(long long __n)
204 sentry __cerb(*this);
209 char_type __c = this->fill();
210 ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
211 if (_M_check_facet(_M_fnumput))
214 if (__fmt & ios_base::oct || __fmt & ios_base::hex)
216 unsigned long long __l;
217 __l = static_cast<unsigned long long>(__n);
218 __b = _M_fnumput->put(*this, *this, __c, __l).failed();
221 __b = _M_fnumput->put(*this, *this, __c, __n).failed();
223 this->setstate(ios_base::badbit);
226 catch(exception& __fail)
228 // 27.6.1.2.1 Common requirements.
229 // Turn this on without causing an ios::failure to be thrown.
230 this->setstate(ios_base::badbit);
231 if ((this->exceptions() & ios_base::badbit) != 0)
232 __throw_exception_again;
238 template<typename _CharT, typename _Traits>
239 basic_ostream<_CharT, _Traits>&
240 basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n)
242 sentry __cerb(*this);
247 if (_M_check_facet(_M_fnumput))
248 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
249 this->setstate(ios_base::badbit);
251 catch(exception& __fail)
253 // 27.6.1.2.1 Common requirements.
254 // Turn this on without causing an ios::failure to be thrown.
255 this->setstate(ios_base::badbit);
256 if ((this->exceptions() & ios_base::badbit) != 0)
257 __throw_exception_again;
264 template<typename _CharT, typename _Traits>
265 basic_ostream<_CharT, _Traits>&
266 basic_ostream<_CharT, _Traits>::operator<<(double __n)
268 sentry __cerb(*this);
273 if (_M_check_facet(_M_fnumput))
274 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
275 this->setstate(ios_base::badbit);
277 catch(exception& __fail)
279 // 27.6.1.2.1 Common requirements.
280 // Turn this on without causing an ios::failure to be thrown.
281 this->setstate(ios_base::badbit);
282 if ((this->exceptions() & ios_base::badbit) != 0)
283 __throw_exception_again;
289 template<typename _CharT, typename _Traits>
290 basic_ostream<_CharT, _Traits>&
291 basic_ostream<_CharT, _Traits>::operator<<(long double __n)
293 sentry __cerb(*this);
298 if (_M_check_facet(_M_fnumput))
299 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
300 this->setstate(ios_base::badbit);
302 catch(exception& __fail)
304 // 27.6.1.2.1 Common requirements.
305 // Turn this on without causing an ios::failure to be thrown.
306 this->setstate(ios_base::badbit);
307 if ((this->exceptions() & ios_base::badbit) != 0)
308 __throw_exception_again;
314 template<typename _CharT, typename _Traits>
315 basic_ostream<_CharT, _Traits>&
316 basic_ostream<_CharT, _Traits>::operator<<(const void* __n)
318 sentry __cerb(*this);
323 if (_M_check_facet(_M_fnumput))
324 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
325 this->setstate(ios_base::badbit);
327 catch(exception& __fail)
329 // 27.6.1.2.1 Common requirements.
330 // Turn this on without causing an ios::failure to be thrown.
331 this->setstate(ios_base::badbit);
332 if ((this->exceptions() & ios_base::badbit) != 0)
333 __throw_exception_again;
339 template<typename _CharT, typename _Traits>
340 basic_ostream<_CharT, _Traits>&
341 basic_ostream<_CharT, _Traits>::operator<<(__streambuf_type* __sbin)
343 streamsize __xtrct = 0;
344 __streambuf_type* __sbout = this->rdbuf();
345 sentry __cerb(*this);
346 if (__sbin && __cerb)
347 __xtrct = __copy_streambufs(*this, __sbin, __sbout);
348 if (!__sbin || !__xtrct)
349 this->setstate(ios_base::failbit);
353 template<typename _CharT, typename _Traits>
354 basic_ostream<_CharT, _Traits>&
355 basic_ostream<_CharT, _Traits>::put(char_type __c)
357 sentry __cerb(*this);
360 int_type __put = rdbuf()->sputc(__c);
361 if (traits_type::eq_int_type(__put, traits_type::eof()))
362 this->setstate(ios_base::badbit);
367 template<typename _CharT, typename _Traits>
368 basic_ostream<_CharT, _Traits>&
369 basic_ostream<_CharT, _Traits>::write(const _CharT* __s, streamsize __n)
371 sentry __cerb(*this);
374 streamsize __put = this->rdbuf()->sputn(__s, __n);
376 this->setstate(ios_base::badbit);
381 template<typename _CharT, typename _Traits>
382 basic_ostream<_CharT, _Traits>&
383 basic_ostream<_CharT, _Traits>::flush()
385 sentry __cerb(*this);
388 if (this->rdbuf() && this->rdbuf()->pubsync() == -1)
389 this->setstate(ios_base::badbit);
394 template<typename _CharT, typename _Traits>
395 typename basic_ostream<_CharT, _Traits>::pos_type
396 basic_ostream<_CharT, _Traits>::tellp()
398 pos_type __ret = pos_type(-1);
399 bool __testok = this->fail() != true;
402 __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
407 template<typename _CharT, typename _Traits>
408 basic_ostream<_CharT, _Traits>&
409 basic_ostream<_CharT, _Traits>::seekp(pos_type __pos)
411 bool __testok = this->fail() != true;
415 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
416 // 136. seekp, seekg setting wrong streams?
417 pos_type __err = this->rdbuf()->pubseekpos(__pos, ios_base::out);
419 // 129. Need error indication from seekp() and seekg()
420 if (__err == pos_type(off_type(-1)))
421 this->setstate(failbit);
427 template<typename _CharT, typename _Traits>
428 basic_ostream<_CharT, _Traits>&
429 basic_ostream<_CharT, _Traits>::
430 seekp(off_type __off, ios_base::seekdir __d)
432 bool __testok = this->fail() != true;
436 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
437 // 136. seekp, seekg setting wrong streams?
438 pos_type __err = this->rdbuf()->pubseekoff(__off, __d,
441 // 129. Need error indication from seekp() and seekg()
442 if (__err == pos_type(off_type(-1)))
443 this->setstate(failbit);
449 // 27.6.2.5.4 Character inserters
451 // Construct correctly padded string, as per 22.2.2.2.2
452 // Similar in theory to __pad_numeric, from num_put, but it doesn't
453 // use _S_fill: perhaps it should.
455 // __newlen > __oldlen
456 // __news is allocated for __newlen size
457 template<typename _CharT, typename _Traits>
459 __pad_char(basic_ios<_CharT, _Traits>& __ios,
460 _CharT* __news, const _CharT* __olds,
461 const streamsize __newlen, const streamsize __oldlen)
463 typedef _CharT char_type;
464 typedef _Traits traits_type;
465 typedef typename traits_type::int_type int_type;
467 int_type __plen = static_cast<size_t>(__newlen - __oldlen);
468 char_type* __pads = static_cast<char_type*>(__builtin_alloca(sizeof(char_type) * __plen));
469 traits_type::assign(__pads, __plen, __ios.fill());
474 size_t __beglen; //either __plen or __oldlen
475 ios_base::fmtflags __fmt = __ios.flags() & ios_base::adjustfield;
477 if (__fmt == ios_base::left)
480 __beg = const_cast<char_type*>(__olds);
484 else if (__fmt == ios_base::internal)
486 // Pad after the sign, if there is one.
487 // Pad after 0[xX], if there is one.
488 // Who came up with these rules, anyway? Jeeze.
489 typedef _Format_cache<_CharT> __cache_type;
490 __cache_type const* __fmt = __cache_type::_S_get(__ios);
491 const char_type* __minus = traits_type::find(__olds, __oldlen,
493 const char_type* __plus = traits_type::find(__olds, __oldlen,
495 bool __testsign = __minus || __plus;
496 bool __testhex = __olds[0] == '0'
497 && (__olds[1] == 'x' || __olds[1] == 'X');
501 __news[0] = __olds[0];
502 __news[1] = __olds[1];
504 __beg = const_cast<char_type*>(__olds + __mod);
505 __beglen = __oldlen - __mod;
511 const char_type* __sign = __minus ? __minus + 1: __plus + 1;
512 __beg = const_cast<char_type*>(__olds);
513 __beglen = __sign - __olds;
514 __end = const_cast<char_type*>(__sign + __plen);
515 traits_type::copy(__news + __beglen, __pads, __plen);
522 __end = const_cast<char_type*>(__olds);
530 __end = const_cast<char_type*>(__olds);
533 traits_type::copy(__news, __beg, __beglen);
534 traits_type::copy(__news + __beglen, __end, __newlen - __beglen - __mod);
537 template<typename _CharT, typename _Traits>
538 basic_ostream<_CharT, _Traits>&
539 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
541 typedef basic_ostream<_CharT, _Traits> __ostream_type;
542 typename __ostream_type::sentry __cerb(__out);
547 streamsize __w = __out.width();
548 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
550 streamsize __len = 1;
553 __pad_char(__out, __pads, &__c, __w, __len);
556 __out.write(__pads, __len);
559 catch(exception& __fail)
561 // 27.6.1.2.1 Common requirements.
562 // Turn this on without causing an ios::failure to be thrown.
563 __out.setstate(ios_base::badbit);
564 if ((__out.exceptions() & ios_base::badbit) != 0)
565 __throw_exception_again;
572 template <class _Traits>
573 basic_ostream<char, _Traits>&
574 operator<<(basic_ostream<char, _Traits>& __out, char __c)
576 typedef basic_ostream<char, _Traits> __ostream_type;
577 typename __ostream_type::sentry __cerb(__out);
582 streamsize __w = __out.width();
583 char* __pads = static_cast<char*>(__builtin_alloca(__w + 1));
585 streamsize __len = 1;
588 __pad_char(__out, __pads, &__c, __w, __len);
591 __out.write(__pads, __len);
594 catch(exception& __fail)
596 // 27.6.1.2.1 Common requirements.
597 // Turn this on without causing an ios::failure to be thrown.
598 __out.setstate(ios_base::badbit);
599 if ((__out.exceptions() & ios_base::badbit) != 0)
600 __throw_exception_again;
606 template<typename _CharT, typename _Traits>
607 basic_ostream<_CharT, _Traits>&
608 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
610 typedef basic_ostream<_CharT, _Traits> __ostream_type;
611 typename __ostream_type::sentry __cerb(__out);
616 streamsize __w = __out.width();
617 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
618 streamsize __len = static_cast<streamsize>(_Traits::length(__s));
621 __pad_char(__out, __pads, __s, __w, __len);
625 __out.write(__s, __len);
628 catch(exception& __fail)
630 // 27.6.1.2.1 Common requirements.
631 // Turn this on without causing an ios::failure to be thrown.
632 __out.setstate(ios_base::badbit);
633 if ((__out.exceptions() & ios_base::badbit) != 0)
634 __throw_exception_again;
640 template<typename _CharT, typename _Traits>
641 basic_ostream<_CharT, _Traits>&
642 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
644 typedef basic_ostream<_CharT, _Traits> __ostream_type;
645 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
646 // 167. Improper use of traits_type::length()
647 typedef char_traits<char> __ctraits_type;
649 typename __ostream_type::sentry __cerb(__out);
652 size_t __clen = __ctraits_type::length(__s);
653 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * (__clen + 1)));
654 for (size_t __i = 0; __i <= __clen; ++__i)
655 __ws[__i] = __out.widen(__s[__i]);
656 _CharT* __str = __ws;
660 streamsize __len = static_cast<streamsize>(__clen);
661 streamsize __w = __out.width();
662 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
666 __pad_char(__out, __pads, __ws, __w, __len);
670 __out.write(__str, __len);
673 catch(exception& __fail)
675 // 27.6.1.2.1 Common requirements.
676 // Turn this on without causing an ios::failure to be thrown.
677 __out.setstate(ios_base::badbit);
678 if ((__out.exceptions() & ios_base::badbit) != 0)
679 __throw_exception_again;
685 // Partial specializationss
686 template<class _Traits>
687 basic_ostream<char, _Traits>&
688 operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
690 typedef basic_ostream<char, _Traits> __ostream_type;
691 typename __ostream_type::sentry __cerb(__out);
696 streamsize __w = __out.width();
697 char* __pads = static_cast<char*>(__builtin_alloca(__w));
698 streamsize __len = static_cast<streamsize>(_Traits::length(__s));
701 __pad_char(__out, __pads, __s, __w, __len);
705 __out.write(__s, __len);
708 catch(exception& __fail)
710 // 27.6.1.2.1 Common requirements.
711 // Turn this on without causing an ios::failure to be thrown.
712 __out.setstate(ios_base::badbit);
713 if ((__out.exceptions() & ios_base::badbit) != 0)
714 __throw_exception_again;
720 // 21.3.7.9 basic_string::operator<<
721 template<typename _CharT, typename _Traits, typename _Alloc>
722 basic_ostream<_CharT, _Traits>&
723 operator<<(basic_ostream<_CharT, _Traits>& __out,
724 const basic_string<_CharT, _Traits, _Alloc>& __str)
726 typedef basic_ostream<_CharT, _Traits> __ostream_type;
727 typename __ostream_type::sentry __cerb(__out);
730 const _CharT* __s = __str.data();
731 streamsize __w = __out.width();
732 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
733 streamsize __len = static_cast<streamsize>(__str.size());
734 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
735 // 25. String operator<< uses width() value wrong
739 __pad_char(__out, __pads, __s, __w, __len);
743 streamsize __res = __out.rdbuf()->sputn(__s, __len);
746 __out.setstate(ios_base::failbit);