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.
30#define _GLIBCXX_PRINT 1
33#pragma GCC system_header
36#include <bits/requires_hosted.h> // for std::format
38#define __glibcxx_want_print
39#include <bits/version.h>
41#ifdef __cpp_lib_print // C++ >= 23
46#include <bits/functexcept.h>
49# include <system_error>
52namespace std _GLIBCXX_VISIBILITY(default)
54_GLIBCXX_BEGIN_NAMESPACE_VERSION
57 vprint_nonunicode(FILE* __stream, string_view __fmt, format_args __args)
59 __format::_Str_sink<char> __buf;
60 std::vformat_to(__buf.out(), __fmt, __args);
61 auto __out = __buf.view();
62 if (std::fwrite(__out.data(), 1, __out.size(), __stream) != __out.size())
63 __throw_system_error(EIO);
67 vprint_unicode(FILE* __stream, string_view __fmt, format_args __args)
69#if !defined(_WIN32) || defined(__CYGWIN__)
70 // For most targets we don't need to do anything special to write
71 // Unicode to a terminal.
72 std::vprint_nonunicode(__stream, __fmt, __args);
74 __format::_Str_sink<char> __buf;
75 std::vformat_to(__buf.out(), __fmt, __args);
76 auto __out = __buf._M_span();
78 void* __open_terminal(FILE*);
79 error_code __write_to_terminal(void*, span<char>);
80 // If stream refers to a terminal, write a native Unicode string to it.
81 if (auto __term = __open_terminal(__stream))
83 string __out = std::vformat(__fmt, __args);
85 if (!std::fflush(__stream))
87 __e = __write_to_terminal(__term, __out);
90 if (__e == std::make_error_code(errc::illegal_byte_sequence))
94 __e = error_code(errno, generic_category());
95 _GLIBCXX_THROW_OR_ABORT(system_error(__e, "std::vprint_unicode"));
98 // Otherwise just write the string to the file as vprint_nonunicode does.
99 if (std::fwrite(__out.data(), 1, __out.size(), __stream) != __out.size())
100 __throw_system_error(EIO);
104 template<typename... _Args>
106 print(FILE* __stream, format_string<_Args...> __fmt, _Args&&... __args)
108 auto __fmtargs = std::make_format_args(__args...);
109 if constexpr (__unicode::__literal_encoding_is_utf8())
110 std::vprint_unicode(__stream, __fmt.get(), __fmtargs);
112 std::vprint_nonunicode(__stream, __fmt.get(), __fmtargs);
115 template<typename... _Args>
117 print(format_string<_Args...> __fmt, _Args&&... __args)
118 { std::print(stdout, __fmt, std::forward<_Args>(__args)...); }
120 template<typename... _Args>
122 println(FILE* __stream, format_string<_Args...> __fmt, _Args&&... __args)
124 std::print(__stream, "{}\n",
125 std::format(__fmt, std::forward<_Args>(__args)...));
128 template<typename... _Args>
130 println(format_string<_Args...> __fmt, _Args&&... __args)
131 { std::println(stdout, __fmt, std::forward<_Args>(__args)...); }
134 vprint_unicode(string_view __fmt, format_args __args)
135 { std::vprint_unicode(stdout, __fmt, __args); }
138 vprint_nonunicode(string_view __fmt, format_args __args)
139 { std::vprint_nonunicode(stdout, __fmt, __args); }
141 // Defined for C++26, supported as an extension to C++23.
142 inline void println(FILE* __stream)
144#if defined(_WIN32) && !defined(__CYGWIN__)
145 if constexpr (__unicode::__literal_encoding_is_utf8())
146 std::vprint_unicode(__stream, "\n", std::make_format_args());
149 if (std::putc('\n', __stream) == EOF)
150 __throw_system_error(EIO);
153 inline void println() { std::println(stdout); }
155_GLIBCXX_END_NAMESPACE_VERSION
157#endif // __cpp_lib_print
158#endif // _GLIBCXX_PRINT