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.
This commit is contained in:
John Moffett 2022-12-08 13:45:51 -05:00
parent c95a4432d7
commit 5408a55fc8
2 changed files with 3 additions and 16 deletions

View File

@ -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<WCHAR*>(&err), 0, nullptr);
std::wstring err_str(err);
LocalFree(err);
return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(err_str);
return Win32ErrorString(GetLastError());
}
FileLock::FileLock(const fs::path& file)

View File

@ -15,11 +15,6 @@
#include <stdexcept>
#include <string>
#ifdef WIN32
#include <codecvt>
#include <locale>
#endif
#ifdef USE_POLL
#include <poll.h>
#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
}