From 8566655b85f0a4e515d57f6686636db516116f95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Mon, 22 Aug 2022 13:13:10 +0200 Subject: Improve Win32Error, add Min. --- c/data.cpp | 7 ++----- c/util.h | 10 +++++++--- c/win.cpp | 10 +++++----- c/win.h | 3 ++- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/c/data.cpp b/c/data.cpp index 58fcde7..c1e7177 100644 --- a/c/data.cpp +++ b/c/data.cpp @@ -1,4 +1,3 @@ -#include #include #include #include @@ -12,9 +11,7 @@ struct InternetFile { InternetFile(const wchar_t* url) { - hi = InternetOpen(L"Episode Browser", - INTERNET_OPEN_TYPE_DIRECT, nullptr, nullptr, - /*INTERNET_FLAG_ASYNC*/0); + hi = InternetOpen(L"Episode Browser", INTERNET_OPEN_TYPE_DIRECT, nullptr, nullptr, 0); if (!hi) throw Win32Error(); @@ -73,7 +70,7 @@ bool WcharsFromXmlchars(wchar_t (&dst)[N], XmlCharPtr utf8) noexcept { /* Truncate if source is larger than destination. */ int lenUtf8 = xmlStrlen(utf8); - utf8[std::min(N, static_cast(lenUtf8))] = 0; + utf8[Min(N, static_cast(lenUtf8))] = 0; /* Convert internal representation from UTF-8 to Latin-1, * which seems to actually convert the string to proper UTF-8 diff --git a/c/util.h b/c/util.h index 698ce10..8281fc9 100644 --- a/c/util.h +++ b/c/util.h @@ -1,13 +1,17 @@ #ifndef UTIL_H #define UTIL_H -#include #include #include #include #include #include +inline size_t Min(size_t a, size_t b) +{ + return a < b? a: b; +} + /* Buf is a span-like structure of a buffer and its size. */ template struct Buf @@ -39,7 +43,7 @@ inline int Sprintf(Buf buf, const char* const fmt, T... xs) /* Copy to static wide string buffer. */ inline wchar_t* Wcscpy(Buf dst, const wchar_t* const src) { - const size_t len = std::min(dst.size, wcslen(src)+1); + const size_t len = Min(dst.size, wcslen(src)+1); memcpy(dst, src, len*sizeof(wchar_t)); dst[len-1] = 0; return dst; @@ -48,7 +52,7 @@ inline wchar_t* Wcscpy(Buf dst, const wchar_t* const src) /* Copy to static narrow string buffer. */ inline char* Strcpy(Buf dst, const char* const src) { - const size_t len = std::min(dst.size, strlen(src)+1); + const size_t len = Min(dst.size, strlen(src)+1); memcpy(dst, src, len); dst[len-1] = 0; return dst; diff --git a/c/win.cpp b/c/win.cpp index 548c951..a09b19b 100644 --- a/c/win.cpp +++ b/c/win.cpp @@ -124,8 +124,8 @@ int GetRelativeCursorPos(const HWND hWnd, POINT* const pt) noexcept return 1; } -Win32Error::Win32Error(const DWORD code) noexcept - : code(code) {} +Win32Error::Win32Error(const DWORD code, const HMODULE hModule) noexcept + : code(code), hModule(hModule) {} Win32Error::~Win32Error() { @@ -143,7 +143,7 @@ const char* Win32Error::what() const noexcept |FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_FROM_HMODULE |FORMAT_MESSAGE_IGNORE_INSERTS, - GetModuleHandle(L"wininet.dll"), + hModule, code, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (char*)&m_szMsg, @@ -159,7 +159,7 @@ const wchar_t* Win32Error::What() const noexcept |FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_FROM_HMODULE |FORMAT_MESSAGE_IGNORE_INSERTS, - GetModuleHandle(L"wininet.dll"), + hModule, code, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (wchar_t*)&m_wszMsg, @@ -170,7 +170,7 @@ const wchar_t* Win32Error::What() const noexcept InternetError::InternetError(DWORD codeSystem) { if (codeSystem != ERROR_INTERNET_EXTENDED_ERROR) - throw Win32Error(codeSystem); + throw Win32Error(codeSystem, GetModuleHandle(L"wininet.dll")); DWORD len, cch; InternetGetLastResponseInfo(&code, nullptr, &len); diff --git a/c/win.h b/c/win.h index 06de198..26a21b2 100644 --- a/c/win.h +++ b/c/win.h @@ -25,12 +25,13 @@ struct WideException : public std::exception /* Exception for Windows API errors. */ struct Win32Error : public WideException { - Win32Error(DWORD code = GetLastError()) noexcept; + Win32Error(DWORD code = GetLastError(), HMODULE hModule = nullptr) noexcept; ~Win32Error() noexcept; const char* what() const noexcept override; const wchar_t* What() const noexcept override; DWORD code; private: + HMODULE hModule; char* m_szMsg = nullptr; wchar_t* m_wszMsg = nullptr; }; -- cgit v1.2.3