From 5408a55fc87350baeae3a32f1003f956d5533a79 Mon Sep 17 00:00:00 2001 From: John Moffett Date: Thu, 8 Dec 2022 13:45:51 -0500 Subject: [PATCH] Consolidate Win32-specific error formatting GetErrorReason()'s Win32 implementation does the same thing as Win32ErrorString(int err) from syserror.cpp, so call the latter. Also remove now-unnecessary headers from sock.cpp and less verbose handling of #ifdefs. --- src/util/fs.cpp | 7 +------ src/util/sock.cpp | 12 ++---------- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/src/util/fs.cpp b/src/util/fs.cpp index e8fb72670f6..14f7a446611 100644 --- a/src/util/fs.cpp +++ b/src/util/fs.cpp @@ -81,12 +81,7 @@ bool FileLock::TryLock() #else static std::string GetErrorReason() { - wchar_t* err; - FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - nullptr, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast(&err), 0, nullptr); - std::wstring err_str(err); - LocalFree(err); - return std::wstring_convert>().to_bytes(err_str); + return Win32ErrorString(GetLastError()); } FileLock::FileLock(const fs::path& file) diff --git a/src/util/sock.cpp b/src/util/sock.cpp index d33ccd135eb..f244f38f3f3 100644 --- a/src/util/sock.cpp +++ b/src/util/sock.cpp @@ -15,11 +15,6 @@ #include #include -#ifdef WIN32 -#include -#include -#endif - #ifdef USE_POLL #include #endif @@ -416,15 +411,12 @@ void Sock::Close() m_socket = INVALID_SOCKET; } -#ifdef WIN32 std::string NetworkErrorString(int err) { +#if defined(WIN32) return Win32ErrorString(err); -} #else -std::string NetworkErrorString(int err) -{ // On BSD sockets implementations, NetworkErrorString is the same as SysErrorString. return SysErrorString(err); -} #endif +}