-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathsyserror_import_lib.cpp
58 lines (46 loc) · 2.12 KB
/
syserror_import_lib.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// This must be as small as possible, because its contents are
// injected into the msvcprt.lib and msvcprtd.lib import libraries.
// Do not include or define anything else here.
// In particular, basic_string must not be included here.
#include <__msvc_system_error_abi.hpp>
#include <Windows.h>
namespace {
struct _Whitespace_bitmap_t {
bool _Is_whitespace[256];
constexpr _Whitespace_bitmap_t() noexcept : _Is_whitespace{} {
_Is_whitespace[' '] = true;
_Is_whitespace['\n'] = true;
_Is_whitespace['\r'] = true;
_Is_whitespace['\t'] = true;
_Is_whitespace['\0'] = true;
}
_NODISCARD constexpr bool _Test(const char _Ch) const noexcept {
return _Is_whitespace[static_cast<unsigned char>(_Ch)];
}
};
constexpr _Whitespace_bitmap_t _Whitespace_bitmap;
} // unnamed namespace
_EXTERN_C
_NODISCARD size_t __CLRCALL_PURE_OR_STDCALL __std_get_string_size_without_trailing_whitespace(
const char* const _Str, size_t _Size) noexcept {
while (_Size != 0 && _Whitespace_bitmap._Test(_Str[_Size - 1])) {
--_Size;
}
return _Size;
}
_NODISCARD size_t __CLRCALL_PURE_OR_STDCALL __std_system_error_allocate_message(
const unsigned long _Message_id, char** const _Ptr_str) noexcept {
// convert to name of Windows error, return 0 for failure, otherwise return number of chars in buffer
// __std_system_error_deallocate_message should be called even if 0 is returned
// pre: *_Ptr_str == nullptr
const unsigned long _Chars =
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, _Message_id, 0, reinterpret_cast<char*>(_Ptr_str), 0, nullptr);
return _CSTD __std_get_string_size_without_trailing_whitespace(*_Ptr_str, _Chars);
}
void __CLRCALL_PURE_OR_STDCALL __std_system_error_deallocate_message(char* const _Str) noexcept {
LocalFree(_Str);
}
_END_EXTERN_C