This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
forked from plusls/FontMod
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtil.hpp
171 lines (143 loc) · 3.12 KB
/
Util.hpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#pragma once
// https://msdn.microsoft.com/en-us/magazine/mt763237
bool Utf8ToUtf16(const std::string_view& utf8, std::wstring& utf16)
{
if (utf8.empty())
{
utf16.clear();
return true;
}
constexpr DWORD kFlags = MB_ERR_INVALID_CHARS;
const int utf8Length = static_cast<int>(utf8.length());
const int utf16Length = MultiByteToWideChar(
CP_UTF8,
kFlags,
utf8.data(),
utf8Length,
nullptr,
0
);
if (utf16Length == 0)
{
return false;
}
utf16.resize(utf16Length);
int result = MultiByteToWideChar(
CP_UTF8,
kFlags,
utf8.data(),
utf8Length,
utf16.data(),
utf16Length
);
if (result == 0)
{
return false;
}
return true;
}
bool Utf16ToUtf8(const std::wstring_view& utf16, std::string& utf8)
{
if (utf16.empty())
{
utf8.clear();
return true;
}
constexpr DWORD kFlags = WC_ERR_INVALID_CHARS;
const int utf16Length = static_cast<int>(utf16.length());
const int utf8Length = WideCharToMultiByte(
CP_UTF8,
kFlags,
utf16.data(),
utf16Length,
nullptr,
0,
nullptr, nullptr
);
if (utf8Length == 0)
{
return false;
}
utf8.resize(utf8Length);
int result = WideCharToMultiByte(
CP_UTF8,
kFlags,
utf16.data(),
utf16Length,
utf8.data(),
utf8Length,
nullptr, nullptr
);
if (result == 0)
{
return false;
}
return true;
}
inline bool stol(const std::string& str, long& out)
{
int& errno_ref = errno;
const char *ptr = str.c_str();
char *eptr;
errno_ref = 0;
out = strtol(ptr, &eptr, 10);
if (ptr == eptr)
return false;
if (errno_ref == ERANGE)
return false;
return true;
}
inline bool stoul(const std::string& str, unsigned long& out)
{
int& errno_ref = errno;
const char *ptr = str.c_str();
char *eptr;
errno_ref = 0;
out = strtoul(ptr, &eptr, 10);
if (ptr == eptr)
return false;
if (errno_ref == ERANGE)
return false;
return true;
}
template <typename Key>
YAML::Node FindNode(const YAML::Node& node, const Key& key)
{
if (auto merge = node["<<"]; merge.IsDefined())
{
if (auto child = FindNode(merge, key); child.IsDefined())
{
return child;
}
}
return node[key];
}
// https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/author-coclasses#add-helper-types-and-functions
auto GetModuleFsPath(HMODULE hModule)
{
std::wstring path(MAX_PATH, L'\0');
DWORD pathSize;
DWORD actualSize;
do
{
pathSize = static_cast<DWORD>(path.size());
actualSize = GetModuleFileNameW(nullptr, path.data(), pathSize);
if (actualSize + 1 > pathSize)
{
path.resize(pathSize * 2);
}
} while (actualSize + 1 > pathSize);
path.resize(actualSize);
return fs::path(path).remove_filename();
}
void SetThreadDpiAware()
{
HMODULE hUser32 = GetModuleHandleW(L"user32.dll");
if (hUser32)
{
using SetThreadDpiAwarenessContextPtr = DPI_AWARENESS_CONTEXT(WINAPI *)(DPI_AWARENESS_CONTEXT);
auto funcPtr = reinterpret_cast<SetThreadDpiAwarenessContextPtr>(GetProcAddress(hUser32, "SetThreadDpiAwarenessContext"));
if (funcPtr)
funcPtr(DPI_AWARENESS_CONTEXT_SYSTEM_AWARE);
}
}