brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.3 KiB · 82cf2af Raw
73 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include <__config>10 11#include <cstdlib>12#include <print>13 14#include <__system_error/system_error.h>15 16#include "filesystem/error.h"17 18#if defined(_LIBCPP_WIN32API)19#  define WIN32_LEAN_AND_MEAN20#  define NOMINMAX21#  include <io.h>22#  include <windows.h>23#elif __has_include(<unistd.h>)24#  include <unistd.h>25#  if defined(_NEWLIB_VERSION)26#    if defined(_POSIX_C_SOURCE) && __has_include(<stdio.h>)27#      include <stdio.h>28#      define HAS_FILENO_AND_ISATTY29#    endif30#  else31#    define HAS_FILENO_AND_ISATTY32#  endif33#endif34 35_LIBCPP_BEGIN_NAMESPACE_STD36 37#if defined(_LIBCPP_WIN32API)38 39_LIBCPP_EXPORTED_FROM_ABI bool __is_windows_terminal(FILE* __stream) {40  // Note the Standard does this in one call, but it's unclear whether41  // an invalid handle is allowed when calling GetConsoleMode.42  //43  // https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle?view=msvc-17044  // https://learn.microsoft.com/en-us/windows/console/getconsolemode45  intptr_t __handle = _get_osfhandle(fileno(__stream));46  if (__handle == -1)47    return false;48 49  unsigned long __mode;50  return GetConsoleMode(reinterpret_cast<void*>(__handle), &__mode);51}52 53#  if _LIBCPP_HAS_WIDE_CHARACTERS54_LIBCPP_EXPORTED_FROM_ABI void55__write_to_windows_console([[maybe_unused]] FILE* __stream, [[maybe_unused]] wstring_view __view) {56  // https://learn.microsoft.com/en-us/windows/console/writeconsole57  if (WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fileno(__stream))), // clang-format aid58                    __view.data(),59                    __view.size(),60                    nullptr,61                    nullptr) == 0) {62    std::__throw_system_error(filesystem::detail::get_last_error(), "failed to write formatted output");63  }64}65#  endif // _LIBCPP_HAS_WIDE_CHARACTERS66 67#elif defined(HAS_FILENO_AND_ISATTY) // !_LIBCPP_WIN32API68 69_LIBCPP_EXPORTED_FROM_ABI bool __is_posix_terminal(FILE* __stream) { return isatty(fileno(__stream)); }70#endif71 72_LIBCPP_END_NAMESPACE_STD73