3
3
4
4
#if defined(_WIN32)
5
5
# include < Windows.h>
6
- # include < bee/win/wtf8 .h>
6
+ # include < bee/win/cwtf8 .h>
7
7
#else
8
8
# include < errno.h>
9
9
#endif
10
10
11
11
namespace bee ::error {
12
12
#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
- );
33
13
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 {
45
15
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
+ };
46
24
const char * name () const noexcept override {
47
25
return " Windows" ;
48
26
}
49
27
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;
51
49
}
52
50
std::error_condition default_error_condition (int error_code) const noexcept override {
53
51
const std::error_condition cond = std::system_category ().default_error_condition (error_code);
@@ -57,8 +55,6 @@ namespace bee::error {
57
55
return std::error_condition (error_code, *this );
58
56
}
59
57
};
60
-
61
- static winCategory g_windows_category;
62
58
#endif
63
59
64
60
std::string errmsg (std::string_view msg, std::error_code ec) noexcept {
@@ -75,23 +71,23 @@ namespace bee::error {
75
71
76
72
std::string sys_errmsg (std::string_view msg) noexcept {
77
73
#if defined(_WIN32)
78
- return errmsg (msg, std::error_code (::WSAGetLastError (), g_windows_category ));
74
+ return errmsg (msg, std::error_code (::GetLastError (), windows_category () ));
79
75
#else
80
76
return errmsg (msg, std::error_code (errno, std::system_category ()));
81
77
#endif
82
78
}
83
79
84
80
std::string net_errmsg (std::string_view msg, int err) noexcept {
85
81
#if defined(_WIN32)
86
- return errmsg (msg, std::error_code (err, g_windows_category ));
82
+ return errmsg (msg, std::error_code (err, windows_category () ));
87
83
#else
88
84
return errmsg (msg, std::error_code (err, std::system_category ()));
89
85
#endif
90
86
}
91
87
92
88
std::string net_errmsg (std::string_view msg) noexcept {
93
89
#if defined(_WIN32)
94
- return net_errmsg (msg, ::GetLastError ());
90
+ return net_errmsg (msg, ::WSAGetLastError ());
95
91
#else
96
92
return net_errmsg (msg, errno);
97
93
#endif
0 commit comments