1 // <print> Print functions -*- C++ -*-
3 // Copyright The GNU Toolchain Authors.
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)
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.
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.
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/>.
25 /** @file include/print
26 * This is a Standard C++ Library header.
29 #ifndef _GLIBCXX_PRINT
30 #define _GLIBCXX_PRINT 1
32 #pragma GCC system_header
34 #include <bits/requires_hosted.h> // for std::format
36 #define __glibcxx_want_print
37 #include <bits/version.h>
39 #ifdef __cpp_lib_print // C++ >= 23
44 #include <bits/functexcept.h>
47 # include <system_error>
50 namespace std _GLIBCXX_VISIBILITY(default)
52 _GLIBCXX_BEGIN_NAMESPACE_VERSION
55 vprint_nonunicode(FILE* __stream, string_view __fmt, format_args __args)
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);
65 vprint_unicode(FILE* __stream, string_view __fmt, format_args __args)
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);
72 __format::_Str_sink<char> __buf;
73 std::vformat_to(__buf.out(), __fmt, __args);
74 auto __out = __buf.view();
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))
81 string __out = std::vformat(__fmt, __args);
83 if (!std::fflush(__stream))
85 __e = __write_to_terminal(__term, __out);
88 if (__e == std::make_error_code(errc::illegal_byte_sequence))
92 __e = error_code(errno, generic_category());
93 _GLIBCXX_THROW_OR_ABORT(system_error(__e, "std::vprint_unicode"));
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);
102 template<typename... _Args>
104 print(FILE* __stream, format_string<_Args...> __fmt, _Args&&... __args)
106 auto __fmtargs = std::make_format_args(__args...);
107 if constexpr (__unicode::__literal_encoding_is_utf8())
108 std::vprint_unicode(__stream, __fmt.get(), __fmtargs);
110 std::vprint_nonunicode(__stream, __fmt.get(), __fmtargs);
113 template<typename... _Args>
115 print(format_string<_Args...> __fmt, _Args&&... __args)
116 { std::print(stdout, __fmt, std::forward<_Args>(__args)...); }
118 template<typename... _Args>
120 println(FILE* __stream, format_string<_Args...> __fmt, _Args&&... __args)
122 std::print(__stream, "{}\n",
123 std::format(__fmt, std::forward<_Args>(__args)...));
126 template<typename... _Args>
128 println(format_string<_Args...> __fmt, _Args&&... __args)
129 { std::println(stdout, __fmt, std::forward<_Args>(__args)...); }
132 vprint_unicode(string_view __fmt, format_args __args)
133 { std::vprint_unicode(stdout, __fmt, __args); }
136 vprint_nonunicode(string_view __fmt, format_args __args)
137 { std::vprint_nonunicode(stdout, __fmt, __args); }
139 // Defined for C++26, supported as an extension to C++23.
140 inline void println(FILE* __stream)
142 #if defined(_WIN32) && !defined(__CYGWIN__)
143 if constexpr (__unicode::__literal_encoding_is_utf8())
144 std::vprint_unicode(__stream, "\n", std::make_format_args());
147 if (std::putc('\n', __stream) == EOF)
148 __throw_system_error(EIO);
151 inline void println() { std::println(stdout); }
153 _GLIBCXX_END_NAMESPACE_VERSION
155 #endif // __cpp_lib_print
156 #endif // _GLIBCXX_PRINT