| 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 |
#ifdef _GLIBCXX_SYSHDR |
| 33 |
#pragma GCC system_header |
| 34 |
#endif |
| 35 |
|
| 36 |
#include <bits/requires_hosted.h> // for std::format |
| 37 |
|
| 38 |
#define __glibcxx_want_print |
| 39 |
#include <bits/version.h> |
| 40 |
|
| 41 |
#ifdef __cpp_lib_print // C++ >= 23 |
| 42 |
|
| 43 |
#include <format> |
| 44 |
#include <cstdio> |
| 45 |
#include <cerrno> |
| 46 |
#include <bits/functexcept.h> |
| 47 |
|
| 48 |
#ifdef _WIN32 |
| 49 |
# include <system_error> |
| 50 |
#endif |
| 51 |
|
| 52 |
namespace std _GLIBCXX_VISIBILITY(default) |
| 53 |
{ |
| 54 |
_GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 55 |
|
| 56 |
inline void |
| 57 |
vprint_nonunicode(FILE* __stream, string_view __fmt, format_args __args) |
| 58 |
{ |
| 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); |
| 64 |
} |
| 65 |
|
| 66 |
inline void |
| 67 |
vprint_unicode(FILE* __stream, string_view __fmt, format_args __args) |
| 68 |
{ |
| 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); |
| 73 |
#else |
| 74 |
__format::_Str_sink<char> __buf; |
| 75 |
std::vformat_to(__buf.out(), __fmt, __args); |
| 76 |
auto __out = __buf.view(); |
| 77 |
|
| 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)) |
| 82 |
{ |
| 83 |
string __out = std::vformat(__fmt, __args); |
| 84 |
error_code __e; |
| 85 |
if (!std::fflush(__stream)) |
| 86 |
{ |
| 87 |
__e = __write_to_terminal(__term, __out); |
| 88 |
if (!__e) |
| 89 |
return; |
| 90 |
if (__e == std::make_error_code(errc::illegal_byte_sequence)) |
| 91 |
return; |
| 92 |
} |
| 93 |
else |
| 94 |
__e = error_code(errno, generic_category()); |
| 95 |
_GLIBCXX_THROW_OR_ABORT(system_error(__e, "std::vprint_unicode")); |
| 96 |
} |
| 97 |
|
| 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); |
| 101 |
#endif |
| 102 |
} |
| 103 |
|
| 104 |
template<typename... _Args> |
| 105 |
inline void |
| 106 |
print(FILE* __stream, format_string<_Args...> __fmt, _Args&&... __args) |
| 107 |
{ |
| 108 |
auto __fmtargs = std::make_format_args(__args...); |
| 109 |
if constexpr (__unicode::__literal_encoding_is_utf8()) |
| 110 |
std::vprint_unicode(__stream, __fmt.get(), __fmtargs); |
| 111 |
else |
| 112 |
std::vprint_nonunicode(__stream, __fmt.get(), __fmtargs); |
| 113 |
} |
| 114 |
|
| 115 |
template<typename... _Args> |
| 116 |
inline void |
| 117 |
print(format_string<_Args...> __fmt, _Args&&... __args) |
| 118 |
{ std::print(stdout, __fmt, std::forward<_Args>(__args)...); } |
| 119 |
|
| 120 |
template<typename... _Args> |
| 121 |
inline void |
| 122 |
println(FILE* __stream, format_string<_Args...> __fmt, _Args&&... __args) |
| 123 |
{ |
| 124 |
std::print(__stream, "{}\n", |
| 125 |
std::format(__fmt, std::forward<_Args>(__args)...)); |
| 126 |
} |
| 127 |
|
| 128 |
template<typename... _Args> |
| 129 |
inline void |
| 130 |
println(format_string<_Args...> __fmt, _Args&&... __args) |
| 131 |
{ std::println(stdout, __fmt, std::forward<_Args>(__args)...); } |
| 132 |
|
| 133 |
inline void |
| 134 |
vprint_unicode(string_view __fmt, format_args __args) |
| 135 |
{ std::vprint_unicode(stdout, __fmt, __args); } |
| 136 |
|
| 137 |
inline void |
| 138 |
vprint_nonunicode(string_view __fmt, format_args __args) |
| 139 |
{ std::vprint_nonunicode(stdout, __fmt, __args); } |
| 140 |
|
| 141 |
// Defined for C++26, supported as an extension to C++23. |
| 142 |
inline void println(FILE* __stream) |
| 143 |
{ |
| 144 |
#if defined(_WIN32) && !defined(__CYGWIN__) |
| 145 |
if constexpr (__unicode::__literal_encoding_is_utf8()) |
| 146 |
std::vprint_unicode(__stream, "\n", std::make_format_args()); |
| 147 |
else |
| 148 |
#endif |
| 149 |
if (std::putc('\n', __stream) == EOF) |
| 150 |
__throw_system_error(EIO); |
| 151 |
} |
| 152 |
|
| 153 |
inline void println() { std::println(stdout); } |
| 154 |
|
| 155 |
_GLIBCXX_END_NAMESPACE_VERSION |
| 156 |
} // namespace std |
| 157 |
#endif // __cpp_lib_print |
| 158 |
#endif // _GLIBCXX_PRINT |