Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  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 <bits/std_locale.h>
  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<<(bool __n)
  115.     {
  116.       sentry __cerb(*this);
  117.       if (__cerb)
  118.         {
  119.           try
  120.             {
  121.               if (_M_check_facet(_M_fnumput))
  122.                 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
  123.                   this->setstate(ios_base::badbit);
  124.             }
  125.           catch(exception& __fail)
  126.             {
  127.               // 27.6.1.2.1 Common requirements.
  128.               // Turn this on without causing an ios::failure to be thrown.
  129.               this->setstate(ios_base::badbit);
  130.               if ((this->exceptions() & ios_base::badbit) != 0)
  131.                 __throw_exception_again;
  132.             }
  133.         }
  134.       return *this;
  135.     }
  136.  
  137.   template<typename _CharT, typename _Traits>
  138.     basic_ostream<_CharT, _Traits>&
  139.     basic_ostream<_CharT, _Traits>::operator<<(long __n)
  140.     {
  141.       sentry __cerb(*this);
  142.       if (__cerb)
  143.         {
  144.           try
  145.             {
  146.               char_type __c = this->fill();
  147.               ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
  148.               if (_M_check_facet(_M_fnumput))
  149.                 {
  150.                   bool __b = false;
  151.                   if (__fmt & ios_base::oct || __fmt & ios_base::hex)
  152.                     {
  153.                       unsigned long __l = static_cast<unsigned long>(__n);
  154.                       __b = _M_fnumput->put(*this, *this, __c, __l).failed();
  155.                     }
  156.                   else
  157.                     __b = _M_fnumput->put(*this, *this, __c, __n).failed();
  158.                   if (__b)  
  159.                     this->setstate(ios_base::badbit);
  160.                 }
  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<<(unsigned long __n)
  177.     {
  178.       sentry __cerb(*this);
  179.       if (__cerb)
  180.         {
  181.           try
  182.             {
  183.               if (_M_check_facet(_M_fnumput))
  184.                 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
  185.                   this->setstate(ios_base::badbit);
  186.             }
  187.           catch(exception& __fail)
  188.             {
  189.               // 27.6.1.2.1 Common requirements.
  190.               // Turn this on without causing an ios::failure to be thrown.
  191.               this->setstate(ios_base::badbit);
  192.               if ((this->exceptions() & ios_base::badbit) != 0)
  193.                 __throw_exception_again;
  194.             }
  195.         }
  196.       return *this;
  197.     }
  198.  
  199. #ifdef _GLIBCPP_USE_LONG_LONG
  200.   template<typename _CharT, typename _Traits>
  201.     basic_ostream<_CharT, _Traits>&
  202.     basic_ostream<_CharT, _Traits>::operator<<(long long __n)
  203.     {
  204.       sentry __cerb(*this);
  205.       if (__cerb)
  206.         {
  207.           try
  208.             {
  209.               char_type __c = this->fill();
  210.               ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
  211.               if (_M_check_facet(_M_fnumput))
  212.                 {
  213.                   bool __b = false;
  214.                   if (__fmt & ios_base::oct || __fmt & ios_base::hex)
  215.                     {
  216.                       unsigned long long __l;
  217.                       __l = static_cast<unsigned long long>(__n);
  218.                       __b = _M_fnumput->put(*this, *this, __c, __l).failed();
  219.                     }
  220.                   else
  221.                     __b = _M_fnumput->put(*this, *this, __c, __n).failed();
  222.                   if (__b)  
  223.                     this->setstate(ios_base::badbit);
  224.                 }
  225.             }
  226.           catch(exception& __fail)
  227.             {
  228.               // 27.6.1.2.1 Common requirements.
  229.               // Turn this on without causing an ios::failure to be thrown.
  230.               this->setstate(ios_base::badbit);
  231.               if ((this->exceptions() & ios_base::badbit) != 0)
  232.                 __throw_exception_again;
  233.             }
  234.         }
  235.       return *this;
  236.     }
  237.  
  238.   template<typename _CharT, typename _Traits>
  239.     basic_ostream<_CharT, _Traits>&
  240.     basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n)
  241.     {
  242.       sentry __cerb(*this);
  243.       if (__cerb)
  244.         {
  245.           try
  246.             {
  247.               if (_M_check_facet(_M_fnumput))
  248.                 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
  249.                   this->setstate(ios_base::badbit);
  250.             }
  251.           catch(exception& __fail)
  252.             {
  253.               // 27.6.1.2.1 Common requirements.
  254.               // Turn this on without causing an ios::failure to be thrown.
  255.               this->setstate(ios_base::badbit);
  256.               if ((this->exceptions() & ios_base::badbit) != 0)
  257.                 __throw_exception_again;
  258.             }
  259.         }
  260.       return *this;
  261.     }
  262. #endif
  263.  
  264.   template<typename _CharT, typename _Traits>
  265.     basic_ostream<_CharT, _Traits>&
  266.     basic_ostream<_CharT, _Traits>::operator<<(double __n)
  267.     {
  268.       sentry __cerb(*this);
  269.       if (__cerb)
  270.         {
  271.           try
  272.             {
  273.               if (_M_check_facet(_M_fnumput))
  274.                 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
  275.                   this->setstate(ios_base::badbit);
  276.             }
  277.           catch(exception& __fail)
  278.             {
  279.               // 27.6.1.2.1 Common requirements.
  280.               // Turn this on without causing an ios::failure to be thrown.
  281.               this->setstate(ios_base::badbit);
  282.               if ((this->exceptions() & ios_base::badbit) != 0)
  283.                 __throw_exception_again;
  284.             }
  285.         }
  286.       return *this;
  287.     }
  288.  
  289.   template<typename _CharT, typename _Traits>
  290.     basic_ostream<_CharT, _Traits>&
  291.     basic_ostream<_CharT, _Traits>::operator<<(long double __n)
  292.     {
  293.       sentry __cerb(*this);
  294.       if (__cerb)
  295.         {
  296.           try
  297.             {
  298.               if (_M_check_facet(_M_fnumput))
  299.                 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
  300.                   this->setstate(ios_base::badbit);
  301.             }
  302.           catch(exception& __fail)
  303.             {
  304.               // 27.6.1.2.1 Common requirements.
  305.               // Turn this on without causing an ios::failure to be thrown.
  306.               this->setstate(ios_base::badbit);
  307.               if ((this->exceptions() & ios_base::badbit) != 0)
  308.                 __throw_exception_again;
  309.             }
  310.         }
  311.       return *this;
  312.     }
  313.  
  314.   template<typename _CharT, typename _Traits>
  315.     basic_ostream<_CharT, _Traits>&
  316.     basic_ostream<_CharT, _Traits>::operator<<(const void* __n)
  317.     {
  318.       sentry __cerb(*this);
  319.       if (__cerb)
  320.         {
  321.           try
  322.             {
  323.               if (_M_check_facet(_M_fnumput))
  324.                 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
  325.                   this->setstate(ios_base::badbit);
  326.             }
  327.           catch(exception& __fail)
  328.             {
  329.               // 27.6.1.2.1 Common requirements.
  330.               // Turn this on without causing an ios::failure to be thrown.
  331.               this->setstate(ios_base::badbit);
  332.               if ((this->exceptions() & ios_base::badbit) != 0)
  333.                 __throw_exception_again;
  334.             }
  335.         }
  336.       return *this;
  337.     }
  338.  
  339.   template<typename _CharT, typename _Traits>
  340.     basic_ostream<_CharT, _Traits>&
  341.     basic_ostream<_CharT, _Traits>::operator<<(__streambuf_type* __sbin)
  342.     {
  343.       streamsize __xtrct = 0;
  344.       __streambuf_type* __sbout = this->rdbuf();
  345.       sentry __cerb(*this);
  346.       if (__sbin && __cerb)
  347.         __xtrct = __copy_streambufs(*this, __sbin, __sbout);
  348.       if (!__sbin || !__xtrct)
  349.         this->setstate(ios_base::failbit);
  350.       return *this;
  351.     }
  352.  
  353.   template<typename _CharT, typename _Traits>
  354.     basic_ostream<_CharT, _Traits>&
  355.     basic_ostream<_CharT, _Traits>::put(char_type __c)
  356.     {
  357.       sentry __cerb(*this);
  358.       if (__cerb)
  359.         {
  360.           int_type __put = rdbuf()->sputc(__c);
  361.           if (__put != traits_type::to_int_type(__c))
  362.             this->setstate(ios_base::badbit);
  363.         }
  364.       return *this;
  365.     }
  366.  
  367.   template<typename _CharT, typename _Traits>
  368.     basic_ostream<_CharT, _Traits>&
  369.     basic_ostream<_CharT, _Traits>::write(const _CharT* __s, streamsize __n)
  370.     {
  371.       sentry __cerb(*this);
  372.       if (__cerb)
  373.         {
  374.           streamsize __put = this->rdbuf()->sputn(__s, __n);
  375.           if ( __put != __n)
  376.             this->setstate(ios_base::badbit);
  377.         }
  378.       return *this;
  379.     }
  380.  
  381.   template<typename _CharT, typename _Traits>
  382.     basic_ostream<_CharT, _Traits>&
  383.     basic_ostream<_CharT, _Traits>::flush()
  384.     {
  385.       sentry __cerb(*this);
  386.       if (__cerb)
  387.         {
  388.           if (this->rdbuf() && this->rdbuf()->pubsync() == -1)
  389.             this->setstate(ios_base::badbit);
  390.         }
  391.       return *this;
  392.     }
  393.  
  394.   template<typename _CharT, typename _Traits>
  395.     typename basic_ostream<_CharT, _Traits>::pos_type
  396.     basic_ostream<_CharT, _Traits>::tellp()
  397.     {
  398.       pos_type __ret = pos_type(-1);
  399.       bool __testok = this->fail() != true;
  400.      
  401.       if (__testok)
  402.         __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
  403.       return __ret;
  404.     }
  405.  
  406.  
  407.   template<typename _CharT, typename _Traits>
  408.     basic_ostream<_CharT, _Traits>&
  409.     basic_ostream<_CharT, _Traits>::seekp(pos_type __pos)
  410.     {
  411.       bool __testok = this->fail() != true;
  412.      
  413.       if (__testok)
  414.         {
  415. #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
  416. // 136.  seekp, seekg setting wrong streams?
  417.           pos_type __err = this->rdbuf()->pubseekpos(__pos, ios_base::out);
  418.  
  419. // 129. Need error indication from seekp() and seekg()
  420.           if (__err == pos_type(off_type(-1)))
  421.             this->setstate(failbit);
  422. #endif
  423.         }
  424.       return *this;
  425.     }
  426.  
  427.   template<typename _CharT, typename _Traits>
  428.     basic_ostream<_CharT, _Traits>&
  429.     basic_ostream<_CharT, _Traits>::
  430.     seekp(off_type __off, ios_base::seekdir __d)
  431.     {
  432.       bool __testok = this->fail() != true;
  433.      
  434.       if (__testok)
  435.         {
  436. #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
  437. // 136.  seekp, seekg setting wrong streams?
  438.           pos_type __err = this->rdbuf()->pubseekoff(__off, __d,
  439.                                                      ios_base::out);
  440.  
  441. // 129. Need error indication from seekp() and seekg()
  442.           if (__err == pos_type(off_type(-1)))
  443.             this->setstate(failbit);
  444.         }
  445. #endif
  446.       return *this;
  447.     }
  448.  
  449.   // 27.6.2.5.4 Character inserters
  450.  
  451.   // Construct correctly padded string, as per 22.2.2.2.2
  452.   // Similar in theory to __pad_numeric, from num_put, but it doesn't
  453.   // use _S_fill: perhaps it should.
  454.   // Assumes
  455.   // __newlen > __oldlen
  456.   // __news is allocated for __newlen size
  457.   template<typename _CharT, typename _Traits>
  458.     void
  459.     __pad_char(basic_ios<_CharT, _Traits>& __ios,
  460.                _CharT* __news, const _CharT* __olds,
  461.                const streamsize __newlen, const streamsize __oldlen)
  462.     {
  463.       typedef _CharT    char_type;
  464.       typedef _Traits   traits_type;
  465.       typedef typename traits_type::int_type int_type;
  466.      
  467.       int_type __plen = static_cast<size_t>(__newlen - __oldlen);
  468.       char_type* __pads = static_cast<char_type*>(__builtin_alloca(sizeof(char_type) * __plen));
  469.       traits_type::assign(__pads, __plen, __ios.fill());
  470.  
  471.       char_type* __beg;
  472.       char_type* __end;
  473.       size_t __mod = 0;
  474.       size_t __beglen; //either __plen or __oldlen
  475.       ios_base::fmtflags __fmt = __ios.flags() & ios_base::adjustfield;
  476.  
  477.       if (__fmt == ios_base::left)
  478.         {
  479.           // Padding last.
  480.           __beg = const_cast<char_type*>(__olds);
  481.           __beglen = __oldlen;
  482.           __end = __pads;
  483.         }
  484.       else if (__fmt == ios_base::internal)
  485.         {
  486.           // Pad after the sign, if there is one.
  487.           // Pad after 0[xX], if there is one.
  488.           // Who came up with these rules, anyway? Jeeze.
  489.           typedef _Format_cache<_CharT> __cache_type;
  490.           __cache_type const* __fmt = __cache_type::_S_get(__ios);
  491.           const char_type* __minus = traits_type::find(__olds, __oldlen,
  492.                                                        __fmt->_S_minus);
  493.           const char_type* __plus = traits_type::find(__olds, __oldlen,
  494.                                                       __fmt->_S_plus);
  495.           bool __testsign = __minus || __plus;
  496.           bool __testhex = __olds[0] == '0'
  497.                            && (__olds[1] == 'x' || __olds[1] == 'X');
  498.  
  499.           if (__testhex)
  500.             {
  501.               __news[0] = __olds[0];
  502.               __news[1] = __olds[1];
  503.               __mod += 2;
  504.               __beg = const_cast<char_type*>(__olds + __mod);
  505.               __beglen = __oldlen - __mod;
  506.               __end = __pads;
  507.             }
  508.           else if (__testsign)
  509.             {
  510.               __mod += __plen;
  511.               const char_type* __sign = __minus ? __minus + 1: __plus + 1;
  512.               __beg = const_cast<char_type*>(__olds);
  513.               __beglen = __sign - __olds;
  514.               __end = const_cast<char_type*>(__sign + __plen);
  515.               traits_type::copy(__news + __beglen, __pads, __plen);
  516.             }
  517.           else
  518.             {
  519.               // Padding first.
  520.               __beg = __pads;
  521.               __beglen = __plen;
  522.               __end = const_cast<char_type*>(__olds);
  523.             }
  524.         }
  525.       else
  526.         {
  527.           // Padding first.
  528.           __beg = __pads;
  529.           __beglen = __plen;
  530.           __end = const_cast<char_type*>(__olds);
  531.         }
  532.  
  533.       traits_type::copy(__news, __beg, __beglen);
  534.       traits_type::copy(__news + __beglen, __end, __newlen - __beglen - __mod);
  535.     }
  536.  
  537.   template<typename _CharT, typename _Traits>
  538.     basic_ostream<_CharT, _Traits>&
  539.     operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
  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.               __pads[0] = __c;
  550.               streamsize __len = 1;
  551.               if (__w > __len)
  552.                 {
  553.                   __pad_char(__out, __pads, &__c, __w, __len);
  554.                   __len = __w;
  555.                 }
  556.               __out.write(__pads, __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.   // Specialization
  572.   template <class _Traits>
  573.     basic_ostream<char, _Traits>&
  574.     operator<<(basic_ostream<char, _Traits>& __out, char __c)
  575.     {
  576.       typedef basic_ostream<char, _Traits> __ostream_type;
  577.       typename __ostream_type::sentry __cerb(__out);
  578.       if (__cerb)
  579.         {
  580.           try
  581.             {
  582.               streamsize __w = __out.width();
  583.               char* __pads = static_cast<char*>(__builtin_alloca(__w + 1));
  584.               __pads[0] = __c;
  585.               streamsize __len = 1;
  586.               if (__w > __len)
  587.                 {
  588.                   __pad_char(__out, __pads, &__c, __w, __len);
  589.                   __len = __w;
  590.                 }
  591.               __out.write(__pads, __len);
  592.               __out.width(0);
  593.             }
  594.           catch(exception& __fail)
  595.             {
  596.               // 27.6.1.2.1 Common requirements.
  597.               // Turn this on without causing an ios::failure to be thrown.
  598.               __out.setstate(ios_base::badbit);
  599.               if ((__out.exceptions() & ios_base::badbit) != 0)
  600.                 __throw_exception_again;
  601.             }
  602.         }
  603.       return __out;
  604.      }
  605.  
  606.   template<typename _CharT, typename _Traits>
  607.     basic_ostream<_CharT, _Traits>&
  608.     operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
  609.     {
  610.       typedef basic_ostream<_CharT, _Traits> __ostream_type;
  611.       typename __ostream_type::sentry __cerb(__out);
  612.       if (__cerb)
  613.         {
  614.           try
  615.             {
  616.               streamsize __w = __out.width();
  617.               _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
  618.               streamsize __len = static_cast<streamsize>(_Traits::length(__s));
  619.               if (__w > __len)
  620.                 {
  621.                   __pad_char(__out, __pads, __s, __w, __len);
  622.                   __s = __pads;
  623.                   __len = __w;
  624.                 }
  625.               __out.write(__s, __len);
  626.               __out.width(0);
  627.             }
  628.           catch(exception& __fail)
  629.             {
  630.               // 27.6.1.2.1 Common requirements.
  631.               // Turn this on without causing an ios::failure to be thrown.
  632.               __out.setstate(ios_base::badbit);
  633.               if ((__out.exceptions() & ios_base::badbit) != 0)
  634.                 __throw_exception_again;
  635.             }
  636.         }
  637.       return __out;
  638.     }
  639.  
  640.   template<typename _CharT, typename _Traits>
  641.     basic_ostream<_CharT, _Traits>&
  642.     operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
  643.     {
  644.       typedef basic_ostream<_CharT, _Traits> __ostream_type;
  645. #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
  646. // 167.  Improper use of traits_type::length()
  647.       typedef char_traits<char>              __ctraits_type;
  648. #endif
  649.       typename __ostream_type::sentry __cerb(__out);
  650.       if (__cerb)
  651.         {
  652.           size_t __clen = __ctraits_type::length(__s);
  653.           _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * (__clen + 1)));
  654.           for (size_t  __i = 0; __i <= __clen; ++__i)
  655.             __ws[__i] = __out.widen(__s[__i]);
  656.           _CharT* __str = __ws;
  657.          
  658.           try
  659.             {
  660.               streamsize __len = static_cast<streamsize>(__clen);
  661.               streamsize __w = __out.width();
  662.               _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
  663.              
  664.               if (__w > __len)
  665.                 {
  666.                   __pad_char(__out, __pads, __ws, __w, __len);
  667.                   __str = __pads;
  668.                   __len = __w;
  669.                 }
  670.               __out.write(__str, __len);
  671.               __out.width(0);
  672.             }
  673.           catch(exception& __fail)
  674.             {
  675.               // 27.6.1.2.1 Common requirements.
  676.               // Turn this on without causing an ios::failure to be thrown.
  677.               __out.setstate(ios_base::badbit);
  678.               if ((__out.exceptions() & ios_base::badbit) != 0)
  679.                 __throw_exception_again;
  680.             }
  681.         }
  682.       return __out;
  683.     }
  684.  
  685.   // Partial specializationss
  686.   template<class _Traits>
  687.     basic_ostream<char, _Traits>&
  688.     operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
  689.     {
  690.       typedef basic_ostream<char, _Traits> __ostream_type;
  691.       typename __ostream_type::sentry __cerb(__out);
  692.       if (__cerb)
  693.         {
  694.           try
  695.             {
  696.               streamsize __w = __out.width();
  697.               char* __pads = static_cast<char*>(__builtin_alloca(__w));
  698.               streamsize __len = static_cast<streamsize>(_Traits::length(__s));
  699.               if (__w > __len)
  700.                 {
  701.                   __pad_char(__out, __pads, __s, __w, __len);
  702.                   __s = __pads;
  703.                   __len = __w;
  704.                 }
  705.               __out.write(__s, __len);
  706.               __out.width(0);
  707.             }
  708.           catch(exception& __fail)
  709.             {
  710.               // 27.6.1.2.1 Common requirements.
  711.               // Turn this on without causing an ios::failure to be thrown.
  712.               __out.setstate(ios_base::badbit);
  713.               if ((__out.exceptions() & ios_base::badbit) != 0)
  714.                 __throw_exception_again;
  715.             }
  716.         }
  717.       return __out;
  718.     }
  719.  
  720.   // 21.3.7.9 basic_string::operator<<
  721.   template<typename _CharT, typename _Traits, typename _Alloc>
  722.     basic_ostream<_CharT, _Traits>&
  723.     operator<<(basic_ostream<_CharT, _Traits>& __out,
  724.                const basic_string<_CharT, _Traits, _Alloc>& __str)
  725.     {
  726.       typedef basic_ostream<_CharT, _Traits> __ostream_type;
  727.       typename __ostream_type::sentry __cerb(__out);
  728.       if (__cerb)
  729.         {
  730.           const _CharT* __s = __str.data();
  731.           streamsize __w = __out.width();
  732.           _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
  733.           streamsize __len = static_cast<streamsize>(__str.size());
  734. #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
  735.           // 25. String operator<< uses width() value wrong
  736. #endif
  737.           if (__w > __len)
  738.             {
  739.               __pad_char(__out, __pads, __s, __w, __len);
  740.               __s = __pads;
  741.               __len = __w;
  742.             }
  743.           streamsize __res = __out.rdbuf()->sputn(__s, __len);
  744.           __out.width(0);
  745.           if (__res != __len)
  746.             __out.setstate(ios_base::failbit);
  747.         }
  748.       return __out;
  749.     }
  750. } // namespace std
  751.  
  752. // Local Variables:
  753. // mode:C++
  754. // End:
  755.  
  756.