aboutsummaryrefslogtreecommitdiff
path: root/c/err.cpp
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-09-07 23:33:08 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-09-07 23:33:08 +0200
commitb5917be252caf9d38ae9466352c4176ef25732e3 (patch)
tree8eee2398b84cd533999a6d6f7539131daef5acfd /c/err.cpp
parentac7d676a1c457f71f1d3ae17d74699dd95c9c985 (diff)
downloadEpisodeBrowser-b5917be252caf9d38ae9466352c4176ef25732e3.tar.gz
Simplify Err message format.
Diffstat (limited to 'c/err.cpp')
-rw-r--r--c/err.cpp63
1 files changed, 34 insertions, 29 deletions
diff --git a/c/err.cpp b/c/err.cpp
index 277bbce..dbbc6ae 100644
--- a/c/err.cpp
+++ b/c/err.cpp
@@ -8,53 +8,58 @@
#include "win32.h"
/* Strip trailing punctuation. */
-static void Strip(wchar_t* s, size_t len = -1)
+static void Strip(wchar_t* s, DWORD* len)
{
- for (int i = len-1; i >= 0; i--)
+ int i;
+ for (i = *len-1; i >= 0; i--)
if (s[i] == '\r' || s[i] == '\n' || s[i] == '.')
s[i] = 0;
+ *len = i+1;
}
-Err::Err(ErrType t, const wchar_t* fmt)
+Err::Err(ErrType t, Buf<const wchar_t> msg)
{
+ const wchar_t fmt[] = L"%s: %s.";
+
switch (t) {
case GENERIC:
- assert(wcscmp(fmt, L"%s.") != 0);
- what = fmt;
+ what = std::wstring(Len(msg)+2, 0);
+ Swprintf(what, L"%s.", msg.data);
break;
case WINDOWS:
{
- wchar_t* msg;
- DWORD lenMsg = FormatMessageW(
+ wchar_t* err;
+ DWORD lenErr = FormatMessageW(
FORMAT_MESSAGE_ALLOCATE_BUFFER
|FORMAT_MESSAGE_FROM_SYSTEM
|FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr,
GetLastError(),
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
- (wchar_t*)&msg,
+ (wchar_t*)&err,
0, nullptr);
- Strip(msg, lenMsg);
- what = std::wstring(lenMsg+wcslen(fmt), 0);
- Swprintf(what, fmt, msg);
- HeapFree(GetProcessHeap(), 0, msg);
+ Strip(err, &lenErr);
+ what = std::wstring(Len(fmt)+Len(msg)+lenErr, 0);
+ Swprintf(what, fmt, msg.data, err);
+ HeapFree(GetProcessHeap(), 0, err);
break;
}
case WININET:
{
DWORD code = GetLastError();
if (code == ERROR_INTERNET_EXTENDED_ERROR) {
- DWORD lenMsg;
- InternetGetLastResponseInfo(&code, nullptr, &lenMsg);
- std::wstring msg(lenMsg, 0);
- if (InternetGetLastResponseInfoW(&code, msg.data(), &lenMsg)) {
- what = std::wstring(lenMsg+wcslen(fmt), 0);
- Swprintf(what, fmt, msg.c_str());
+ DWORD lenErr;
+ InternetGetLastResponseInfo(&code, nullptr, &lenErr);
+ std::wstring err(lenErr, 0);
+ if (InternetGetLastResponseInfoW(&code, err.data(), &lenErr)) {
+ Strip(err.data(), &lenErr);
+ what = std::wstring(Len(fmt)+Len(msg)+lenErr, 0);
+ Swprintf(what, fmt, msg.data, err.c_str());
} else
- what = Err(WINDOWS, fmt).what;
+ what = Err(WINDOWS, msg).what;
} else {
- wchar_t* msg;
- DWORD lenMsg = FormatMessageW(
+ wchar_t* err;
+ DWORD lenErr = FormatMessageW(
FORMAT_MESSAGE_ALLOCATE_BUFFER
|FORMAT_MESSAGE_FROM_SYSTEM
|FORMAT_MESSAGE_FROM_HMODULE
@@ -62,20 +67,20 @@ Err::Err(ErrType t, const wchar_t* fmt)
GetModuleHandle(L"wininet.dll"),
code,
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
- (wchar_t*)&msg,
+ (wchar_t*)&err,
0, nullptr);
- Strip(msg, lenMsg);
- what = std::wstring(lenMsg+wcslen(fmt), 0);
- Swprintf(what, fmt, msg);
- HeapFree(GetProcessHeap(), 0, msg);
+ Strip(err, &lenErr);
+ what = std::wstring(Len(fmt)+Len(msg)+lenErr, 0);
+ Swprintf(what, fmt, msg.data, err);
+ HeapFree(GetProcessHeap(), 0, err);
}
break;
}
case LIBXML2:
{
- std::wstring msg = WideFromNarrow(xmlGetLastError()->message);
- what = std::wstring(msg.size()+wcslen(fmt), 0);
- Swprintf(what, fmt, msg.c_str());
+ std::wstring err = WideFromNarrow(xmlGetLastError()->message);
+ what = std::wstring(Len(fmt)+Len(msg)+err.size(), 0);
+ Swprintf(what, fmt, msg.data, err.c_str());
break;
}
}