ostream_insert.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Helpers for ostream inserters -*- C++ -*-
  2. // Copyright (C) 2007-2023 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. #include <bits/exception_defines.h>
  30. namespace std _GLIBCXX_VISIBILITY(default)
  31. {
  32. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  33. /// @cond undocumented
  34. template<typename _CharT, typename _Traits>
  35. inline void
  36. __ostream_write(basic_ostream<_CharT, _Traits>& __out,
  37. const _CharT* __s, streamsize __n)
  38. {
  39. typedef basic_ostream<_CharT, _Traits> __ostream_type;
  40. typedef typename __ostream_type::ios_base __ios_base;
  41. const streamsize __put = __out.rdbuf()->sputn(__s, __n);
  42. if (__put != __n)
  43. __out.setstate(__ios_base::badbit);
  44. }
  45. template<typename _CharT, typename _Traits>
  46. inline void
  47. __ostream_fill(basic_ostream<_CharT, _Traits>& __out, streamsize __n)
  48. {
  49. typedef basic_ostream<_CharT, _Traits> __ostream_type;
  50. typedef typename __ostream_type::ios_base __ios_base;
  51. const _CharT __c = __out.fill();
  52. for (; __n > 0; --__n)
  53. {
  54. const typename _Traits::int_type __put = __out.rdbuf()->sputc(__c);
  55. if (_Traits::eq_int_type(__put, _Traits::eof()))
  56. {
  57. __out.setstate(__ios_base::badbit);
  58. break;
  59. }
  60. }
  61. }
  62. template<typename _CharT, typename _Traits>
  63. basic_ostream<_CharT, _Traits>&
  64. __ostream_insert(basic_ostream<_CharT, _Traits>& __out,
  65. const _CharT* __s, streamsize __n)
  66. {
  67. typedef basic_ostream<_CharT, _Traits> __ostream_type;
  68. typedef typename __ostream_type::ios_base __ios_base;
  69. typename __ostream_type::sentry __cerb(__out);
  70. if (__cerb)
  71. {
  72. __try
  73. {
  74. const streamsize __w = __out.width();
  75. if (__w > __n)
  76. {
  77. const bool __left = ((__out.flags()
  78. & __ios_base::adjustfield)
  79. == __ios_base::left);
  80. if (!__left)
  81. __ostream_fill(__out, __w - __n);
  82. if (__out.good())
  83. __ostream_write(__out, __s, __n);
  84. if (__left && __out.good())
  85. __ostream_fill(__out, __w - __n);
  86. }
  87. else
  88. __ostream_write(__out, __s, __n);
  89. __out.width(0);
  90. }
  91. __catch(__cxxabiv1::__forced_unwind&)
  92. {
  93. __out._M_setstate(__ios_base::badbit);
  94. __throw_exception_again;
  95. }
  96. __catch(...)
  97. { __out._M_setstate(__ios_base::badbit); }
  98. }
  99. return __out;
  100. }
  101. // Inhibit implicit instantiations for required instantiations,
  102. // which are defined via explicit instantiations elsewhere.
  103. #if _GLIBCXX_EXTERN_TEMPLATE
  104. extern template ostream& __ostream_insert(ostream&, const char*, streamsize);
  105. #ifdef _GLIBCXX_USE_WCHAR_T
  106. extern template wostream& __ostream_insert(wostream&, const wchar_t*,
  107. streamsize);
  108. #endif
  109. #endif
  110. /// @endcond
  111. _GLIBCXX_END_NAMESPACE_VERSION
  112. } // namespace std
  113. #endif /* _OSTREAM_INSERT_H */