-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtt_wxString.cpp
More file actions
458 lines (397 loc) · 10.7 KB
/
Copy pathtt_wxString.cpp
File metadata and controls
458 lines (397 loc) · 10.7 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/////////////////////////////////////////////////////////////////////////////
// Purpose: Enhanced version of wxString
// Author: Ralph Walden
// Copyright: Copyright (c) 2020-2023 KeyWorks Software (Ralph Walden)
// License: Apache License -- see ../../LICENSE
/////////////////////////////////////////////////////////////////////////////
#include <wx/filename.h>
#include <cwctype>
#include <filesystem>
#include "tt.h"
#include "tt_wxString.h"
namespace fs = std::filesystem;
#ifdef __cpp_lib_char8_t
#define CHAR8_T_CAST (char8_t const*)
#else
#define CHAR8_T_CAST (char const*)
#endif
std::string tt_wxString::sub_cstr(size_type start_pos, size_type count) const
{
if (start_pos == 0 && count == tt::npos)
{
return utf8_string();
}
if (start_pos < size())
{
return Mid(start_pos, size() - start_pos).utf8_string();
}
return std::string();
}
tt_wxString& tt_wxString::append_view(std::string_view str, size_t posStart, size_t len)
{
if (posStart >= str.size())
{
ASSERT_MSG(posStart < str.size(), "invalid starting position for append_view");
return *this;
}
if (len == tt::npos)
len = str.size() - posStart;
this->append(wxString::FromUTF8(str.data() + posStart, len));
return *this;
}
tt_wxString& tt_wxString::assign_view(std::string_view str, size_t posStart, size_t len)
{
if (str.empty())
{
clear();
return *this;
}
if (posStart >= str.size())
{
ASSERT_MSG(posStart < str.size(), "invalid starting position for append_view");
return *this;
}
if (len == tt::npos)
len = str.size() - posStart;
this->assign(wxString::FromUTF8(str.data() + posStart, len));
return *this;
}
size_t tt_wxString::find_oneof(std::string_view set) const
{
if (set.empty())
{
return npos;
}
#if defined(_WIN32) && !(wxUSE_UNICODE_UTF8)
auto wset = tt::utf8to16(set);
auto found = std::wcspbrk(c_str(), wset.c_str());
#else
std::string wset(set);
auto found = std::strpbrk(c_str(), wset.c_str());
#endif // _WIN32
if (!found)
{
return npos;
}
return (static_cast<size_t>(found - c_str()));
}
bool tt_wxString::is_sameas(std::string_view str, tt::CASE checkcase) const
{
return tt::is_sameas(utf8_string(), str, checkcase);
}
tt_wxString& tt_wxString::backslashestoforward()
{
for (auto pos = find('\\'); pos != wxString::npos; pos = find('\\'))
{
replace(pos, 1, "/");
}
return *this;
}
tt_wxString& tt_wxString::forwardslashestoback()
{
for (auto pos = find('/'); pos != tt::npos; pos = find('/'))
{
replace(pos, 1, "\\");
}
return *this;
}
void tt_wxString::erase_from(char ch)
{
if (auto pos = find(ch); pos != npos)
{
erase(pos);
}
}
void tt_wxString::erase_from(std::string_view sub)
{
#if defined(_WIN32) && !(wxUSE_UNICODE_UTF8)
auto pos = find(tt::utf8to16(sub));
#else
auto pos = find(sub.data(), 0, sub.size());
#endif // _WIN32
if (pos != npos)
{
erase(pos);
}
}
void tt_wxString::erase_from_wx(const wxString& sub)
{
if (auto pos = find(sub); pos != npos)
{
erase(pos);
}
}
size_t tt_wxString::replace_view(std::string_view oldtext, std::string_view newtext, bool replace_all)
{
return Replace(wxString::FromUTF8(oldtext.data(), oldtext.size()), wxString::FromUTF8(newtext.data(), newtext.size()),
replace_all);
}
tt_wxString& tt_wxString::replace_extension(std::string_view newExtension)
{
if (empty())
{
if (newExtension.empty())
return *this;
if (newExtension.at(0) != '.')
*this = '.';
append_view(newExtension);
return *this;
}
auto pos_file = find_filename();
if (!tt::is_found(pos_file))
pos_file = 0;
if (auto pos = find_last_of('.'); is_found(pos) && pos > pos_file)
{
// If the string only contains . or .. then it is a folder
if (pos == 0 || (pos == 1 && at(0) != '.'))
return *this; // can't add an extension if it isn't a valid filename
if (newExtension.empty())
{
// If the new extension is empty, then just erase the old extension.
erase(pos);
}
else
{
erase(pos);
if (newExtension.at(0) != '.')
*this += '.';
append_view(newExtension);
}
}
else if (newExtension.size())
{
// Current filename doesn't have an extension, so append the new one
if (newExtension.at(0) != '.')
*this += '.';
append_view(newExtension);
}
return *this;
}
tt_wxString& tt_wxString::replace_extension_wx(const wxString& newExtension)
{
if (empty())
{
if (newExtension.empty())
return *this;
if (newExtension.at(0) != '.')
*this = '.';
append(newExtension);
return *this;
}
auto pos_file = find_filename();
if (!tt::is_found(pos_file))
pos_file = 0;
if (auto pos = find_last_of('.'); is_found(pos) && pos > pos_file)
{
// If the string only contains . or .. then it is a folder
if (pos == 0 || (pos == 1 && at(0) != '.'))
return *this; // can't add an extension if it isn't a valid filename
if (newExtension.empty())
{
// If the new extension is empty, then just erase the old extension.
erase(pos);
}
else
{
erase(pos);
if (newExtension.at(0) != '.')
*this += '.';
append(newExtension);
}
}
else if (newExtension.size())
{
// Current filename doesn't have an extension, so append the new one
if (newExtension.at(0) != '.')
*this += '.';
append(newExtension);
}
return *this;
}
tt_wxString& tt_wxString::replace_filename(std::string_view newFilename)
{
if (empty())
{
assign_view(newFilename);
return *this;
}
auto pos = find_last_of('/');
#if defined(_WIN32)
// Windows filenames can contain both forward and back slashes, so check for a backslash as well.
auto back = find_last_of('\\');
if (back != npos)
{
// If there is no forward slash, or the backslash appears after the forward slash, then use it's position.
if (pos == npos || back > pos)
pos = back;
}
#endif
if (pos == npos)
{
pos = find_last_of(':');
if (pos == npos)
{
// If we get here, we think the entire current string is a filename.
assign_view(newFilename);
return *this;
}
}
erase(pos + 1);
if (newFilename.size())
append_view(newFilename);
return *this;
}
tt_wxString& tt_wxString::replace_filename_wx(const wxString& newFilename)
{
if (empty())
{
assign(newFilename);
return *this;
}
auto pos = find_last_of('/');
#if defined(_WIN32)
// Windows filenames can contain both forward and back slashes, so check for a backslash as well.
auto back = find_last_of('\\');
if (back != npos)
{
// If there is no forward slash, or the backslash appears after the forward slash, then use it's position.
if (pos == npos || back > pos)
pos = back;
}
#endif
if (pos == npos)
{
pos = find_last_of(':');
if (pos == npos)
{
// If we get here, we think the entire current string is a filename.
assign(newFilename);
return *this;
}
}
erase(pos + 1);
if (newFilename.size())
append(newFilename);
return *this;
}
tt_wxString& tt_wxString::append_filename(std::string_view filename)
{
if (filename.empty())
return *this;
if (empty())
{
assign_view(filename);
return *this;
}
auto last = Last();
if (last != '/' && last != '\\')
*this += '/';
append_view(filename);
return *this;
}
tt_wxString& tt_wxString::append_filename_wx(const wxString& filename)
{
if (filename.empty())
return *this;
if (empty())
{
assign(filename);
return *this;
}
auto last = Last();
if (last != '/' && last != '\\')
*this += '/';
append(filename);
return *this;
}
tt_wxString& tt_wxString::assign_path(std::filesystem::path path)
{
#ifdef _WIN32
assign(tt::utf16to8(path.wstring()));
#else
assign(path.string());
#endif
return *this;
}
std::filesystem::path tt_wxString::make_path() const
{
return fs::path(CHAR8_T_CAST utf8_string().c_str());
}
tt_wxString& tt_wxString::make_absolute()
{
wxFileName file(*this);
file.MakeAbsolute();
assign(file.GetFullPath());
return *this;
}
tt_wxString& tt_wxString::make_relative_wx(const wxString& pathBase)
{
wxFileName file(*this);
file.MakeRelativeTo(pathBase);
assign(file.GetFullPath());
return *this;
}
tt_wxString& tt_wxString::make_relative(std::string_view pathBase)
{
wxFileName file(*this);
file.MakeRelativeTo(tt_wxString(pathBase));
assign(file.GetFullPath());
return *this;
}
std::filesystem::file_time_type tt_wxString::last_write_time() const
{
fs::path path(CHAR8_T_CAST utf8_string().c_str());
return fs::last_write_time(path, tt::error_code);
}
std::uintmax_t tt_wxString::file_size() const
{
fs::path path(CHAR8_T_CAST utf8_string().c_str());
return fs::file_size(path, tt::error_code);
}
bool tt_wxString::ChangeDir(bool is_dir) const
{
if (empty())
return false;
if (is_dir)
return wxFileName::SetCwd(*this);
else
{
tt_wxString tmp(*this);
tmp.remove_filename();
return wxFileName::SetCwd(tmp);
}
}
tt_wxString tt_wxString::find_file(const tt_wxString& dir, const tt_wxString& filename)
{
auto dir_iterator = std::filesystem::recursive_directory_iterator(dir.utf8_string());
for (auto& entry: dir_iterator)
{
if (entry.is_regular_file())
{
if (entry.path().filename() == filename.utf8_string())
{
return entry.path().string();
}
}
}
return wxEmptyString;
}
std::string tt_wxString::sub_find_nonspace(size_t start) const
{
return sub_cstr(find_nonspace(start));
}
std::string tt_wxString::sub_find_space(size_t start) const
{
return sub_cstr(find_space(start));
}
std::string tt_wxString::sub_stepover(size_t start) const
{
return sub_cstr(stepover(start));
}
tt_string_view tt_wxString::subview(size_t start) const
{
if (static_cast<ptrdiff_t>(start) == -1)
start = length();
assert(start <= length());
return std::string_view(ToStdString().c_str() + start, length() - start);
}