1*38fd1498Szrj // Helpers for ostream inserters -*- C++ -*-
2*38fd1498Szrj
3*38fd1498Szrj // Copyright (C) 2007-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/ostream_insert.h
26*38fd1498Szrj * This is an internal header file, included by other library headers.
27*38fd1498Szrj * Do not attempt to use it directly. @headername{ostream}
28*38fd1498Szrj */
29*38fd1498Szrj
30*38fd1498Szrj #ifndef _OSTREAM_INSERT_H
31*38fd1498Szrj #define _OSTREAM_INSERT_H 1
32*38fd1498Szrj
33*38fd1498Szrj #pragma GCC system_header
34*38fd1498Szrj
35*38fd1498Szrj #include <iosfwd>
36*38fd1498Szrj #include <bits/cxxabi_forced.h>
37*38fd1498Szrj
_GLIBCXX_VISIBILITY(default)38*38fd1498Szrj namespace std _GLIBCXX_VISIBILITY(default)
39*38fd1498Szrj {
40*38fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION
41*38fd1498Szrj
42*38fd1498Szrj template<typename _CharT, typename _Traits>
43*38fd1498Szrj inline void
44*38fd1498Szrj __ostream_write(basic_ostream<_CharT, _Traits>& __out,
45*38fd1498Szrj const _CharT* __s, streamsize __n)
46*38fd1498Szrj {
47*38fd1498Szrj typedef basic_ostream<_CharT, _Traits> __ostream_type;
48*38fd1498Szrj typedef typename __ostream_type::ios_base __ios_base;
49*38fd1498Szrj
50*38fd1498Szrj const streamsize __put = __out.rdbuf()->sputn(__s, __n);
51*38fd1498Szrj if (__put != __n)
52*38fd1498Szrj __out.setstate(__ios_base::badbit);
53*38fd1498Szrj }
54*38fd1498Szrj
55*38fd1498Szrj template<typename _CharT, typename _Traits>
56*38fd1498Szrj inline void
57*38fd1498Szrj __ostream_fill(basic_ostream<_CharT, _Traits>& __out, streamsize __n)
58*38fd1498Szrj {
59*38fd1498Szrj typedef basic_ostream<_CharT, _Traits> __ostream_type;
60*38fd1498Szrj typedef typename __ostream_type::ios_base __ios_base;
61*38fd1498Szrj
62*38fd1498Szrj const _CharT __c = __out.fill();
63*38fd1498Szrj for (; __n > 0; --__n)
64*38fd1498Szrj {
65*38fd1498Szrj const typename _Traits::int_type __put = __out.rdbuf()->sputc(__c);
66*38fd1498Szrj if (_Traits::eq_int_type(__put, _Traits::eof()))
67*38fd1498Szrj {
68*38fd1498Szrj __out.setstate(__ios_base::badbit);
69*38fd1498Szrj break;
70*38fd1498Szrj }
71*38fd1498Szrj }
72*38fd1498Szrj }
73*38fd1498Szrj
74*38fd1498Szrj template<typename _CharT, typename _Traits>
75*38fd1498Szrj basic_ostream<_CharT, _Traits>&
76*38fd1498Szrj __ostream_insert(basic_ostream<_CharT, _Traits>& __out,
77*38fd1498Szrj const _CharT* __s, streamsize __n)
78*38fd1498Szrj {
79*38fd1498Szrj typedef basic_ostream<_CharT, _Traits> __ostream_type;
80*38fd1498Szrj typedef typename __ostream_type::ios_base __ios_base;
81*38fd1498Szrj
82*38fd1498Szrj typename __ostream_type::sentry __cerb(__out);
83*38fd1498Szrj if (__cerb)
84*38fd1498Szrj {
85*38fd1498Szrj __try
86*38fd1498Szrj {
87*38fd1498Szrj const streamsize __w = __out.width();
88*38fd1498Szrj if (__w > __n)
89*38fd1498Szrj {
90*38fd1498Szrj const bool __left = ((__out.flags()
91*38fd1498Szrj & __ios_base::adjustfield)
92*38fd1498Szrj == __ios_base::left);
93*38fd1498Szrj if (!__left)
94*38fd1498Szrj __ostream_fill(__out, __w - __n);
95*38fd1498Szrj if (__out.good())
96*38fd1498Szrj __ostream_write(__out, __s, __n);
97*38fd1498Szrj if (__left && __out.good())
98*38fd1498Szrj __ostream_fill(__out, __w - __n);
99*38fd1498Szrj }
100*38fd1498Szrj else
101*38fd1498Szrj __ostream_write(__out, __s, __n);
102*38fd1498Szrj __out.width(0);
103*38fd1498Szrj }
104*38fd1498Szrj __catch(__cxxabiv1::__forced_unwind&)
105*38fd1498Szrj {
106*38fd1498Szrj __out._M_setstate(__ios_base::badbit);
107*38fd1498Szrj __throw_exception_again;
108*38fd1498Szrj }
109*38fd1498Szrj __catch(...)
110*38fd1498Szrj { __out._M_setstate(__ios_base::badbit); }
111*38fd1498Szrj }
112*38fd1498Szrj return __out;
113*38fd1498Szrj }
114*38fd1498Szrj
115*38fd1498Szrj // Inhibit implicit instantiations for required instantiations,
116*38fd1498Szrj // which are defined via explicit instantiations elsewhere.
117*38fd1498Szrj #if _GLIBCXX_EXTERN_TEMPLATE
118*38fd1498Szrj extern template ostream& __ostream_insert(ostream&, const char*, streamsize);
119*38fd1498Szrj
120*38fd1498Szrj #ifdef _GLIBCXX_USE_WCHAR_T
121*38fd1498Szrj extern template wostream& __ostream_insert(wostream&, const wchar_t*,
122*38fd1498Szrj streamsize);
123*38fd1498Szrj #endif
124*38fd1498Szrj #endif
125*38fd1498Szrj
126*38fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION
127*38fd1498Szrj } // namespace std
128*38fd1498Szrj
129*38fd1498Szrj #endif /* _OSTREAM_INSERT_H */
130