ostream.tcc revision 117397
1118611Snjl// ostream classes -*- C++ -*-
2118611Snjl
3118611Snjl// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
4118611Snjl// Free Software Foundation, Inc.
5118611Snjl//
6118611Snjl// This file is part of the GNU ISO C++ Library.  This library is free
7118611Snjl// software; you can redistribute it and/or modify it under the
8217365Sjkim// terms of the GNU General Public License as published by the
9229989Sjkim// Free Software Foundation; either version 2, or (at your option)
10118611Snjl// any later version.
11118611Snjl
12217365Sjkim// This library is distributed in the hope that it will be useful,
13217365Sjkim// but WITHOUT ANY WARRANTY; without even the implied warranty of
14217365Sjkim// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15217365Sjkim// GNU General Public License for more details.
16217365Sjkim
17217365Sjkim// You should have received a copy of the GNU General Public License along
18217365Sjkim// with this library; see the file COPYING.  If not, write to the Free
19217365Sjkim// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20217365Sjkim// USA.
21217365Sjkim
22217365Sjkim// As a special exception, you may use this file as part of a free software
23217365Sjkim// library without restriction.  Specifically, if other files instantiate
24217365Sjkim// templates or use macros or inline functions from this file, or you compile
25217365Sjkim// this file and link it with other files to produce an executable, this
26118611Snjl// file does not by itself cause the resulting executable to be covered by
27217365Sjkim// the GNU General Public License.  This exception does not however
28217365Sjkim// invalidate any other reasons why the executable file might be covered by
29217365Sjkim// the GNU General Public License.
30118611Snjl
31217365Sjkim//
32217365Sjkim// ISO C++ 14882: 27.6.2  Output streams
33217365Sjkim//
34217365Sjkim
35217365Sjkim#pragma GCC system_header
36217365Sjkim
37217365Sjkim#include <locale>
38217365Sjkim
39217365Sjkimnamespace std 
40217365Sjkim{
41217365Sjkim  template<typename _CharT, typename _Traits>
42217365Sjkim    basic_ostream<_CharT, _Traits>::sentry::
43217365Sjkim    sentry(basic_ostream<_CharT,_Traits>& __os)
44118611Snjl    : _M_os(__os)
45217365Sjkim    {
46217365Sjkim      // XXX MT
47118611Snjl      if (__os.tie() && __os.good())
48151937Sjkim	__os.tie()->flush();
49213806Sjkim
50118611Snjl      if (__os.good())
51118611Snjl	_M_ok = true;
52118611Snjl      else
53118611Snjl	{
54151937Sjkim	  _M_ok = false;
55118611Snjl	  __os.setstate(ios_base::failbit);
56151937Sjkim	}
57151937Sjkim    }
58151937Sjkim  
59151937Sjkim  template<typename _CharT, typename _Traits>
60212761Sjkim    basic_ostream<_CharT, _Traits>& 
61193529Sjkim    basic_ostream<_CharT, _Traits>::
62235945Sjkim    operator<<(__ostream_type& (*__pf)(__ostream_type&))
63193529Sjkim    {
64151937Sjkim      sentry __cerb(*this);
65212761Sjkim      if (__cerb)
66193529Sjkim	{ 
67235945Sjkim	  try 
68193529Sjkim	    { __pf(*this); }
69193529Sjkim	  catch(...)
70237412Sjkim	    {
71237412Sjkim	      // 27.6.2.5.1 Common requirements.
72237412Sjkim	      // Turn this on without causing an ios::failure to be thrown.
73193529Sjkim	      this->_M_setstate(ios_base::badbit);
74237412Sjkim	      if ((this->exceptions() & ios_base::badbit) != 0)
75118611Snjl		__throw_exception_again;
76118611Snjl	    }
77118611Snjl	}
78118611Snjl      return *this;
79118611Snjl    }
80118611Snjl  
81118611Snjl  template<typename _CharT, typename _Traits>
82118611Snjl    basic_ostream<_CharT, _Traits>& 
83118611Snjl    basic_ostream<_CharT, _Traits>::
84118611Snjl    operator<<(__ios_type& (*__pf)(__ios_type&))
85118611Snjl    {
86118611Snjl      sentry __cerb(*this);
87118611Snjl      if (__cerb)
88118611Snjl	{ 
89118611Snjl	  try 
90118611Snjl	    { __pf(*this); }
91118611Snjl	  catch(...)
92213806Sjkim	    {
93118611Snjl	      // 27.6.2.5.1 Common requirements.
94118611Snjl	      // Turn this on without causing an ios::failure to be thrown.
95151937Sjkim	      this->_M_setstate(ios_base::badbit);
96151937Sjkim	      if ((this->exceptions() & ios_base::badbit) != 0)
97118611Snjl		__throw_exception_again;
98118611Snjl	    }
99118611Snjl	}
100118611Snjl      return *this;
101118611Snjl    }
102118611Snjl
103118611Snjl  template<typename _CharT, typename _Traits>
104118611Snjl    basic_ostream<_CharT, _Traits>& 
105118611Snjl    basic_ostream<_CharT, _Traits>::
106118611Snjl    operator<<(ios_base& (*__pf)(ios_base&))
107118611Snjl    {
108118611Snjl      sentry __cerb(*this);
109118611Snjl      if (__cerb)
110118611Snjl	{ 
111207344Sjkim	  try 
112207344Sjkim	    { __pf(*this); }
113118611Snjl	  catch(...)
114118611Snjl	    {
115118611Snjl	      // 27.6.2.5.1 Common requirements.
116118611Snjl	      // Turn this on without causing an ios::failure to be thrown.
117118611Snjl	      this->_M_setstate(ios_base::badbit);
118118611Snjl	      if ((this->exceptions() & ios_base::badbit) != 0)
119118611Snjl		__throw_exception_again;
120118611Snjl	    }
121118611Snjl	}
122118611Snjl      return *this;
123118611Snjl    }
124118611Snjl
125118611Snjl  template<typename _CharT, typename _Traits>
126118611Snjl    basic_ostream<_CharT, _Traits>& 
127118611Snjl    basic_ostream<_CharT, _Traits>::operator<<(__streambuf_type* __sbin)
128118611Snjl    {
129118611Snjl      sentry __cerb(*this);
130151937Sjkim      if (__cerb && __sbin)
131151937Sjkim	{
132151937Sjkim	  try
133151937Sjkim	    {
134213806Sjkim	      if (!__copy_streambufs(*this, __sbin, this->rdbuf()))
135151937Sjkim		this->setstate(ios_base::failbit);
136151937Sjkim	    }
137151937Sjkim	  catch(...)
138213806Sjkim	    {
139151937Sjkim	      // 27.6.2.5.1 Common requirements.
140151937Sjkim	      // Turn this on without causing an ios::failure to be thrown.
141213806Sjkim	      this->_M_setstate(ios_base::badbit);
142151937Sjkim	      if ((this->exceptions() & ios_base::badbit) != 0)
143213806Sjkim		__throw_exception_again;
144213806Sjkim	    }
145118611Snjl	}
146118611Snjl      else if (!__sbin)
147118611Snjl	this->setstate(ios_base::badbit);
148118611Snjl      return *this;
149118611Snjl    }
150118611Snjl
151118611Snjl  template<typename _CharT, typename _Traits>
152118611Snjl    basic_ostream<_CharT, _Traits>& 
153118611Snjl    basic_ostream<_CharT, _Traits>::operator<<(bool __n)
154118611Snjl    {
155118611Snjl      sentry __cerb(*this);
156118611Snjl      if (__cerb) 
157118611Snjl	{
158118611Snjl	  try 
159118611Snjl	    {
160118611Snjl	      if (_M_check_facet(_M_fnumput))
161118611Snjl		if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
162118611Snjl		  this->setstate(ios_base::badbit);
163118611Snjl	    }
164118611Snjl	  catch(...)
165118611Snjl	    {
166118611Snjl	      // 27.6.1.2.1 Common requirements.
167118611Snjl	      // Turn this on without causing an ios::failure to be thrown.
168118611Snjl	      this->_M_setstate(ios_base::badbit);
169151937Sjkim	      if ((this->exceptions() & ios_base::badbit) != 0)
170151937Sjkim		__throw_exception_again;
171118611Snjl	    }
172118611Snjl	}
173118611Snjl      return *this;
174118611Snjl    }
175118611Snjl
176118611Snjl  template<typename _CharT, typename _Traits>
177118611Snjl    basic_ostream<_CharT, _Traits>& 
178118611Snjl    basic_ostream<_CharT, _Traits>::operator<<(long __n)
179118611Snjl    {
180118611Snjl      sentry __cerb(*this);
181118611Snjl      if (__cerb) 
182118611Snjl	{
183118611Snjl	  try 
184118611Snjl	    {
185207344Sjkim	      char_type __c = this->fill();
186207344Sjkim	      ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
187118611Snjl	      if (_M_check_facet(_M_fnumput))
188118611Snjl		{
189118611Snjl		  bool __b = false;
190118611Snjl		  if ((__fmt & ios_base::oct) || (__fmt & ios_base::hex))
191118611Snjl		    {
192118611Snjl		      unsigned long __l = static_cast<unsigned long>(__n);
193118611Snjl		      __b = _M_fnumput->put(*this, *this, __c, __l).failed();
194118611Snjl		    }
195118611Snjl		  else
196118611Snjl		    __b = _M_fnumput->put(*this, *this, __c, __n).failed();
197118611Snjl		  if (__b)  
198118611Snjl		    this->setstate(ios_base::badbit);
199118611Snjl		}
200118611Snjl	    }
201118611Snjl	  catch(...)
202118611Snjl	    {
203118611Snjl	      // 27.6.1.2.1 Common requirements.
204118611Snjl	      // Turn this on without causing an ios::failure to be thrown.
205118611Snjl	      this->_M_setstate(ios_base::badbit);
206118611Snjl	      if ((this->exceptions() & ios_base::badbit) != 0)
207118611Snjl		__throw_exception_again;
208118611Snjl	    }
209118611Snjl	}
210118611Snjl      return *this;
211118611Snjl    }
212118611Snjl
213118611Snjl  template<typename _CharT, typename _Traits>
214118611Snjl    basic_ostream<_CharT, _Traits>& 
215118611Snjl    basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n)
216118611Snjl    {
217118611Snjl      sentry __cerb(*this);
218118611Snjl      if (__cerb) 
219118611Snjl	{
220118611Snjl	  try 
221118611Snjl	    {
222118611Snjl	      if (_M_check_facet(_M_fnumput))
223118611Snjl		if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
224118611Snjl		  this->setstate(ios_base::badbit);
225118611Snjl	    }
226118611Snjl	  catch(...)
227118611Snjl	    {
228118611Snjl	      // 27.6.1.2.1 Common requirements.
229118611Snjl	      // Turn this on without causing an ios::failure to be thrown.
230118611Snjl	      this->_M_setstate(ios_base::badbit);
231118611Snjl	      if ((this->exceptions() & ios_base::badbit) != 0)
232118611Snjl		__throw_exception_again;
233118611Snjl	    }
234118611Snjl	}
235118611Snjl      return *this;
236118611Snjl    }
237118611Snjl
238118611Snjl#ifdef _GLIBCPP_USE_LONG_LONG
239118611Snjl  template<typename _CharT, typename _Traits>
240151937Sjkim    basic_ostream<_CharT, _Traits>& 
241151937Sjkim    basic_ostream<_CharT, _Traits>::operator<<(long long __n)
242151937Sjkim    {
243118611Snjl      sentry __cerb(*this);
244118611Snjl      if (__cerb) 
245118611Snjl	{
246118611Snjl	  try 
247118611Snjl	    {
248118611Snjl	      char_type __c = this->fill();
249234623Sjkim	      ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
250118611Snjl	      if (_M_check_facet(_M_fnumput))
251118611Snjl		{
252234623Sjkim		  bool __b = false;
253118611Snjl		  if ((__fmt & ios_base::oct) || (__fmt & ios_base::hex))
254118611Snjl		    {
255118611Snjl		      unsigned long long __l;
256118611Snjl		      __l = static_cast<unsigned long long>(__n);
257118611Snjl		      __b = _M_fnumput->put(*this, *this, __c, __l).failed();
258167802Sjkim		    }
259167802Sjkim		  else
260235945Sjkim		    __b = _M_fnumput->put(*this, *this, __c, __n).failed();
261235945Sjkim		  if (__b)  
262167802Sjkim		    this->setstate(ios_base::badbit);
263167802Sjkim		}
264167802Sjkim	    }
265167802Sjkim	  catch(...)
266167802Sjkim	    {
267167802Sjkim	      // 27.6.1.2.1 Common requirements.
268167802Sjkim	      // Turn this on without causing an ios::failure to be thrown.
269212761Sjkim	      this->_M_setstate(ios_base::badbit);
270167802Sjkim	      if ((this->exceptions() & ios_base::badbit) != 0)
271235945Sjkim		__throw_exception_again;
272167802Sjkim	    }
273167802Sjkim	}
274167802Sjkim      return *this;
275167802Sjkim    }
276167802Sjkim
277167802Sjkim  template<typename _CharT, typename _Traits>
278235945Sjkim    basic_ostream<_CharT, _Traits>& 
279167802Sjkim    basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n)
280167802Sjkim    {
281167802Sjkim      sentry __cerb(*this);
282167802Sjkim      if (__cerb) 
283167802Sjkim	{
284167802Sjkim	  try 
285167802Sjkim	    {
286167802Sjkim	      if (_M_check_facet(_M_fnumput))
287167802Sjkim		if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
288167802Sjkim		  this->setstate(ios_base::badbit);
289167802Sjkim	    }
290167802Sjkim	  catch(...)
291167802Sjkim	    {
292167802Sjkim	      // 27.6.1.2.1 Common requirements.
293167802Sjkim	      // Turn this on without causing an ios::failure to be thrown.
294167802Sjkim	      this->_M_setstate(ios_base::badbit);
295167802Sjkim	      if ((this->exceptions() & ios_base::badbit) != 0)
296167802Sjkim		__throw_exception_again;
297167802Sjkim	    }
298167802Sjkim	}
299167802Sjkim      return *this;
300167802Sjkim    }
301167802Sjkim#endif
302167802Sjkim  
303167802Sjkim  template<typename _CharT, typename _Traits>
304167802Sjkim    basic_ostream<_CharT, _Traits>& 
305167802Sjkim    basic_ostream<_CharT, _Traits>::operator<<(double __n)
306167802Sjkim    {
307167802Sjkim      sentry __cerb(*this);
308167802Sjkim      if (__cerb) 
309167802Sjkim	{
310167802Sjkim	  try 
311167802Sjkim	    {
312167802Sjkim	      if (_M_check_facet(_M_fnumput))
313212761Sjkim		if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
314167802Sjkim		  this->setstate(ios_base::badbit);
315235945Sjkim	    }
316167802Sjkim	  catch(...)
317167802Sjkim	    {
318167802Sjkim	      // 27.6.1.2.1 Common requirements.
319167802Sjkim	      // Turn this on without causing an ios::failure to be thrown.
320167802Sjkim	      this->_M_setstate(ios_base::badbit);
321235945Sjkim	      if ((this->exceptions() & ios_base::badbit) != 0)
322167802Sjkim		__throw_exception_again;
323167802Sjkim	    }
324167802Sjkim	}
325167802Sjkim      return *this;
326167802Sjkim    }
327167802Sjkim  
328167802Sjkim  template<typename _CharT, typename _Traits>
329167802Sjkim    basic_ostream<_CharT, _Traits>& 
330167802Sjkim    basic_ostream<_CharT, _Traits>::operator<<(long double __n)
331167802Sjkim    {
332167802Sjkim      sentry __cerb(*this);
333167802Sjkim      if (__cerb) 
334167802Sjkim	{
335167802Sjkim	  try 
336167802Sjkim	    {
337167802Sjkim	      if (_M_check_facet(_M_fnumput))
338123315Snjl		if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
339123315Snjl		  this->setstate(ios_base::badbit);
340235945Sjkim	    }
341235945Sjkim	  catch(...)
342235945Sjkim	    {
343123315Snjl	      // 27.6.1.2.1 Common requirements.
344151937Sjkim	      // Turn this on without causing an ios::failure to be thrown.
345123315Snjl	      this->_M_setstate(ios_base::badbit);
346167802Sjkim	      if ((this->exceptions() & ios_base::badbit) != 0)
347167802Sjkim		__throw_exception_again;
348167802Sjkim	    }
349167802Sjkim	}
350167802Sjkim      return *this;
351167802Sjkim    }
352123315Snjl
353123315Snjl  template<typename _CharT, typename _Traits>
354123315Snjl    basic_ostream<_CharT, _Traits>& 
355209746Sjkim    basic_ostream<_CharT, _Traits>::operator<<(const void* __n)
356123315Snjl    {
357235945Sjkim      sentry __cerb(*this);
358235945Sjkim      if (__cerb) 
359235945Sjkim	{
360123315Snjl	  try 
361123315Snjl	    {
362123315Snjl	      if (_M_check_facet(_M_fnumput))
363167802Sjkim		if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
364167802Sjkim		  this->setstate(ios_base::badbit);
365123315Snjl	    }
366123315Snjl	  catch(...)
367167802Sjkim	    {
368167802Sjkim	      // 27.6.1.2.1 Common requirements.
369167802Sjkim	      // Turn this on without causing an ios::failure to be thrown.
370123315Snjl	      this->_M_setstate(ios_base::badbit);
371123315Snjl	      if ((this->exceptions() & ios_base::badbit) != 0)
372235945Sjkim		__throw_exception_again;
373123315Snjl	    }
374167802Sjkim	}
375167802Sjkim      return *this;
376167802Sjkim    }
377167802Sjkim
378167802Sjkim  template<typename _CharT, typename _Traits>
379167802Sjkim    basic_ostream<_CharT, _Traits>&
380167802Sjkim    basic_ostream<_CharT, _Traits>::put(char_type __c)
381167802Sjkim    { 
382235945Sjkim      sentry __cerb(*this);
383167802Sjkim      if (__cerb) 
384167802Sjkim	{
385167802Sjkim	  int_type __put = rdbuf()->sputc(__c); 
386167802Sjkim	  if (traits_type::eq_int_type(__put, traits_type::eof()))
387235945Sjkim	    this->setstate(ios_base::badbit);
388167802Sjkim	}
389167802Sjkim      return *this;
390167802Sjkim    }
391167802Sjkim
392167802Sjkim  template<typename _CharT, typename _Traits>
393167802Sjkim    basic_ostream<_CharT, _Traits>&
394167802Sjkim    basic_ostream<_CharT, _Traits>::write(const _CharT* __s, streamsize __n)
395167802Sjkim    {
396167802Sjkim      sentry __cerb(*this);
397167802Sjkim      if (__cerb)
398167802Sjkim	{
399123315Snjl	  streamsize __put = this->rdbuf()->sputn(__s, __n);
400123315Snjl	  if ( __put != __n)
401193529Sjkim	    this->setstate(ios_base::badbit);
402123315Snjl	}
403235945Sjkim      return *this;
404123315Snjl    }
405151937Sjkim
406167802Sjkim  template<typename _CharT, typename _Traits>
407167802Sjkim    basic_ostream<_CharT, _Traits>&
408123315Snjl    basic_ostream<_CharT, _Traits>::flush()
409167802Sjkim    {
410123315Snjl      sentry __cerb(*this);
411123315Snjl      if (__cerb) 
412167802Sjkim	{
413167802Sjkim	  if (this->rdbuf() && this->rdbuf()->pubsync() == -1)
414167802Sjkim	    this->setstate(ios_base::badbit);
415167802Sjkim	}
416167802Sjkim      return *this;
417167802Sjkim    }
418167802Sjkim  
419167802Sjkim  template<typename _CharT, typename _Traits>
420167802Sjkim    typename basic_ostream<_CharT, _Traits>::pos_type
421123315Snjl    basic_ostream<_CharT, _Traits>::tellp()
422123315Snjl    {
423151937Sjkim      pos_type __ret = pos_type(-1);
424151937Sjkim      if (!this->fail())
425235945Sjkim	__ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
426151937Sjkim      return __ret;
427123315Snjl    }
428123315Snjl
429123315Snjl
430123315Snjl  template<typename _CharT, typename _Traits>
431235945Sjkim    basic_ostream<_CharT, _Traits>&
432235945Sjkim    basic_ostream<_CharT, _Traits>::seekp(pos_type __pos)
433235945Sjkim    {
434235945Sjkim      if (!this->fail())
435235945Sjkim	{
436235945Sjkim#ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
437235945Sjkim// 136.  seekp, seekg setting wrong streams?
438235945Sjkim	  pos_type __err = this->rdbuf()->pubseekpos(__pos, ios_base::out);
439123315Snjl
440123315Snjl// 129. Need error indication from seekp() and seekg()
441123315Snjl	  if (__err == pos_type(off_type(-1)))
442235945Sjkim	    this->setstate(ios_base::failbit);
443123315Snjl#endif
444123315Snjl	}
445123315Snjl      return *this;
446123315Snjl    }
447123315Snjl
448123315Snjl  template<typename _CharT, typename _Traits>
449123315Snjl    basic_ostream<_CharT, _Traits>&
450118611Snjl    basic_ostream<_CharT, _Traits>::
451118611Snjl    seekp(off_type __off, ios_base::seekdir __d)
452118611Snjl    {
453118611Snjl      if (!this->fail())
454118611Snjl	{
455118611Snjl#ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
456118611Snjl// 136.  seekp, seekg setting wrong streams?
457118611Snjl	  pos_type __err = this->rdbuf()->pubseekoff(__off, __d, 
458118611Snjl						     ios_base::out);
459118611Snjl
460118611Snjl// 129. Need error indication from seekp() and seekg()
461151937Sjkim	  if (__err == pos_type(off_type(-1)))
462151937Sjkim	    this->setstate(ios_base::failbit);
463118611Snjl#endif
464118611Snjl	}
465151937Sjkim      return *this;
466151937Sjkim    }
467118611Snjl
468118611Snjl  // 27.6.2.5.4 Character inserters.
469151937Sjkim  template<typename _CharT, typename _Traits>
470151937Sjkim    basic_ostream<_CharT, _Traits>&
471151937Sjkim    operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
472118611Snjl    {
473233250Sjkim      typedef basic_ostream<_CharT, _Traits> __ostream_type;
474234623Sjkim      typename __ostream_type::sentry __cerb(__out);
475233250Sjkim      if (__cerb)
476234623Sjkim	{
477234623Sjkim	  try 
478234623Sjkim	    {
479234623Sjkim	      const streamsize __w = __out.width() > 0 ? __out.width() : 0;
480234623Sjkim	      _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * (__w + 1)));
481234623Sjkim	      __pads[0] = __c;
482234623Sjkim	      streamsize __len = 1;
483234623Sjkim	      if (__w > __len)
484234623Sjkim		{
485233250Sjkim		  __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __pads, 
486234623Sjkim						 &__c, __w, __len, false);
487233250Sjkim		  __len = __w;
488118611Snjl		}
489118611Snjl	      __out.write(__pads, __len);
490151937Sjkim	      __out.width(0);
491118611Snjl	    }
492151937Sjkim	  catch(...)
493118611Snjl	    {
494118611Snjl	      // 27.6.1.2.1 Common requirements.
495118611Snjl	      // Turn this on without causing an ios::failure to be thrown.
496151937Sjkim	      __out._M_setstate(ios_base::badbit);
497118611Snjl	      if ((__out.exceptions() & ios_base::badbit) != 0)
498118611Snjl		__throw_exception_again;
499118611Snjl	    }
500118611Snjl	}
501118611Snjl      return __out;
502118611Snjl    }
503234623Sjkim  
504234623Sjkim  // Specializations.
505234623Sjkim  template <class _Traits> 
506234623Sjkim    basic_ostream<char, _Traits>&
507234623Sjkim    operator<<(basic_ostream<char, _Traits>& __out, char __c)
508234623Sjkim    {
509234623Sjkim      typedef basic_ostream<char, _Traits> __ostream_type;
510234623Sjkim      typename __ostream_type::sentry __cerb(__out);
511234623Sjkim      if (__cerb)
512234623Sjkim	{
513234623Sjkim	  try 
514233250Sjkim	    {
515118611Snjl	      const streamsize __w = __out.width() > 0 ? __out.width() : 0;
516118611Snjl	      char* __pads = static_cast<char*>(__builtin_alloca(__w + 1));
517167802Sjkim	      __pads[0] = __c;
518167802Sjkim	      streamsize __len = 1;
519167802Sjkim	      if (__w > __len)
520167802Sjkim		{
521118611Snjl		  __pad<char, _Traits>::_S_pad(__out, __out.fill(), __pads, 
522151937Sjkim					       &__c, __w, __len, false);
523118611Snjl		  __len = __w;
524118611Snjl		}
525118611Snjl	      __out.write(__pads, __len);
526151937Sjkim	      __out.width(0);
527118611Snjl	    }
528151937Sjkim	  catch(...)
529151937Sjkim	    {
530151937Sjkim	      // 27.6.1.2.1 Common requirements.
531118611Snjl	      // Turn this on without causing an ios::failure to be thrown.
532118611Snjl	      __out._M_setstate(ios_base::badbit);
533118611Snjl	      if ((__out.exceptions() & ios_base::badbit) != 0)
534151937Sjkim		__throw_exception_again;
535118611Snjl	    }
536151937Sjkim	}
537151937Sjkim      return __out;
538151937Sjkim     }
539118611Snjl
540118611Snjl  template<typename _CharT, typename _Traits>
541118611Snjl    basic_ostream<_CharT, _Traits>&
542118611Snjl    operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
543118611Snjl    {
544118611Snjl      typedef basic_ostream<_CharT, _Traits> __ostream_type;
545151937Sjkim      typename __ostream_type::sentry __cerb(__out);
546118611Snjl      if (__cerb && __s)
547233250Sjkim	{
548118611Snjl	  try 
549118611Snjl	    {
550118611Snjl	      const streamsize __w = __out.width() > 0 ? __out.width() : 0;
551118611Snjl	      _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
552118611Snjl	      streamsize __len = static_cast<streamsize>(_Traits::length(__s));
553118611Snjl	      if (__w > __len)
554118611Snjl		{
555118611Snjl		  __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __pads, 
556151937Sjkim						 __s, __w, __len, false);
557151937Sjkim		  __s = __pads;
558151937Sjkim		  __len = __w;
559151937Sjkim		}
560151937Sjkim	      __out.write(__s, __len);
561151937Sjkim	      __out.width(0);
562118611Snjl	    }
563151937Sjkim	  catch(...)
564151937Sjkim	    {
565151937Sjkim	      // 27.6.1.2.1 Common requirements.
566151937Sjkim	      // Turn this on without causing an ios::failure to be thrown.
567151937Sjkim	      __out._M_setstate(ios_base::badbit);
568151937Sjkim	      if ((__out.exceptions() & ios_base::badbit) != 0)
569151937Sjkim		__throw_exception_again;
570151937Sjkim	    }
571151937Sjkim	}
572118611Snjl      else if (!__s)
573118611Snjl	__out.setstate(ios_base::badbit);
574151937Sjkim      return __out;
575118611Snjl    }
576151937Sjkim
577151937Sjkim  template<typename _CharT, typename _Traits>
578151937Sjkim    basic_ostream<_CharT, _Traits>&
579118611Snjl    operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
580118611Snjl    {
581118611Snjl      typedef basic_ostream<_CharT, _Traits> __ostream_type;
582234623Sjkim#ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
583234623Sjkim// 167.  Improper use of traits_type::length()
584118611Snjl// Note that this is only in 'Review' status.
585118611Snjl      typedef char_traits<char>		     __traits_type;
586234623Sjkim#endif
587118611Snjl      typename __ostream_type::sentry __cerb(__out);
588234623Sjkim      if (__cerb && __s)
589234623Sjkim	{
590118611Snjl	  size_t __clen = __traits_type::length(__s);
591233250Sjkim	  _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * (__clen + 1)));
592118611Snjl	  for (size_t  __i = 0; __i < __clen; ++__i)
593118611Snjl	    __ws[__i] = __out.widen(__s[__i]);
594118611Snjl	  _CharT* __str = __ws;
595118611Snjl	  
596118611Snjl	  try 
597118611Snjl	    {
598118611Snjl	      streamsize __len = static_cast<streamsize>(__clen);
599118611Snjl	      const streamsize __w = __out.width() > 0 ? __out.width() : 0;
600118611Snjl	      _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
601151937Sjkim	      
602118611Snjl	      if (__w > __len)
603151937Sjkim		{
604118611Snjl		  __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __pads, 
605118611Snjl						 __ws, __w, __len, false);
606233250Sjkim		  __str = __pads;
607118611Snjl		  __len = __w;
608118611Snjl		}
609167802Sjkim	      __out.write(__str, __len);
610118611Snjl	      __out.width(0);
611151937Sjkim	    }
612118611Snjl	  catch(...)
613118611Snjl	    {
614118611Snjl	      // 27.6.1.2.1 Common requirements.
615233250Sjkim	      // Turn this on without causing an ios::failure to be thrown.
616118611Snjl	      __out._M_setstate(ios_base::badbit);
617118611Snjl	      if ((__out.exceptions() & ios_base::badbit) != 0)
618167802Sjkim		__throw_exception_again;
619167802Sjkim	    }
620167802Sjkim	}
621167802Sjkim      else if (!__s)
622167802Sjkim	__out.setstate(ios_base::badbit);
623118611Snjl      return __out;
624118611Snjl    }
625118611Snjl
626118611Snjl  // Partial specializations.
627118611Snjl  template<class _Traits>
628118611Snjl    basic_ostream<char, _Traits>&
629151937Sjkim    operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
630118611Snjl    {
631118611Snjl      typedef basic_ostream<char, _Traits> __ostream_type;
632118611Snjl      typename __ostream_type::sentry __cerb(__out);
633151937Sjkim      if (__cerb && __s)
634151937Sjkim	{
635151937Sjkim	  try 
636151937Sjkim	    {
637118611Snjl	      const streamsize __w = __out.width() > 0 ? __out.width() : 0;
638118611Snjl	      char* __pads = static_cast<char*>(__builtin_alloca(__w));
639118611Snjl	      streamsize __len = static_cast<streamsize>(_Traits::length(__s));
640151937Sjkim
641151937Sjkim	      if (__w > __len)
642218590Sjkim		{
643218590Sjkim		  __pad<char, _Traits>::_S_pad(__out, __out.fill(), __pads, 
644151937Sjkim						 __s, __w, __len, false);
645118611Snjl		  __s = __pads;
646118611Snjl		  __len = __w;
647118611Snjl		}
648151937Sjkim	      __out.write(__s, __len);
649151937Sjkim	      __out.width(0);
650218590Sjkim	    }
651218590Sjkim	  catch(...)
652151937Sjkim	    {
653118611Snjl	      // 27.6.1.2.1 Common requirements.
654118611Snjl	      // Turn this on without causing an ios::failure to be thrown.
655118611Snjl	      __out._M_setstate(ios_base::badbit);
656151937Sjkim	      if ((__out.exceptions() & ios_base::badbit) != 0)
657151937Sjkim		__throw_exception_again;
658218590Sjkim	    }
659151937Sjkim	}
660218590Sjkim      else if (!__s)
661151937Sjkim	__out.setstate(ios_base::badbit);
662118611Snjl      return __out;
663118611Snjl    }
664118611Snjl
665151937Sjkim  // 21.3.7.9 basic_string::operator<<
666118611Snjl  template<typename _CharT, typename _Traits, typename _Alloc>
667151937Sjkim    basic_ostream<_CharT, _Traits>&
668151937Sjkim    operator<<(basic_ostream<_CharT, _Traits>& __out,
669151937Sjkim	       const basic_string<_CharT, _Traits, _Alloc>& __str)
670151937Sjkim    { 
671151937Sjkim      typedef basic_ostream<_CharT, _Traits> __ostream_type;
672118611Snjl      typename __ostream_type::sentry __cerb(__out);
673118611Snjl      if (__cerb)
674118611Snjl	{
675151937Sjkim	  const _CharT* __s = __str.data();
676118611Snjl	  const streamsize __w = __out.width() > 0 ? __out.width() : 0;
677151937Sjkim	  _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
678118611Snjl	  streamsize __len = static_cast<streamsize>(__str.size());
679151937Sjkim#ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
680118611Snjl	  // 25. String operator<< uses width() value wrong
681151937Sjkim#endif
682118611Snjl	  if (__w > __len)
683151937Sjkim	    {
684118611Snjl	      __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __pads, __s, 
685118611Snjl					     __w, __len, false);
686233250Sjkim	      __s = __pads;
687233250Sjkim	      __len = __w;
688233250Sjkim	    }
689233250Sjkim	  streamsize __res = __out.rdbuf()->sputn(__s, __len);
690233250Sjkim	  __out.width(0);
691118611Snjl	  if (__res != __len)
692118611Snjl	    __out.setstate(ios_base::failbit);
693151937Sjkim	}
694151937Sjkim      return __out;
695151937Sjkim    }
696151937Sjkim
697151937Sjkim  // Inhibit implicit instantiations for required instantiations,
698151937Sjkim  // which are defined via explicit instantiations elsewhere.  
699151937Sjkim  // NB:  This syntax is a GNU extension.
700151937Sjkim#if _GLIBCPP_EXTERN_TEMPLATE
701151937Sjkim  extern template class basic_ostream<char>;
702151937Sjkim  extern template ostream& endl(ostream&);
703151937Sjkim  extern template ostream& ends(ostream&);
704151937Sjkim  extern template ostream& flush(ostream&);
705151937Sjkim  extern template ostream& operator<<(ostream&, char);
706118611Snjl  extern template ostream& operator<<(ostream&, unsigned char);
707151937Sjkim  extern template ostream& operator<<(ostream&, signed char);
708151937Sjkim  extern template ostream& operator<<(ostream&, const char*);
709118611Snjl  extern template ostream& operator<<(ostream&, const unsigned char*);
710118611Snjl  extern template ostream& operator<<(ostream&, const signed char*);
711118611Snjl
712118611Snjl#ifdef _GLIBCPP_USE_WCHAR_T
713118611Snjl  extern template class basic_ostream<wchar_t>;
714118611Snjl  extern template wostream& endl(wostream&);
715118611Snjl  extern template wostream& ends(wostream&);
716118611Snjl  extern template wostream& flush(wostream&);
717118611Snjl  extern template wostream& operator<<(wostream&, wchar_t);
718151937Sjkim  extern template wostream& operator<<(wostream&, char);
719118611Snjl  extern template wostream& operator<<(wostream&, const wchar_t*);
720118611Snjl  extern template wostream& operator<<(wostream&, const char*);
721118611Snjl#endif
722118611Snjl#endif
723118611Snjl} // namespace std
724237412Sjkim