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