ostream_insert.h 3.9 KB

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