Don't use windows.h if FMT_USE_WINDOWS_H is set to 0

This commit is contained in:
vitaut 2015-06-12 07:15:57 -07:00
parent 9d09214e7a
commit 24c309fbfa
3 changed files with 48 additions and 38 deletions

View File

@ -35,10 +35,11 @@
#include <cmath> #include <cmath>
#include <cstdarg> #include <cstdarg>
#ifdef _WIN32 #if defined(_WIN32) && defined(__MINGW32__)
# ifdef __MINGW32__ # include <cstring>
# include <cstring> #endif
# endif
#if FMT_USE_WINDOWS_H
# if defined(NOMINMAX) || defined(FMT_WIN_MINMAX) # if defined(NOMINMAX) || defined(FMT_WIN_MINMAX)
# include <windows.h> # include <windows.h>
# else # else
@ -488,7 +489,7 @@ FMT_FUNC void fmt::internal::report_unknown_type(char code, const char *type) {
static_cast<unsigned>(code), type))); static_cast<unsigned>(code), type)));
} }
#ifdef _WIN32 #if FMT_USE_WINDOWS_H
FMT_FUNC fmt::internal::UTF8ToUTF16::UTF8ToUTF16(fmt::StringRef s) { FMT_FUNC fmt::internal::UTF8ToUTF16::UTF8ToUTF16(fmt::StringRef s) {
int length = MultiByteToWideChar( int length = MultiByteToWideChar(
@ -531,30 +532,6 @@ FMT_FUNC void fmt::WindowsError::init(
base = std::runtime_error(w.str()); 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<char, INLINE_BUFFER_SIZE> 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_FUNC void fmt::internal::format_windows_error(
fmt::Writer &out, int error_code, fmt::Writer &out, int error_code,
fmt::StringRef message) FMT_NOEXCEPT { fmt::StringRef message) FMT_NOEXCEPT {
@ -583,7 +560,29 @@ FMT_FUNC void fmt::internal::format_windows_error(
} FMT_CATCH(...) {} } FMT_CATCH(...) {}
format_error_code(out, error_code, message); 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<char, INLINE_BUFFER_SIZE> 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 <typename Char> template <typename Char>
void fmt::internal::ArgMap<Char>::init(const ArgList &args) { void fmt::internal::ArgMap<Char>::init(const ArgList &args) {
@ -1251,7 +1250,7 @@ FMT_FUNC void fmt::report_system_error(
report_error(internal::format_system_error, error_code, message); report_error(internal::format_system_error, error_code, message);
} }
#ifdef _WIN32 #if FMT_USE_WINDOWS_H
FMT_FUNC void fmt::report_windows_error( FMT_FUNC void fmt::report_windows_error(
int error_code, fmt::StringRef message) FMT_NOEXCEPT { int error_code, fmt::StringRef message) FMT_NOEXCEPT {
report_error(internal::format_windows_error, error_code, message); report_error(internal::format_windows_error, error_code, message);

View File

@ -682,7 +682,15 @@ inline void format_decimal(Char *buffer, UInt value, unsigned num_digits) {
buffer[0] = Data::DIGITS[index]; 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. // A converter from UTF-8 to UTF-16.
// It is only provided for Windows since other systems support UTF-8 natively. // It is only provided for Windows since other systems support UTF-8 natively.
class UTF8ToUTF16 { class UTF8ToUTF16 {
@ -716,16 +724,14 @@ class UTF16ToUTF8 {
// in case of memory allocation error. // in case of memory allocation error.
int convert(WStringRef s); int convert(WStringRef s);
}; };
void format_windows_error(fmt::Writer &out, int error_code,
fmt::StringRef message) FMT_NOEXCEPT;
#endif #endif
void format_system_error(fmt::Writer &out, int error_code, void format_system_error(fmt::Writer &out, int error_code,
fmt::StringRef message) FMT_NOEXCEPT; fmt::StringRef message) FMT_NOEXCEPT;
#ifdef _WIN32
void format_windows_error(fmt::Writer &out, int error_code,
fmt::StringRef message) FMT_NOEXCEPT;
#endif
// A formatting argument value. // A formatting argument value.
struct Value { struct Value {
template <typename Char> template <typename Char>
@ -2486,7 +2492,7 @@ void format(BasicFormatter<Char> &f, const Char *&format_str, const T &value) {
// Can be used to report errors from destructors. // Can be used to report errors from destructors.
void report_system_error(int error_code, StringRef message) FMT_NOEXCEPT; void report_system_error(int error_code, StringRef message) FMT_NOEXCEPT;
#ifdef _WIN32 #if FMT_USE_WINDOWS_H
/** A Windows error. */ /** A Windows error. */
class WindowsError : public SystemError { class WindowsError : public SystemError {

View File

@ -89,4 +89,9 @@ if (FMT_PEDANTIC)
"${CMAKE_CURRENT_BINARY_DIR}/compile-test" "${CMAKE_CURRENT_BINARY_DIR}/compile-test"
--build-generator ${CMAKE_GENERATOR} --build-generator ${CMAKE_GENERATOR}
--build-makeprogram ${CMAKE_MAKE_PROGRAM}) --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 () endif ()