From 24c309fbfafc4339a084d450c36e0b9458ea25f4 Mon Sep 17 00:00:00 2001 From: vitaut Date: Fri, 12 Jun 2015 07:15:57 -0700 Subject: [PATCH] Don't use windows.h if FMT_USE_WINDOWS_H is set to 0 --- format.cc | 61 ++++++++++++++++++++++----------------------- format.h | 20 +++++++++------ test/CMakeLists.txt | 5 ++++ 3 files changed, 48 insertions(+), 38 deletions(-) diff --git a/format.cc b/format.cc index d46ea4cc16b0..8ab10960960e 100644 --- a/format.cc +++ b/format.cc @@ -35,10 +35,11 @@ #include #include -#ifdef _WIN32 -# ifdef __MINGW32__ -# include -# endif +#if defined(_WIN32) && defined(__MINGW32__) +# include +#endif + +#if FMT_USE_WINDOWS_H # if defined(NOMINMAX) || defined(FMT_WIN_MINMAX) # include # else @@ -488,7 +489,7 @@ FMT_FUNC void fmt::internal::report_unknown_type(char code, const char *type) { static_cast(code), type))); } -#ifdef _WIN32 +#if FMT_USE_WINDOWS_H FMT_FUNC fmt::internal::UTF8ToUTF16::UTF8ToUTF16(fmt::StringRef s) { int length = MultiByteToWideChar( @@ -531,30 +532,6 @@ FMT_FUNC void fmt::WindowsError::init( base = std::runtime_error(w.str()); } -#endif - -FMT_FUNC void fmt::internal::format_system_error( - fmt::Writer &out, int error_code, - fmt::StringRef message) FMT_NOEXCEPT { - FMT_TRY { - MemoryBuffer buffer; - buffer.resize(INLINE_BUFFER_SIZE); - for (;;) { - char *system_message = &buffer[0]; - int result = safe_strerror(error_code, system_message, buffer.size()); - if (result == 0) { - out << message << ": " << system_message; - return; - } - if (result != ERANGE) - break; // Can't get error message, report error code instead. - buffer.resize(buffer.size() * 2); - } - } FMT_CATCH(...) {} - format_error_code(out, error_code, message); -} - -#ifdef _WIN32 FMT_FUNC void fmt::internal::format_windows_error( fmt::Writer &out, int error_code, fmt::StringRef message) FMT_NOEXCEPT { @@ -583,7 +560,29 @@ FMT_FUNC void fmt::internal::format_windows_error( } FMT_CATCH(...) {} format_error_code(out, error_code, message); } -#endif + +#endif // FMT_USE_WINDOWS_H + +FMT_FUNC void fmt::internal::format_system_error( + fmt::Writer &out, int error_code, + fmt::StringRef message) FMT_NOEXCEPT { + FMT_TRY { + MemoryBuffer buffer; + buffer.resize(INLINE_BUFFER_SIZE); + for (;;) { + char *system_message = &buffer[0]; + int result = safe_strerror(error_code, system_message, buffer.size()); + if (result == 0) { + out << message << ": " << system_message; + return; + } + if (result != ERANGE) + break; // Can't get error message, report error code instead. + buffer.resize(buffer.size() * 2); + } + } FMT_CATCH(...) {} + format_error_code(out, error_code, message); +} template void fmt::internal::ArgMap::init(const ArgList &args) { @@ -1251,7 +1250,7 @@ FMT_FUNC void fmt::report_system_error( report_error(internal::format_system_error, error_code, message); } -#ifdef _WIN32 +#if FMT_USE_WINDOWS_H FMT_FUNC void fmt::report_windows_error( int error_code, fmt::StringRef message) FMT_NOEXCEPT { report_error(internal::format_windows_error, error_code, message); diff --git a/format.h b/format.h index 411768ae9624..4df755e4d445 100644 --- a/format.h +++ b/format.h @@ -682,7 +682,15 @@ inline void format_decimal(Char *buffer, UInt value, unsigned num_digits) { buffer[0] = Data::DIGITS[index]; } -#ifdef _WIN32 +#ifndef _WIN32 +# define FMT_USE_WINDOWS_H 0 +#elif !defined(FMT_USE_WINDOWS_H) +# define FMT_USE_WINDOWS_H 1 +#endif + +// Define FMT_USE_WINDOWS_H to 0 to disable use of windows.h. +// All the functionality that relies on it will be disabled too. +#if FMT_USE_WINDOWS_H // A converter from UTF-8 to UTF-16. // It is only provided for Windows since other systems support UTF-8 natively. class UTF8ToUTF16 { @@ -716,16 +724,14 @@ class UTF16ToUTF8 { // in case of memory allocation error. int convert(WStringRef s); }; -#endif -void format_system_error(fmt::Writer &out, int error_code, - fmt::StringRef message) FMT_NOEXCEPT; - -#ifdef _WIN32 void format_windows_error(fmt::Writer &out, int error_code, fmt::StringRef message) FMT_NOEXCEPT; #endif +void format_system_error(fmt::Writer &out, int error_code, + fmt::StringRef message) FMT_NOEXCEPT; + // A formatting argument value. struct Value { template @@ -2486,7 +2492,7 @@ void format(BasicFormatter &f, const Char *&format_str, const T &value) { // Can be used to report errors from destructors. void report_system_error(int error_code, StringRef message) FMT_NOEXCEPT; -#ifdef _WIN32 +#if FMT_USE_WINDOWS_H /** A Windows error. */ class WindowsError : public SystemError { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f7001924c23e..ed827d16189d 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -89,4 +89,9 @@ if (FMT_PEDANTIC) "${CMAKE_CURRENT_BINARY_DIR}/compile-test" --build-generator ${CMAKE_GENERATOR} --build-makeprogram ${CMAKE_MAKE_PROGRAM}) + + # Test that the library compiles without windows.h. + add_library(no-windows-h-test ../format.cc) + set_target_properties(no-windows-h-test + PROPERTIES COMPILE_DEFINITIONS "FMT_USE_WINDOWS_H=0") endif ()