diff options
author | John Ankarström <john@ankarstrom.se> | 2022-09-07 01:22:30 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2022-09-07 01:22:30 +0200 |
commit | ac7d676a1c457f71f1d3ae17d74699dd95c9c985 (patch) | |
tree | 96d44cc6740de053db377ac51f3102b9871a90d4 /c/err.cpp | |
parent | b2a018ff822bcdb5b9b691025a5fc1eb6fc13b4b (diff) | |
download | EpisodeBrowser-ac7d676a1c457f71f1d3ae17d74699dd95c9c985.tar.gz |
Normalize error message formatting.
Diffstat (limited to 'c/err.cpp')
-rw-r--r-- | c/err.cpp | 20 |
1 files changed, 15 insertions, 5 deletions
@@ -7,10 +7,19 @@ #include "util.h" #include "win32.h" +/* Strip trailing punctuation. */ +static void Strip(wchar_t* s, size_t len = -1) +{ + for (int i = len-1; i >= 0; i--) + if (s[i] == '\r' || s[i] == '\n' || s[i] == '.') + s[i] = 0; +} + Err::Err(ErrType t, const wchar_t* fmt) { switch (t) { case GENERIC: + assert(wcscmp(fmt, L"%s.") != 0); what = fmt; break; case WINDOWS: @@ -25,6 +34,7 @@ Err::Err(ErrType t, const wchar_t* fmt) MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (wchar_t*)&msg, 0, nullptr); + Strip(msg, lenMsg); what = std::wstring(lenMsg+wcslen(fmt), 0); Swprintf(what, fmt, msg); HeapFree(GetProcessHeap(), 0, msg); @@ -40,11 +50,10 @@ Err::Err(ErrType t, const wchar_t* fmt) if (InternetGetLastResponseInfoW(&code, msg.data(), &lenMsg)) { what = std::wstring(lenMsg+wcslen(fmt), 0); Swprintf(what, fmt, msg.c_str()); - EBMessageBox(msg, L"Test"); } else what = Err(WINDOWS, fmt).what; } else { - wchar_t* wszMsg; + wchar_t* msg; DWORD lenMsg = FormatMessageW( FORMAT_MESSAGE_ALLOCATE_BUFFER |FORMAT_MESSAGE_FROM_SYSTEM @@ -53,11 +62,12 @@ Err::Err(ErrType t, const wchar_t* fmt) GetModuleHandle(L"wininet.dll"), code, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), - (wchar_t*)&wszMsg, + (wchar_t*)&msg, 0, nullptr); + Strip(msg, lenMsg); what = std::wstring(lenMsg+wcslen(fmt), 0); - Swprintf(what, fmt, wszMsg); - HeapFree(GetProcessHeap(), 0, wszMsg); + Swprintf(what, fmt, msg); + HeapFree(GetProcessHeap(), 0, msg); } break; } |