Skip to content

Commit 5408a55

Browse files
committed
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.
1 parent c95a443 commit 5408a55

File tree

2 files changed

+3
-16
lines changed

2 files changed

+3
-16
lines changed

src/util/fs.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,7 @@ bool FileLock::TryLock()
8181
#else
8282

8383
static std::string GetErrorReason() {
84-
wchar_t* err;
85-
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
86-
nullptr, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<WCHAR*>(&err), 0, nullptr);
87-
std::wstring err_str(err);
88-
LocalFree(err);
89-
return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(err_str);
84+
return Win32ErrorString(GetLastError());
9085
}
9186

9287
FileLock::FileLock(const fs::path& file)

src/util/sock.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@
1515
#include <stdexcept>
1616
#include <string>
1717

18-
#ifdef WIN32
19-
#include <codecvt>
20-
#include <locale>
21-
#endif
22-
2318
#ifdef USE_POLL
2419
#include <poll.h>
2520
#endif
@@ -416,15 +411,12 @@ void Sock::Close()
416411
m_socket = INVALID_SOCKET;
417412
}
418413

419-
#ifdef WIN32
420414
std::string NetworkErrorString(int err)
421415
{
416+
#if defined(WIN32)
422417
return Win32ErrorString(err);
423-
}
424418
#else
425-
std::string NetworkErrorString(int err)
426-
{
427419
// On BSD sockets implementations, NetworkErrorString is the same as SysErrorString.
428420
return SysErrorString(err);
429-
}
430421
#endif
422+
}

0 commit comments

Comments
 (0)