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
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<<(__streambuf_type* __sbin)
116 sentry __cerb(*this);
121 streamsize __xtrct = 0;
124 __streambuf_type* __sbout = this->rdbuf();
125 __xtrct = __copy_streambufs(*this, __sbin, __sbout);
128 this->setstate(ios_base::badbit);
130 this->setstate(ios_base::failbit);
132 catch(exception& __fail)
134 // 27.6.2.5.1 Common requirements.
135 // Turn this on without causing an ios::failure to be thrown.
136 this->setstate(ios_base::badbit);
137 if ((this->exceptions() & ios_base::badbit) != 0)
138 __throw_exception_again;
144 template<typename _CharT, typename _Traits>
145 basic_ostream<_CharT, _Traits>&
146 basic_ostream<_CharT, _Traits>::operator<<(bool __n)
148 sentry __cerb(*this);
153 if (_M_check_facet(_M_fnumput))
154 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
155 this->setstate(ios_base::badbit);
157 catch(exception& __fail)
159 // 27.6.1.2.1 Common requirements.
160 // Turn this on without causing an ios::failure to be thrown.
161 this->setstate(ios_base::badbit);
162 if ((this->exceptions() & ios_base::badbit) != 0)
163 __throw_exception_again;
169 template<typename _CharT, typename _Traits>
170 basic_ostream<_CharT, _Traits>&
171 basic_ostream<_CharT, _Traits>::operator<<(long __n)
173 sentry __cerb(*this);
178 char_type __c = this->fill();
179 ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
180 if (_M_check_facet(_M_fnumput))
183 if (__fmt & ios_base::oct || __fmt & ios_base::hex)
185 unsigned long __l = static_cast<unsigned long>(__n);
186 __b = _M_fnumput->put(*this, *this, __c, __l).failed();
189 __b = _M_fnumput->put(*this, *this, __c, __n).failed();
191 this->setstate(ios_base::badbit);
194 catch(exception& __fail)
196 // 27.6.1.2.1 Common requirements.
197 // Turn this on without causing an ios::failure to be thrown.
198 this->setstate(ios_base::badbit);
199 if ((this->exceptions() & ios_base::badbit) != 0)
200 __throw_exception_again;
206 template<typename _CharT, typename _Traits>
207 basic_ostream<_CharT, _Traits>&
208 basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n)
210 sentry __cerb(*this);
215 if (_M_check_facet(_M_fnumput))
216 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
217 this->setstate(ios_base::badbit);
219 catch(exception& __fail)
221 // 27.6.1.2.1 Common requirements.
222 // Turn this on without causing an ios::failure to be thrown.
223 this->setstate(ios_base::badbit);
224 if ((this->exceptions() & ios_base::badbit) != 0)
225 __throw_exception_again;
231 #ifdef _GLIBCPP_USE_LONG_LONG
232 template<typename _CharT, typename _Traits>
233 basic_ostream<_CharT, _Traits>&
234 basic_ostream<_CharT, _Traits>::operator<<(long long __n)
236 sentry __cerb(*this);
241 char_type __c = this->fill();
242 ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
243 if (_M_check_facet(_M_fnumput))
246 if (__fmt & ios_base::oct || __fmt & ios_base::hex)
248 unsigned long long __l;
249 __l = static_cast<unsigned long long>(__n);
250 __b = _M_fnumput->put(*this, *this, __c, __l).failed();
253 __b = _M_fnumput->put(*this, *this, __c, __n).failed();
255 this->setstate(ios_base::badbit);
258 catch(exception& __fail)
260 // 27.6.1.2.1 Common requirements.
261 // Turn this on without causing an ios::failure to be thrown.
262 this->setstate(ios_base::badbit);
263 if ((this->exceptions() & ios_base::badbit) != 0)
264 __throw_exception_again;
270 template<typename _CharT, typename _Traits>
271 basic_ostream<_CharT, _Traits>&
272 basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n)
274 sentry __cerb(*this);
279 if (_M_check_facet(_M_fnumput))
280 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
281 this->setstate(ios_base::badbit);
283 catch(exception& __fail)
285 // 27.6.1.2.1 Common requirements.
286 // Turn this on without causing an ios::failure to be thrown.
287 this->setstate(ios_base::badbit);
288 if ((this->exceptions() & ios_base::badbit) != 0)
289 __throw_exception_again;
296 template<typename _CharT, typename _Traits>
297 basic_ostream<_CharT, _Traits>&
298 basic_ostream<_CharT, _Traits>::operator<<(double __n)
300 sentry __cerb(*this);
305 if (_M_check_facet(_M_fnumput))
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<<(long double __n)
325 sentry __cerb(*this);
330 if (_M_check_facet(_M_fnumput))
331 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
332 this->setstate(ios_base::badbit);
334 catch(exception& __fail)
336 // 27.6.1.2.1 Common requirements.
337 // Turn this on without causing an ios::failure to be thrown.
338 this->setstate(ios_base::badbit);
339 if ((this->exceptions() & ios_base::badbit) != 0)
340 __throw_exception_again;
346 template<typename _CharT, typename _Traits>
347 basic_ostream<_CharT, _Traits>&
348 basic_ostream<_CharT, _Traits>::operator<<(const void* __n)
350 sentry __cerb(*this);
355 if (_M_check_facet(_M_fnumput))
356 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
357 this->setstate(ios_base::badbit);
359 catch(exception& __fail)
361 // 27.6.1.2.1 Common requirements.
362 // Turn this on without causing an ios::failure to be thrown.
363 this->setstate(ios_base::badbit);
364 if ((this->exceptions() & ios_base::badbit) != 0)
365 __throw_exception_again;
371 template<typename _CharT, typename _Traits>
372 basic_ostream<_CharT, _Traits>&
373 basic_ostream<_CharT, _Traits>::put(char_type __c)
375 sentry __cerb(*this);
378 int_type __put = rdbuf()->sputc(__c);
379 if (traits_type::eq_int_type(__put, traits_type::eof()))
380 this->setstate(ios_base::badbit);
385 template<typename _CharT, typename _Traits>
386 basic_ostream<_CharT, _Traits>&
387 basic_ostream<_CharT, _Traits>::write(const _CharT* __s, streamsize __n)
389 sentry __cerb(*this);
392 streamsize __put = this->rdbuf()->sputn(__s, __n);
394 this->setstate(ios_base::badbit);
399 template<typename _CharT, typename _Traits>
400 basic_ostream<_CharT, _Traits>&
401 basic_ostream<_CharT, _Traits>::flush()
403 sentry __cerb(*this);
406 if (this->rdbuf() && this->rdbuf()->pubsync() == -1)
407 this->setstate(ios_base::badbit);
412 template<typename _CharT, typename _Traits>
413 typename basic_ostream<_CharT, _Traits>::pos_type
414 basic_ostream<_CharT, _Traits>::tellp()
416 pos_type __ret = pos_type(-1);
417 bool __testok = this->fail() != true;
420 __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
425 template<typename _CharT, typename _Traits>
426 basic_ostream<_CharT, _Traits>&
427 basic_ostream<_CharT, _Traits>::seekp(pos_type __pos)
429 bool __testok = this->fail() != true;
433 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
434 // 136. seekp, seekg setting wrong streams?
435 pos_type __err = this->rdbuf()->pubseekpos(__pos, ios_base::out);
437 // 129. Need error indication from seekp() and seekg()
438 if (__err == pos_type(off_type(-1)))
439 this->setstate(ios_base::failbit);
445 template<typename _CharT, typename _Traits>
446 basic_ostream<_CharT, _Traits>&
447 basic_ostream<_CharT, _Traits>::
448 seekp(off_type __off, ios_base::seekdir __d)
450 bool __testok = this->fail() != true;
454 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
455 // 136. seekp, seekg setting wrong streams?
456 pos_type __err = this->rdbuf()->pubseekoff(__off, __d,
459 // 129. Need error indication from seekp() and seekg()
460 if (__err == pos_type(off_type(-1)))
461 this->setstate(ios_base::failbit);
467 // 27.6.2.5.4 Character inserters.
468 template<typename _CharT, typename _Traits>
469 basic_ostream<_CharT, _Traits>&
470 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
472 typedef basic_ostream<_CharT, _Traits> __ostream_type;
473 typename __ostream_type::sentry __cerb(__out);
478 streamsize __w = __out.width();
479 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
481 streamsize __len = 1;
484 __pad(__out, __out.fill(), __pads, &__c, __w, __len, false);
487 __out.write(__pads, __len);
490 catch(exception& __fail)
492 // 27.6.1.2.1 Common requirements.
493 // Turn this on without causing an ios::failure to be thrown.
494 __out.setstate(ios_base::badbit);
495 if ((__out.exceptions() & ios_base::badbit) != 0)
496 __throw_exception_again;
503 template <class _Traits>
504 basic_ostream<char, _Traits>&
505 operator<<(basic_ostream<char, _Traits>& __out, char __c)
507 typedef basic_ostream<char, _Traits> __ostream_type;
508 typename __ostream_type::sentry __cerb(__out);
513 streamsize __w = __out.width();
514 char* __pads = static_cast<char*>(__builtin_alloca(__w + 1));
516 streamsize __len = 1;
519 __pad(__out, __out.fill(), __pads, &__c, __w, __len, false);
522 __out.write(__pads, __len);
525 catch(exception& __fail)
527 // 27.6.1.2.1 Common requirements.
528 // Turn this on without causing an ios::failure to be thrown.
529 __out.setstate(ios_base::badbit);
530 if ((__out.exceptions() & ios_base::badbit) != 0)
531 __throw_exception_again;
537 template<typename _CharT, typename _Traits>
538 basic_ostream<_CharT, _Traits>&
539 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
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));
549 streamsize __len = static_cast<streamsize>(_Traits::length(__s));
552 __pad(__out, __out.fill(), __pads, __s, __w, __len, false);
556 __out.write(__s, __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;
571 template<typename _CharT, typename _Traits>
572 basic_ostream<_CharT, _Traits>&
573 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
575 typedef basic_ostream<_CharT, _Traits> __ostream_type;
576 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
577 // 167. Improper use of traits_type::length()
578 // Note that this is only in 'Review' status.
579 typedef char_traits<char> __ctraits_type;
581 typename __ostream_type::sentry __cerb(__out);
584 size_t __clen = __ctraits_type::length(__s);
585 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * (__clen + 1)));
586 for (size_t __i = 0; __i <= __clen; ++__i)
587 __ws[__i] = __out.widen(__s[__i]);
588 _CharT* __str = __ws;
592 streamsize __len = static_cast<streamsize>(__clen);
593 streamsize __w = __out.width();
594 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
598 __pad(__out, __out.fill(), __pads, __ws, __w, __len, false);
602 __out.write(__str, __len);
605 catch(exception& __fail)
607 // 27.6.1.2.1 Common requirements.
608 // Turn this on without causing an ios::failure to be thrown.
609 __out.setstate(ios_base::badbit);
610 if ((__out.exceptions() & ios_base::badbit) != 0)
611 __throw_exception_again;
617 // Partial specializations.
618 template<class _Traits>
619 basic_ostream<char, _Traits>&
620 operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
622 typedef basic_ostream<char, _Traits> __ostream_type;
623 typename __ostream_type::sentry __cerb(__out);
628 streamsize __w = __out.width();
629 char* __pads = static_cast<char*>(__builtin_alloca(__w));
630 streamsize __len = static_cast<streamsize>(_Traits::length(__s));
633 __pad(__out, __out.fill(), __pads, __s, __w, __len, false);
637 __out.write(__s, __len);
640 catch(exception& __fail)
642 // 27.6.1.2.1 Common requirements.
643 // Turn this on without causing an ios::failure to be thrown.
644 __out.setstate(ios_base::badbit);
645 if ((__out.exceptions() & ios_base::badbit) != 0)
646 __throw_exception_again;
652 // 21.3.7.9 basic_string::operator<<
653 template<typename _CharT, typename _Traits, typename _Alloc>
654 basic_ostream<_CharT, _Traits>&
655 operator<<(basic_ostream<_CharT, _Traits>& __out,
656 const basic_string<_CharT, _Traits, _Alloc>& __str)
658 typedef basic_ostream<_CharT, _Traits> __ostream_type;
659 typename __ostream_type::sentry __cerb(__out);
662 const _CharT* __s = __str.data();
663 streamsize __w = __out.width();
664 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
665 streamsize __len = static_cast<streamsize>(__str.size());
666 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
667 // 25. String operator<< uses width() value wrong
671 __pad(__out, __out.fill(), __pads, __s, __w, __len, false);
675 streamsize __res = __out.rdbuf()->sputn(__s, __len);
678 __out.setstate(ios_base::failbit);