libstdc++
streambuf.tcc
Go to the documentation of this file.
00001 // Stream buffer classes -*- C++ -*-
00002 
00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
00004 // 2006, 2007, 2008, 2009, 2010, 2011  Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 3, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // Under Section 7 of GPL version 3, you are granted additional
00018 // permissions described in the GCC Runtime Library Exception, version
00019 // 3.1, as published by the Free Software Foundation.
00020 
00021 // You should have received a copy of the GNU General Public License and
00022 // a copy of the GCC Runtime Library Exception along with this program;
00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00024 // <http://www.gnu.org/licenses/>.
00025 
00026 /** @file bits/streambuf.tcc
00027  *  This is an internal header file, included by other library headers.
00028  *  Do not attempt to use it directly. @headername{streambuf}
00029  */
00030 
00031 //
00032 // ISO C++ 14882: 27.5  Stream buffers
00033 //
00034 
00035 #ifndef _STREAMBUF_TCC
00036 #define _STREAMBUF_TCC 1
00037 
00038 #pragma GCC system_header
00039 
00040 namespace std _GLIBCXX_VISIBILITY(default)
00041 {
00042 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00043 
00044   template<typename _CharT, typename _Traits>
00045     streamsize
00046     basic_streambuf<_CharT, _Traits>::
00047     xsgetn(char_type* __s, streamsize __n)
00048     {
00049       streamsize __ret = 0;
00050       while (__ret < __n)
00051     {
00052       const streamsize __buf_len = this->egptr() - this->gptr();
00053       if (__buf_len)
00054         {
00055           const streamsize __remaining = __n - __ret;
00056           const streamsize __len = std::min(__buf_len, __remaining);
00057           traits_type::copy(__s, this->gptr(), __len);
00058           __ret += __len;
00059           __s += __len;
00060           this->__safe_gbump(__len);
00061         }
00062 
00063       if (__ret < __n)
00064         {
00065           const int_type __c = this->uflow();
00066           if (!traits_type::eq_int_type(__c, traits_type::eof()))
00067         {
00068           traits_type::assign(*__s++, traits_type::to_char_type(__c));
00069           ++__ret;
00070         }
00071           else
00072         break;
00073         }
00074     }
00075       return __ret;
00076     }
00077 
00078   template<typename _CharT, typename _Traits>
00079     streamsize
00080     basic_streambuf<_CharT, _Traits>::
00081     xsputn(const char_type* __s, streamsize __n)
00082     {
00083       streamsize __ret = 0;
00084       while (__ret < __n)
00085     {
00086       const streamsize __buf_len = this->epptr() - this->pptr();
00087       if (__buf_len)
00088         {
00089           const streamsize __remaining = __n - __ret;
00090           const streamsize __len = std::min(__buf_len, __remaining);
00091           traits_type::copy(this->pptr(), __s, __len);
00092           __ret += __len;
00093           __s += __len;
00094           this->__safe_pbump(__len);
00095         }
00096 
00097       if (__ret < __n)
00098         {
00099           int_type __c = this->overflow(traits_type::to_int_type(*__s));
00100           if (!traits_type::eq_int_type(__c, traits_type::eof()))
00101         {
00102           ++__ret;
00103           ++__s;
00104         }
00105           else
00106         break;
00107         }
00108     }
00109       return __ret;
00110     }
00111 
00112   // Conceivably, this could be used to implement buffer-to-buffer
00113   // copies, if this was ever desired in an un-ambiguous way by the
00114   // standard.
00115   template<typename _CharT, typename _Traits>
00116     streamsize
00117     __copy_streambufs_eof(basic_streambuf<_CharT, _Traits>* __sbin,
00118               basic_streambuf<_CharT, _Traits>* __sbout,
00119               bool& __ineof)
00120     {
00121       streamsize __ret = 0;
00122       __ineof = true;
00123       typename _Traits::int_type __c = __sbin->sgetc();
00124       while (!_Traits::eq_int_type(__c, _Traits::eof()))
00125     {
00126       __c = __sbout->sputc(_Traits::to_char_type(__c));
00127       if (_Traits::eq_int_type(__c, _Traits::eof()))
00128         {
00129           __ineof = false;
00130           break;
00131         }
00132       ++__ret;
00133       __c = __sbin->snextc();
00134     }
00135       return __ret;
00136     }
00137 
00138   template<typename _CharT, typename _Traits>
00139     inline streamsize
00140     __copy_streambufs(basic_streambuf<_CharT, _Traits>* __sbin,
00141               basic_streambuf<_CharT, _Traits>* __sbout)
00142     {
00143       bool __ineof;
00144       return __copy_streambufs_eof(__sbin, __sbout, __ineof);
00145     }
00146 
00147   // Inhibit implicit instantiations for required instantiations,
00148   // which are defined via explicit instantiations elsewhere.
00149 #if _GLIBCXX_EXTERN_TEMPLATE
00150   extern template class basic_streambuf<char>;
00151   extern template
00152     streamsize
00153     __copy_streambufs(basic_streambuf<char>*,
00154               basic_streambuf<char>*);
00155   extern template
00156     streamsize
00157     __copy_streambufs_eof(basic_streambuf<char>*,
00158               basic_streambuf<char>*, bool&);
00159 
00160 #ifdef _GLIBCXX_USE_WCHAR_T
00161   extern template class basic_streambuf<wchar_t>;
00162   extern template
00163     streamsize
00164     __copy_streambufs(basic_streambuf<wchar_t>*,
00165               basic_streambuf<wchar_t>*);
00166   extern template
00167     streamsize
00168     __copy_streambufs_eof(basic_streambuf<wchar_t>*,
00169               basic_streambuf<wchar_t>*, bool&);
00170 #endif
00171 #endif
00172 
00173 _GLIBCXX_END_NAMESPACE_VERSION
00174 } // namespace std
00175 
00176 #endif