OSDN Git Service

e8c2a4f01ca3054d46d1d77d7e8b6362aab06412
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / ostream.tcc
1 // ostream classes -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
4 // Free Software Foundation, Inc.
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
35 #pragma GCC system_header
36
37 #include <locale>
38
39 namespace std 
40 {
41   template<typename _CharT, typename _Traits>
42     basic_ostream<_CharT, _Traits>::sentry::
43     sentry(basic_ostream<_CharT,_Traits>& __os)
44     : _M_ok(__os.good()), _M_os(__os)
45     {
46       // XXX MT 
47       if (_M_ok && __os.tie())
48         __os.tie()->flush();  
49     }
50   
51   template<typename _CharT, typename _Traits>
52     basic_ostream<_CharT, _Traits>& 
53     basic_ostream<_CharT, _Traits>::
54     operator<<(__ostream_type& (*__pf)(__ostream_type&))
55     {
56       sentry __cerb(*this);
57       if (__cerb)
58         { 
59           try 
60             { __pf(*this); }
61           catch(exception& __fail)
62             {
63               // 27.6.2.5.1 Common requirements.
64               // Turn this on without causing an ios::failure to be thrown.
65               this->setstate(ios_base::badbit);
66               if ((this->exceptions() & ios_base::badbit) != 0)
67                 __throw_exception_again;
68             }
69         }
70       return *this;
71     }
72   
73   template<typename _CharT, typename _Traits>
74     basic_ostream<_CharT, _Traits>& 
75     basic_ostream<_CharT, _Traits>::
76     operator<<(__ios_type& (*__pf)(__ios_type&))
77     {
78       sentry __cerb(*this);
79       if (__cerb)
80         { 
81           try 
82             { __pf(*this); }
83           catch(exception& __fail)
84             {
85               // 27.6.2.5.1 Common requirements.
86               // Turn this on without causing an ios::failure to be thrown.
87               this->setstate(ios_base::badbit);
88               if ((this->exceptions() & ios_base::badbit) != 0)
89                 __throw_exception_again;
90             }
91         }
92       return *this;
93     }
94
95   template<typename _CharT, typename _Traits>
96     basic_ostream<_CharT, _Traits>& 
97     basic_ostream<_CharT, _Traits>::
98     operator<<(ios_base& (*__pf)(ios_base&))
99     {
100       sentry __cerb(*this);
101       if (__cerb)
102         { 
103           try 
104             { __pf(*this); }
105           catch(exception& __fail)
106             {
107               // 27.6.2.5.1 Common requirements.
108               // Turn this on without causing an ios::failure to be thrown.
109               this->setstate(ios_base::badbit);
110               if ((this->exceptions() & ios_base::badbit) != 0)
111                 __throw_exception_again;
112             }
113         }
114       return *this;
115     }
116
117   template<typename _CharT, typename _Traits>
118     basic_ostream<_CharT, _Traits>& 
119     basic_ostream<_CharT, _Traits>::operator<<(__streambuf_type* __sbin)
120     {
121       sentry __cerb(*this);
122       if (__cerb)
123         {
124           try
125             {
126               streamsize __xtrct = 0;
127               if (__sbin)
128                 {
129                   __streambuf_type* __sbout = this->rdbuf();
130                   __xtrct = __copy_streambufs(*this, __sbin, __sbout);
131                 }
132               else
133                 this->setstate(ios_base::badbit);
134               if (!__xtrct)
135                 this->setstate(ios_base::failbit);
136             }
137           catch(exception& __fail)
138             {
139               // 27.6.2.5.1 Common requirements.
140               // Turn this on without causing an ios::failure to be thrown.
141               this->setstate(ios_base::badbit);
142               if ((this->exceptions() & ios_base::badbit) != 0)
143                 __throw_exception_again;
144             }
145         }
146       return *this;
147     }
148
149   template<typename _CharT, typename _Traits>
150     basic_ostream<_CharT, _Traits>& 
151     basic_ostream<_CharT, _Traits>::operator<<(bool __n)
152     {
153       sentry __cerb(*this);
154       if (__cerb) 
155         {
156           try 
157             {
158               if (_M_check_facet(_M_fnumput))
159                 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
160                   this->setstate(ios_base::badbit);
161             }
162           catch(exception& __fail)
163             {
164               // 27.6.1.2.1 Common requirements.
165               // Turn this on without causing an ios::failure to be thrown.
166               this->setstate(ios_base::badbit);
167               if ((this->exceptions() & ios_base::badbit) != 0)
168                 __throw_exception_again;
169             }
170         }
171       return *this;
172     }
173
174   template<typename _CharT, typename _Traits>
175     basic_ostream<_CharT, _Traits>& 
176     basic_ostream<_CharT, _Traits>::operator<<(long __n)
177     {
178       sentry __cerb(*this);
179       if (__cerb) 
180         {
181           try 
182             {
183               char_type __c = this->fill();
184               ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
185               if (_M_check_facet(_M_fnumput))
186                 {
187                   bool __b = false;
188                   if (__fmt & ios_base::oct || __fmt & ios_base::hex)
189                     {
190                       unsigned long __l = static_cast<unsigned long>(__n);
191                       __b = _M_fnumput->put(*this, *this, __c, __l).failed();
192                     }
193                   else
194                     __b = _M_fnumput->put(*this, *this, __c, __n).failed();
195                   if (__b)  
196                     this->setstate(ios_base::badbit);
197                 }
198             }
199           catch(exception& __fail)
200             {
201               // 27.6.1.2.1 Common requirements.
202               // Turn this on without causing an ios::failure to be thrown.
203               this->setstate(ios_base::badbit);
204               if ((this->exceptions() & ios_base::badbit) != 0)
205                 __throw_exception_again;
206             }
207         }
208       return *this;
209     }
210
211   template<typename _CharT, typename _Traits>
212     basic_ostream<_CharT, _Traits>& 
213     basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n)
214     {
215       sentry __cerb(*this);
216       if (__cerb) 
217         {
218           try 
219             {
220               if (_M_check_facet(_M_fnumput))
221                 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
222                   this->setstate(ios_base::badbit);
223             }
224           catch(exception& __fail)
225             {
226               // 27.6.1.2.1 Common requirements.
227               // Turn this on without causing an ios::failure to be thrown.
228               this->setstate(ios_base::badbit);
229               if ((this->exceptions() & ios_base::badbit) != 0)
230                 __throw_exception_again;
231             }
232         }
233       return *this;
234     }
235
236 #ifdef _GLIBCPP_USE_LONG_LONG
237   template<typename _CharT, typename _Traits>
238     basic_ostream<_CharT, _Traits>& 
239     basic_ostream<_CharT, _Traits>::operator<<(long long __n)
240     {
241       sentry __cerb(*this);
242       if (__cerb) 
243         {
244           try 
245             {
246               char_type __c = this->fill();
247               ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
248               if (_M_check_facet(_M_fnumput))
249                 {
250                   bool __b = false;
251                   if (__fmt & ios_base::oct || __fmt & ios_base::hex)
252                     {
253                       unsigned long long __l;
254                       __l = static_cast<unsigned long long>(__n);
255                       __b = _M_fnumput->put(*this, *this, __c, __l).failed();
256                     }
257                   else
258                     __b = _M_fnumput->put(*this, *this, __c, __n).failed();
259                   if (__b)  
260                     this->setstate(ios_base::badbit);
261                 }
262             }
263           catch(exception& __fail)
264             {
265               // 27.6.1.2.1 Common requirements.
266               // Turn this on without causing an ios::failure to be thrown.
267               this->setstate(ios_base::badbit);
268               if ((this->exceptions() & ios_base::badbit) != 0)
269                 __throw_exception_again;
270             }
271         }
272       return *this;
273     }
274
275   template<typename _CharT, typename _Traits>
276     basic_ostream<_CharT, _Traits>& 
277     basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n)
278     {
279       sentry __cerb(*this);
280       if (__cerb) 
281         {
282           try 
283             {
284               if (_M_check_facet(_M_fnumput))
285                 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
286                   this->setstate(ios_base::badbit);
287             }
288           catch(exception& __fail)
289             {
290               // 27.6.1.2.1 Common requirements.
291               // Turn this on without causing an ios::failure to be thrown.
292               this->setstate(ios_base::badbit);
293               if ((this->exceptions() & ios_base::badbit) != 0)
294                 __throw_exception_again;
295             }
296         }
297       return *this;
298     }
299 #endif
300   
301   template<typename _CharT, typename _Traits>
302     basic_ostream<_CharT, _Traits>& 
303     basic_ostream<_CharT, _Traits>::operator<<(double __n)
304     {
305       sentry __cerb(*this);
306       if (__cerb) 
307         {
308           try 
309             {
310               if (_M_check_facet(_M_fnumput))
311                 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
312                   this->setstate(ios_base::badbit);
313             }
314           catch(exception& __fail)
315             {
316               // 27.6.1.2.1 Common requirements.
317               // Turn this on without causing an ios::failure to be thrown.
318               this->setstate(ios_base::badbit);
319               if ((this->exceptions() & ios_base::badbit) != 0)
320                 __throw_exception_again;
321             }
322         }
323       return *this;
324     }
325   
326   template<typename _CharT, typename _Traits>
327     basic_ostream<_CharT, _Traits>& 
328     basic_ostream<_CharT, _Traits>::operator<<(long double __n)
329     {
330       sentry __cerb(*this);
331       if (__cerb) 
332         {
333           try 
334             {
335               if (_M_check_facet(_M_fnumput))
336                 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
337                   this->setstate(ios_base::badbit);
338             }
339           catch(exception& __fail)
340             {
341               // 27.6.1.2.1 Common requirements.
342               // Turn this on without causing an ios::failure to be thrown.
343               this->setstate(ios_base::badbit);
344               if ((this->exceptions() & ios_base::badbit) != 0)
345                 __throw_exception_again;
346             }
347         }
348       return *this;
349     }
350
351   template<typename _CharT, typename _Traits>
352     basic_ostream<_CharT, _Traits>& 
353     basic_ostream<_CharT, _Traits>::operator<<(const void* __n)
354     {
355       sentry __cerb(*this);
356       if (__cerb) 
357         {
358           try 
359             {
360               if (_M_check_facet(_M_fnumput))
361                 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
362                   this->setstate(ios_base::badbit);
363             }
364           catch(exception& __fail)
365             {
366               // 27.6.1.2.1 Common requirements.
367               // Turn this on without causing an ios::failure to be thrown.
368               this->setstate(ios_base::badbit);
369               if ((this->exceptions() & ios_base::badbit) != 0)
370                 __throw_exception_again;
371             }
372         }
373       return *this;
374     }
375
376   template<typename _CharT, typename _Traits>
377     basic_ostream<_CharT, _Traits>&
378     basic_ostream<_CharT, _Traits>::put(char_type __c)
379     { 
380       sentry __cerb(*this);
381       if (__cerb) 
382         {
383           int_type __put = rdbuf()->sputc(__c); 
384           if (traits_type::eq_int_type(__put, traits_type::eof()))
385             this->setstate(ios_base::badbit);
386         }
387       return *this;
388     }
389
390   template<typename _CharT, typename _Traits>
391     basic_ostream<_CharT, _Traits>&
392     basic_ostream<_CharT, _Traits>::write(const _CharT* __s, streamsize __n)
393     {
394       sentry __cerb(*this);
395       if (__cerb)
396         {
397           streamsize __put = this->rdbuf()->sputn(__s, __n);
398           if ( __put != __n)
399             this->setstate(ios_base::badbit);
400         }
401       return *this;
402     }
403
404   template<typename _CharT, typename _Traits>
405     basic_ostream<_CharT, _Traits>&
406     basic_ostream<_CharT, _Traits>::flush()
407     {
408       sentry __cerb(*this);
409       if (__cerb) 
410         {
411           if (this->rdbuf() && this->rdbuf()->pubsync() == -1)
412             this->setstate(ios_base::badbit);
413         }
414       return *this;
415     }
416   
417   template<typename _CharT, typename _Traits>
418     typename basic_ostream<_CharT, _Traits>::pos_type
419     basic_ostream<_CharT, _Traits>::tellp()
420     {
421       pos_type __ret = pos_type(-1);
422       bool __testok = this->fail() != true;
423       
424       if (__testok)
425         __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
426       return __ret;
427     }
428
429
430   template<typename _CharT, typename _Traits>
431     basic_ostream<_CharT, _Traits>&
432     basic_ostream<_CharT, _Traits>::seekp(pos_type __pos)
433     {
434       bool __testok = this->fail() != true;
435       
436       if (__testok)
437         {
438 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
439 // 136.  seekp, seekg setting wrong streams?
440           pos_type __err = this->rdbuf()->pubseekpos(__pos, ios_base::out);
441
442 // 129. Need error indication from seekp() and seekg()
443           if (__err == pos_type(off_type(-1)))
444             this->setstate(ios_base::failbit);
445 #endif
446         }
447       return *this;
448     }
449
450   template<typename _CharT, typename _Traits>
451     basic_ostream<_CharT, _Traits>&
452     basic_ostream<_CharT, _Traits>::
453     seekp(off_type __off, ios_base::seekdir __d)
454     {
455       bool __testok = this->fail() != true;
456       
457       if (__testok)
458         {
459 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
460 // 136.  seekp, seekg setting wrong streams?
461           pos_type __err = this->rdbuf()->pubseekoff(__off, __d, 
462                                                      ios_base::out);
463
464 // 129. Need error indication from seekp() and seekg()
465           if (__err == pos_type(off_type(-1)))
466             this->setstate(ios_base::failbit);
467 #endif
468         }
469       return *this;
470     }
471
472   // 27.6.2.5.4 Character inserters.
473   template<typename _CharT, typename _Traits>
474     basic_ostream<_CharT, _Traits>&
475     operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
476     {
477       typedef basic_ostream<_CharT, _Traits> __ostream_type;
478       typename __ostream_type::sentry __cerb(__out);
479       if (__cerb)
480         {
481           try 
482             {
483               streamsize __w = __out.width();
484               _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
485               __pads[0] = __c;
486               streamsize __len = 1;
487               if (__w > __len)
488                 {
489                   __pad(__out, __out.fill(), __pads, &__c, __w, __len, false);
490                   __len = __w;
491                 }
492               __out.write(__pads, __len);
493               __out.width(0);
494             }
495           catch(exception& __fail)
496             {
497               // 27.6.1.2.1 Common requirements.
498               // Turn this on without causing an ios::failure to be thrown.
499               __out.setstate(ios_base::badbit);
500               if ((__out.exceptions() & ios_base::badbit) != 0)
501                 __throw_exception_again;
502             }
503         }
504       return __out;
505     }
506   
507   // Specializations.
508   template <class _Traits> 
509     basic_ostream<char, _Traits>&
510     operator<<(basic_ostream<char, _Traits>& __out, char __c)
511     {
512       typedef basic_ostream<char, _Traits> __ostream_type;
513       typename __ostream_type::sentry __cerb(__out);
514       if (__cerb)
515         {
516           try 
517             {
518               streamsize __w = __out.width();
519               char* __pads = static_cast<char*>(__builtin_alloca(__w + 1));
520               __pads[0] = __c;
521               streamsize __len = 1;
522               if (__w > __len)
523                 {
524                   __pad(__out, __out.fill(), __pads, &__c, __w, __len, false);
525                   __len = __w;
526                 }
527               __out.write(__pads, __len);
528               __out.width(0);
529             }
530           catch(exception& __fail)
531             {
532               // 27.6.1.2.1 Common requirements.
533               // Turn this on without causing an ios::failure to be thrown.
534               __out.setstate(ios_base::badbit);
535               if ((__out.exceptions() & ios_base::badbit) != 0)
536                 __throw_exception_again;
537             }
538         }
539       return __out;
540      }
541
542   template<typename _CharT, typename _Traits>
543     basic_ostream<_CharT, _Traits>&
544     operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
545     {
546       typedef basic_ostream<_CharT, _Traits> __ostream_type;
547       typename __ostream_type::sentry __cerb(__out);
548       if (__cerb)
549         {
550           try 
551             {
552               streamsize __w = __out.width();
553               _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
554               streamsize __len = static_cast<streamsize>(_Traits::length(__s));
555               if (__w > __len)
556                 {
557                   __pad(__out, __out.fill(), __pads, __s, __w, __len, false);
558                   __s = __pads;
559                   __len = __w;
560                 }
561               __out.write(__s, __len);
562               __out.width(0);
563             }
564           catch(exception& __fail)
565             {
566               // 27.6.1.2.1 Common requirements.
567               // Turn this on without causing an ios::failure to be thrown.
568               __out.setstate(ios_base::badbit);
569               if ((__out.exceptions() & ios_base::badbit) != 0)
570                 __throw_exception_again;
571             }
572         }
573       return __out;
574     }
575
576   template<typename _CharT, typename _Traits>
577     basic_ostream<_CharT, _Traits>&
578     operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
579     {
580       typedef basic_ostream<_CharT, _Traits> __ostream_type;
581 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
582 // 167.  Improper use of traits_type::length()
583 // Note that this is only in 'Review' status.
584       typedef char_traits<char>              __ctraits_type;
585 #endif
586       typename __ostream_type::sentry __cerb(__out);
587       if (__cerb)
588         {
589           size_t __clen = __ctraits_type::length(__s);
590           _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * (__clen + 1)));
591           for (size_t  __i = 0; __i <= __clen; ++__i)
592             __ws[__i] = __out.widen(__s[__i]);
593           _CharT* __str = __ws;
594           
595           try 
596             {
597               streamsize __len = static_cast<streamsize>(__clen);
598               streamsize __w = __out.width();
599               _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
600               
601               if (__w > __len)
602                 {
603                   __pad(__out, __out.fill(), __pads, __ws, __w, __len, false);
604                   __str = __pads;
605                   __len = __w;
606                 }
607               __out.write(__str, __len);
608               __out.width(0);
609             }
610           catch(exception& __fail)
611             {
612               // 27.6.1.2.1 Common requirements.
613               // Turn this on without causing an ios::failure to be thrown.
614               __out.setstate(ios_base::badbit);
615               if ((__out.exceptions() & ios_base::badbit) != 0)
616                 __throw_exception_again;
617             }
618         }
619       return __out;
620     }
621
622   // Partial specializations.
623   template<class _Traits>
624     basic_ostream<char, _Traits>&
625     operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
626     {
627       typedef basic_ostream<char, _Traits> __ostream_type;
628       typename __ostream_type::sentry __cerb(__out);
629       if (__cerb)
630         {
631           try 
632             {
633               streamsize __w = __out.width();
634               char* __pads = static_cast<char*>(__builtin_alloca(__w));
635               streamsize __len = static_cast<streamsize>(_Traits::length(__s));
636               if (__w > __len)
637                 {
638                   __pad(__out, __out.fill(), __pads, __s, __w, __len, false);
639                   __s = __pads;
640                   __len = __w;
641                 }
642               __out.write(__s, __len);
643               __out.width(0);
644             }
645           catch(exception& __fail)
646             {
647               // 27.6.1.2.1 Common requirements.
648               // Turn this on without causing an ios::failure to be thrown.
649               __out.setstate(ios_base::badbit);
650               if ((__out.exceptions() & ios_base::badbit) != 0)
651                 __throw_exception_again;
652             }
653         }
654       return __out;
655     }
656
657   // 21.3.7.9 basic_string::operator<<
658   template<typename _CharT, typename _Traits, typename _Alloc>
659     basic_ostream<_CharT, _Traits>&
660     operator<<(basic_ostream<_CharT, _Traits>& __out,
661                const basic_string<_CharT, _Traits, _Alloc>& __str)
662     { 
663       typedef basic_ostream<_CharT, _Traits> __ostream_type;
664       typename __ostream_type::sentry __cerb(__out);
665       if (__cerb)
666         {
667           const _CharT* __s = __str.data();
668           streamsize __w = __out.width();
669           _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
670           streamsize __len = static_cast<streamsize>(__str.size());
671 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
672           // 25. String operator<< uses width() value wrong
673 #endif
674           if (__w > __len)
675             {
676               __pad(__out, __out.fill(), __pads, __s, __w, __len, false);
677               __s = __pads;
678               __len = __w;
679             }
680           streamsize __res = __out.rdbuf()->sputn(__s, __len);
681           __out.width(0);
682           if (__res != __len)
683             __out.setstate(ios_base::failbit);
684         }
685       return __out;
686     }
687
688   // Inhibit implicit instantiations for required instantiations,
689   // which are defined via explicit instantiations elsewhere.  
690   // NB:  This syntax is a GNU extension.
691   extern template class basic_ostream<char>;
692   extern template ostream& endl(ostream&);
693   extern template ostream& ends(ostream&);
694   extern template ostream& flush(ostream&);
695   extern template ostream& operator<<(ostream&, char);
696   extern template ostream& operator<<(ostream&, unsigned char);
697   extern template ostream& operator<<(ostream&, signed char);
698   extern template ostream& operator<<(ostream&, const char*);
699   extern template ostream& operator<<(ostream&, const unsigned char*);
700   extern template ostream& operator<<(ostream&, const signed char*);
701
702   extern template class basic_ostream<wchar_t>;
703   extern template wostream& endl(wostream&);
704   extern template wostream& ends(wostream&);
705   extern template wostream& flush(wostream&);
706   extern template wostream& operator<<(wostream&, wchar_t);
707   extern template wostream& operator<<(wostream&, char);
708   extern template wostream& operator<<(wostream&, const wchar_t*);
709   extern template wostream& operator<<(wostream&, const char*);
710 } // namespace std