Path: blob/main/contrib/llvm-project/libcxx/src/print.cpp
35147 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include <__config>910#include <cstdlib>11#include <print>1213#include <__system_error/system_error.h>1415#include "filesystem/error.h"1617#if defined(_LIBCPP_WIN32API)18# define WIN32_LEAN_AND_MEAN19# define NOMINMAX20# include <io.h>21# include <windows.h>22#elif __has_include(<unistd.h>)23# include <unistd.h>24#endif2526_LIBCPP_BEGIN_NAMESPACE_STD2728#if defined(_LIBCPP_WIN32API)2930_LIBCPP_EXPORTED_FROM_ABI bool __is_windows_terminal(FILE* __stream) {31// Note the Standard does this in one call, but it's unclear whether32// an invalid handle is allowed when calling GetConsoleMode.33//34// https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle?view=msvc-17035// https://learn.microsoft.com/en-us/windows/console/getconsolemode36intptr_t __handle = _get_osfhandle(fileno(__stream));37if (__handle == -1)38return false;3940unsigned long __mode;41return GetConsoleMode(reinterpret_cast<void*>(__handle), &__mode);42}4344# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS45_LIBCPP_EXPORTED_FROM_ABI void46__write_to_windows_console([[maybe_unused]] FILE* __stream, [[maybe_unused]] wstring_view __view) {47// https://learn.microsoft.com/en-us/windows/console/writeconsole48if (WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fileno(__stream))), // clang-format aid49__view.data(),50__view.size(),51nullptr,52nullptr) == 0) {53__throw_system_error(filesystem::detail::make_windows_error(GetLastError()), "failed to write formatted output");54}55}56# endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS5758#elif __has_include(<unistd.h>) // !_LIBCPP_WIN32API5960_LIBCPP_EXPORTED_FROM_ABI bool __is_posix_terminal(FILE* __stream) { return isatty(fileno(__stream)); }61#endif6263_LIBCPP_END_NAMESPACE_STD646566