libstdc++
print
Go to the documentation of this file.
1 // <print> Print functions -*- C++ -*-
2 
3 // Copyright The GNU Toolchain Authors.
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 include/print
26  * This is a Standard C++ Library header.
27  */
28 
29 #ifndef _GLIBCXX_PRINT
30 #define _GLIBCXX_PRINT 1
31 
32 #pragma GCC system_header
33 
34 #include <bits/requires_hosted.h> // for std::format
35 
36 #define __glibcxx_want_print
37 #include <bits/version.h>
38 
39 #ifdef __cpp_lib_print // C++ >= 23
40 
41 #include <format>
42 #include <cstdio>
43 #include <cerrno>
44 #include <bits/functexcept.h>
45 
46 #ifdef _WIN32
47 # include <system_error>
48 #endif
49 
50 namespace std _GLIBCXX_VISIBILITY(default)
51 {
52 _GLIBCXX_BEGIN_NAMESPACE_VERSION
53 
54  inline void
55  vprint_nonunicode(FILE* __stream, string_view __fmt, format_args __args)
56  {
57  __format::_Str_sink<char> __buf;
58  std::vformat_to(__buf.out(), __fmt, __args);
59  auto __out = __buf.view();
60  if (std::fwrite(__out.data(), 1, __out.size(), __stream) != __out.size())
61  __throw_system_error(EIO);
62  }
63 
64  inline void
65  vprint_unicode(FILE* __stream, string_view __fmt, format_args __args)
66  {
67 #if !defined(_WIN32) || defined(__CYGWIN__)
68  // For most targets we don't need to do anything special to write
69  // Unicode to a terminal.
70  std::vprint_nonunicode(__stream, __fmt, __args);
71 #else
72  __format::_Str_sink<char> __buf;
73  std::vformat_to(__buf.out(), __fmt, __args);
74  auto __out = __buf.view();
75 
76  void* __open_terminal(FILE*);
77  error_code __write_to_terminal(void*, span<char>);
78  // If stream refers to a terminal, write a native Unicode string to it.
79  if (auto __term = __open_terminal(__stream))
80  {
81  string __out = std::vformat(__fmt, __args);
82  error_code __e;
83  if (!std::fflush(__stream))
84  {
85  __e = __write_to_terminal(__term, __out);
86  if (!__e)
87  return;
88  if (__e == std::make_error_code(errc::illegal_byte_sequence))
89  return;
90  }
91  else
92  __e = error_code(errno, generic_category());
93  _GLIBCXX_THROW_OR_ABORT(system_error(__e, "std::vprint_unicode"));
94  }
95 
96  // Otherwise just write the string to the file as vprint_nonunicode does.
97  if (std::fwrite(__out.data(), 1, __out.size(), __stream) != __out.size())
98  __throw_system_error(EIO);
99 #endif
100  }
101 
102  template<typename... _Args>
103  inline void
104  print(FILE* __stream, format_string<_Args...> __fmt, _Args&&... __args)
105  {
106  auto __fmtargs = std::make_format_args(__args...);
107  if constexpr (__unicode::__literal_encoding_is_utf8())
108  std::vprint_unicode(__stream, __fmt.get(), __fmtargs);
109  else
110  std::vprint_nonunicode(__stream, __fmt.get(), __fmtargs);
111  }
112 
113  template<typename... _Args>
114  inline void
115  print(format_string<_Args...> __fmt, _Args&&... __args)
116  { std::print(stdout, __fmt, std::forward<_Args>(__args)...); }
117 
118  template<typename... _Args>
119  inline void
120  println(FILE* __stream, format_string<_Args...> __fmt, _Args&&... __args)
121  {
122  std::print(__stream, "{}\n",
123  std::format(__fmt, std::forward<_Args>(__args)...));
124  }
125 
126  template<typename... _Args>
127  inline void
128  println(format_string<_Args...> __fmt, _Args&&... __args)
129  { std::println(stdout, __fmt, std::forward<_Args>(__args)...); }
130 
131  inline void
132  vprint_unicode(string_view __fmt, format_args __args)
133  { std::vprint_unicode(stdout, __fmt, __args); }
134 
135  inline void
136  vprint_nonunicode(string_view __fmt, format_args __args)
137  { std::vprint_nonunicode(stdout, __fmt, __args); }
138 
139  // Defined for C++26, supported as an extension to C++23.
140  inline void println(FILE* __stream)
141  {
142 #if defined(_WIN32) && !defined(__CYGWIN__)
143  if constexpr (__unicode::__literal_encoding_is_utf8())
144  std::vprint_unicode(__stream, "\n", std::make_format_args());
145  else
146 #endif
147  if (std::putc('\n', __stream) == EOF)
148  __throw_system_error(EIO);
149  }
150 
151  inline void println() { std::println(stdout); }
152 
153 _GLIBCXX_END_NAMESPACE_VERSION
154 } // namespace std
155 #endif // __cpp_lib_print
156 #endif // _GLIBCXX_PRINT