From 9ec020592a2df91daab38405860eadaf5c332ada Mon Sep 17 00:00:00 2001 From: Bernd Baumanns Date: Thu, 24 Sep 2020 17:27:52 +0200 Subject: [PATCH 1/4] remove dependency to windows.h --- include/fmt/format-inl.h | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index ee5ac889833d..b1882dc5b2fd 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -23,16 +23,15 @@ #endif #ifdef _WIN32 -# if defined(FMT_BIG_WIN) || defined(NOMINMAX) -# include -# else -# define NOMINMAX -# define WIN32_LEAN_AND_MEAN -# include -# undef WIN32_LEAN_AND_MEAN -# undef NOMINMAX -# endif # include +FMT_BEGIN_NAMESPACE + using DWORD = uint32_t; + using HANDLE = void*; + + extern "C" { + int __stdcall WriteConsoleW(void* hConsoleOutput, const void* lpBuffer, uint32_t nNumberOfCharsToWrite, uint32_t* lpNumberOfCharsWritten, void* lpReserved); + } +FMT_END_NAMESPACE #endif // Dummy implementations of strerror_r and strerror_s called if corresponding From f350cc3a383c26370394bc2d59450ec3175a78d9 Mon Sep 17 00:00:00 2001 From: Bernd Baumanns Date: Fri, 25 Sep 2020 17:05:23 +0200 Subject: [PATCH 2/4] remove usings --- include/fmt/format-inl.h | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index b1882dc5b2fd..8f5fb3dc125d 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -23,17 +23,15 @@ #endif #ifdef _WIN32 -# include +#include FMT_BEGIN_NAMESPACE - using DWORD = uint32_t; - using HANDLE = void*; - - extern "C" { - int __stdcall WriteConsoleW(void* hConsoleOutput, const void* lpBuffer, uint32_t nNumberOfCharsToWrite, uint32_t* lpNumberOfCharsWritten, void* lpReserved); - } + extern "C" { + int __stdcall WriteConsoleW(void* hConsoleOutput, const void* lpBuffer, uint32_t nNumberOfCharsToWrite, uint32_t* lpNumberOfCharsWritten, void* lpReserved); + } FMT_END_NAMESPACE #endif + // Dummy implementations of strerror_r and strerror_s called if corresponding // system functions are not available. inline fmt::detail::null<> strerror_r(int, char*, ...) { return {}; } @@ -2896,9 +2894,9 @@ FMT_FUNC void vprint(std::FILE* f, string_view format_str, format_args args) { auto fd = _fileno(f); if (_isatty(fd)) { detail::utf8_to_utf16 u16(string_view(buffer.data(), buffer.size())); - auto written = DWORD(); - if (!WriteConsoleW(reinterpret_cast(_get_osfhandle(fd)), - u16.c_str(), static_cast(u16.size()), &written, + auto written = uint32_t(); + if (!WriteConsoleW(reinterpret_cast(_get_osfhandle(fd)), + u16.c_str(), static_cast(u16.size()), &written, nullptr)) { FMT_THROW(format_error("failed to write to console")); } From fb58a7e8a2d449e517597702a109d8d7dc27ebeb Mon Sep 17 00:00:00 2001 From: Bernd Baumanns Date: Fri, 25 Sep 2020 17:14:21 +0200 Subject: [PATCH 3/4] clang-format --- include/fmt/format-inl.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 8f5fb3dc125d..c84d2c316c6c 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -23,15 +23,16 @@ #endif #ifdef _WIN32 -#include +# include FMT_BEGIN_NAMESPACE - extern "C" { - int __stdcall WriteConsoleW(void* hConsoleOutput, const void* lpBuffer, uint32_t nNumberOfCharsToWrite, uint32_t* lpNumberOfCharsWritten, void* lpReserved); - } +extern "C" { +int __stdcall WriteConsoleW(void* hConsoleOutput, const void* lpBuffer, + uint32_t nNumberOfCharsToWrite, + uint32_t* lpNumberOfCharsWritten, void* lpReserved); +} FMT_END_NAMESPACE #endif - // Dummy implementations of strerror_r and strerror_s called if corresponding // system functions are not available. inline fmt::detail::null<> strerror_r(int, char*, ...) { return {}; } @@ -2895,9 +2896,8 @@ FMT_FUNC void vprint(std::FILE* f, string_view format_str, format_args args) { if (_isatty(fd)) { detail::utf8_to_utf16 u16(string_view(buffer.data(), buffer.size())); auto written = uint32_t(); - if (!WriteConsoleW(reinterpret_cast(_get_osfhandle(fd)), - u16.c_str(), static_cast(u16.size()), &written, - nullptr)) { + if (!WriteConsoleW(reinterpret_cast(_get_osfhandle(fd)), u16.c_str(), + static_cast(u16.size()), &written, nullptr)) { FMT_THROW(format_error("failed to write to console")); } return; From 481660af60e9d58110c8b06bdee9267cb75e11be Mon Sep 17 00:00:00 2001 From: Bernd Baumanns Date: Fri, 25 Sep 2020 21:00:42 +0200 Subject: [PATCH 4/4] DWORD is always a 32 bit unsigned integer. But "unsigned long" is sometimes a 32 or 64 bits... --- include/fmt/format-inl.h | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index c84d2c316c6c..e568c9178d6c 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -25,11 +25,20 @@ #ifdef _WIN32 # include FMT_BEGIN_NAMESPACE +namespace details { + +# if !defined(__LP64__) +using DWORD = unsigned long; +# else +using DWORD = unsigned int; +# endif +// WriteConsoleW should not become visible in global namespace extern "C" { int __stdcall WriteConsoleW(void* hConsoleOutput, const void* lpBuffer, - uint32_t nNumberOfCharsToWrite, - uint32_t* lpNumberOfCharsWritten, void* lpReserved); + DWORD nNumberOfCharsToWrite, + DWORD* lpNumberOfCharsWritten, void* lpReserved); } +} // namespace details FMT_END_NAMESPACE #endif @@ -2895,9 +2904,10 @@ FMT_FUNC void vprint(std::FILE* f, string_view format_str, format_args args) { auto fd = _fileno(f); if (_isatty(fd)) { detail::utf8_to_utf16 u16(string_view(buffer.data(), buffer.size())); - auto written = uint32_t(); - if (!WriteConsoleW(reinterpret_cast(_get_osfhandle(fd)), u16.c_str(), - static_cast(u16.size()), &written, nullptr)) { + auto written = details::DWORD(); + if (!details::WriteConsoleW( + reinterpret_cast(_get_osfhandle(fd)), u16.c_str(), + static_cast(u16.size()), &written, nullptr)) { FMT_THROW(format_error("failed to write to console")); } return;