]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/ostream.tcc
builtins.c (expand_builtin_strstr, [...]): Replace calls to build with calls to build...
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / ostream.tcc
CommitLineData
a32e3c09
BK
1// ostream classes -*- C++ -*-
2
3adf6cad 3// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
a32e3c09 4// Free Software Foundation, Inc.
725dc051
BK
5//
6// This file is part of the GNU ISO C++ Library. This library is free
7// software; you can redistribute it and/or modify it under the
8// terms of the GNU General Public License as published by the
9// Free Software Foundation; either version 2, or (at your option)
10// any later version.
11
12// This library is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16
17// You should have received a copy of the GNU General Public License along
18// with this library; see the file COPYING. If not, write to the Free
19// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20// USA.
21
22// As a special exception, you may use this file as part of a free software
23// library without restriction. Specifically, if other files instantiate
24// templates or use macros or inline functions from this file, or you compile
25// this file and link it with other files to produce an executable, this
26// file does not by itself cause the resulting executable to be covered by
27// the GNU General Public License. This exception does not however
28// invalidate any other reasons why the executable file might be covered by
29// the GNU General Public License.
30
31//
32// ISO C++ 14882: 27.6.2 Output streams
33//
34
3d7c150e
BK
35#ifndef _OSTREAM_TCC
36#define _OSTREAM_TCC 1
37
3b794528
BK
38#pragma GCC system_header
39
54c1bf78 40#include <locale>
725dc051 41
ed6814f7 42namespace std
a16ad779 43{
725dc051
BK
44 template<typename _CharT, typename _Traits>
45 basic_ostream<_CharT, _Traits>::sentry::
12841eb3 46 sentry(basic_ostream<_CharT, _Traits>& __os)
ff5d863f 47 : _M_os(__os)
725dc051 48 {
ff5d863f 49 // XXX MT
b7bb6d4a 50 if (__os.tie() && __os.good())
ff5d863f
PC
51 __os.tie()->flush();
52
53 if (__os.good())
54 _M_ok = true;
55 else
56 {
57 _M_ok = false;
58 __os.setstate(ios_base::failbit);
59 }
725dc051 60 }
ed6814f7 61
725dc051 62 template<typename _CharT, typename _Traits>
ed6814f7 63 basic_ostream<_CharT, _Traits>&
725dc051
BK
64 basic_ostream<_CharT, _Traits>::
65 operator<<(__ostream_type& (*__pf)(__ostream_type&))
66 {
5681c890
PR
67 // _GLIBCXX_RESOLVE_LIB_DEFECTS
68 // DR 60. What is a formatted input function?
69 // The inserters for manipulators are *not* formatted output functions.
70 return __pf(*this);
725dc051 71 }
ed6814f7 72
725dc051 73 template<typename _CharT, typename _Traits>
ed6814f7 74 basic_ostream<_CharT, _Traits>&
725dc051
BK
75 basic_ostream<_CharT, _Traits>::
76 operator<<(__ios_type& (*__pf)(__ios_type&))
77 {
5681c890
PR
78 // _GLIBCXX_RESOLVE_LIB_DEFECTS
79 // DR 60. What is a formatted input function?
80 // The inserters for manipulators are *not* formatted output functions.
81 __pf(*this);
725dc051
BK
82 return *this;
83 }
84
85 template<typename _CharT, typename _Traits>
ed6814f7 86 basic_ostream<_CharT, _Traits>&
725dc051
BK
87 basic_ostream<_CharT, _Traits>::
88 operator<<(ios_base& (*__pf)(ios_base&))
89 {
5681c890
PR
90 // _GLIBCXX_RESOLVE_LIB_DEFECTS
91 // DR 60. What is a formatted input function?
92 // The inserters for manipulators are *not* formatted output functions.
93 __pf(*this);
725dc051
BK
94 return *this;
95 }
96
6cf5465d 97 template<typename _CharT, typename _Traits>
ed6814f7 98 basic_ostream<_CharT, _Traits>&
12841eb3
BK
99 basic_ostream<_CharT, _Traits>::
100 operator<<(bool __n)
725dc051
BK
101 {
102 sentry __cerb(*this);
ed6814f7 103 if (__cerb)
725dc051 104 {
12841eb3 105 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
ed6814f7 106 try
a16ad779 107 {
2803847d
BK
108 const __num_put_type& __np = __check_facet(this->_M_num_put);
109 if (__np.put(*this, *this, this->fill(), __n).failed())
12841eb3 110 __err |= ios_base::badbit;
a16ad779 111 }
06e7ca50 112 catch(...)
12841eb3 113 { this->_M_setstate(ios_base::badbit); }
6b98580b
BK
114 if (__err)
115 this->setstate(__err);
725dc051
BK
116 }
117 return *this;
118 }
119
120 template<typename _CharT, typename _Traits>
ed6814f7 121 basic_ostream<_CharT, _Traits>&
12841eb3
BK
122 basic_ostream<_CharT, _Traits>::
123 operator<<(long __n)
725dc051
BK
124 {
125 sentry __cerb(*this);
ed6814f7 126 if (__cerb)
725dc051 127 {
12841eb3 128 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
ed6814f7 129 try
a16ad779 130 {
0cd1de6f 131 bool __b = false;
3adf6cad
PC
132 const char_type __c = this->fill();
133 const ios_base::fmtflags __fmt = (this->flags()
134 & ios_base::basefield);
2803847d 135 const __num_put_type& __np = __check_facet(this->_M_num_put);
0cd1de6f 136 if ((__fmt & ios_base::oct) || (__fmt & ios_base::hex))
bfa1e6b1 137 {
3adf6cad 138 const unsigned long __l = static_cast<unsigned long>(__n);
2803847d 139 __b = __np.put(*this, *this, __c, __l).failed();
bfa1e6b1 140 }
0cd1de6f 141 else
2803847d 142 __b = __np.put(*this, *this, __c, __n).failed();
ed6814f7 143 if (__b)
12841eb3 144 __err |= ios_base::badbit;
a16ad779 145 }
06e7ca50 146 catch(...)
12841eb3 147 { this->_M_setstate(ios_base::badbit); }
6b98580b
BK
148 if (__err)
149 this->setstate(__err);
725dc051
BK
150 }
151 return *this;
152 }
153
154 template<typename _CharT, typename _Traits>
ed6814f7 155 basic_ostream<_CharT, _Traits>&
12841eb3
BK
156 basic_ostream<_CharT, _Traits>::
157 operator<<(unsigned long __n)
725dc051
BK
158 {
159 sentry __cerb(*this);
ed6814f7 160 if (__cerb)
725dc051 161 {
12841eb3 162 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
ed6814f7 163 try
a16ad779 164 {
2803847d
BK
165 const __num_put_type& __np = __check_facet(this->_M_num_put);
166 if (__np.put(*this, *this, this->fill(), __n).failed())
12841eb3 167 __err |= ios_base::badbit;
a16ad779 168 }
06e7ca50 169 catch(...)
12841eb3 170 { this->_M_setstate(ios_base::badbit); }
6b98580b
BK
171 if (__err)
172 this->setstate(__err);
725dc051
BK
173 }
174 return *this;
175 }
176
3d7c150e 177#ifdef _GLIBCXX_USE_LONG_LONG
725dc051 178 template<typename _CharT, typename _Traits>
ed6814f7 179 basic_ostream<_CharT, _Traits>&
12841eb3
BK
180 basic_ostream<_CharT, _Traits>::
181 operator<<(long long __n)
725dc051
BK
182 {
183 sentry __cerb(*this);
ed6814f7 184 if (__cerb)
725dc051 185 {
12841eb3 186 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
ed6814f7 187 try
a16ad779 188 {
0cd1de6f 189 bool __b = false;
3adf6cad
PC
190 const char_type __c = this->fill();
191 const ios_base::fmtflags __fmt = (this->flags()
192 & ios_base::basefield);
2803847d 193 const __num_put_type& __np = __check_facet(this->_M_num_put);
0cd1de6f 194 if ((__fmt & ios_base::oct) || (__fmt & ios_base::hex))
bfa1e6b1 195 {
3adf6cad
PC
196 const unsigned long long __l = (static_cast<
197 unsigned long long>(__n));
2803847d 198 __b = __np.put(*this, *this, __c, __l).failed();
bfa1e6b1 199 }
0cd1de6f 200 else
2803847d 201 __b = __np.put(*this, *this, __c, __n).failed();
ed6814f7 202 if (__b)
12841eb3 203 __err |= ios_base::badbit;
a16ad779 204 }
06e7ca50 205 catch(...)
12841eb3 206 { this->_M_setstate(ios_base::badbit); }
6b98580b
BK
207 if (__err)
208 this->setstate(__err);
725dc051
BK
209 }
210 return *this;
211 }
212
213 template<typename _CharT, typename _Traits>
ed6814f7 214 basic_ostream<_CharT, _Traits>&
12841eb3
BK
215 basic_ostream<_CharT, _Traits>::
216 operator<<(unsigned long long __n)
725dc051
BK
217 {
218 sentry __cerb(*this);
ed6814f7 219 if (__cerb)
725dc051 220 {
12841eb3 221 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
ed6814f7 222 try
a16ad779 223 {
2803847d
BK
224 const __num_put_type& __np = __check_facet(this->_M_num_put);
225 if (__np.put(*this, *this, this->fill(), __n).failed())
12841eb3 226 __err |= ios_base::badbit;
a16ad779 227 }
06e7ca50 228 catch(...)
12841eb3 229 { this->_M_setstate(ios_base::badbit); }
6b98580b
BK
230 if (__err)
231 this->setstate(__err);
725dc051
BK
232 }
233 return *this;
234 }
235#endif
ed6814f7 236
725dc051 237 template<typename _CharT, typename _Traits>
ed6814f7 238 basic_ostream<_CharT, _Traits>&
12841eb3
BK
239 basic_ostream<_CharT, _Traits>::
240 operator<<(double __n)
725dc051
BK
241 {
242 sentry __cerb(*this);
ed6814f7 243 if (__cerb)
725dc051 244 {
12841eb3 245 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
ed6814f7 246 try
a16ad779 247 {
2803847d
BK
248 const __num_put_type& __np = __check_facet(this->_M_num_put);
249 if (__np.put(*this, *this, this->fill(), __n).failed())
12841eb3 250 __err |= ios_base::badbit;
a16ad779 251 }
06e7ca50 252 catch(...)
12841eb3 253 { this->_M_setstate(ios_base::badbit); }
6b98580b
BK
254 if (__err)
255 this->setstate(__err);
725dc051
BK
256 }
257 return *this;
258 }
ed6814f7 259
725dc051 260 template<typename _CharT, typename _Traits>
ed6814f7 261 basic_ostream<_CharT, _Traits>&
12841eb3
BK
262 basic_ostream<_CharT, _Traits>::
263 operator<<(long double __n)
725dc051
BK
264 {
265 sentry __cerb(*this);
ed6814f7 266 if (__cerb)
725dc051 267 {
12841eb3 268 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
ed6814f7 269 try
a16ad779 270 {
2803847d
BK
271 const __num_put_type& __np = __check_facet(this->_M_num_put);
272 if (__np.put(*this, *this, this->fill(), __n).failed())
12841eb3 273 __err |= ios_base::badbit;
a16ad779 274 }
06e7ca50 275 catch(...)
12841eb3 276 { this->_M_setstate(ios_base::badbit); }
6b98580b
BK
277 if (__err)
278 this->setstate(__err);
725dc051
BK
279 }
280 return *this;
281 }
282
283 template<typename _CharT, typename _Traits>
ed6814f7 284 basic_ostream<_CharT, _Traits>&
12841eb3
BK
285 basic_ostream<_CharT, _Traits>::
286 operator<<(const void* __n)
725dc051
BK
287 {
288 sentry __cerb(*this);
ed6814f7 289 if (__cerb)
725dc051 290 {
12841eb3 291 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
ed6814f7 292 try
a16ad779 293 {
2803847d
BK
294 const __num_put_type& __np = __check_facet(this->_M_num_put);
295 if (__np.put(*this, *this, this->fill(), __n).failed())
12841eb3 296 __err |= ios_base::badbit;
a16ad779 297 }
06e7ca50 298 catch(...)
12841eb3 299 { this->_M_setstate(ios_base::badbit); }
6b98580b
BK
300 if (__err)
301 this->setstate(__err);
12841eb3
BK
302 }
303 return *this;
304 }
305
306 template<typename _CharT, typename _Traits>
ed6814f7 307 basic_ostream<_CharT, _Traits>&
12841eb3
BK
308 basic_ostream<_CharT, _Traits>::
309 operator<<(__streambuf_type* __sbin)
310 {
311 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
312 sentry __cerb(*this);
313 if (__cerb && __sbin)
314 {
315 try
a16ad779 316 {
fb8d4638 317 if (!__copy_streambufs(__sbin, this->rdbuf()))
12841eb3 318 __err |= ios_base::failbit;
a16ad779 319 }
12841eb3
BK
320 catch(...)
321 { this->_M_setstate(ios_base::failbit); }
725dc051 322 }
12841eb3
BK
323 else if (!__sbin)
324 __err |= ios_base::badbit;
6b98580b
BK
325 if (__err)
326 this->setstate(__err);
725dc051
BK
327 return *this;
328 }
329
725dc051
BK
330 template<typename _CharT, typename _Traits>
331 basic_ostream<_CharT, _Traits>&
12841eb3
BK
332 basic_ostream<_CharT, _Traits>::
333 put(char_type __c)
ed6814f7 334 {
5681c890
PR
335 // _GLIBCXX_RESOLVE_LIB_DEFECTS
336 // DR 60. What is a formatted input function?
337 // basic_ostream::put(char_type) is an unformatted output function.
338 // DR 63. Exception-handling policy for unformatted output.
339 // Unformatted output functions should catch exceptions thrown
340 // from streambuf members.
725dc051 341 sentry __cerb(*this);
ed6814f7 342 if (__cerb)
725dc051 343 {
12841eb3 344 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
5681c890
PR
345 try
346 {
3adf6cad 347 const int_type __put = this->rdbuf()->sputc(__c);
5681c890 348 if (traits_type::eq_int_type(__put, traits_type::eof()))
12841eb3 349 __err |= ios_base::badbit;
5681c890
PR
350 }
351 catch (...)
12841eb3 352 { this->_M_setstate(ios_base::badbit); }
6b98580b
BK
353 if (__err)
354 this->setstate(__err);
725dc051 355 }
ed6814f7 356 return *this;
725dc051
BK
357 }
358
359 template<typename _CharT, typename _Traits>
360 basic_ostream<_CharT, _Traits>&
12841eb3
BK
361 basic_ostream<_CharT, _Traits>::
362 write(const _CharT* __s, streamsize __n)
725dc051 363 {
5681c890
PR
364 // _GLIBCXX_RESOLVE_LIB_DEFECTS
365 // DR 60. What is a formatted input function?
366 // basic_ostream::write(const char_type*, streamsize) is an
367 // unformatted output function.
368 // DR 63. Exception-handling policy for unformatted output.
369 // Unformatted output functions should catch exceptions thrown
370 // from streambuf members.
725dc051
BK
371 sentry __cerb(*this);
372 if (__cerb)
5681c890
PR
373 {
374 try
375 { _M_write(__s, __n); }
376 catch (...)
12841eb3 377 { this->_M_setstate(ios_base::badbit); }
5681c890 378 }
725dc051
BK
379 return *this;
380 }
381
382 template<typename _CharT, typename _Traits>
383 basic_ostream<_CharT, _Traits>&
12841eb3
BK
384 basic_ostream<_CharT, _Traits>::
385 flush()
725dc051 386 {
5681c890
PR
387 // _GLIBCXX_RESOLVE_LIB_DEFECTS
388 // DR 60. What is a formatted input function?
389 // basic_ostream::flush() is *not* an unformatted output function.
12841eb3
BK
390 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
391 try
392 {
393 if (this->rdbuf() && this->rdbuf()->pubsync() == -1)
394 __err |= ios_base::badbit;
395 }
396 catch(...)
397 { this->_M_setstate(ios_base::badbit); }
6b98580b
BK
398 if (__err)
399 this->setstate(__err);
725dc051
BK
400 return *this;
401 }
ed6814f7 402
725dc051
BK
403 template<typename _CharT, typename _Traits>
404 typename basic_ostream<_CharT, _Traits>::pos_type
12841eb3
BK
405 basic_ostream<_CharT, _Traits>::
406 tellp()
725dc051
BK
407 {
408 pos_type __ret = pos_type(-1);
12841eb3
BK
409 try
410 {
411 if (!this->fail())
412 __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
413 }
414 catch(...)
415 { this->_M_setstate(ios_base::badbit); }
725dc051
BK
416 return __ret;
417 }
418
725dc051
BK
419 template<typename _CharT, typename _Traits>
420 basic_ostream<_CharT, _Traits>&
12841eb3
BK
421 basic_ostream<_CharT, _Traits>::
422 seekp(pos_type __pos)
725dc051 423 {
12841eb3
BK
424 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
425 try
d52a9847 426 {
12841eb3
BK
427 if (!this->fail())
428 {
429 // _GLIBCXX_RESOLVE_LIB_DEFECTS
430 // 136. seekp, seekg setting wrong streams?
3adf6cad
PC
431 const pos_type __p = this->rdbuf()->pubseekpos(__pos,
432 ios_base::out);
ed6814f7 433
12841eb3
BK
434 // 129. Need error indication from seekp() and seekg()
435 if (__p == pos_type(off_type(-1)))
436 __err |= ios_base::failbit;
437 }
d52a9847 438 }
12841eb3
BK
439 catch(...)
440 { this->_M_setstate(ios_base::badbit); }
6b98580b
BK
441 if (__err)
442 this->setstate(__err);
725dc051
BK
443 return *this;
444 }
445
446 template<typename _CharT, typename _Traits>
447 basic_ostream<_CharT, _Traits>&
448 basic_ostream<_CharT, _Traits>::
12841eb3 449 seekp(off_type __off, ios_base::seekdir __dir)
725dc051 450 {
12841eb3
BK
451 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
452 try
d52a9847 453 {
12841eb3
BK
454 if (!this->fail())
455 {
456 // _GLIBCXX_RESOLVE_LIB_DEFECTS
457 // 136. seekp, seekg setting wrong streams?
3adf6cad
PC
458 const pos_type __p = this->rdbuf()->pubseekoff(__off, __dir,
459 ios_base::out);
ed6814f7 460
12841eb3
BK
461 // 129. Need error indication from seekp() and seekg()
462 if (__p == pos_type(off_type(-1)))
463 __err |= ios_base::failbit;
464 }
037cfe71 465 }
12841eb3
BK
466 catch(...)
467 { this->_M_setstate(ios_base::badbit); }
6b98580b
BK
468 if (__err)
469 this->setstate(__err);
725dc051
BK
470 return *this;
471 }
472
b6a95741 473 // 27.6.2.5.4 Character inserters.
725dc051
BK
474 template<typename _CharT, typename _Traits>
475 basic_ostream<_CharT, _Traits>&
476 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
477 {
478 typedef basic_ostream<_CharT, _Traits> __ostream_type;
bcd0cabb 479 typename __ostream_type::sentry __cerb(__out);
725dc051
BK
480 if (__cerb)
481 {
ed6814f7 482 try
a16ad779 483 {
6b61bca4 484 const streamsize __w = __out.width();
a16ad779 485 streamsize __len = 1;
6b61bca4 486 _CharT* __cs = &__c;
a16ad779
BK
487 if (__w > __len)
488 {
6b61bca4
PC
489 __cs = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
490 * __w));
ed6814f7 491 __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __cs,
f13a69ec 492 &__c, __w, __len, false);
a16ad779
BK
493 __len = __w;
494 }
6b61bca4 495 __out._M_write(__cs, __len);
a16ad779
BK
496 __out.width(0);
497 }
06e7ca50 498 catch(...)
12841eb3 499 { __out._M_setstate(ios_base::badbit); }
725dc051
BK
500 }
501 return __out;
502 }
ed6814f7 503
b6a95741 504 // Specializations.
ed6814f7 505 template <class _Traits>
725dc051
BK
506 basic_ostream<char, _Traits>&
507 operator<<(basic_ostream<char, _Traits>& __out, char __c)
508 {
509 typedef basic_ostream<char, _Traits> __ostream_type;
bcd0cabb 510 typename __ostream_type::sentry __cerb(__out);
725dc051
BK
511 if (__cerb)
512 {
ed6814f7 513 try
a16ad779 514 {
6b61bca4 515 const streamsize __w = __out.width();
a16ad779 516 streamsize __len = 1;
6b61bca4 517 char* __cs = &__c;
a16ad779
BK
518 if (__w > __len)
519 {
6b61bca4 520 __cs = static_cast<char*>(__builtin_alloca(__w));
ed6814f7 521 __pad<char, _Traits>::_S_pad(__out, __out.fill(), __cs,
f13a69ec 522 &__c, __w, __len, false);
a16ad779
BK
523 __len = __w;
524 }
6b61bca4 525 __out._M_write(__cs, __len);
a16ad779
BK
526 __out.width(0);
527 }
06e7ca50 528 catch(...)
12841eb3 529 { __out._M_setstate(ios_base::badbit); }
725dc051
BK
530 }
531 return __out;
532 }
533
534 template<typename _CharT, typename _Traits>
535 basic_ostream<_CharT, _Traits>&
536 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
537 {
538 typedef basic_ostream<_CharT, _Traits> __ostream_type;
bcd0cabb 539 typename __ostream_type::sentry __cerb(__out);
52f02d94 540 if (__cerb && __s)
725dc051 541 {
ed6814f7 542 try
a16ad779 543 {
6b61bca4 544 const streamsize __w = __out.width();
52f02d94 545 streamsize __len = static_cast<streamsize>(_Traits::length(__s));
a16ad779
BK
546 if (__w > __len)
547 {
3adf6cad
PC
548 _CharT* __cs = (static_cast<
549 _CharT*>(__builtin_alloca(sizeof(_CharT)
550 * __w)));
ed6814f7 551 __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __cs,
f13a69ec 552 __s, __w, __len, false);
6b61bca4 553 __s = __cs;
a16ad779
BK
554 __len = __w;
555 }
8d0a564b 556 __out._M_write(__s, __len);
a16ad779
BK
557 __out.width(0);
558 }
06e7ca50 559 catch(...)
12841eb3 560 { __out._M_setstate(ios_base::badbit); }
725dc051 561 }
52f02d94
BK
562 else if (!__s)
563 __out.setstate(ios_base::badbit);
725dc051
BK
564 return __out;
565 }
566
567 template<typename _CharT, typename _Traits>
568 basic_ostream<_CharT, _Traits>&
569 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
570 {
571 typedef basic_ostream<_CharT, _Traits> __ostream_type;
f5677b15
PC
572 // _GLIBCXX_RESOLVE_LIB_DEFECTS
573 // 167. Improper use of traits_type::length()
574 // Note that this is only in 'Review' status.
7bd9b6d4 575 typedef char_traits<char> __traits_type;
bcd0cabb 576 typename __ostream_type::sentry __cerb(__out);
52f02d94 577 if (__cerb && __s)
725dc051 578 {
52f02d94 579 size_t __clen = __traits_type::length(__s);
6b61bca4
PC
580 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
581 * __clen));
7bd9b6d4 582 for (size_t __i = 0; __i < __clen; ++__i)
725dc051
BK
583 __ws[__i] = __out.widen(__s[__i]);
584 _CharT* __str = __ws;
ed6814f7
BI
585
586 try
a16ad779 587 {
6b61bca4 588 const streamsize __w = __out.width();
a16ad779 589 streamsize __len = static_cast<streamsize>(__clen);
a16ad779
BK
590 if (__w > __len)
591 {
3adf6cad
PC
592 _CharT* __cs = (static_cast<
593 _CharT*>(__builtin_alloca(sizeof(_CharT)
594 * __w)));
ed6814f7 595 __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __cs,
f13a69ec 596 __ws, __w, __len, false);
6b61bca4 597 __str = __cs;
a16ad779
BK
598 __len = __w;
599 }
8d0a564b 600 __out._M_write(__str, __len);
a16ad779
BK
601 __out.width(0);
602 }
06e7ca50 603 catch(...)
12841eb3 604 { __out._M_setstate(ios_base::badbit); }
725dc051 605 }
52f02d94
BK
606 else if (!__s)
607 __out.setstate(ios_base::badbit);
725dc051
BK
608 return __out;
609 }
610
b6a95741 611 // Partial specializations.
725dc051
BK
612 template<class _Traits>
613 basic_ostream<char, _Traits>&
614 operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
615 {
616 typedef basic_ostream<char, _Traits> __ostream_type;
bcd0cabb 617 typename __ostream_type::sentry __cerb(__out);
52f02d94 618 if (__cerb && __s)
725dc051 619 {
ed6814f7 620 try
a16ad779 621 {
6b61bca4 622 const streamsize __w = __out.width();
52f02d94 623 streamsize __len = static_cast<streamsize>(_Traits::length(__s));
a16ad779
BK
624 if (__w > __len)
625 {
6b61bca4 626 char* __cs = static_cast<char*>(__builtin_alloca(__w));
ed6814f7 627 __pad<char, _Traits>::_S_pad(__out, __out.fill(), __cs,
f13a69ec 628 __s, __w, __len, false);
6b61bca4 629 __s = __cs;
a16ad779
BK
630 __len = __w;
631 }
8d0a564b 632 __out._M_write(__s, __len);
a16ad779
BK
633 __out.width(0);
634 }
06e7ca50 635 catch(...)
12841eb3 636 { __out._M_setstate(ios_base::badbit); }
725dc051 637 }
52f02d94
BK
638 else if (!__s)
639 __out.setstate(ios_base::badbit);
725dc051
BK
640 return __out;
641 }
642
a16ad779 643 // 21.3.7.9 basic_string::operator<<
725dc051
BK
644 template<typename _CharT, typename _Traits, typename _Alloc>
645 basic_ostream<_CharT, _Traits>&
646 operator<<(basic_ostream<_CharT, _Traits>& __out,
a16ad779 647 const basic_string<_CharT, _Traits, _Alloc>& __str)
ed6814f7 648 {
a16ad779
BK
649 typedef basic_ostream<_CharT, _Traits> __ostream_type;
650 typename __ostream_type::sentry __cerb(__out);
651 if (__cerb)
652 {
6b61bca4 653 const streamsize __w = __out.width();
a16ad779 654 streamsize __len = static_cast<streamsize>(__str.size());
6b61bca4 655 const _CharT* __s = __str.data();
12841eb3 656
f5677b15 657 // _GLIBCXX_RESOLVE_LIB_DEFECTS
a16ad779 658 // 25. String operator<< uses width() value wrong
a16ad779
BK
659 if (__w > __len)
660 {
3adf6cad
PC
661 _CharT* __cs = (static_cast<
662 _CharT*>(__builtin_alloca(sizeof(_CharT) * __w)));
6b61bca4 663 __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __cs, __s,
f13a69ec 664 __w, __len, false);
6b61bca4 665 __s = __cs;
a16ad779
BK
666 __len = __w;
667 }
8d0a564b 668 __out._M_write(__s, __len);
a16ad779 669 __out.width(0);
a16ad779
BK
670 }
671 return __out;
672 }
a32e3c09
BK
673
674 // Inhibit implicit instantiations for required instantiations,
ed6814f7 675 // which are defined via explicit instantiations elsewhere.
a32e3c09 676 // NB: This syntax is a GNU extension.
3d7c150e 677#if _GLIBCXX_EXTERN_TEMPLATE
a32e3c09
BK
678 extern template class basic_ostream<char>;
679 extern template ostream& endl(ostream&);
680 extern template ostream& ends(ostream&);
681 extern template ostream& flush(ostream&);
682 extern template ostream& operator<<(ostream&, char);
683 extern template ostream& operator<<(ostream&, unsigned char);
684 extern template ostream& operator<<(ostream&, signed char);
685 extern template ostream& operator<<(ostream&, const char*);
686 extern template ostream& operator<<(ostream&, const unsigned char*);
687 extern template ostream& operator<<(ostream&, const signed char*);
688
3d7c150e 689#ifdef _GLIBCXX_USE_WCHAR_T
a32e3c09
BK
690 extern template class basic_ostream<wchar_t>;
691 extern template wostream& endl(wostream&);
692 extern template wostream& ends(wostream&);
693 extern template wostream& flush(wostream&);
694 extern template wostream& operator<<(wostream&, wchar_t);
695 extern template wostream& operator<<(wostream&, char);
696 extern template wostream& operator<<(wostream&, const wchar_t*);
697 extern template wostream& operator<<(wostream&, const char*);
3522a91b 698#endif
1bc8b0ad 699#endif
725dc051 700} // namespace std
3d7c150e
BK
701
702#endif