diff options
Diffstat (limited to 'c')
-rw-r--r-- | c/data.cpp | 7 | ||||
-rw-r--r-- | c/util.h | 10 | ||||
-rw-r--r-- | c/win.cpp | 10 | ||||
-rw-r--r-- | c/win.h | 3 |
4 files changed, 16 insertions, 14 deletions
@@ -1,4 +1,3 @@ -#include <algorithm> #include <windows.h> #include <wininet.h> #include <libxml/HTMLparser.h> @@ -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<size_t>(lenUtf8))] = 0; + utf8[Min(N, static_cast<size_t>(lenUtf8))] = 0; /* Convert internal representation from UTF-8 to Latin-1, * which seems to actually convert the string to proper UTF-8 @@ -1,13 +1,17 @@ #ifndef UTIL_H #define UTIL_H -#include <algorithm> #include <cstring> #include <memory> #include <string> #include <SWI-Prolog.h> #include <windows.h> +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 <typename T> struct Buf @@ -39,7 +43,7 @@ inline int Sprintf(Buf<char> buf, const char* const fmt, T... xs) /* Copy to static wide string buffer. */ inline wchar_t* Wcscpy(Buf<wchar_t> 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<wchar_t> dst, const wchar_t* const src) /* Copy to static narrow string buffer. */ inline char* Strcpy(Buf<char> 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; @@ -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); @@ -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; }; |