You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.9KB

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