OSDN Git Service

2002-01-24 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / ostream.tcc
1 // Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
2 //
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)
7 // any later version.
8
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.
13
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,
17 // USA.
18
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.
27
28 //
29 // ISO C++ 14882: 27.6.2  Output streams
30 //
31
32 #include <locale>
33
34 namespace std 
35 {
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)
40     {
41       // XXX MT 
42       if (_M_ok && __os.tie())
43         __os.tie()->flush();  
44     }
45   
46   template<typename _CharT, typename _Traits>
47     basic_ostream<_CharT, _Traits>& 
48     basic_ostream<_CharT, _Traits>::
49     operator<<(__ostream_type& (*__pf)(__ostream_type&))
50     {
51       sentry __cerb(*this);
52       if (__cerb)
53         { 
54           try 
55             { __pf(*this); }
56           catch(exception& __fail)
57             {
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;
63             }
64         }
65       return *this;
66     }
67   
68   template<typename _CharT, typename _Traits>
69     basic_ostream<_CharT, _Traits>& 
70     basic_ostream<_CharT, _Traits>::
71     operator<<(__ios_type& (*__pf)(__ios_type&))
72     {
73       sentry __cerb(*this);
74       if (__cerb)
75         { 
76           try 
77             { __pf(*this); }
78           catch(exception& __fail)
79             {
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;
85             }
86         }
87       return *this;
88     }
89
90   template<typename _CharT, typename _Traits>
91     basic_ostream<_CharT, _Traits>& 
92     basic_ostream<_CharT, _Traits>::
93     operator<<(ios_base& (*__pf)(ios_base&))
94     {
95       sentry __cerb(*this);
96       if (__cerb)
97         { 
98           try 
99             { __pf(*this); }
100           catch(exception& __fail)
101             {
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;
107             }
108         }
109       return *this;
110     }
111
112   template<typename _CharT, typename _Traits>
113     basic_ostream<_CharT, _Traits>& 
114     basic_ostream<_CharT, _Traits>::operator<<(__streambuf_type* __sbin)
115     {
116       sentry __cerb(*this);
117       if (__cerb)
118         {
119           try
120             {
121               streamsize __xtrct = 0;
122               if (__sbin)
123                 {
124                   __streambuf_type* __sbout = this->rdbuf();
125                   __xtrct = __copy_streambufs(*this, __sbin, __sbout);
126                 }
127               else
128                 this->setstate(ios_base::badbit);
129               if (!__xtrct)
130                 this->setstate(ios_base::failbit);
131             }
132           catch(exception& __fail)
133             {
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;
139             }
140         }
141       return *this;
142     }
143
144   template<typename _CharT, typename _Traits>
145     basic_ostream<_CharT, _Traits>& 
146     basic_ostream<_CharT, _Traits>::operator<<(bool __n)
147     {
148       sentry __cerb(*this);
149       if (__cerb) 
150         {
151           try 
152             {
153               if (_M_check_facet(_M_fnumput))
154                 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
155                   this->setstate(ios_base::badbit);
156             }
157           catch(exception& __fail)
158             {
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;
164             }
165         }
166       return *this;
167     }
168
169   template<typename _CharT, typename _Traits>
170     basic_ostream<_CharT, _Traits>& 
171     basic_ostream<_CharT, _Traits>::operator<<(long __n)
172     {
173       sentry __cerb(*this);
174       if (__cerb) 
175         {
176           try 
177             {
178               char_type __c = this->fill();
179               ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
180               if (_M_check_facet(_M_fnumput))
181                 {
182                   bool __b = false;
183                   if (__fmt & ios_base::oct || __fmt & ios_base::hex)
184                     {
185                       unsigned long __l = static_cast<unsigned long>(__n);
186                       __b = _M_fnumput->put(*this, *this, __c, __l).failed();
187                     }
188                   else
189                     __b = _M_fnumput->put(*this, *this, __c, __n).failed();
190                   if (__b)  
191                     this->setstate(ios_base::badbit);
192                 }
193             }
194           catch(exception& __fail)
195             {
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;
201             }
202         }
203       return *this;
204     }
205
206   template<typename _CharT, typename _Traits>
207     basic_ostream<_CharT, _Traits>& 
208     basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n)
209     {
210       sentry __cerb(*this);
211       if (__cerb) 
212         {
213           try 
214             {
215               if (_M_check_facet(_M_fnumput))
216                 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
217                   this->setstate(ios_base::badbit);
218             }
219           catch(exception& __fail)
220             {
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;
226             }
227         }
228       return *this;
229     }
230
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)
235     {
236       sentry __cerb(*this);
237       if (__cerb) 
238         {
239           try 
240             {
241               char_type __c = this->fill();
242               ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
243               if (_M_check_facet(_M_fnumput))
244                 {
245                   bool __b = false;
246                   if (__fmt & ios_base::oct || __fmt & ios_base::hex)
247                     {
248                       unsigned long long __l;
249                       __l = static_cast<unsigned long long>(__n);
250                       __b = _M_fnumput->put(*this, *this, __c, __l).failed();
251                     }
252                   else
253                     __b = _M_fnumput->put(*this, *this, __c, __n).failed();
254                   if (__b)  
255                     this->setstate(ios_base::badbit);
256                 }
257             }
258           catch(exception& __fail)
259             {
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;
265             }
266         }
267       return *this;
268     }
269
270   template<typename _CharT, typename _Traits>
271     basic_ostream<_CharT, _Traits>& 
272     basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n)
273     {
274       sentry __cerb(*this);
275       if (__cerb) 
276         {
277           try 
278             {
279               if (_M_check_facet(_M_fnumput))
280                 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
281                   this->setstate(ios_base::badbit);
282             }
283           catch(exception& __fail)
284             {
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;
290             }
291         }
292       return *this;
293     }
294 #endif
295   
296   template<typename _CharT, typename _Traits>
297     basic_ostream<_CharT, _Traits>& 
298     basic_ostream<_CharT, _Traits>::operator<<(double __n)
299     {
300       sentry __cerb(*this);
301       if (__cerb) 
302         {
303           try 
304             {
305               if (_M_check_facet(_M_fnumput))
306                 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
307                   this->setstate(ios_base::badbit);
308             }
309           catch(exception& __fail)
310             {
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;
316             }
317         }
318       return *this;
319     }
320   
321   template<typename _CharT, typename _Traits>
322     basic_ostream<_CharT, _Traits>& 
323     basic_ostream<_CharT, _Traits>::operator<<(long double __n)
324     {
325       sentry __cerb(*this);
326       if (__cerb) 
327         {
328           try 
329             {
330               if (_M_check_facet(_M_fnumput))
331                 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
332                   this->setstate(ios_base::badbit);
333             }
334           catch(exception& __fail)
335             {
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;
341             }
342         }
343       return *this;
344     }
345
346   template<typename _CharT, typename _Traits>
347     basic_ostream<_CharT, _Traits>& 
348     basic_ostream<_CharT, _Traits>::operator<<(const void* __n)
349     {
350       sentry __cerb(*this);
351       if (__cerb) 
352         {
353           try 
354             {
355               if (_M_check_facet(_M_fnumput))
356                 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
357                   this->setstate(ios_base::badbit);
358             }
359           catch(exception& __fail)
360             {
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;
366             }
367         }
368       return *this;
369     }
370
371   template<typename _CharT, typename _Traits>
372     basic_ostream<_CharT, _Traits>&
373     basic_ostream<_CharT, _Traits>::put(char_type __c)
374     { 
375       sentry __cerb(*this);
376       if (__cerb) 
377         {
378           int_type __put = rdbuf()->sputc(__c); 
379           if (traits_type::eq_int_type(__put, traits_type::eof()))
380             this->setstate(ios_base::badbit);
381         }
382       return *this;
383     }
384
385   template<typename _CharT, typename _Traits>
386     basic_ostream<_CharT, _Traits>&
387     basic_ostream<_CharT, _Traits>::write(const _CharT* __s, streamsize __n)
388     {
389       sentry __cerb(*this);
390       if (__cerb)
391         {
392           streamsize __put = this->rdbuf()->sputn(__s, __n);
393           if ( __put != __n)
394             this->setstate(ios_base::badbit);
395         }
396       return *this;
397     }
398
399   template<typename _CharT, typename _Traits>
400     basic_ostream<_CharT, _Traits>&
401     basic_ostream<_CharT, _Traits>::flush()
402     {
403       sentry __cerb(*this);
404       if (__cerb) 
405         {
406           if (this->rdbuf() && this->rdbuf()->pubsync() == -1)
407             this->setstate(ios_base::badbit);
408         }
409       return *this;
410     }
411   
412   template<typename _CharT, typename _Traits>
413     typename basic_ostream<_CharT, _Traits>::pos_type
414     basic_ostream<_CharT, _Traits>::tellp()
415     {
416       pos_type __ret = pos_type(-1);
417       bool __testok = this->fail() != true;
418       
419       if (__testok)
420         __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
421       return __ret;
422     }
423
424
425   template<typename _CharT, typename _Traits>
426     basic_ostream<_CharT, _Traits>&
427     basic_ostream<_CharT, _Traits>::seekp(pos_type __pos)
428     {
429       bool __testok = this->fail() != true;
430       
431       if (__testok)
432         {
433 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
434 // 136.  seekp, seekg setting wrong streams?
435           pos_type __err = this->rdbuf()->pubseekpos(__pos, ios_base::out);
436
437 // 129. Need error indication from seekp() and seekg()
438           if (__err == pos_type(off_type(-1)))
439             this->setstate(ios_base::failbit);
440 #endif
441         }
442       return *this;
443     }
444
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)
449     {
450       bool __testok = this->fail() != true;
451       
452       if (__testok)
453         {
454 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
455 // 136.  seekp, seekg setting wrong streams?
456           pos_type __err = this->rdbuf()->pubseekoff(__off, __d, 
457                                                      ios_base::out);
458
459 // 129. Need error indication from seekp() and seekg()
460           if (__err == pos_type(off_type(-1)))
461             this->setstate(ios_base::failbit);
462         }
463 #endif
464       return *this;
465     }
466
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)
471     {
472       typedef basic_ostream<_CharT, _Traits> __ostream_type;
473       typename __ostream_type::sentry __cerb(__out);
474       if (__cerb)
475         {
476           try 
477             {
478               streamsize __w = __out.width();
479               _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
480               __pads[0] = __c;
481               streamsize __len = 1;
482               if (__w > __len)
483                 {
484                   __pad(__out, __out.fill(), __pads, &__c, __w, __len, false);
485                   __len = __w;
486                 }
487               __out.write(__pads, __len);
488               __out.width(0);
489             }
490           catch(exception& __fail)
491             {
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;
497             }
498         }
499       return __out;
500     }
501   
502   // Specializations.
503   template <class _Traits> 
504     basic_ostream<char, _Traits>&
505     operator<<(basic_ostream<char, _Traits>& __out, char __c)
506     {
507       typedef basic_ostream<char, _Traits> __ostream_type;
508       typename __ostream_type::sentry __cerb(__out);
509       if (__cerb)
510         {
511           try 
512             {
513               streamsize __w = __out.width();
514               char* __pads = static_cast<char*>(__builtin_alloca(__w + 1));
515               __pads[0] = __c;
516               streamsize __len = 1;
517               if (__w > __len)
518                 {
519                   __pad(__out, __out.fill(), __pads, &__c, __w, __len, false);
520                   __len = __w;
521                 }
522               __out.write(__pads, __len);
523               __out.width(0);
524             }
525           catch(exception& __fail)
526             {
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;
532             }
533         }
534       return __out;
535      }
536
537   template<typename _CharT, typename _Traits>
538     basic_ostream<_CharT, _Traits>&
539     operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
540     {
541       typedef basic_ostream<_CharT, _Traits> __ostream_type;
542       typename __ostream_type::sentry __cerb(__out);
543       if (__cerb)
544         {
545           try 
546             {
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));
550               if (__w > __len)
551                 {
552                   __pad(__out, __out.fill(), __pads, __s, __w, __len, false);
553                   __s = __pads;
554                   __len = __w;
555                 }
556               __out.write(__s, __len);
557               __out.width(0);
558             }
559           catch(exception& __fail)
560             {
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;
566             }
567         }
568       return __out;
569     }
570
571   template<typename _CharT, typename _Traits>
572     basic_ostream<_CharT, _Traits>&
573     operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
574     {
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;
580 #endif
581       typename __ostream_type::sentry __cerb(__out);
582       if (__cerb)
583         {
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;
589           
590           try 
591             {
592               streamsize __len = static_cast<streamsize>(__clen);
593               streamsize __w = __out.width();
594               _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
595               
596               if (__w > __len)
597                 {
598                   __pad(__out, __out.fill(), __pads, __ws, __w, __len, false);
599                   __str = __pads;
600                   __len = __w;
601                 }
602               __out.write(__str, __len);
603               __out.width(0);
604             }
605           catch(exception& __fail)
606             {
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;
612             }
613         }
614       return __out;
615     }
616
617   // Partial specializations.
618   template<class _Traits>
619     basic_ostream<char, _Traits>&
620     operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
621     {
622       typedef basic_ostream<char, _Traits> __ostream_type;
623       typename __ostream_type::sentry __cerb(__out);
624       if (__cerb)
625         {
626           try 
627             {
628               streamsize __w = __out.width();
629               char* __pads = static_cast<char*>(__builtin_alloca(__w));
630               streamsize __len = static_cast<streamsize>(_Traits::length(__s));
631               if (__w > __len)
632                 {
633                   __pad(__out, __out.fill(), __pads, __s, __w, __len, false);
634                   __s = __pads;
635                   __len = __w;
636                 }
637               __out.write(__s, __len);
638               __out.width(0);
639             }
640           catch(exception& __fail)
641             {
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;
647             }
648         }
649       return __out;
650     }
651
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)
657     { 
658       typedef basic_ostream<_CharT, _Traits> __ostream_type;
659       typename __ostream_type::sentry __cerb(__out);
660       if (__cerb)
661         {
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
668 #endif
669           if (__w > __len)
670             {
671               __pad(__out, __out.fill(), __pads, __s, __w, __len, false);
672               __s = __pads;
673               __len = __w;
674             }
675           streamsize __res = __out.rdbuf()->sputn(__s, __len);
676           __out.width(0);
677           if (__res != __len)
678             __out.setstate(ios_base::failbit);
679         }
680       return __out;
681     }
682 } // namespace std
683  
684 // Local Variables:
685 // mode:C++
686 // End: