Skip to content

Commit 784c7c9

Browse files
committed
整理代码
1 parent cf4d6c7 commit 784c7c9

File tree

1 file changed

+34
-38
lines changed

1 file changed

+34
-38
lines changed

bee/error.cpp

+34-38
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,49 @@
33

44
#if defined(_WIN32)
55
# include <Windows.h>
6-
# include <bee/win/wtf8.h>
6+
# include <bee/win/cwtf8.h>
77
#else
88
# include <errno.h>
99
#endif
1010

1111
namespace bee::error {
1212
#if defined(_WIN32)
13-
struct errormsg : public std::wstring_view {
14-
using mybase = std::wstring_view;
15-
errormsg(wchar_t* str) noexcept
16-
: mybase(str) {}
17-
~errormsg() noexcept {
18-
::LocalFree(reinterpret_cast<HLOCAL>(const_cast<wchar_t*>(mybase::data())));
19-
}
20-
};
21-
22-
static std::wstring error_message(int error_code) {
23-
wchar_t* message = 0;
24-
const unsigned long result = ::FormatMessageW(
25-
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
26-
NULL,
27-
error_code,
28-
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
29-
reinterpret_cast<LPWSTR>(&message),
30-
0,
31-
NULL
32-
);
3313

34-
if ((result == 0) || !message) {
35-
return std::format(L"Unable to get an error message for error code: {}.", error_code);
36-
}
37-
errormsg str(message);
38-
while (str.size() && ((str.back() == L'\n') || (str.back() == L'\r'))) {
39-
str.remove_suffix(1);
40-
}
41-
return std::wstring(str);
42-
}
43-
44-
class winCategory : public std::error_category {
14+
class windows_category : public std::error_category {
4515
public:
16+
struct errormsg : public std::wstring_view {
17+
using mybase = std::wstring_view;
18+
errormsg(wchar_t* str) noexcept
19+
: mybase(str) {}
20+
~errormsg() noexcept {
21+
::LocalFree(reinterpret_cast<HLOCAL>(const_cast<wchar_t*>(mybase::data())));
22+
}
23+
};
4624
const char* name() const noexcept override {
4725
return "Windows";
4826
}
4927
std::string message(int error_code) const override {
50-
return wtf8::w2u(error_message(error_code));
28+
wchar_t* message = 0;
29+
const unsigned long result = ::FormatMessageW(
30+
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
31+
NULL,
32+
error_code,
33+
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
34+
reinterpret_cast<LPWSTR>(&message),
35+
0,
36+
NULL
37+
);
38+
if ((result == 0) || !message) {
39+
return std::format("Unable to get an error message for error code: {}.", error_code);
40+
}
41+
errormsg wstr(message);
42+
while (wstr.size() && ((wstr.back() == L'\n') || (wstr.back() == L'\r'))) {
43+
wstr.remove_suffix(1);
44+
}
45+
size_t len = wtf8_from_utf16_length(wstr.data(), wstr.size());
46+
std::string ret(len, '\0');
47+
wtf8_from_utf16(wstr.data(), wstr.size(), ret.data(), len);
48+
return ret;
5149
}
5250
std::error_condition default_error_condition(int error_code) const noexcept override {
5351
const std::error_condition cond = std::system_category().default_error_condition(error_code);
@@ -57,8 +55,6 @@ namespace bee::error {
5755
return std::error_condition(error_code, *this);
5856
}
5957
};
60-
61-
static winCategory g_windows_category;
6258
#endif
6359

6460
std::string errmsg(std::string_view msg, std::error_code ec) noexcept {
@@ -75,23 +71,23 @@ namespace bee::error {
7571

7672
std::string sys_errmsg(std::string_view msg) noexcept {
7773
#if defined(_WIN32)
78-
return errmsg(msg, std::error_code(::WSAGetLastError(), g_windows_category));
74+
return errmsg(msg, std::error_code(::GetLastError(), windows_category()));
7975
#else
8076
return errmsg(msg, std::error_code(errno, std::system_category()));
8177
#endif
8278
}
8379

8480
std::string net_errmsg(std::string_view msg, int err) noexcept {
8581
#if defined(_WIN32)
86-
return errmsg(msg, std::error_code(err, g_windows_category));
82+
return errmsg(msg, std::error_code(err, windows_category()));
8783
#else
8884
return errmsg(msg, std::error_code(err, std::system_category()));
8985
#endif
9086
}
9187

9288
std::string net_errmsg(std::string_view msg) noexcept {
9389
#if defined(_WIN32)
94-
return net_errmsg(msg, ::GetLastError());
90+
return net_errmsg(msg, ::WSAGetLastError());
9591
#else
9692
return net_errmsg(msg, errno);
9793
#endif

0 commit comments

Comments
 (0)