xref: /dflybsd-src/contrib/gcc-8.0/libstdc++-v3/include/bits/streambuf.tcc (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj // Stream buffer classes -*- C++ -*-
2*38fd1498Szrj 
3*38fd1498Szrj // Copyright (C) 1997-2018 Free Software Foundation, Inc.
4*38fd1498Szrj //
5*38fd1498Szrj // This file is part of the GNU ISO C++ Library.  This library is free
6*38fd1498Szrj // software; you can redistribute it and/or modify it under the
7*38fd1498Szrj // terms of the GNU General Public License as published by the
8*38fd1498Szrj // Free Software Foundation; either version 3, or (at your option)
9*38fd1498Szrj // any later version.
10*38fd1498Szrj 
11*38fd1498Szrj // This library is distributed in the hope that it will be useful,
12*38fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*38fd1498Szrj // GNU General Public License for more details.
15*38fd1498Szrj 
16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional
17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version
18*38fd1498Szrj // 3.1, as published by the Free Software Foundation.
19*38fd1498Szrj 
20*38fd1498Szrj // You should have received a copy of the GNU General Public License and
21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program;
22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23*38fd1498Szrj // <http://www.gnu.org/licenses/>.
24*38fd1498Szrj 
25*38fd1498Szrj /** @file bits/streambuf.tcc
26*38fd1498Szrj  *  This is an internal header file, included by other library headers.
27*38fd1498Szrj  *  Do not attempt to use it directly. @headername{streambuf}
28*38fd1498Szrj  */
29*38fd1498Szrj 
30*38fd1498Szrj //
31*38fd1498Szrj // ISO C++ 14882: 27.5  Stream buffers
32*38fd1498Szrj //
33*38fd1498Szrj 
34*38fd1498Szrj #ifndef _STREAMBUF_TCC
35*38fd1498Szrj #define _STREAMBUF_TCC 1
36*38fd1498Szrj 
37*38fd1498Szrj #pragma GCC system_header
38*38fd1498Szrj 
39*38fd1498Szrj namespace std _GLIBCXX_VISIBILITY(default)
40*38fd1498Szrj {
41*38fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION
42*38fd1498Szrj 
43*38fd1498Szrj   template<typename _CharT, typename _Traits>
44*38fd1498Szrj     streamsize
45*38fd1498Szrj     basic_streambuf<_CharT, _Traits>::
xsgetn(char_type * __s,streamsize __n)46*38fd1498Szrj     xsgetn(char_type* __s, streamsize __n)
47*38fd1498Szrj     {
48*38fd1498Szrj       streamsize __ret = 0;
49*38fd1498Szrj       while (__ret < __n)
50*38fd1498Szrj 	{
51*38fd1498Szrj 	  const streamsize __buf_len = this->egptr() - this->gptr();
52*38fd1498Szrj 	  if (__buf_len)
53*38fd1498Szrj 	    {
54*38fd1498Szrj 	      const streamsize __remaining = __n - __ret;
55*38fd1498Szrj 	      const streamsize __len = std::min(__buf_len, __remaining);
56*38fd1498Szrj 	      traits_type::copy(__s, this->gptr(), __len);
57*38fd1498Szrj 	      __ret += __len;
58*38fd1498Szrj 	      __s += __len;
59*38fd1498Szrj 	      this->__safe_gbump(__len);
60*38fd1498Szrj 	    }
61*38fd1498Szrj 
62*38fd1498Szrj 	  if (__ret < __n)
63*38fd1498Szrj 	    {
64*38fd1498Szrj 	      const int_type __c = this->uflow();
65*38fd1498Szrj 	      if (!traits_type::eq_int_type(__c, traits_type::eof()))
66*38fd1498Szrj 		{
67*38fd1498Szrj 		  traits_type::assign(*__s++, traits_type::to_char_type(__c));
68*38fd1498Szrj 		  ++__ret;
69*38fd1498Szrj 		}
70*38fd1498Szrj 	      else
71*38fd1498Szrj 		break;
72*38fd1498Szrj 	    }
73*38fd1498Szrj 	}
74*38fd1498Szrj       return __ret;
75*38fd1498Szrj     }
76*38fd1498Szrj 
77*38fd1498Szrj   template<typename _CharT, typename _Traits>
78*38fd1498Szrj     streamsize
79*38fd1498Szrj     basic_streambuf<_CharT, _Traits>::
xsputn(const char_type * __s,streamsize __n)80*38fd1498Szrj     xsputn(const char_type* __s, streamsize __n)
81*38fd1498Szrj     {
82*38fd1498Szrj       streamsize __ret = 0;
83*38fd1498Szrj       while (__ret < __n)
84*38fd1498Szrj 	{
85*38fd1498Szrj 	  const streamsize __buf_len = this->epptr() - this->pptr();
86*38fd1498Szrj 	  if (__buf_len)
87*38fd1498Szrj 	    {
88*38fd1498Szrj 	      const streamsize __remaining = __n - __ret;
89*38fd1498Szrj 	      const streamsize __len = std::min(__buf_len, __remaining);
90*38fd1498Szrj 	      traits_type::copy(this->pptr(), __s, __len);
91*38fd1498Szrj 	      __ret += __len;
92*38fd1498Szrj 	      __s += __len;
93*38fd1498Szrj 	      this->__safe_pbump(__len);
94*38fd1498Szrj 	    }
95*38fd1498Szrj 
96*38fd1498Szrj 	  if (__ret < __n)
97*38fd1498Szrj 	    {
98*38fd1498Szrj 	      int_type __c = this->overflow(traits_type::to_int_type(*__s));
99*38fd1498Szrj 	      if (!traits_type::eq_int_type(__c, traits_type::eof()))
100*38fd1498Szrj 		{
101*38fd1498Szrj 		  ++__ret;
102*38fd1498Szrj 		  ++__s;
103*38fd1498Szrj 		}
104*38fd1498Szrj 	      else
105*38fd1498Szrj 		break;
106*38fd1498Szrj 	    }
107*38fd1498Szrj 	}
108*38fd1498Szrj       return __ret;
109*38fd1498Szrj     }
110*38fd1498Szrj 
111*38fd1498Szrj   // Conceivably, this could be used to implement buffer-to-buffer
112*38fd1498Szrj   // copies, if this was ever desired in an un-ambiguous way by the
113*38fd1498Szrj   // standard.
114*38fd1498Szrj   template<typename _CharT, typename _Traits>
115*38fd1498Szrj     streamsize
__copy_streambufs_eof(basic_streambuf<_CharT,_Traits> * __sbin,basic_streambuf<_CharT,_Traits> * __sbout,bool & __ineof)116*38fd1498Szrj     __copy_streambufs_eof(basic_streambuf<_CharT, _Traits>* __sbin,
117*38fd1498Szrj 			  basic_streambuf<_CharT, _Traits>* __sbout,
118*38fd1498Szrj 			  bool& __ineof)
119*38fd1498Szrj     {
120*38fd1498Szrj       streamsize __ret = 0;
121*38fd1498Szrj       __ineof = true;
122*38fd1498Szrj       typename _Traits::int_type __c = __sbin->sgetc();
123*38fd1498Szrj       while (!_Traits::eq_int_type(__c, _Traits::eof()))
124*38fd1498Szrj 	{
125*38fd1498Szrj 	  __c = __sbout->sputc(_Traits::to_char_type(__c));
126*38fd1498Szrj 	  if (_Traits::eq_int_type(__c, _Traits::eof()))
127*38fd1498Szrj 	    {
128*38fd1498Szrj 	      __ineof = false;
129*38fd1498Szrj 	      break;
130*38fd1498Szrj 	    }
131*38fd1498Szrj 	  ++__ret;
132*38fd1498Szrj 	  __c = __sbin->snextc();
133*38fd1498Szrj 	}
134*38fd1498Szrj       return __ret;
135*38fd1498Szrj     }
136*38fd1498Szrj 
137*38fd1498Szrj   template<typename _CharT, typename _Traits>
138*38fd1498Szrj     inline streamsize
__copy_streambufs(basic_streambuf<_CharT,_Traits> * __sbin,basic_streambuf<_CharT,_Traits> * __sbout)139*38fd1498Szrj     __copy_streambufs(basic_streambuf<_CharT, _Traits>* __sbin,
140*38fd1498Szrj 		      basic_streambuf<_CharT, _Traits>* __sbout)
141*38fd1498Szrj     {
142*38fd1498Szrj       bool __ineof;
143*38fd1498Szrj       return __copy_streambufs_eof(__sbin, __sbout, __ineof);
144*38fd1498Szrj     }
145*38fd1498Szrj 
146*38fd1498Szrj   // Inhibit implicit instantiations for required instantiations,
147*38fd1498Szrj   // which are defined via explicit instantiations elsewhere.
148*38fd1498Szrj #if _GLIBCXX_EXTERN_TEMPLATE
149*38fd1498Szrj   extern template class basic_streambuf<char>;
150*38fd1498Szrj   extern template
151*38fd1498Szrj     streamsize
152*38fd1498Szrj     __copy_streambufs(basic_streambuf<char>*,
153*38fd1498Szrj 		      basic_streambuf<char>*);
154*38fd1498Szrj   extern template
155*38fd1498Szrj     streamsize
156*38fd1498Szrj     __copy_streambufs_eof(basic_streambuf<char>*,
157*38fd1498Szrj 			  basic_streambuf<char>*, bool&);
158*38fd1498Szrj 
159*38fd1498Szrj #ifdef _GLIBCXX_USE_WCHAR_T
160*38fd1498Szrj   extern template class basic_streambuf<wchar_t>;
161*38fd1498Szrj   extern template
162*38fd1498Szrj     streamsize
163*38fd1498Szrj     __copy_streambufs(basic_streambuf<wchar_t>*,
164*38fd1498Szrj 		      basic_streambuf<wchar_t>*);
165*38fd1498Szrj   extern template
166*38fd1498Szrj     streamsize
167*38fd1498Szrj     __copy_streambufs_eof(basic_streambuf<wchar_t>*,
168*38fd1498Szrj 			  basic_streambuf<wchar_t>*, bool&);
169*38fd1498Szrj #endif
170*38fd1498Szrj #endif
171*38fd1498Szrj 
172*38fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION
173*38fd1498Szrj } // namespace std
174*38fd1498Szrj 
175*38fd1498Szrj #endif
176