-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathCommon.cpp
More file actions
260 lines (218 loc) · 6.37 KB
/
Common.cpp
File metadata and controls
260 lines (218 loc) · 6.37 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// This file was developed by Thomas Müller <contact@tom94.net>.
// It is published under the BSD 3-Clause License within the LICENSE file.
#include <tev/Common.h>
#include <nanogui/opengl.h>
#include <utf8.h>
#include <algorithm>
#include <atomic>
#include <cctype>
#include <map>
#include <regex>
#ifdef _WIN32
# include <Shlobj.h>
#else
# include <cstring>
# include <pwd.h>
# include <unistd.h>
#endif
using namespace nanogui;
using namespace std;
namespace tev {
u8string toU8string(const string& str) {
u8string temp;
utf8::replace_invalid(begin(str), end(str), back_inserter(temp));
return temp;
}
string fromU8string(const u8string& str) {
string temp;
utf8::replace_invalid(begin(str), end(str), back_inserter(temp));
return temp;
}
string ensureUtf8(const string& str) {
string temp;
utf8::replace_invalid(begin(str), end(str), back_inserter(temp));
return temp;
}
string utf16to8(const wstring& utf16) {
string utf8;
utf8::utf16to8(begin(utf16), end(utf16), back_inserter(utf8));
return utf8;
}
fs::path toPath(const string& utf8) {
// tev's strings are always utf8 encoded, however fs::path does
// not know this. Therefore: convert the string to a std::u8string
// and pass _that_ string to the fs::path constructor, which will
// then take care of converting the utf8 string to the native
// file name encoding.
return toU8string(utf8);
}
string toString(const fs::path& path) {
// Conversely to `toPath`, ensure that the returned string is
// utf8 encoded by requesting a std::u8string from the path
// object and then convert _that_ string to a regular char string.
return fromU8string(path.u8string());
}
vector<string> split(string text, const string& delim) {
vector<string> result;
size_t begin = 0;
while (true) {
size_t end = text.find_first_of(delim, begin);
if (end == string::npos) {
result.emplace_back(text.substr(begin));
return result;
} else {
result.emplace_back(text.substr(begin, end - begin));
begin = end + 1;
}
}
return result;
}
string toLower(string str) {
transform(begin(str), end(str), begin(str), [](unsigned char c) { return (char)tolower(c); });
return str;
}
string toUpper(string str) {
transform(begin(str), end(str), begin(str), [](unsigned char c) { return (char)toupper(c); });
return str;
}
bool matchesFuzzy(string text, string filter, size_t* matchedPartId) {
if (matchedPartId) {
// Default value of 0. Is actually returned when the filter
// is empty or when there is no match.
*matchedPartId = 0;
}
if (filter.empty()) {
return true;
}
// Perform matching on lowercase strings
text = toLower(text);
filter = toLower(filter);
auto words = split(filter, ", ");
// We don't want people entering multiple spaces in a row to match everything.
words.erase(remove(begin(words), end(words), ""), end(words));
if (words.empty()) {
return true;
}
// Match every word of the filter separately.
for (size_t i = 0; i < words.size(); ++i) {
if (text.find(words[i]) != string::npos) {
if (matchedPartId) {
*matchedPartId = i;
}
return true;
}
}
return false;
}
bool matchesRegex(string text, string filter) {
if (filter.empty()) {
return true;
}
try {
regex searchRegex{filter, std::regex_constants::ECMAScript | std::regex_constants::icase};
return regex_search(text, searchRegex);
} catch (const regex_error&) {
return false;
}
}
void drawTextWithShadow(NVGcontext* ctx, float x, float y, string text, float shadowAlpha) {
nvgSave(ctx);
nvgFontBlur(ctx, 2);
nvgFillColor(ctx, Color{0.0f, shadowAlpha});
nvgText(ctx, x + 1, y + 1, text.c_str(), NULL);
nvgRestore(ctx);
nvgText(ctx, x, y, text.c_str(), NULL);
}
int lastError() {
#ifdef _WIN32
return GetLastError();
#else
return errno;
#endif
}
int lastSocketError() {
#ifdef _WIN32
return WSAGetLastError();
#else
return errno;
#endif
}
string errorString(int errorId) {
#ifdef _WIN32
char* s = NULL;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorId, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&s, 0, NULL);
string result = fmt::format("{} ({})", s, errorId);
LocalFree(s);
return result;
#else
return fmt::format("{} ({})", strerror(errorId), errno);
#endif
}
fs::path homeDirectory() {
#ifdef _WIN32
char path[MAX_PATH];
if (SHGetFolderPath(NULL, CSIDL_PROFILE, NULL, 0, path) != S_OK) {
return "";
}
return path;
#else
struct passwd* pw = getpwuid(getuid());
return pw->pw_dir;
#endif
}
void toggleConsole() {
#ifdef _WIN32
HWND console = GetConsoleWindow();
DWORD consoleProcessId;
GetWindowThreadProcessId(console, &consoleProcessId);
// Only toggle the console if it was actually spawned by tev. If we are
// running in a foreign console, then we should leave it be.
if (GetCurrentProcessId() == consoleProcessId) {
ShowWindow(console, IsWindowVisible(console) ? SW_HIDE : SW_SHOW);
}
#endif
}
static std::atomic<bool> sShuttingDown{false};
bool shuttingDown() {
return sShuttingDown;
}
void setShuttingDown() {
sShuttingDown = true;
}
ETonemap toTonemap(string name) {
// Perform matching on uppercase strings
name = toUpper(name);
if (name == "SRGB") {
return SRGB;
} else if (name == "GAMMA") {
return Gamma;
} else if (name == "FALSECOLOR" || name == "FC") {
return FalseColor;
} else if (name == "POSITIVENEGATIVE" || name == "POSNEG" || name == "PN" ||name == "+-") {
return PositiveNegative;
} else if (name == "HASH") {
return Hash;
} else {
return SRGB;
}
}
EMetric toMetric(string name) {
// Perform matching on uppercase strings
name = toUpper(name);
if (name == "E") {
return Error;
} else if (name == "AE") {
return AbsoluteError;
} else if (name == "SE") {
return SquaredError;
} else if (name == "RAE") {
return RelativeAbsoluteError;
} else if (name == "RSE") {
return RelativeSquaredError;
} else {
return Error;
}
}
}