1*404b540aSrobert // ostream classes -*- C++ -*-
2*404b540aSrobert
3*404b540aSrobert // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4*404b540aSrobert // 2006, 2007
5*404b540aSrobert // Free Software Foundation, Inc.
6*404b540aSrobert //
7*404b540aSrobert // This file is part of the GNU ISO C++ Library. This library is free
8*404b540aSrobert // software; you can redistribute it and/or modify it under the
9*404b540aSrobert // terms of the GNU General Public License as published by the
10*404b540aSrobert // Free Software Foundation; either version 2, or (at your option)
11*404b540aSrobert // any later version.
12*404b540aSrobert
13*404b540aSrobert // This library is distributed in the hope that it will be useful,
14*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
15*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16*404b540aSrobert // GNU General Public License for more details.
17*404b540aSrobert
18*404b540aSrobert // You should have received a copy of the GNU General Public License along
19*404b540aSrobert // with this library; see the file COPYING. If not, write to the Free
20*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21*404b540aSrobert // USA.
22*404b540aSrobert
23*404b540aSrobert // As a special exception, you may use this file as part of a free software
24*404b540aSrobert // library without restriction. Specifically, if other files instantiate
25*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
26*404b540aSrobert // this file and link it with other files to produce an executable, this
27*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
28*404b540aSrobert // the GNU General Public License. This exception does not however
29*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
30*404b540aSrobert // the GNU General Public License.
31*404b540aSrobert
32*404b540aSrobert /** @file ostream.tcc
33*404b540aSrobert * This is an internal header file, included by other library headers.
34*404b540aSrobert * You should not attempt to use it directly.
35*404b540aSrobert */
36*404b540aSrobert
37*404b540aSrobert //
38*404b540aSrobert // ISO C++ 14882: 27.6.2 Output streams
39*404b540aSrobert //
40*404b540aSrobert
41*404b540aSrobert #ifndef _OSTREAM_TCC
42*404b540aSrobert #define _OSTREAM_TCC 1
43*404b540aSrobert
44*404b540aSrobert #pragma GCC system_header
45*404b540aSrobert
46*404b540aSrobert #include <locale>
47*404b540aSrobert
_GLIBCXX_BEGIN_NAMESPACE(std)48*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(std)
49*404b540aSrobert
50*404b540aSrobert template<typename _CharT, typename _Traits>
51*404b540aSrobert basic_ostream<_CharT, _Traits>::sentry::
52*404b540aSrobert sentry(basic_ostream<_CharT, _Traits>& __os)
53*404b540aSrobert : _M_ok(false), _M_os(__os)
54*404b540aSrobert {
55*404b540aSrobert // XXX MT
56*404b540aSrobert if (__os.tie() && __os.good())
57*404b540aSrobert __os.tie()->flush();
58*404b540aSrobert
59*404b540aSrobert if (__os.good())
60*404b540aSrobert _M_ok = true;
61*404b540aSrobert else
62*404b540aSrobert __os.setstate(ios_base::failbit);
63*404b540aSrobert }
64*404b540aSrobert
65*404b540aSrobert template<typename _CharT, typename _Traits>
66*404b540aSrobert template<typename _ValueT>
67*404b540aSrobert basic_ostream<_CharT, _Traits>&
68*404b540aSrobert basic_ostream<_CharT, _Traits>::
_M_insert(_ValueT __v)69*404b540aSrobert _M_insert(_ValueT __v)
70*404b540aSrobert {
71*404b540aSrobert sentry __cerb(*this);
72*404b540aSrobert if (__cerb)
73*404b540aSrobert {
74*404b540aSrobert ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
75*404b540aSrobert try
76*404b540aSrobert {
77*404b540aSrobert const __num_put_type& __np = __check_facet(this->_M_num_put);
78*404b540aSrobert if (__np.put(*this, *this, this->fill(), __v).failed())
79*404b540aSrobert __err |= ios_base::badbit;
80*404b540aSrobert }
81*404b540aSrobert catch(...)
82*404b540aSrobert { this->_M_setstate(ios_base::badbit); }
83*404b540aSrobert if (__err)
84*404b540aSrobert this->setstate(__err);
85*404b540aSrobert }
86*404b540aSrobert return *this;
87*404b540aSrobert }
88*404b540aSrobert
89*404b540aSrobert template<typename _CharT, typename _Traits>
90*404b540aSrobert basic_ostream<_CharT, _Traits>&
91*404b540aSrobert basic_ostream<_CharT, _Traits>::
operator <<(short __n)92*404b540aSrobert operator<<(short __n)
93*404b540aSrobert {
94*404b540aSrobert // _GLIBCXX_RESOLVE_LIB_DEFECTS
95*404b540aSrobert // 117. basic_ostream uses nonexistent num_put member functions.
96*404b540aSrobert const ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
97*404b540aSrobert if (__fmt == ios_base::oct || __fmt == ios_base::hex)
98*404b540aSrobert return _M_insert(static_cast<long>(static_cast<unsigned short>(__n)));
99*404b540aSrobert else
100*404b540aSrobert return _M_insert(static_cast<long>(__n));
101*404b540aSrobert }
102*404b540aSrobert
103*404b540aSrobert template<typename _CharT, typename _Traits>
104*404b540aSrobert basic_ostream<_CharT, _Traits>&
105*404b540aSrobert basic_ostream<_CharT, _Traits>::
operator <<(int __n)106*404b540aSrobert operator<<(int __n)
107*404b540aSrobert {
108*404b540aSrobert // _GLIBCXX_RESOLVE_LIB_DEFECTS
109*404b540aSrobert // 117. basic_ostream uses nonexistent num_put member functions.
110*404b540aSrobert const ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
111*404b540aSrobert if (__fmt == ios_base::oct || __fmt == ios_base::hex)
112*404b540aSrobert return _M_insert(static_cast<long>(static_cast<unsigned int>(__n)));
113*404b540aSrobert else
114*404b540aSrobert return _M_insert(static_cast<long>(__n));
115*404b540aSrobert }
116*404b540aSrobert
117*404b540aSrobert template<typename _CharT, typename _Traits>
118*404b540aSrobert basic_ostream<_CharT, _Traits>&
119*404b540aSrobert basic_ostream<_CharT, _Traits>::
operator <<(__streambuf_type * __sbin)120*404b540aSrobert operator<<(__streambuf_type* __sbin)
121*404b540aSrobert {
122*404b540aSrobert ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
123*404b540aSrobert sentry __cerb(*this);
124*404b540aSrobert if (__cerb && __sbin)
125*404b540aSrobert {
126*404b540aSrobert try
127*404b540aSrobert {
128*404b540aSrobert if (!__copy_streambufs(__sbin, this->rdbuf()))
129*404b540aSrobert __err |= ios_base::failbit;
130*404b540aSrobert }
131*404b540aSrobert catch(...)
132*404b540aSrobert { this->_M_setstate(ios_base::failbit); }
133*404b540aSrobert }
134*404b540aSrobert else if (!__sbin)
135*404b540aSrobert __err |= ios_base::badbit;
136*404b540aSrobert if (__err)
137*404b540aSrobert this->setstate(__err);
138*404b540aSrobert return *this;
139*404b540aSrobert }
140*404b540aSrobert
141*404b540aSrobert template<typename _CharT, typename _Traits>
142*404b540aSrobert basic_ostream<_CharT, _Traits>&
143*404b540aSrobert basic_ostream<_CharT, _Traits>::
put(char_type __c)144*404b540aSrobert put(char_type __c)
145*404b540aSrobert {
146*404b540aSrobert // _GLIBCXX_RESOLVE_LIB_DEFECTS
147*404b540aSrobert // DR 60. What is a formatted input function?
148*404b540aSrobert // basic_ostream::put(char_type) is an unformatted output function.
149*404b540aSrobert // DR 63. Exception-handling policy for unformatted output.
150*404b540aSrobert // Unformatted output functions should catch exceptions thrown
151*404b540aSrobert // from streambuf members.
152*404b540aSrobert sentry __cerb(*this);
153*404b540aSrobert if (__cerb)
154*404b540aSrobert {
155*404b540aSrobert ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
156*404b540aSrobert try
157*404b540aSrobert {
158*404b540aSrobert const int_type __put = this->rdbuf()->sputc(__c);
159*404b540aSrobert if (traits_type::eq_int_type(__put, traits_type::eof()))
160*404b540aSrobert __err |= ios_base::badbit;
161*404b540aSrobert }
162*404b540aSrobert catch (...)
163*404b540aSrobert { this->_M_setstate(ios_base::badbit); }
164*404b540aSrobert if (__err)
165*404b540aSrobert this->setstate(__err);
166*404b540aSrobert }
167*404b540aSrobert return *this;
168*404b540aSrobert }
169*404b540aSrobert
170*404b540aSrobert template<typename _CharT, typename _Traits>
171*404b540aSrobert basic_ostream<_CharT, _Traits>&
172*404b540aSrobert basic_ostream<_CharT, _Traits>::
write(const _CharT * __s,streamsize __n)173*404b540aSrobert write(const _CharT* __s, streamsize __n)
174*404b540aSrobert {
175*404b540aSrobert // _GLIBCXX_RESOLVE_LIB_DEFECTS
176*404b540aSrobert // DR 60. What is a formatted input function?
177*404b540aSrobert // basic_ostream::write(const char_type*, streamsize) is an
178*404b540aSrobert // unformatted output function.
179*404b540aSrobert // DR 63. Exception-handling policy for unformatted output.
180*404b540aSrobert // Unformatted output functions should catch exceptions thrown
181*404b540aSrobert // from streambuf members.
182*404b540aSrobert sentry __cerb(*this);
183*404b540aSrobert if (__cerb)
184*404b540aSrobert {
185*404b540aSrobert try
186*404b540aSrobert { _M_write(__s, __n); }
187*404b540aSrobert catch (...)
188*404b540aSrobert { this->_M_setstate(ios_base::badbit); }
189*404b540aSrobert }
190*404b540aSrobert return *this;
191*404b540aSrobert }
192*404b540aSrobert
193*404b540aSrobert template<typename _CharT, typename _Traits>
194*404b540aSrobert basic_ostream<_CharT, _Traits>&
195*404b540aSrobert basic_ostream<_CharT, _Traits>::
flush()196*404b540aSrobert flush()
197*404b540aSrobert {
198*404b540aSrobert // _GLIBCXX_RESOLVE_LIB_DEFECTS
199*404b540aSrobert // DR 60. What is a formatted input function?
200*404b540aSrobert // basic_ostream::flush() is *not* an unformatted output function.
201*404b540aSrobert ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
202*404b540aSrobert try
203*404b540aSrobert {
204*404b540aSrobert if (this->rdbuf() && this->rdbuf()->pubsync() == -1)
205*404b540aSrobert __err |= ios_base::badbit;
206*404b540aSrobert }
207*404b540aSrobert catch(...)
208*404b540aSrobert { this->_M_setstate(ios_base::badbit); }
209*404b540aSrobert if (__err)
210*404b540aSrobert this->setstate(__err);
211*404b540aSrobert return *this;
212*404b540aSrobert }
213*404b540aSrobert
214*404b540aSrobert template<typename _CharT, typename _Traits>
215*404b540aSrobert typename basic_ostream<_CharT, _Traits>::pos_type
216*404b540aSrobert basic_ostream<_CharT, _Traits>::
tellp()217*404b540aSrobert tellp()
218*404b540aSrobert {
219*404b540aSrobert pos_type __ret = pos_type(-1);
220*404b540aSrobert try
221*404b540aSrobert {
222*404b540aSrobert if (!this->fail())
223*404b540aSrobert __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
224*404b540aSrobert }
225*404b540aSrobert catch(...)
226*404b540aSrobert { this->_M_setstate(ios_base::badbit); }
227*404b540aSrobert return __ret;
228*404b540aSrobert }
229*404b540aSrobert
230*404b540aSrobert template<typename _CharT, typename _Traits>
231*404b540aSrobert basic_ostream<_CharT, _Traits>&
232*404b540aSrobert basic_ostream<_CharT, _Traits>::
seekp(pos_type __pos)233*404b540aSrobert seekp(pos_type __pos)
234*404b540aSrobert {
235*404b540aSrobert ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
236*404b540aSrobert try
237*404b540aSrobert {
238*404b540aSrobert if (!this->fail())
239*404b540aSrobert {
240*404b540aSrobert // _GLIBCXX_RESOLVE_LIB_DEFECTS
241*404b540aSrobert // 136. seekp, seekg setting wrong streams?
242*404b540aSrobert const pos_type __p = this->rdbuf()->pubseekpos(__pos,
243*404b540aSrobert ios_base::out);
244*404b540aSrobert
245*404b540aSrobert // 129. Need error indication from seekp() and seekg()
246*404b540aSrobert if (__p == pos_type(off_type(-1)))
247*404b540aSrobert __err |= ios_base::failbit;
248*404b540aSrobert }
249*404b540aSrobert }
250*404b540aSrobert catch(...)
251*404b540aSrobert { this->_M_setstate(ios_base::badbit); }
252*404b540aSrobert if (__err)
253*404b540aSrobert this->setstate(__err);
254*404b540aSrobert return *this;
255*404b540aSrobert }
256*404b540aSrobert
257*404b540aSrobert template<typename _CharT, typename _Traits>
258*404b540aSrobert basic_ostream<_CharT, _Traits>&
259*404b540aSrobert basic_ostream<_CharT, _Traits>::
seekp(off_type __off,ios_base::seekdir __dir)260*404b540aSrobert seekp(off_type __off, ios_base::seekdir __dir)
261*404b540aSrobert {
262*404b540aSrobert ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
263*404b540aSrobert try
264*404b540aSrobert {
265*404b540aSrobert if (!this->fail())
266*404b540aSrobert {
267*404b540aSrobert // _GLIBCXX_RESOLVE_LIB_DEFECTS
268*404b540aSrobert // 136. seekp, seekg setting wrong streams?
269*404b540aSrobert const pos_type __p = this->rdbuf()->pubseekoff(__off, __dir,
270*404b540aSrobert ios_base::out);
271*404b540aSrobert
272*404b540aSrobert // 129. Need error indication from seekp() and seekg()
273*404b540aSrobert if (__p == pos_type(off_type(-1)))
274*404b540aSrobert __err |= ios_base::failbit;
275*404b540aSrobert }
276*404b540aSrobert }
277*404b540aSrobert catch(...)
278*404b540aSrobert { this->_M_setstate(ios_base::badbit); }
279*404b540aSrobert if (__err)
280*404b540aSrobert this->setstate(__err);
281*404b540aSrobert return *this;
282*404b540aSrobert }
283*404b540aSrobert
284*404b540aSrobert template<typename _CharT, typename _Traits>
285*404b540aSrobert basic_ostream<_CharT, _Traits>&
operator <<(basic_ostream<_CharT,_Traits> & __out,const char * __s)286*404b540aSrobert operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
287*404b540aSrobert {
288*404b540aSrobert if (!__s)
289*404b540aSrobert __out.setstate(ios_base::badbit);
290*404b540aSrobert else
291*404b540aSrobert {
292*404b540aSrobert // _GLIBCXX_RESOLVE_LIB_DEFECTS
293*404b540aSrobert // 167. Improper use of traits_type::length()
294*404b540aSrobert const size_t __clen = char_traits<char>::length(__s);
295*404b540aSrobert _CharT* __ws = 0;
296*404b540aSrobert try
297*404b540aSrobert {
298*404b540aSrobert __ws = new _CharT[__clen];
299*404b540aSrobert for (size_t __i = 0; __i < __clen; ++__i)
300*404b540aSrobert __ws[__i] = __out.widen(__s[__i]);
301*404b540aSrobert }
302*404b540aSrobert catch(...)
303*404b540aSrobert {
304*404b540aSrobert delete [] __ws;
305*404b540aSrobert __out._M_setstate(ios_base::badbit);
306*404b540aSrobert return __out;
307*404b540aSrobert }
308*404b540aSrobert
309*404b540aSrobert try
310*404b540aSrobert {
311*404b540aSrobert __ostream_insert(__out, __ws, __clen);
312*404b540aSrobert delete [] __ws;
313*404b540aSrobert }
314*404b540aSrobert catch(...)
315*404b540aSrobert {
316*404b540aSrobert delete [] __ws;
317*404b540aSrobert __throw_exception_again;
318*404b540aSrobert }
319*404b540aSrobert }
320*404b540aSrobert return __out;
321*404b540aSrobert }
322*404b540aSrobert
323*404b540aSrobert // Inhibit implicit instantiations for required instantiations,
324*404b540aSrobert // which are defined via explicit instantiations elsewhere.
325*404b540aSrobert // NB: This syntax is a GNU extension.
326*404b540aSrobert #if _GLIBCXX_EXTERN_TEMPLATE
327*404b540aSrobert extern template class basic_ostream<char>;
328*404b540aSrobert extern template ostream& endl(ostream&);
329*404b540aSrobert extern template ostream& ends(ostream&);
330*404b540aSrobert extern template ostream& flush(ostream&);
331*404b540aSrobert extern template ostream& operator<<(ostream&, char);
332*404b540aSrobert extern template ostream& operator<<(ostream&, unsigned char);
333*404b540aSrobert extern template ostream& operator<<(ostream&, signed char);
334*404b540aSrobert extern template ostream& operator<<(ostream&, const char*);
335*404b540aSrobert extern template ostream& operator<<(ostream&, const unsigned char*);
336*404b540aSrobert extern template ostream& operator<<(ostream&, const signed char*);
337*404b540aSrobert
338*404b540aSrobert extern template ostream& ostream::_M_insert(long);
339*404b540aSrobert extern template ostream& ostream::_M_insert(unsigned long);
340*404b540aSrobert extern template ostream& ostream::_M_insert(bool);
341*404b540aSrobert #ifdef _GLIBCXX_USE_LONG_LONG
342*404b540aSrobert extern template ostream& ostream::_M_insert(long long);
343*404b540aSrobert extern template ostream& ostream::_M_insert(unsigned long long);
344*404b540aSrobert #endif
345*404b540aSrobert extern template ostream& ostream::_M_insert(double);
346*404b540aSrobert extern template ostream& ostream::_M_insert(long double);
347*404b540aSrobert extern template ostream& ostream::_M_insert(const void*);
348*404b540aSrobert
349*404b540aSrobert #ifdef _GLIBCXX_USE_WCHAR_T
350*404b540aSrobert extern template class basic_ostream<wchar_t>;
351*404b540aSrobert extern template wostream& endl(wostream&);
352*404b540aSrobert extern template wostream& ends(wostream&);
353*404b540aSrobert extern template wostream& flush(wostream&);
354*404b540aSrobert extern template wostream& operator<<(wostream&, wchar_t);
355*404b540aSrobert extern template wostream& operator<<(wostream&, char);
356*404b540aSrobert extern template wostream& operator<<(wostream&, const wchar_t*);
357*404b540aSrobert extern template wostream& operator<<(wostream&, const char*);
358*404b540aSrobert
359*404b540aSrobert extern template wostream& wostream::_M_insert(long);
360*404b540aSrobert extern template wostream& wostream::_M_insert(unsigned long);
361*404b540aSrobert extern template wostream& wostream::_M_insert(bool);
362*404b540aSrobert #ifdef _GLIBCXX_USE_LONG_LONG
363*404b540aSrobert extern template wostream& wostream::_M_insert(long long);
364*404b540aSrobert extern template wostream& wostream::_M_insert(unsigned long long);
365*404b540aSrobert #endif
366*404b540aSrobert extern template wostream& wostream::_M_insert(double);
367*404b540aSrobert extern template wostream& wostream::_M_insert(long double);
368*404b540aSrobert extern template wostream& wostream::_M_insert(const void*);
369*404b540aSrobert #endif
370*404b540aSrobert #endif
371*404b540aSrobert
372*404b540aSrobert _GLIBCXX_END_NAMESPACE
373*404b540aSrobert
374*404b540aSrobert #endif
375