|
1 |
| -#include <string> |
2 | 1 | #include <array>
|
| 2 | +#include <string> |
3 | 3 |
|
4 | 4 | #ifdef _WIN32
|
5 | 5 | #include <windows.h>
|
|
8 | 8 | #endif
|
9 | 9 |
|
10 | 10 | #ifdef _WIN32
|
11 |
| -std::string Narrow(const std::wstring_view &utf16) |
12 |
| -{ |
13 |
| - if (utf16.empty()) |
14 |
| - { |
15 |
| - return {}; |
16 |
| - } |
17 |
| - int requiredSize = WideCharToMultiByte(CP_UTF8, 0, utf16.data(), static_cast<int>(utf16.size()), |
18 |
| - nullptr, 0, nullptr, nullptr); |
19 |
| - std::string utf8(requiredSize, '\0'); |
20 |
| - WideCharToMultiByte(CP_UTF8, 0, utf16.data(), static_cast<int>(utf16.size()), &utf8[0], |
21 |
| - requiredSize, nullptr, nullptr); |
22 |
| - return utf8; |
| 11 | +std::string Narrow(const std::wstring_view &utf16) { |
| 12 | + if (utf16.empty()) { |
| 13 | + return {}; |
| 14 | + } |
| 15 | + int requiredSize = WideCharToMultiByte(CP_UTF8, 0, utf16.data(), static_cast<int>(utf16.size()), |
| 16 | + nullptr, 0, nullptr, nullptr); |
| 17 | + std::string utf8(requiredSize, '\0'); |
| 18 | + WideCharToMultiByte(CP_UTF8, 0, utf16.data(), static_cast<int>(utf16.size()), &utf8[0], |
| 19 | + requiredSize, nullptr, nullptr); |
| 20 | + return utf8; |
23 | 21 | }
|
24 | 22 |
|
25 |
| -std::string GetPath(HMODULE module) |
26 |
| -{ |
27 |
| - std::array<wchar_t, MAX_PATH> executableFileBuf; |
28 |
| - DWORD executablePathLen = GetModuleFileNameW(module, executableFileBuf.data(), |
29 |
| - static_cast<DWORD>(executableFileBuf.size())); |
30 |
| - return Narrow(executablePathLen > 0 ? executableFileBuf.data() : L""); |
| 23 | +std::string GetPath(HMODULE module) { |
| 24 | + std::array<wchar_t, MAX_PATH> executableFileBuf; |
| 25 | + DWORD executablePathLen = GetModuleFileNameW(module, executableFileBuf.data(), |
| 26 | + static_cast<DWORD>(executableFileBuf.size())); |
| 27 | + return Narrow(executablePathLen > 0 ? executableFileBuf.data() : L""); |
31 | 28 | }
|
32 | 29 |
|
33 |
| -std::string GetModulePath(void *moduleOrSymbol) |
34 |
| -{ |
35 |
| - HMODULE module = nullptr; |
36 |
| - if (GetModuleHandleExW( |
37 |
| - GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, |
38 |
| - reinterpret_cast<LPCWSTR>(moduleOrSymbol), &module)) |
39 |
| - { |
40 |
| - return GetPath(module); |
41 |
| - } |
| 30 | +std::string GetModulePath(void *moduleOrSymbol) { |
| 31 | + HMODULE module = nullptr; |
| 32 | + if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | |
| 33 | + GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, |
| 34 | + reinterpret_cast<LPCWSTR>(moduleOrSymbol), &module)) { |
| 35 | + return GetPath(module); |
| 36 | + } |
42 | 37 |
|
43 |
| - return ""; |
| 38 | + return ""; |
44 | 39 | }
|
45 | 40 | #else
|
46 |
| -std::string GetModulePath(void *moduleOrSymbol) |
47 |
| -{ |
48 |
| - Dl_info dlInfo; |
49 |
| - if (dladdr(moduleOrSymbol, &dlInfo) == 0) |
50 |
| - { |
51 |
| - return ""; |
52 |
| - } |
| 41 | +std::string GetModulePath(void *moduleOrSymbol) { |
| 42 | + Dl_info dlInfo; |
| 43 | + if (dladdr(moduleOrSymbol, &dlInfo) == 0) { |
| 44 | + return ""; |
| 45 | + } |
53 | 46 |
|
54 |
| - return dlInfo.dli_fname; |
| 47 | + return dlInfo.dli_fname; |
55 | 48 | }
|
56 | 49 | #endif
|
57 | 50 |
|
58 |
| -std::string StripFilenameFromPath(const std::string &path) |
59 |
| -{ |
60 |
| - size_t lastPathSepLoc = path.find_last_of("\\/"); |
61 |
| - return (lastPathSepLoc != std::string::npos) ? path.substr(0, lastPathSepLoc) : ""; |
| 51 | +std::string StripFilenameFromPath(const std::string &path) { |
| 52 | + size_t lastPathSepLoc = path.find_last_of("\\/"); |
| 53 | + return (lastPathSepLoc != std::string::npos) ? path.substr(0, lastPathSepLoc) : ""; |
62 | 54 | }
|
63 | 55 |
|
64 |
| -std::string GetModuleDirectory() |
65 |
| -{ |
66 |
| - static int placeholderSymbol = 0; |
67 |
| - std::string moduleName = GetModulePath(&placeholderSymbol); |
68 |
| - return StripFilenameFromPath(moduleName); |
| 56 | +std::string GetModuleDirectory() { |
| 57 | + static int placeholderSymbol = 0; |
| 58 | + std::string moduleName = GetModulePath(&placeholderSymbol); |
| 59 | + return StripFilenameFromPath(moduleName); |
69 | 60 | }
|
0 commit comments