30#define _GLIBCXX_PRINT 1
33#pragma GCC system_header
38#define __glibcxx_want_print
52namespace std _GLIBCXX_VISIBILITY(default)
54_GLIBCXX_BEGIN_NAMESPACE_VERSION
58#if _GLIBCXX_USE_STDIO_LOCKING && _GLIBCXX_USE_GLIBC_STDIO_EXT
60 extern "C" int __fwritable(FILE*)
noexcept;
61 extern "C" int __flbf(FILE*)
noexcept;
62 extern "C" size_t __fbufsize(FILE*)
noexcept;
66 class _File_sink final : _Buf_sink<char>
71 _File(FILE* __f) : _M_file(__f)
75 if (!__fwritable(__f))
78 __throw_system_error(EACCES);
81 if (_M_write_buf().
empty())
82 if (::__overflow(__f, EOF) == EOF)
84 const int __err = errno;
86 __throw_system_error(__err);
90 ~_File() { ::funlockfile(_M_file); }
92 _File(_File&&) =
delete;
96 _M_write_buf() noexcept
98 return {_M_file->_IO_write_ptr,
99 size_t(_M_file->_IO_buf_end - _M_file->_IO_write_ptr)};
106 if (::fflush_unlocked(_M_file))
107 __throw_system_error(errno);
112 _M_bump(
size_t __n)
noexcept
113 { _M_file->_IO_write_ptr += __n; }
116 _M_line_buffered() const noexcept
117 {
return __flbf(_M_file); }
120 _M_unbuffered() const noexcept
121 {
return __fbufsize(_M_file) == 1; }
130 _M_overflow()
override
132 auto __s = this->_M_used();
133 if (__s.data() == this->_M_buf)
136 auto __n = ::fwrite_unlocked(__s.data(), 1, __s.size(),
138 if (__n != __s.size())
139 __throw_system_error(errno);
140 this->_M_reset(this->_M_buf);
145 _M_file._M_bump(__s.size());
147 this->_M_reset(_M_file._M_write_buf());
152 _File_sink(FILE* __f,
bool __add_newline)
153 : _M_file(__f), _M_add_newline(__add_newline)
155 if (!_M_file._M_unbuffered())
157 this->_M_reset(_M_file._M_write_buf());
160 ~_File_sink() noexcept(false)
162 auto __s = this->_M_used();
163 if (__s.data() == this->_M_buf)
165 _File_sink::_M_overflow();
167 ::putc_unlocked(
'\n', _M_file._M_file);
171 _M_file._M_bump(__s.size());
173 ::putc_unlocked(
'\n', _M_file._M_file);
174 else if (_M_file._M_line_buffered() && __s.size()
175 && (__s.back() ==
'\n'
176 || __builtin_memchr(__s.data(),
'\n', __s.size())))
181 using _Sink<
char>::out;
183#elif _GLIBCXX_USE_STDIO_LOCKING
186 class _File_sink final : _Buf_sink<char>
193 _M_overflow()
override
195 auto __s = this->_M_used();
196#if _GLIBCXX_HAVE_FWRITE_UNLOCKED
197 auto __n = ::fwrite_unlocked(__s.data(), 1, __s.size(), _M_file);
198 if (__n != __s.size())
199 __throw_system_error(errno);
202 ::putc_unlocked(__c, _M_file);
203 if (::ferror(_M_file))
204 __throw_system_error(errno);
206 this->_M_reset(this->_M_buf);
210 _File_sink(FILE* __f,
bool __add_newline) noexcept
211 : _Buf_sink<char>(), _M_file(__f), _M_add_newline(__add_newline)
212 { ::flockfile(__f); }
214 ~_File_sink() noexcept(false)
216 _File_sink::_M_overflow();
218 ::putc_unlocked(
'\n', _M_file);
219 ::funlockfile(_M_file);
222 using _Sink<
char>::out;
228 class _File_sink final
231 _Str_sink<char> _M_sink;
235 _File_sink(FILE* __f,
bool __add_newline) noexcept
236 : _M_file(__f), _M_add_newline(__add_newline)
239 ~_File_sink() noexcept(false)
244 auto __n = std::fwrite(__s.data(), 1, __s.size(), _M_file);
245 if (__n < __s.size())
246 __throw_system_error(EIO);
249 auto out() {
return _M_sink.out(); }
255 vprint_nonunicode(FILE* __stream, string_view __fmt, format_args __args)
257 std::vformat_to(__format::_File_sink(__stream,
false).out(), __fmt, __args);
261 vprint_nonunicode_buffered(FILE* __stream, string_view __fmt,
264 __format::_Str_sink<char> __buf;
265 std::vformat_to(__buf.out(), __fmt, __args);
266 auto __out = __buf.view();
267 if (std::fwrite(__out.data(), 1, __out.size(), __stream) != __out.size())
268 __throw_system_error(EIO);
272 vprint_unicode(FILE* __stream, string_view __fmt, format_args __args)
274#if !defined(_WIN32) || defined(__CYGWIN__)
277 std::vprint_nonunicode(__stream, __fmt, __args);
279 __format::_Str_sink<char> __buf;
280 std::vformat_to(__buf.out(), __fmt, __args);
281 auto __out = __buf._M_span();
283 void* __open_terminal(FILE*);
284 error_code __write_to_terminal(
void*, span<char>);
286 if (
auto __term = __open_terminal(__stream))
289 if (!std::fflush(__stream))
291 __e = __write_to_terminal(__term, __out);
299 _GLIBCXX_THROW_OR_ABORT(
system_error(__e,
"std::vprint_unicode"));
303 if (std::fwrite(__out.data(), 1, __out.size(), __stream) != __out.size())
304 __throw_system_error(EIO);
309 vprint_unicode_buffered(FILE* __stream, string_view __fmt, format_args __args)
311#if !defined(_WIN32) || defined(__CYGWIN__)
314 std::vprint_nonunicode_buffered(__stream, __fmt, __args);
318 std::vprint_unicode(__stream, __fmt, __args);
322 template<
typename... _Args>
324 print(FILE* __stream, format_string<_Args...> __fmt, _Args&&... __args)
326 constexpr bool __locksafe =
327 (enable_nonlocking_formatter_optimization<remove_cvref_t<_Args>> && ...);
329 auto __fmtargs = std::make_format_args(__args...);
330#if defined(_WIN32) && !defined(__CYGWIN__)
331 if constexpr (__unicode::__literal_encoding_is_utf8())
332 std::vprint_unicode_buffered(__stream, __fmt.get(), __fmtargs);
336 if constexpr (__locksafe)
337 std::vprint_nonunicode(__stream, __fmt.get(), __fmtargs);
339 std::vprint_nonunicode_buffered(__stream, __fmt.get(), __fmtargs);
342 template<
typename... _Args>
344 print(format_string<_Args...> __fmt, _Args&&... __args)
347 template<
typename... _Args>
349 println(FILE* __stream, format_string<_Args...> __fmt, _Args&&... __args)
351 constexpr bool __locksafe =
352 (enable_nonlocking_formatter_optimization<remove_cvref_t<_Args>> && ...);
359 auto __fmtargs = std::make_format_args(__args...);
360#if defined(_WIN32) && !defined(__CYGWIN__)
361 if constexpr (__unicode::__literal_encoding_is_utf8())
366 __fmtn.reserve(__fmt.get().size() + 1);
367 __fmtn = __fmt.get();
369 std::vprint_unicode_buffered(__stream, __fmtn, __fmtargs);
378 if constexpr (__locksafe)
379 std::vformat_to(__format::_File_sink(__stream,
true).out(),
380 __fmt.get(), __fmtargs);
385 __format::_Str_sink<char> __buf;
386 std::vformat_to(__buf.out(), __fmt.get(), __fmtargs);
387 string_view __s(__buf.view());
388 __format::_File_sink(__stream,
true).out() = __s;
392 template<
typename... _Args>
394 println(format_string<_Args...> __fmt, _Args&&... __args)
398 vprint_unicode_buffered(string_view __fmt, format_args __args)
399 { std::vprint_unicode_buffered(stdout, __fmt, __args); }
402 vprint_nonunicode_buffered(string_view __fmt, format_args __args)
403 { std::vprint_nonunicode_buffered(stdout, __fmt, __args); }
406 inline void println(FILE* __stream)
408#if defined(_WIN32) && !defined(__CYGWIN__)
409 if constexpr (__unicode::__literal_encoding_is_utf8())
410 std::vprint_unicode_buffered(__stream,
"\n", std::make_format_args());
413 if (std::putc(
'\n', __stream) == EOF)
414 __throw_system_error(EIO);
417 inline void println() { std::println(stdout); }
419_GLIBCXX_END_NAMESPACE_VERSION
const error_category & generic_category() noexcept
Error category for errno error codes.
error_code make_error_code(future_errc __errc) noexcept
Overload of make_error_code for future_errc.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
ISO C++ entities toplevel namespace is std.
constexpr auto empty(const _Container &__cont) noexcept(noexcept(__cont.empty())) -> decltype(__cont.empty())
Return whether a container is empty.
An exception type that includes an error_code value.