| 1 | // Helpers for ostream inserters -*- C++ -*-
|
|---|
| 2 |
|
|---|
| 3 | // Copyright (C) 2007-2021 Free Software Foundation, Inc.
|
|---|
| 4 | //
|
|---|
| 5 | // This file is part of the GNU ISO C++ Library. This library is free
|
|---|
| 6 | // software; you can redistribute it and/or modify it under the
|
|---|
| 7 | // terms of the GNU General Public License as published by the
|
|---|
| 8 | // Free Software Foundation; either version 3, or (at your option)
|
|---|
| 9 | // any later version.
|
|---|
| 10 |
|
|---|
| 11 | // This library is distributed in the hope that it will be useful,
|
|---|
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 14 | // GNU General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | // Under Section 7 of GPL version 3, you are granted additional
|
|---|
| 17 | // permissions described in the GCC Runtime Library Exception, version
|
|---|
| 18 | // 3.1, as published by the Free Software Foundation.
|
|---|
| 19 |
|
|---|
| 20 | // You should have received a copy of the GNU General Public License and
|
|---|
| 21 | // a copy of the GCC Runtime Library Exception along with this program;
|
|---|
| 22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
|---|
| 23 | // <http://www.gnu.org/licenses/>.
|
|---|
| 24 |
|
|---|
| 25 | /** @file bits/ostream_insert.h
|
|---|
| 26 | * This is an internal header file, included by other library headers.
|
|---|
| 27 | * Do not attempt to use it directly. @headername{ostream}
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | #ifndef _OSTREAM_INSERT_H
|
|---|
| 31 | #define _OSTREAM_INSERT_H 1
|
|---|
| 32 |
|
|---|
| 33 | #pragma GCC system_header
|
|---|
| 34 |
|
|---|
| 35 | #include <iosfwd>
|
|---|
| 36 | #include <bits/cxxabi_forced.h>
|
|---|
| 37 | #include <bits/exception_defines.h>
|
|---|
| 38 |
|
|---|
| 39 | namespace std _GLIBCXX_VISIBILITY(default)
|
|---|
| 40 | {
|
|---|
| 41 | _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
|---|
| 42 |
|
|---|
| 43 | template<typename _CharT, typename _Traits>
|
|---|
| 44 | inline void
|
|---|
| 45 | __ostream_write(basic_ostream<_CharT, _Traits>& __out,
|
|---|
| 46 | const _CharT* __s, streamsize __n)
|
|---|
| 47 | {
|
|---|
| 48 | typedef basic_ostream<_CharT, _Traits> __ostream_type;
|
|---|
| 49 | typedef typename __ostream_type::ios_base __ios_base;
|
|---|
| 50 |
|
|---|
| 51 | const streamsize __put = __out.rdbuf()->sputn(__s, __n);
|
|---|
| 52 | if (__put != __n)
|
|---|
| 53 | __out.setstate(__ios_base::badbit);
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | template<typename _CharT, typename _Traits>
|
|---|
| 57 | inline void
|
|---|
| 58 | __ostream_fill(basic_ostream<_CharT, _Traits>& __out, streamsize __n)
|
|---|
| 59 | {
|
|---|
| 60 | typedef basic_ostream<_CharT, _Traits> __ostream_type;
|
|---|
| 61 | typedef typename __ostream_type::ios_base __ios_base;
|
|---|
| 62 |
|
|---|
| 63 | const _CharT __c = __out.fill();
|
|---|
| 64 | for (; __n > 0; --__n)
|
|---|
| 65 | {
|
|---|
| 66 | const typename _Traits::int_type __put = __out.rdbuf()->sputc(__c);
|
|---|
| 67 | if (_Traits::eq_int_type(__put, _Traits::eof()))
|
|---|
| 68 | {
|
|---|
| 69 | __out.setstate(__ios_base::badbit);
|
|---|
| 70 | break;
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | template<typename _CharT, typename _Traits>
|
|---|
| 76 | basic_ostream<_CharT, _Traits>&
|
|---|
| 77 | __ostream_insert(basic_ostream<_CharT, _Traits>& __out,
|
|---|
| 78 | const _CharT* __s, streamsize __n)
|
|---|
| 79 | {
|
|---|
| 80 | typedef basic_ostream<_CharT, _Traits> __ostream_type;
|
|---|
| 81 | typedef typename __ostream_type::ios_base __ios_base;
|
|---|
| 82 |
|
|---|
| 83 | typename __ostream_type::sentry __cerb(__out);
|
|---|
| 84 | if (__cerb)
|
|---|
| 85 | {
|
|---|
| 86 | __try
|
|---|
| 87 | {
|
|---|
| 88 | const streamsize __w = __out.width();
|
|---|
| 89 | if (__w > __n)
|
|---|
| 90 | {
|
|---|
| 91 | const bool __left = ((__out.flags()
|
|---|
| 92 | & __ios_base::adjustfield)
|
|---|
| 93 | == __ios_base::left);
|
|---|
| 94 | if (!__left)
|
|---|
| 95 | __ostream_fill(__out, __w - __n);
|
|---|
| 96 | if (__out.good())
|
|---|
| 97 | __ostream_write(__out, __s, __n);
|
|---|
| 98 | if (__left && __out.good())
|
|---|
| 99 | __ostream_fill(__out, __w - __n);
|
|---|
| 100 | }
|
|---|
| 101 | else
|
|---|
| 102 | __ostream_write(__out, __s, __n);
|
|---|
| 103 | __out.width(0);
|
|---|
| 104 | }
|
|---|
| 105 | __catch(__cxxabiv1::__forced_unwind&)
|
|---|
| 106 | {
|
|---|
| 107 | __out._M_setstate(__ios_base::badbit);
|
|---|
| 108 | __throw_exception_again;
|
|---|
| 109 | }
|
|---|
| 110 | __catch(...)
|
|---|
| 111 | { __out._M_setstate(__ios_base::badbit); }
|
|---|
| 112 | }
|
|---|
| 113 | return __out;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | // Inhibit implicit instantiations for required instantiations,
|
|---|
| 117 | // which are defined via explicit instantiations elsewhere.
|
|---|
| 118 | #if _GLIBCXX_EXTERN_TEMPLATE
|
|---|
| 119 | extern template ostream& __ostream_insert(ostream&, const char*, streamsize);
|
|---|
| 120 |
|
|---|
| 121 | #ifdef _GLIBCXX_USE_WCHAR_T
|
|---|
| 122 | extern template wostream& __ostream_insert(wostream&, const wchar_t*,
|
|---|
| 123 | streamsize);
|
|---|
| 124 | #endif
|
|---|
| 125 | #endif
|
|---|
| 126 |
|
|---|
| 127 | _GLIBCXX_END_NAMESPACE_VERSION
|
|---|
| 128 | } // namespace std
|
|---|
| 129 |
|
|---|
| 130 | #endif /* _OSTREAM_INSERT_H */
|
|---|