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