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_fnumput->put(*this, *this, this->fill(), __n).failed())
122 this->setstate(ios_base::badbit);
124 catch(exception& __fail)
126 // 27.6.1.2.1 Common requirements.
127 // Turn this on without causing an ios::failure to be thrown.
128 this->setstate(ios_base::badbit);
129 if ((this->exceptions() & ios_base::badbit) != 0)
130 __throw_exception_again;
136 template<typename _CharT, typename _Traits>
137 basic_ostream<_CharT, _Traits>&
138 basic_ostream<_CharT, _Traits>::operator<<(long __n)
140 sentry __cerb(*this);
146 ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
147 if (__fmt & ios_base::oct || __fmt & ios_base::hex)
148 __f = _M_fnumput->put(*this, *this, this->fill(),
149 static_cast<unsigned long>(__n)).failed();
151 __f = _M_fnumput->put(*this, *this, this->fill(), __n).failed();
154 this->setstate(ios_base::badbit);
156 catch(exception& __fail)
158 // 27.6.1.2.1 Common requirements.
159 // Turn this on without causing an ios::failure to be thrown.
160 this->setstate(ios_base::badbit);
161 if ((this->exceptions() & ios_base::badbit) != 0)
162 __throw_exception_again;
168 template<typename _CharT, typename _Traits>
169 basic_ostream<_CharT, _Traits>&
170 basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n)
172 sentry __cerb(*this);
177 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
178 this->setstate(ios_base::badbit);
180 catch(exception& __fail)
182 // 27.6.1.2.1 Common requirements.
183 // Turn this on without causing an ios::failure to be thrown.
184 this->setstate(ios_base::badbit);
185 if ((this->exceptions() & ios_base::badbit) != 0)
186 __throw_exception_again;
192 #ifdef _GLIBCPP_USE_LONG_LONG
193 template<typename _CharT, typename _Traits>
194 basic_ostream<_CharT, _Traits>&
195 basic_ostream<_CharT, _Traits>::operator<<(long long __n)
197 sentry __cerb(*this);
203 ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
204 if (__fmt & ios_base::oct || __fmt & ios_base::hex)
205 __f = _M_fnumput->put(*this, *this, this->fill(),
206 static_cast<unsigned long long>(__n)).failed();
208 __f = _M_fnumput->put(*this, *this, this->fill(), __n).failed();
210 this->setstate(ios_base::badbit);
212 catch(exception& __fail)
214 // 27.6.1.2.1 Common requirements.
215 // Turn this on without causing an ios::failure to be thrown.
216 this->setstate(ios_base::badbit);
217 if ((this->exceptions() & ios_base::badbit) != 0)
218 __throw_exception_again;
224 template<typename _CharT, typename _Traits>
225 basic_ostream<_CharT, _Traits>&
226 basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n)
228 sentry __cerb(*this);
233 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
234 this->setstate(ios_base::badbit);
236 catch(exception& __fail)
238 // 27.6.1.2.1 Common requirements.
239 // Turn this on without causing an ios::failure to be thrown.
240 this->setstate(ios_base::badbit);
241 if ((this->exceptions() & ios_base::badbit) != 0)
242 __throw_exception_again;
249 template<typename _CharT, typename _Traits>
250 basic_ostream<_CharT, _Traits>&
251 basic_ostream<_CharT, _Traits>::operator<<(double __n)
253 sentry __cerb(*this);
258 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
259 this->setstate(ios_base::badbit);
261 catch(exception& __fail)
263 // 27.6.1.2.1 Common requirements.
264 // Turn this on without causing an ios::failure to be thrown.
265 this->setstate(ios_base::badbit);
266 if ((this->exceptions() & ios_base::badbit) != 0)
267 __throw_exception_again;
273 template<typename _CharT, typename _Traits>
274 basic_ostream<_CharT, _Traits>&
275 basic_ostream<_CharT, _Traits>::operator<<(long double __n)
277 sentry __cerb(*this);
282 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
283 this->setstate(ios_base::badbit);
285 catch(exception& __fail)
287 // 27.6.1.2.1 Common requirements.
288 // Turn this on without causing an ios::failure to be thrown.
289 this->setstate(ios_base::badbit);
290 if ((this->exceptions() & ios_base::badbit) != 0)
291 __throw_exception_again;
297 template<typename _CharT, typename _Traits>
298 basic_ostream<_CharT, _Traits>&
299 basic_ostream<_CharT, _Traits>::operator<<(const void* __n)
301 sentry __cerb(*this);
306 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
307 this->setstate(ios_base::badbit);
309 catch(exception& __fail)
311 // 27.6.1.2.1 Common requirements.
312 // Turn this on without causing an ios::failure to be thrown.
313 this->setstate(ios_base::badbit);
314 if ((this->exceptions() & ios_base::badbit) != 0)
315 __throw_exception_again;
321 template<typename _CharT, typename _Traits>
322 basic_ostream<_CharT, _Traits>&
323 basic_ostream<_CharT, _Traits>::operator<<(__streambuf_type* __sbin)
325 streamsize __xtrct = 0;
326 __streambuf_type* __sbout = this->rdbuf();
327 sentry __cerb(*this);
328 if (__sbin && __cerb)
329 __xtrct = __copy_streambufs(*this, __sbin, __sbout);
330 if (!__sbin || !__xtrct)
331 this->setstate(ios_base::failbit);
335 template<typename _CharT, typename _Traits>
336 basic_ostream<_CharT, _Traits>&
337 basic_ostream<_CharT, _Traits>::put(char_type __c)
339 sentry __cerb(*this);
342 int_type __put = rdbuf()->sputc(__c);
343 if (__put != traits_type::to_int_type(__c))
344 this->setstate(ios_base::badbit);
349 template<typename _CharT, typename _Traits>
350 basic_ostream<_CharT, _Traits>&
351 basic_ostream<_CharT, _Traits>::write(const _CharT* __s, streamsize __n)
353 sentry __cerb(*this);
356 streamsize __put = this->rdbuf()->sputn(__s, __n);
358 this->setstate(ios_base::badbit);
363 template<typename _CharT, typename _Traits>
364 basic_ostream<_CharT, _Traits>&
365 basic_ostream<_CharT, _Traits>::flush()
367 sentry __cerb(*this);
370 if (this->rdbuf() && this->rdbuf()->pubsync() == -1)
371 this->setstate(ios_base::badbit);
376 template<typename _CharT, typename _Traits>
377 typename basic_ostream<_CharT, _Traits>::pos_type
378 basic_ostream<_CharT, _Traits>::tellp()
380 pos_type __ret = pos_type(-1);
381 bool __testok = this->fail() != true;
384 __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
389 template<typename _CharT, typename _Traits>
390 basic_ostream<_CharT, _Traits>&
391 basic_ostream<_CharT, _Traits>::seekp(pos_type __pos)
393 bool __testok = this->fail() != true;
396 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
397 // 136. seekp, seekg setting wrong streams?
398 this->rdbuf()->pubseekpos(__pos, ios_base::out);
403 template<typename _CharT, typename _Traits>
404 basic_ostream<_CharT, _Traits>&
405 basic_ostream<_CharT, _Traits>::
406 seekp(off_type __off, ios_base::seekdir __d)
408 bool __testok = this->fail() != true;
411 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
412 // 136. seekp, seekg setting wrong streams?
413 this->rdbuf()->pubseekoff(__off, __d, ios_base::out);
418 // 27.6.2.5.4 Character inserters
420 // Construct correctly padded string, as per 22.2.2.2.2
421 // Similar in theory to __pad_numeric, from num_put, but it doesn't
422 // use _S_fill: perhaps it should.
424 // __newlen > __oldlen
425 // __news is allocated for __newlen size
426 template<typename _CharT, typename _Traits>
428 __pad_char(basic_ios<_CharT, _Traits>& __ios,
429 _CharT* __news, const _CharT* __olds,
430 const streamsize __newlen, const streamsize __oldlen)
432 typedef _CharT char_type;
433 typedef _Traits traits_type;
434 typedef typename traits_type::int_type int_type;
436 int_type __plen = static_cast<size_t>(__newlen - __oldlen);
437 char_type* __pads = static_cast<char_type*>(__builtin_alloca(sizeof(char_type) * __plen));
438 traits_type::assign(__pads, __plen, __ios.fill());
443 size_t __beglen; //either __plen or __oldlen
444 ios_base::fmtflags __fmt = __ios.flags() & ios_base::adjustfield;
446 if (__fmt == ios_base::left)
449 __beg = const_cast<char_type*>(__olds);
453 else if (__fmt == ios_base::internal)
455 // Pad after the sign, if there is one.
456 // Pad after 0[xX], if there is one.
457 // Who came up with these rules, anyway? Jeeze.
458 typedef _Format_cache<_CharT> __cache_type;
459 __cache_type const* __fmt = __cache_type::_S_get(__ios);
460 const char_type* __minus = traits_type::find(__olds, __oldlen,
462 const char_type* __plus = traits_type::find(__olds, __oldlen,
464 bool __testsign = __minus || __plus;
465 bool __testhex = __olds[0] == '0'
466 && (__olds[1] == 'x' || __olds[1] == 'X');
470 __news[0] = __olds[0];
471 __news[1] = __olds[1];
473 __beg = const_cast<char_type*>(__olds + __mod);
474 __beglen = __oldlen - __mod;
480 const char_type* __sign = __minus ? __minus + 1: __plus + 1;
481 __beg = const_cast<char_type*>(__olds);
482 __beglen = __sign - __olds;
483 __end = const_cast<char_type*>(__sign + __plen);
484 traits_type::copy(__news + __beglen, __pads, __plen);
491 __end = const_cast<char_type*>(__olds);
499 __end = const_cast<char_type*>(__olds);
502 traits_type::copy(__news, __beg, __beglen);
503 traits_type::copy(__news + __beglen, __end, __newlen - __beglen - __mod);
506 template<typename _CharT, typename _Traits>
507 basic_ostream<_CharT, _Traits>&
508 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
510 typedef basic_ostream<_CharT, _Traits> __ostream_type;
511 typename __ostream_type::sentry __cerb(__out);
516 streamsize __w = __out.width();
517 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
519 streamsize __len = 1;
522 __pad_char(__out, __pads, &__c, __w, __len);
525 __out.write(__pads, __len);
528 catch(exception& __fail)
530 // 27.6.1.2.1 Common requirements.
531 // Turn this on without causing an ios::failure to be thrown.
532 __out.setstate(ios_base::badbit);
533 if ((__out.exceptions() & ios_base::badbit) != 0)
534 __throw_exception_again;
541 template <class _Traits>
542 basic_ostream<char, _Traits>&
543 operator<<(basic_ostream<char, _Traits>& __out, char __c)
545 typedef basic_ostream<char, _Traits> __ostream_type;
546 typename __ostream_type::sentry __cerb(__out);
551 streamsize __w = __out.width();
552 char* __pads = static_cast<char*>(__builtin_alloca(__w + 1));
554 streamsize __len = 1;
557 __pad_char(__out, __pads, &__c, __w, __len);
560 __out.write(__pads, __len);
563 catch(exception& __fail)
565 // 27.6.1.2.1 Common requirements.
566 // Turn this on without causing an ios::failure to be thrown.
567 __out.setstate(ios_base::badbit);
568 if ((__out.exceptions() & ios_base::badbit) != 0)
569 __throw_exception_again;
575 template<typename _CharT, typename _Traits>
576 basic_ostream<_CharT, _Traits>&
577 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
579 typedef basic_ostream<_CharT, _Traits> __ostream_type;
580 typename __ostream_type::sentry __cerb(__out);
585 streamsize __w = __out.width();
586 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
587 streamsize __len = static_cast<streamsize>(_Traits::length(__s));
590 __pad_char(__out, __pads, __s, __w, __len);
594 __out.write(__s, __len);
597 catch(exception& __fail)
599 // 27.6.1.2.1 Common requirements.
600 // Turn this on without causing an ios::failure to be thrown.
601 __out.setstate(ios_base::badbit);
602 if ((__out.exceptions() & ios_base::badbit) != 0)
603 __throw_exception_again;
609 template<typename _CharT, typename _Traits>
610 basic_ostream<_CharT, _Traits>&
611 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
613 typedef basic_ostream<_CharT, _Traits> __ostream_type;
614 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
615 // 167. Improper use of traits_type::length()
616 typedef char_traits<char> __ctraits_type;
618 typename __ostream_type::sentry __cerb(__out);
621 size_t __clen = __ctraits_type::length(__s);
622 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * (__clen + 1)));
623 for (size_t __i = 0; __i <= __clen; ++__i)
624 __ws[__i] = __out.widen(__s[__i]);
625 _CharT* __str = __ws;
629 streamsize __len = static_cast<streamsize>(__clen);
630 streamsize __w = __out.width();
631 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
635 __pad_char(__out, __pads, __ws, __w, __len);
639 __out.write(__str, __len);
642 catch(exception& __fail)
644 // 27.6.1.2.1 Common requirements.
645 // Turn this on without causing an ios::failure to be thrown.
646 __out.setstate(ios_base::badbit);
647 if ((__out.exceptions() & ios_base::badbit) != 0)
648 __throw_exception_again;
654 // Partial specializationss
655 template<class _Traits>
656 basic_ostream<char, _Traits>&
657 operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
659 typedef basic_ostream<char, _Traits> __ostream_type;
660 typename __ostream_type::sentry __cerb(__out);
665 streamsize __w = __out.width();
666 char* __pads = static_cast<char*>(__builtin_alloca(__w));
667 streamsize __len = static_cast<streamsize>(_Traits::length(__s));
670 __pad_char(__out, __pads, __s, __w, __len);
674 __out.write(__s, __len);
677 catch(exception& __fail)
679 // 27.6.1.2.1 Common requirements.
680 // Turn this on without causing an ios::failure to be thrown.
681 __out.setstate(ios_base::badbit);
682 if ((__out.exceptions() & ios_base::badbit) != 0)
683 __throw_exception_again;
689 // 21.3.7.9 basic_string::operator<<
690 template<typename _CharT, typename _Traits, typename _Alloc>
691 basic_ostream<_CharT, _Traits>&
692 operator<<(basic_ostream<_CharT, _Traits>& __out,
693 const basic_string<_CharT, _Traits, _Alloc>& __str)
695 typedef basic_ostream<_CharT, _Traits> __ostream_type;
696 typename __ostream_type::sentry __cerb(__out);
699 const _CharT* __s = __str.data();
700 streamsize __w = __out.width();
701 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
702 streamsize __len = static_cast<streamsize>(__str.size());
703 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
704 // 25. String operator<< uses width() value wrong
708 __pad_char(__out, __pads, __s, __w, __len);
712 streamsize __res = __out.rdbuf()->sputn(__s, __len);
715 __out.setstate(ios_base::failbit);