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