OSDN Git Service

Merge basic-improvements-branch to trunk
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / sstream.tcc
index f83bb69..b5848d8 100644 (file)
@@ -91,17 +91,17 @@ namespace std
        {
          if (!__testeof)
            {
-             __size_type __len = max(_M_buf_size, _M_buf_size_opt);
+             __size_type __len = std::max(_M_buf_size, _M_buf_size_opt);
              __len *= 2;
 
              if (__testwrite)
-               __ret = this->sputc(__c);
+               __ret = this->sputc(traits_type::to_char_type(__c));
              else if (__len <= _M_string.max_size())
                {
                  // Force-allocate, re-sync.
                  _M_string = this->str();
                  _M_string.reserve(__len);
-                 _M_buf_size = static_cast<int_type>(__len);
+                 _M_buf_size = __len;
                  _M_really_sync(_M_in_cur - _M_in_beg, 
                                 _M_out_cur - _M_out_beg);
                  *_M_out_cur = traits_type::to_char_type(__c);
@@ -121,11 +121,13 @@ namespace std
     seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode)
     {
       pos_type __ret =  pos_type(off_type(-1)); 
-      bool __testin = __mode & ios_base::in && _M_mode & ios_base::in;
-      bool __testout = __mode & ios_base::out && _M_mode & ios_base::out;
+      bool __testin = (ios_base::in & _M_mode & __mode) != 0;
+      bool __testout = (ios_base::out & _M_mode & __mode) != 0;
       bool __testboth = __testin && __testout && __way != ios_base::cur;
-      
-      if (_M_buf_size && ((__testin != __testout) || __testboth))
+      __testin &= !(__mode & ios_base::out);
+      __testout &= !(__mode & ios_base::in);
+
+      if (_M_buf_size && (__testin || __testout || __testboth))
        {
          char_type* __beg = _M_buf;
          char_type* __curi = NULL;
@@ -133,12 +135,12 @@ namespace std
          char_type* __endi = NULL;
          char_type* __endo = NULL;
 
-         if (__testin)
+         if (__testin || __testboth)
            {
              __curi = this->gptr();
              __endi = this->egptr();
            }
-         if (__testout)
+         if (__testout || __testboth)
            {
              __curo = this->pptr();
              __endo = this->epptr();
@@ -157,13 +159,13 @@ namespace std
              __newoffo = __endo - __beg;
            }
 
-         if (__testin
+         if ((__testin || __testboth)
              && __newoffi + __off >= 0 && __endi - __beg >= __newoffi + __off)
            {
              _M_in_cur = __beg + __newoffi + __off;
              __ret = pos_type(__newoffi);
            }
-         if (__testout
+         if ((__testout || __testboth)
              && __newoffo + __off >= 0 && __endo - __beg >= __newoffo + __off)
            {
              _M_out_cur_move(__newoffo + __off - (_M_out_cur - __beg));
@@ -179,33 +181,44 @@ namespace std
     seekpos(pos_type __sp, ios_base::openmode __mode)
     {
       pos_type __ret =  pos_type(off_type(-1)); 
-      off_type __pos = __sp._M_position();
-      char_type* __beg = NULL;
-      char_type* __end = NULL;
-      bool __testin = __mode & ios_base::in && _M_mode & ios_base::in;
-      bool __testout = __mode & ios_base::out && _M_mode & ios_base::out;
       
-      if (__testin)
-       {
-         __beg = this->eback();
-         __end = this->egptr();
-       }
-      if (__testout)
+      if (_M_buf_size)
        {
-         __beg = this->pbase();
-         __end = _M_buf + _M_buf_size;
-       }
-      if (0 <= __pos && __pos <= __end - __beg)
-       {
-         // Need to set both of these if applicable
-         if (__testin)
-           _M_in_cur = _M_in_beg + __pos;
-         if (__testout)
-           _M_out_cur_move((__pos) - (_M_out_cur - __beg));
-         __ret = pos_type(off_type(__pos));
+         off_type __pos = __sp; // Use streamoff operator to do conversion.
+         char_type* __beg = NULL;
+         char_type* __end = NULL;
+         bool __testin = (ios_base::in & _M_mode & __mode) != 0;
+         bool __testout = (ios_base::out & _M_mode & __mode) != 0;
+         bool __testboth = __testin && __testout;
+         __testin &= !(__mode & ios_base::out);
+         __testout &= !(__mode & ios_base::in);
+         
+         // NB: Ordered.
+         bool __testposi = false;
+         bool __testposo = false;
+         if (__testin || __testboth)
+           {
+             __beg = this->eback();
+             __end = this->egptr();
+             if (0 <= __pos && __pos <= __end - __beg)
+               __testposi = true;
+           }
+         if (__testout || __testboth)
+           {
+             __beg = this->pbase();
+             __end = _M_buf + _M_buf_size;
+             if (0 <= __pos && __pos <= __end - __beg)
+               __testposo = true;
+           }
+         if (__testposi || __testposo)
+           {
+             if (__testposi)
+               _M_in_cur = _M_in_beg + __pos;
+             if (__testposo)
+               _M_out_cur_move((__pos) - (_M_out_cur - __beg));
+             __ret = pos_type(off_type(__pos));
+           }
        }
-      
       return __ret;
     }
 
@@ -213,13 +226,16 @@ namespace std
   // which are defined via explicit instantiations elsewhere.  
   // NB:  This syntax is a GNU extension.
   extern template class basic_stringbuf<char>;
-  extern template class basic_stringbuf<wchar_t>;
   extern template class basic_istringstream<char>;
-  extern template class basic_istringstream<wchar_t>;
   extern template class basic_ostringstream<char>;
-  extern template class basic_ostringstream<wchar_t>;
   extern template class basic_stringstream<char>;
+
+#ifdef _GLIBCPP_USE_WCHAR_T
+  extern template class basic_stringbuf<wchar_t>;
+  extern template class basic_istringstream<wchar_t>;
+  extern template class basic_ostringstream<wchar_t>;
   extern template class basic_stringstream<wchar_t>;
+#endif
 } // namespace std
 
 #endif