-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathutils_win.h
63 lines (49 loc) · 1.77 KB
/
utils_win.h
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
59
60
61
62
63
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#ifndef RUNTIME_PLATFORM_UTILS_WIN_H_
#define RUNTIME_PLATFORM_UTILS_WIN_H_
#if !defined(RUNTIME_PLATFORM_UTILS_H_)
#error Do not include utils_win.h directly; use utils.h instead.
#endif
#include <intrin.h>
#include <stdlib.h>
namespace dart {
// WARNING: The below functions assume host is always Little Endian!
inline uint16_t Utils::HostToBigEndian16(uint16_t value) {
return _byteswap_ushort(value);
}
inline uint32_t Utils::HostToBigEndian32(uint32_t value) {
return _byteswap_ulong(value);
}
inline uint64_t Utils::HostToBigEndian64(uint64_t value) {
return _byteswap_uint64(value);
}
inline uint16_t Utils::HostToLittleEndian16(uint16_t value) {
return value;
}
inline uint32_t Utils::HostToLittleEndian32(uint32_t value) {
return value;
}
inline uint64_t Utils::HostToLittleEndian64(uint64_t value) {
return value;
}
inline char* Utils::StrError(int err, char* buffer, size_t bufsize) {
DWORD message_size =
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
buffer, static_cast<DWORD>(bufsize), nullptr);
if (message_size == 0) {
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
snprintf(buffer, bufsize,
"FormatMessage failed for error code %d (error %d)\n", err,
GetLastError());
}
snprintf(buffer, bufsize, "OS Error %d", err);
}
// Ensure string termination.
buffer[bufsize - 1] = 0;
return buffer;
}
} // namespace dart
#endif // RUNTIME_PLATFORM_UTILS_WIN_H_