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 (__put != traits_type::to_int_type(__c))
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;
414 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
415 // 136. seekp, seekg setting wrong streams?
416 this->rdbuf()->pubseekpos(__pos, ios_base::out);
421 template<typename _CharT, typename _Traits>
422 basic_ostream<_CharT, _Traits>&
423 basic_ostream<_CharT, _Traits>::
424 seekp(off_type __off, ios_base::seekdir __d)
426 bool __testok = this->fail() != true;
429 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
430 // 136. seekp, seekg setting wrong streams?
431 this->rdbuf()->pubseekoff(__off, __d, ios_base::out);
436 // 27.6.2.5.4 Character inserters
438 // Construct correctly padded string, as per 22.2.2.2.2
439 // Similar in theory to __pad_numeric, from num_put, but it doesn't
440 // use _S_fill: perhaps it should.
442 // __newlen > __oldlen
443 // __news is allocated for __newlen size
444 template<typename _CharT, typename _Traits>
446 __pad_char(basic_ios<_CharT, _Traits>& __ios,
447 _CharT* __news, const _CharT* __olds,
448 const streamsize __newlen, const streamsize __oldlen)
450 typedef _CharT char_type;
451 typedef _Traits traits_type;
452 typedef typename traits_type::int_type int_type;
454 int_type __plen = static_cast<size_t>(__newlen - __oldlen);
455 char_type* __pads = static_cast<char_type*>(__builtin_alloca(sizeof(char_type) * __plen));
456 traits_type::assign(__pads, __plen, __ios.fill());
461 size_t __beglen; //either __plen or __oldlen
462 ios_base::fmtflags __fmt = __ios.flags() & ios_base::adjustfield;
464 if (__fmt == ios_base::left)
467 __beg = const_cast<char_type*>(__olds);
471 else if (__fmt == ios_base::internal)
473 // Pad after the sign, if there is one.
474 // Pad after 0[xX], if there is one.
475 // Who came up with these rules, anyway? Jeeze.
476 typedef _Format_cache<_CharT> __cache_type;
477 __cache_type const* __fmt = __cache_type::_S_get(__ios);
478 const char_type* __minus = traits_type::find(__olds, __oldlen,
480 const char_type* __plus = traits_type::find(__olds, __oldlen,
482 bool __testsign = __minus || __plus;
483 bool __testhex = __olds[0] == '0'
484 && (__olds[1] == 'x' || __olds[1] == 'X');
488 __news[0] = __olds[0];
489 __news[1] = __olds[1];
491 __beg = const_cast<char_type*>(__olds + __mod);
492 __beglen = __oldlen - __mod;
498 const char_type* __sign = __minus ? __minus + 1: __plus + 1;
499 __beg = const_cast<char_type*>(__olds);
500 __beglen = __sign - __olds;
501 __end = const_cast<char_type*>(__sign + __plen);
502 traits_type::copy(__news + __beglen, __pads, __plen);
509 __end = const_cast<char_type*>(__olds);
517 __end = const_cast<char_type*>(__olds);
520 traits_type::copy(__news, __beg, __beglen);
521 traits_type::copy(__news + __beglen, __end, __newlen - __beglen - __mod);
524 template<typename _CharT, typename _Traits>
525 basic_ostream<_CharT, _Traits>&
526 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
528 typedef basic_ostream<_CharT, _Traits> __ostream_type;
529 typename __ostream_type::sentry __cerb(__out);
534 streamsize __w = __out.width();
535 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
537 streamsize __len = 1;
540 __pad_char(__out, __pads, &__c, __w, __len);
543 __out.write(__pads, __len);
546 catch(exception& __fail)
548 // 27.6.1.2.1 Common requirements.
549 // Turn this on without causing an ios::failure to be thrown.
550 __out.setstate(ios_base::badbit);
551 if ((__out.exceptions() & ios_base::badbit) != 0)
552 __throw_exception_again;
559 template <class _Traits>
560 basic_ostream<char, _Traits>&
561 operator<<(basic_ostream<char, _Traits>& __out, char __c)
563 typedef basic_ostream<char, _Traits> __ostream_type;
564 typename __ostream_type::sentry __cerb(__out);
569 streamsize __w = __out.width();
570 char* __pads = static_cast<char*>(__builtin_alloca(__w + 1));
572 streamsize __len = 1;
575 __pad_char(__out, __pads, &__c, __w, __len);
578 __out.write(__pads, __len);
581 catch(exception& __fail)
583 // 27.6.1.2.1 Common requirements.
584 // Turn this on without causing an ios::failure to be thrown.
585 __out.setstate(ios_base::badbit);
586 if ((__out.exceptions() & ios_base::badbit) != 0)
587 __throw_exception_again;
593 template<typename _CharT, typename _Traits>
594 basic_ostream<_CharT, _Traits>&
595 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
597 typedef basic_ostream<_CharT, _Traits> __ostream_type;
598 typename __ostream_type::sentry __cerb(__out);
603 streamsize __w = __out.width();
604 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
605 streamsize __len = static_cast<streamsize>(_Traits::length(__s));
608 __pad_char(__out, __pads, __s, __w, __len);
612 __out.write(__s, __len);
615 catch(exception& __fail)
617 // 27.6.1.2.1 Common requirements.
618 // Turn this on without causing an ios::failure to be thrown.
619 __out.setstate(ios_base::badbit);
620 if ((__out.exceptions() & ios_base::badbit) != 0)
621 __throw_exception_again;
627 template<typename _CharT, typename _Traits>
628 basic_ostream<_CharT, _Traits>&
629 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
631 typedef basic_ostream<_CharT, _Traits> __ostream_type;
632 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
633 // 167. Improper use of traits_type::length()
634 typedef char_traits<char> __ctraits_type;
636 typename __ostream_type::sentry __cerb(__out);
639 size_t __clen = __ctraits_type::length(__s);
640 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * (__clen + 1)));
641 for (size_t __i = 0; __i <= __clen; ++__i)
642 __ws[__i] = __out.widen(__s[__i]);
643 _CharT* __str = __ws;
647 streamsize __len = static_cast<streamsize>(__clen);
648 streamsize __w = __out.width();
649 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
653 __pad_char(__out, __pads, __ws, __w, __len);
657 __out.write(__str, __len);
660 catch(exception& __fail)
662 // 27.6.1.2.1 Common requirements.
663 // Turn this on without causing an ios::failure to be thrown.
664 __out.setstate(ios_base::badbit);
665 if ((__out.exceptions() & ios_base::badbit) != 0)
666 __throw_exception_again;
672 // Partial specializationss
673 template<class _Traits>
674 basic_ostream<char, _Traits>&
675 operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
677 typedef basic_ostream<char, _Traits> __ostream_type;
678 typename __ostream_type::sentry __cerb(__out);
683 streamsize __w = __out.width();
684 char* __pads = static_cast<char*>(__builtin_alloca(__w));
685 streamsize __len = static_cast<streamsize>(_Traits::length(__s));
688 __pad_char(__out, __pads, __s, __w, __len);
692 __out.write(__s, __len);
695 catch(exception& __fail)
697 // 27.6.1.2.1 Common requirements.
698 // Turn this on without causing an ios::failure to be thrown.
699 __out.setstate(ios_base::badbit);
700 if ((__out.exceptions() & ios_base::badbit) != 0)
701 __throw_exception_again;
707 // 21.3.7.9 basic_string::operator<<
708 template<typename _CharT, typename _Traits, typename _Alloc>
709 basic_ostream<_CharT, _Traits>&
710 operator<<(basic_ostream<_CharT, _Traits>& __out,
711 const basic_string<_CharT, _Traits, _Alloc>& __str)
713 typedef basic_ostream<_CharT, _Traits> __ostream_type;
714 typename __ostream_type::sentry __cerb(__out);
717 const _CharT* __s = __str.data();
718 streamsize __w = __out.width();
719 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
720 streamsize __len = static_cast<streamsize>(__str.size());
721 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
722 // 25. String operator<< uses width() value wrong
726 __pad_char(__out, __pads, __s, __w, __len);
730 streamsize __res = __out.rdbuf()->sputn(__s, __len);
733 __out.setstate(ios_base::failbit);