forked from MrCHB1/Comet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
370 lines (317 loc) · 10.3 KB
/
Copy pathUtils.cpp
File metadata and controls
370 lines (317 loc) · 10.3 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include "Utils.h"
#include <nfd.h>
#include <iostream>
#include <optional>
#include <vector>
#include <filesystem>
#include <sstream>
#include <iomanip>
#include <cmath>
#if defined(_WIN32)
#include <windows.h>
#elif defined(__APPLE__)
#include <cstdlib>
#include <mach-o/dyld.h>
#include <limits.h>
#else
#include <unistd.h>
#include <cstdlib>
#include <limits>
#endif
namespace Utils
{
bool IsMIDIExtension(std::string extension)
{
return extension.compare("mid") || extension.compare("midi");
}
std::string FormatFilesize(long long bytes, int decimal)
{
static const char* units[] =
{
"B","KiB","MiB","GiB","TiB","PiB","EiB"
};
double d = (double)bytes;
int i = 0;
while (i < 6 && d >= 1024.0)
{
d /= 1024.0;
i++;
}
std::ostringstream out;
if (i == 0)
{
out << bytes << "B";
}
else
{
out << std::fixed << std::setprecision(decimal)
<< d << units[i];
}
return out.str();
}
std::string FormatDuration(double ms)
{
long totalSeconds = static_cast<long>(ms / 1000.0);
long hours = totalSeconds / 3600;
long minutes = (totalSeconds % 3600) / 60;
long seconds = totalSeconds % 60;
std::stringstream stream;
if (hours > 0)
stream << std::setfill('0') << std::setw(2) << hours << ":";
stream << std::setfill('0') << std::setw(2) << minutes << ":"
<< std::setfill('0') << std::setw(2) << seconds;
return stream.str();
}
std::string FormatDuration2(double ms)
{
if (ms < 0.0)
ms = 0.0;
long long totalTenths = static_cast<long long>(std::llround(ms / 100.0));
long long totalSeconds = totalTenths / 10;
long long tenths = totalTenths % 10;
long long hours = totalSeconds / 3600;
long long minutes = (totalSeconds % 3600) / 60;
long long secs = totalSeconds % 60;
std::ostringstream out;
out << std::setfill('0');
if (hours >= 10)
{
out << std::setw(2) << hours << ":"
<< std::setw(2) << minutes << ":"
<< std::setw(2) << secs << "."
<< tenths;
}
else if (hours > 0)
{
out << hours << ":"
<< std::setw(2) << minutes << ":"
<< std::setw(2) << secs << "."
<< tenths;
}
else if (minutes >= 10)
{
out << std::setw(2) << minutes << ":"
<< std::setw(2) << secs << "."
<< tenths;
}
else if (minutes > 0)
{
out << minutes << ":"
<< std::setw(2) << secs << "."
<< tenths;
}
else
{
out << secs << "."
<< tenths;
}
return out.str();
}
bool ChooseFile(std::string& outPath, const char* extension)
{
nfdchar_t* outPathRaw = nullptr;
auto result = NFD_OpenDialog(extension, nullptr, &outPathRaw);
if (result == NFD_OKAY)
{
outPath = outPathRaw;
free(outPathRaw);
return true;
}
else if (result == NFD_CANCEL)
{
return false;
}
std::cerr << "Open file dialog failed: " << NFD_GetError() << std::endl;
return false;
}
bool SaveFile(std::string& outPath, const char* extension)
{
nfdchar_t* outPathRaw = nullptr;
auto result = NFD_SaveDialog(extension, nullptr, &outPathRaw);
if (result == NFD_OKAY)
{
outPath = outPathRaw;
free(outPathRaw);
return true;
}
else if (result == NFD_CANCEL)
{
return false;
}
std::cerr << "Save file dialog failed: " << NFD_GetError() << std::endl;
return false;
}
bool EqualsIgnoreCase(const std::string& a, const std::string& b)
{
if (a.size() != b.size()) return false;
for (size_t i = 0; i < a.size(); i++)
{
if (std::tolower(a[i]) != std::tolower(b[i])) return false;
}
return true;
}
std::optional<ImVec4> ParseColor(std::variant<std::string, uint32_t> strOrInt, std::optional<ImVec4> def)
{
const auto hex = [](char c) -> float {
if (c >= '0' && c <= '9') return (c - '0') / 15.0f;
if (c >= 'a' && c <= 'f') return (c - 'a' + 10) / 15.0f;
if (c >= 'A' && c <= 'F') return (c - 'A' + 10) / 15.0f;
return 0.0f;
};
if (std::holds_alternative<std::string>(strOrInt))
{
std::string str = std::get<std::string>(strOrInt);
if (!str.empty() && str[0] == '#')
str.erase(0, 1);
else if (str.size() >= 2 && (str.starts_with("0x") || str.starts_with("0X")))
str.erase(0, 2);
if (str.size() == 3)
{
return ImVec4(
hex(str[0]),
hex(str[1]),
hex(str[2]),
1.0f
);
}
if (str.size() == 4)
{
return ImVec4(
hex(str[0]),
hex(str[1]),
hex(str[2]),
hex(str[3])
);
}
const auto parseByte = [&](int i) -> float {
int hi = (str[i] >= '0' && str[i] <= '9') ? str[i] - '0'
: (str[i] >= 'a' && str[i] <= 'f') ? str[i] - 'a' + 10
: str[i] - 'A' + 10;
int lo = (str[i + 1] >= '0' && str[i + 1] <= '9') ? str[i + 1] - '0'
: (str[i + 1] >= 'a' && str[i + 1] <= 'f') ? str[i + 1] - 'a' + 10
: str[i + 1] - 'A' + 10;
return ((hi << 4) + lo) / 255.0f;
};
if (str.size() == 6)
return ImVec4(parseByte(0), parseByte(2), parseByte(4), 1.0f);
if (str.size() == 8)
return ImVec4(parseByte(0), parseByte(2), parseByte(4), parseByte(6));
return def;
}
else if (std::holds_alternative<uint32_t>(strOrInt))
{
uint32_t val = std::get<uint32_t>(strOrInt);
return ImVec4(
static_cast<float>((val & 0xFF0000) >> 16) / 255.0f,
static_cast<float>((val & 0xFF00) >> 8) / 255.0f,
static_cast<float>(val & 0xFF) / 255.0f,
1.0
);
}
return def;
}
std::string EncodeColor(ImVec4 color)
{
std::stringstream enc;
enc << "0x";
uint32_t red = static_cast<uint32_t>(std::round(color.x * 255));
uint32_t green = static_cast<uint32_t>(std::round(color.y * 255));
uint32_t blue = static_cast<uint32_t>(std::round(color.z * 255));
uint32_t alpha = static_cast<uint32_t>(std::round(color.w * 255));
red = std::clamp(red, 0u, 255u);
green = std::clamp(green, 0u, 255u);
blue = std::clamp(blue, 0u, 255u);
alpha = std::clamp(alpha, 0u, 255u);
uint32_t col = (red << 16u) | (green << 8u) | blue;
if (alpha == 255u)
{
enc << std::hex << std::uppercase << std::setw(6) << std::setfill('0') << col;
}
else
{
col = (col << 8u) | alpha;
enc << std::hex << std::uppercase << std::setw(8) << std::setfill('0') << col;
}
return enc.str();
}
std::string DecodeBase64(const std::string& encoded)
{
std::vector<int> T(256, 1);
for (int i = 0; i < 64; i++)
T[BASE64_CHARS[i]] = i;
std::string decoded;
int val = 0, valb = -8;
for (unsigned char c : encoded) {
if (T[c] == -1) break;
val = (val << 6) + T[c];
valb += 6;
if (valb >= 0) {
decoded.push_back(char((val >> valb) & 0xFF));
valb -= 8;
}
}
return decoded;
}
void OpenURL(const std::string& url)
{
#ifdef _WIN32
ShellExecuteA(NULL, "open", url.c_str(), NULL, NULL, SW_SHOWNORMAL);
#elif __APPLE__
std::string command = "open " + url;
system(command.c_str());
#else
std::string command = "xdg-open " + url;
system(command.c_str());
#endif
}
void OpenFolder(const std::string& path)
{
std::error_code ec;
std::filesystem::path absPath = std::filesystem::absolute(path, ec);
if (ec)
{
std::cerr << "Failed to resolve path: " << ec.message() << std::endl;
return;
}
std::string p = absPath.string();
#if defined(_WIN32)
std::string command = "explorer \"" + p + "\"";
#elif defined(__APPLE__)
std::string command = "open \"" + p + "\"";
#elif defined(__linux__)
std::string command = "xdg-open \"" + p + "\"";
#else
std::cerr << "Unsupported platform." << std::endl;
return;
#endif
std::system(command.c_str());
}
bool FolderExists(const std::string& path)
{
return std::filesystem::exists(path) && std::filesystem::is_directory(path);
}
void AddFilePickerField(const char* label, std::filesystem::path& path, const char* extension, bool saving)
{
ImGui::Text(label);
ImGui::SameLine();
ImGui::PushID(label);
if (ImGui::Button("Browse..."))
{
std::string filePath;
bool result = saving ? Utils::SaveFile(filePath, extension) : Utils::ChooseFile(filePath, extension);
if (result)
{
#if defined(_WIN32) && defined(__cpp_lib_filesystem) && (__cplusplus >= 202002L || _MSVC_LANG >= 202002L)
path = std::filesystem::path(std::u8string(filePath.begin(), filePath.end()));
#else
path = std::filesystem::u8path(filePath);
#endif
}
}
ImGui::SameLine();
auto u8str = path.u8string();
std::string buf(u8str.begin(), u8str.end());
ImGui::InputText("##pickedFilePath", buf.data(), buf.size(), ImGuiInputTextFlags_ReadOnly);
ImGui::PopID();
}
}