-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathutility.cc
More file actions
146 lines (121 loc) · 3.69 KB
/
utility.cc
File metadata and controls
146 lines (121 loc) · 3.69 KB
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
//==============================================================================
// Copyright Advanced Micro Devices, Inc. All rights reserved.
/// @author AMD Developer Tools Team
/// @file
/// @brief Utility macros, constants and function declarations.
//==============================================================================
#include "gpu_perf_api_common/utility.h"
#include <array>
#include <cstring>
#include <cstdlib>
#include <locale>
#include <string_view>
#include "gpu_perf_api_common/gpa_common_defs.h"
#ifdef _WIN32
EXTERN_C IMAGE_DOS_HEADER __ImageBase; ///< __ImageBase symbol exported by MSVC linker.
/// Macro for the HINST of the owning module.
#define HINST_THISCOMPONENT ((HINSTANCE)&__ImageBase)
#else
#include <unistd.h>
#endif
bool gpa_util::GetCurrentModulePath(std::string& current_module_path)
{
bool success = true;
#ifdef _WIN32
char sz_this_module_name[MAX_PATH] = {0};
if (0 == ::GetModuleFileNameA(HINST_THISCOMPONENT, sz_this_module_name, MAX_PATH))
{
success = false;
}
if (success)
{
char sz_this_module_path[MAX_PATH] = {0};
strncpy_s(sz_this_module_path, MAX_PATH, sz_this_module_name, std::strrchr(sz_this_module_name, '\\') + 1 - sz_this_module_name);
current_module_path = sz_this_module_path;
}
#else
char sz_this_module_name[4096] = {0};
int len;
len = readlink("/proc/self/exe", sz_this_module_name, 4096 - 1);
if (len != -1)
{
sz_this_module_name[len] = '\0';
}
else
{
success = false;
}
if (success)
{
char sz_this_module_path[4096] = {0};
strncpy_s(sz_this_module_path, 4096, sz_this_module_name, std::strrchr(sz_this_module_name, '/') + 1 - sz_this_module_name);
current_module_path = sz_this_module_path;
}
#endif
return success;
}
std::optional<std::string> gpa_util::GetEnv(const char* var)
{
assert(var != nullptr);
if (var == nullptr) [[unlikely]]
{
return std::nullopt;
}
std::string result;
#ifdef _WIN32
if (const DWORD size = ::GetEnvironmentVariableA(var, nullptr, 0); size > 0)
{
// 'size' includes space for the null terminator.
std::string buffer;
buffer.resize(size);
const DWORD written = ::GetEnvironmentVariableA(var, buffer.data(), size);
// On success, 'written' is the number of characters excluding the null terminator.
// Treat truncation or failure as "no value".
if (written > 0 && written < size)
{
buffer.resize(written);
result = std::move(buffer);
}
}
#else
if (const char* value = std::getenv(var); value != nullptr)
{
result = value;
}
#endif
if (result.empty())
{
return std::nullopt;
}
assert(!result.empty());
return result;
}
[[nodiscard]] bool gpa_util::IsEnvVarForceEnabled(const char* var)
{
const std::optional<std::string> env_var = gpa_util::GetEnv(var);
if (!env_var.has_value())
{
return false;
}
const std::string& value = env_var.value();
constexpr auto kEnabledValues = std::to_array<std::string_view>({"1", "TRUE", "True", "true"});
for (const std::string_view enabled_value : kEnabledValues)
{
if (value == enabled_value)
{
return true;
}
}
return false;
}
std::string gpa_util::ConvertToStdString(const std::wstring_view wide)
{
const std::locale& loc = std::locale();
const auto& facet = std::use_facet<std::ctype<wchar_t>>(loc);
std::string str(wide.size(), '\0');
for (size_t i = 0; i < wide.size(); ++i)
{
str[i] = facet.narrow(wide[i], '\0');
}
return str;
}