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&))
57 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&))
79 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&))
101 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);
120 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
121 this->setstate(ios_base::badbit);
123 catch(exception& __fail){
124 // 27.6.1.2.1 Common requirements.
125 // Turn this on without causing an ios::failure to be thrown.
126 this->setstate(ios_base::badbit);
127 if ((this->exceptions() & ios_base::badbit) != 0)
128 __throw_exception_again;
134 template<typename _CharT, typename _Traits>
135 basic_ostream<_CharT, _Traits>&
136 basic_ostream<_CharT, _Traits>::operator<<(long __n)
138 sentry __cerb(*this);
143 ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
144 if (__fmt & ios_base::oct || __fmt & ios_base::hex)
145 __f = _M_fnumput->put(*this, *this, this->fill(),
146 static_cast<unsigned long>(__n)).failed();
148 __f = _M_fnumput->put(*this, *this, this->fill(), __n).failed();
151 this->setstate(ios_base::badbit);
153 catch(exception& __fail){
154 // 27.6.1.2.1 Common requirements.
155 // Turn this on without causing an ios::failure to be thrown.
156 this->setstate(ios_base::badbit);
157 if ((this->exceptions() & ios_base::badbit) != 0)
158 __throw_exception_again;
164 template<typename _CharT, typename _Traits>
165 basic_ostream<_CharT, _Traits>&
166 basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n)
168 sentry __cerb(*this);
172 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
173 this->setstate(ios_base::badbit);
175 catch(exception& __fail){
176 // 27.6.1.2.1 Common requirements.
177 // Turn this on without causing an ios::failure to be thrown.
178 this->setstate(ios_base::badbit);
179 if ((this->exceptions() & ios_base::badbit) != 0)
180 __throw_exception_again;
186 #ifdef _GLIBCPP_USE_LONG_LONG
187 template<typename _CharT, typename _Traits>
188 basic_ostream<_CharT, _Traits>&
189 basic_ostream<_CharT, _Traits>::operator<<(long long __n)
191 sentry __cerb(*this);
196 ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
197 if (__fmt & ios_base::oct || __fmt & ios_base::hex)
198 __f = _M_fnumput->put(*this, *this, this->fill(),
199 static_cast<unsigned long long>(__n)).failed();
201 __f = _M_fnumput->put(*this, *this, this->fill(), __n).failed();
204 catch(exception& __fail){
205 // 27.6.1.2.1 Common requirements.
206 // Turn this on without causing an ios::failure to be thrown.
207 this->setstate(ios_base::badbit);
208 if ((this->exceptions() & ios_base::badbit) != 0)
209 __throw_exception_again;
215 template<typename _CharT, typename _Traits>
216 basic_ostream<_CharT, _Traits>&
217 basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n)
219 sentry __cerb(*this);
223 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
224 this->setstate(ios_base::badbit);
226 catch(exception& __fail){
227 // 27.6.1.2.1 Common requirements.
228 // Turn this on without causing an ios::failure to be thrown.
229 this->setstate(ios_base::badbit);
230 if ((this->exceptions() & ios_base::badbit) != 0)
231 __throw_exception_again;
238 template<typename _CharT, typename _Traits>
239 basic_ostream<_CharT, _Traits>&
240 basic_ostream<_CharT, _Traits>::operator<<(double __n)
242 sentry __cerb(*this);
246 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
247 this->setstate(ios_base::badbit);
249 catch(exception& __fail){
250 // 27.6.1.2.1 Common requirements.
251 // Turn this on without causing an ios::failure to be thrown.
252 this->setstate(ios_base::badbit);
253 if ((this->exceptions() & ios_base::badbit) != 0)
254 __throw_exception_again;
260 template<typename _CharT, typename _Traits>
261 basic_ostream<_CharT, _Traits>&
262 basic_ostream<_CharT, _Traits>::operator<<(long double __n)
264 sentry __cerb(*this);
268 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
269 this->setstate(ios_base::badbit);
271 catch(exception& __fail){
272 // 27.6.1.2.1 Common requirements.
273 // Turn this on without causing an ios::failure to be thrown.
274 this->setstate(ios_base::badbit);
275 if ((this->exceptions() & ios_base::badbit) != 0)
276 __throw_exception_again;
282 template<typename _CharT, typename _Traits>
283 basic_ostream<_CharT, _Traits>&
284 basic_ostream<_CharT, _Traits>::operator<<(const void* __n)
286 sentry __cerb(*this);
290 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
291 this->setstate(ios_base::badbit);
293 catch(exception& __fail){
294 // 27.6.1.2.1 Common requirements.
295 // Turn this on without causing an ios::failure to be thrown.
296 this->setstate(ios_base::badbit);
297 if ((this->exceptions() & ios_base::badbit) != 0)
298 __throw_exception_again;
304 template<typename _CharT, typename _Traits>
305 basic_ostream<_CharT, _Traits>&
306 basic_ostream<_CharT, _Traits>::operator<<(__streambuf_type* __sbin)
308 streamsize __xtrct = 0;
309 __streambuf_type* __sbout = this->rdbuf();
310 sentry __cerb(*this);
311 if (__sbin && __cerb)
312 __xtrct = __copy_streambufs(*this, __sbin, __sbout);
313 if (!__sbin || !__xtrct)
314 this->setstate(ios_base::failbit);
318 template<typename _CharT, typename _Traits>
319 basic_ostream<_CharT, _Traits>&
320 basic_ostream<_CharT, _Traits>::put(char_type __c)
322 sentry __cerb(*this);
325 int_type __put = rdbuf()->sputc(__c);
326 if (__put != traits_type::to_int_type(__c))
327 this->setstate(ios_base::badbit);
332 template<typename _CharT, typename _Traits>
333 basic_ostream<_CharT, _Traits>&
334 basic_ostream<_CharT, _Traits>::write(const _CharT* __s, streamsize __n)
336 sentry __cerb(*this);
339 streamsize __put = this->rdbuf()->sputn(__s, __n);
341 this->setstate(ios_base::badbit);
346 template<typename _CharT, typename _Traits>
347 basic_ostream<_CharT, _Traits>&
348 basic_ostream<_CharT, _Traits>::flush()
350 sentry __cerb(*this);
353 if (this->rdbuf() && this->rdbuf()->pubsync() == -1)
354 this->setstate(ios_base::badbit);
359 template<typename _CharT, typename _Traits>
360 typename basic_ostream<_CharT, _Traits>::pos_type
361 basic_ostream<_CharT, _Traits>::tellp()
363 pos_type __ret = pos_type(-1);
364 bool __testok = this->fail() != true;
367 __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
372 template<typename _CharT, typename _Traits>
373 basic_ostream<_CharT, _Traits>&
374 basic_ostream<_CharT, _Traits>::seekp(pos_type __pos)
376 bool __testok = this->fail() != true;
379 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
380 // 136. seekp, seekg setting wrong streams?
381 this->rdbuf()->pubseekpos(__pos, ios_base::out);
386 template<typename _CharT, typename _Traits>
387 basic_ostream<_CharT, _Traits>&
388 basic_ostream<_CharT, _Traits>::
389 seekp(off_type __off, ios_base::seekdir __d)
391 bool __testok = this->fail() != true;
394 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
395 // 136. seekp, seekg setting wrong streams?
396 rdbuf()->pubseekoff(__off, __d, ios_base::out);
401 // 27.6.2.5.4 Character inserters
403 // Construct correctly padded string, as per 22.2.2.2.2
404 // Similar in theory to __pad_numeric, from num_put, but it doesn't
405 // use _S_fill: perhaps it should.
407 // __newlen > __oldlen
408 // __news is allocated for __newlen size
409 template<typename _CharT, typename _Traits>
411 __pad_char(basic_ios<_CharT, _Traits>& __ios,
412 _CharT* __news, const _CharT* __olds,
413 const streamsize __newlen, const streamsize __oldlen)
415 typedef _CharT char_type;
416 typedef _Traits traits_type;
417 typedef typename traits_type::int_type int_type;
419 int_type __plen = static_cast<size_t>(__newlen - __oldlen);
420 char_type* __pads = static_cast<char_type*>(__builtin_alloca(sizeof(char_type) * __plen));
421 traits_type::assign(__pads, __plen, __ios.fill());
426 size_t __beglen; //either __plen or __oldlen
427 ios_base::fmtflags __fmt = __ios.flags() & ios_base::adjustfield;
429 if (__fmt == ios_base::left)
432 __beg = const_cast<char_type*>(__olds);
436 else if (__fmt == ios_base::internal)
438 // Pad after the sign, if there is one.
439 // Pad after 0[xX], if there is one.
440 // Who came up with these rules, anyway? Jeeze.
441 typedef _Format_cache<_CharT> __cache_type;
442 __cache_type const* __fmt = __cache_type::_S_get(__ios);
443 const char_type* __minus = traits_type::find(__olds, __oldlen,
445 const char_type* __plus = traits_type::find(__olds, __oldlen,
447 bool __testsign = __minus || __plus;
448 bool __testhex = __olds[0] == '0'
449 && (__olds[1] == 'x' || __olds[1] == 'X');
453 __news[0] = __olds[0];
454 __news[1] = __olds[1];
456 __beg = const_cast<char_type*>(__olds + __mod);
457 __beglen = __oldlen - __mod;
463 const char_type* __sign = __minus ? __minus + 1: __plus + 1;
464 __beg = const_cast<char_type*>(__olds);
465 __beglen = __sign - __olds;
466 __end = const_cast<char_type*>(__sign + __plen);
467 traits_type::copy(__news + __beglen, __pads, __plen);
474 __end = const_cast<char_type*>(__olds);
482 __end = const_cast<char_type*>(__olds);
485 traits_type::copy(__news, __beg, __beglen);
486 traits_type::copy(__news + __beglen, __end, __newlen - __beglen - __mod);
489 template<typename _CharT, typename _Traits>
490 basic_ostream<_CharT, _Traits>&
491 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
493 typedef basic_ostream<_CharT, _Traits> __ostream_type;
494 typename __ostream_type::sentry __cerb(__out);
498 streamsize __w = __out.width();
499 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
501 streamsize __len = 1;
504 __pad_char(__out, __pads, &__c, __w, __len);
507 __out.write(__pads, __len);
510 catch(exception& __fail){
511 // 27.6.1.2.1 Common requirements.
512 // Turn this on without causing an ios::failure to be thrown.
513 __out.setstate(ios_base::badbit);
514 if ((__out.exceptions() & ios_base::badbit) != 0)
515 __throw_exception_again;
522 template <class _Traits>
523 basic_ostream<char, _Traits>&
524 operator<<(basic_ostream<char, _Traits>& __out, char __c)
526 typedef basic_ostream<char, _Traits> __ostream_type;
527 typename __ostream_type::sentry __cerb(__out);
531 streamsize __w = __out.width();
532 char* __pads = static_cast<char*>(__builtin_alloca(__w + 1));
534 streamsize __len = 1;
537 __pad_char(__out, __pads, &__c, __w, __len);
540 __out.write(__pads, __len);
543 catch(exception& __fail){
544 // 27.6.1.2.1 Common requirements.
545 // Turn this on without causing an ios::failure to be thrown.
546 __out.setstate(ios_base::badbit);
547 if ((__out.exceptions() & ios_base::badbit) != 0)
548 __throw_exception_again;
554 template<typename _CharT, typename _Traits>
555 basic_ostream<_CharT, _Traits>&
556 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
558 typedef basic_ostream<_CharT, _Traits> __ostream_type;
559 typename __ostream_type::sentry __cerb(__out);
563 streamsize __w = __out.width();
564 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
565 streamsize __len = static_cast<streamsize>(_Traits::length(__s));
568 __pad_char(__out, __pads, __s, __w, __len);
572 __out.write(__s, __len);
575 catch(exception& __fail){
576 // 27.6.1.2.1 Common requirements.
577 // Turn this on without causing an ios::failure to be thrown.
578 __out.setstate(ios_base::badbit);
579 if ((__out.exceptions() & ios_base::badbit) != 0)
580 __throw_exception_again;
586 template<typename _CharT, typename _Traits>
587 basic_ostream<_CharT, _Traits>&
588 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
590 typedef basic_ostream<_CharT, _Traits> __ostream_type;
591 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
592 // 167. Improper use of traits_type::length()
593 typedef char_traits<char> __ctraits_type;
595 typename __ostream_type::sentry __cerb(__out);
598 size_t __clen = __ctraits_type::length(__s);
599 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * (__clen + 1)));
600 for (size_t __i = 0; __i <= __clen; ++__i)
601 __ws[__i] = __out.widen(__s[__i]);
602 _CharT* __str = __ws;
605 streamsize __len = static_cast<streamsize>(__clen);
606 streamsize __w = __out.width();
607 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
611 __pad_char(__out, __pads, __ws, __w, __len);
615 __out.write(__str, __len);
618 catch(exception& __fail){
619 // 27.6.1.2.1 Common requirements.
620 // Turn this on without causing an ios::failure to be thrown.
621 __out.setstate(ios_base::badbit);
622 if ((__out.exceptions() & ios_base::badbit) != 0)
623 __throw_exception_again;
629 // Partial specializationss
630 template<class _Traits>
631 basic_ostream<char, _Traits>&
632 operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
634 typedef basic_ostream<char, _Traits> __ostream_type;
635 typename __ostream_type::sentry __cerb(__out);
639 streamsize __w = __out.width();
640 char* __pads = static_cast<char*>(__builtin_alloca(__w));
641 streamsize __len = static_cast<streamsize>(_Traits::length(__s));
644 __pad_char(__out, __pads, __s, __w, __len);
648 __out.write(__s, __len);
651 catch(exception& __fail){
652 // 27.6.1.2.1 Common requirements.
653 // Turn this on without causing an ios::failure to be thrown.
654 __out.setstate(ios_base::badbit);
655 if ((__out.exceptions() & ios_base::badbit) != 0)
656 __throw_exception_again;
662 // 21.3.7.8 basic_string::operator<<
663 template<typename _CharT, typename _Traits, typename _Alloc>
664 basic_ostream<_CharT, _Traits>&
665 operator<<(basic_ostream<_CharT, _Traits>& __out,
666 const basic_string<_CharT, _Traits, _Alloc>& __s)
667 { return (__out << __s.c_str()); }