| 1 |
#include "utils/utils.hpp" |
| 2 |
#include "utils/string_view.hpp" |
| 3 |
|
| 4 |
#if IS_WINDOWS |
| 5 |
#include <io.h> |
| 6 |
#ifndef WIN32_LEAN_AND_MEAN |
| 7 |
#define WIN32_LEAN_AND_MEAN |
| 8 |
#endif |
| 9 |
#include <windows.h> |
| 10 |
#else |
| 11 |
#include <sys/stat.h> |
| 12 |
#include <unistd.h> |
| 13 |
#endif |
| 14 |
|
| 15 |
CPPTRACE_BEGIN_NAMESPACE |
| 16 |
namespace detail { |
| 17 |
|
| 18 |
bool isatty(int fd) { |
| 19 |
#if IS_WINDOWS |
| 20 |
return _isatty(fd); |
| 21 |
#else |
| 22 |
return ::isatty(fd); |
| 23 |
#endif |
| 24 |
} |
| 25 |
|
| 26 |
int fileno(std::FILE* stream) { |
| 27 |
#if IS_WINDOWS |
| 28 |
return _fileno(stream); |
| 29 |
#else |
| 30 |
return ::fileno(stream); |
| 31 |
#endif |
| 32 |
} |
| 33 |
|
| 34 |
void enable_virtual_terminal_processing_if_needed() noexcept { |
| 35 |
// enable colors / ansi processing if necessary |
| 36 |
#if IS_WINDOWS |
| 37 |
// https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#example-of-enabling-virtual-terminal-processing |
| 38 |
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING |
| 39 |
constexpr DWORD ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x4; |
| 40 |
#endif |
| 41 |
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); |
| 42 |
DWORD dwMode = 0; |
| 43 |
if(hOut == INVALID_HANDLE_VALUE) return; |
| 44 |
if(!GetConsoleMode(hOut, &dwMode)) return; |
| 45 |
if(dwMode != (dwMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)) |
| 46 |
if(!SetConsoleMode(hOut, dwMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)) return; |
| 47 |
#endif |
| 48 |
} |
| 49 |
|
| 50 |
bool directory_exists(cstring_view path) { |
| 51 |
#if IS_WINDOWS |
| 52 |
DWORD dwAttrib = GetFileAttributesA(path.c_str()); |
| 53 |
return dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY); |
| 54 |
#else |
| 55 |
struct stat sb; |
| 56 |
return stat(path.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode); |
| 57 |
#endif |
| 58 |
} |
| 59 |
|
| 60 |
} |
| 61 |
CPPTRACE_END_NAMESPACE |