-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfilefunc.cpp
436 lines (400 loc) · 10.9 KB
/
filefunc.cpp
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
#include "filefunc.h"
#include <cctype>
#include <chrono>
#include <filesystem>
//#include <format>
#include <fstream>
#include <functional>
#include <sstream>
#ifdef _WIN32
#include <windows.h>
// include windows.h before stringapiset.h
#include <stringapiset.h> //for MultiByteToWideChar() and WideCharToMultiByte()
#else
#include "dirent.h"
#ifndef __APPLE__
//#include <sys/io.h>
#endif
#include <sys/uio.h>
#include <unistd.h>
#define _mkdir(p) mkdir(p, S_IRWXU)
#endif
#ifdef __GNUC__
#include <sys/stat.h>
#include <sys/types.h>
#endif
namespace
{
std::wstring str2wstr(const std::string& str)
{
int wlen = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), nullptr, 0);
std::wstring wstr;
wstr.resize(wlen);
MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), wstr.data(), wlen);
return wstr;
}
}
bool filefunc::fileExist(const std::string& name)
{
#ifdef _MSC_VER
return std::filesystem::is_regular_file(str2wstr(name));
#endif
return std::filesystem::is_regular_file(name);
}
bool filefunc::pathExist(const std::string& name)
{
#ifdef _MSC_VER
return std::filesystem::is_directory(str2wstr(name));
#endif
return std::filesystem::is_directory(name);
}
std::vector<char> filefunc::readFile(const std::string& filename, int length)
{
std::ifstream ifs(filename, std::fstream::binary);
if (!ifs) { return {}; }
if (length <= 0)
{
ifs.seekg(0, std::ios::end);
length = ifs.tellg();
ifs.seekg(0, std::ios::beg);
}
std::vector<char> buffer(length);
buffer.assign((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
return buffer;
}
int filefunc::writeFile(const char* data, int length, const std::string& filename)
{
if (filename.empty()) { return 0; }
std::ofstream ofs(filename, std::fstream::binary);
ofs.write(data, length);
return length;
}
int filefunc::writeFile(const std::vector<char>& data, const std::string& filename)
{
return writeFile(data.data(), data.size(), filename);
}
std::string filefunc::readFileToString(const std::string& filename)
{
std::ifstream ifs(filename, std::fstream::binary);
if (!ifs) { return {}; }
std::string str;
str.assign((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
return str;
}
int filefunc::writeStringToFile(const std::string& str, const std::string& filename)
{
if (filename.empty()) { return 0; }
std::ofstream ofs(filename, std::fstream::binary);
ofs << str;
return str.size();
}
bool filefunc::is_path_char(char c)
{
#ifdef _WIN32
return c == '\\' || c == '/';
#else
return c == '/';
#endif
}
char filefunc::get_path_char()
{
#ifdef _WIN32
return '\\';
#else
return '/';
#endif
}
size_t filefunc::getLastPathCharPos(const std::string& filename, int utf8)
{
#ifndef _WIN32
utf8 = 1;
#endif
size_t pos = std::string::npos;
if (utf8 == 0)
{
//ansi
for (int i = 0; i < filename.size(); i++)
{
if (utf8 == 0 && uint8_t(filename[i]) >= 128)
{
i++;
}
else if (is_path_char(filename[i]))
{
pos = i;
}
}
}
else
{
for (auto i = filename.size(); i--;)
{
if (is_path_char(filename[i]))
{
pos = i;
break;
}
}
}
return pos;
}
size_t filefunc::getLastEftPathCharPos(const std::string& filename, int utf8)
{
#ifndef _WIN32
utf8 = 1;
#endif
size_t pos = std::string::npos;
//avoid mistakes for continued path char
if (utf8 == 0)
{
//ansi
bool found = false;
size_t pos1 = std::string::npos;
for (int i = 0; i < filename.size(); i++)
{
if (is_path_char(filename[i]))
{
if (!found)
{
pos1 = i;
found = true;
}
}
else
{
pos = pos1;
found = false;
if (uint8_t(filename[i]) >= 128)
{
i++;
}
}
}
}
else
{
bool found_not_path = false;
int found_not_path_count = 0;
for (auto i = filename.size(); i--;)
{
if (is_path_char(filename[i]))
{
found_not_path = false;
}
else
{
if (!found_not_path)
{
found_not_path_count++;
if (found_not_path_count == 2)
{
return i + 1;
}
}
found_not_path = true;
}
}
}
return pos;
}
std::vector<std::string> filefunc::getFilesInPath(const std::string& pathname, int recursive /*= 0*/, int include_path /*= 0*/, int absolute_path /*= 0*/)
{
if (!pathname.empty() && !std::filesystem::is_directory(pathname.c_str())) { return {}; }
std::vector<std::string> ret;
std::filesystem::directory_entry path0;
if (pathname.empty())
{
path0.assign(".");
}
else
{
path0.assign(pathname);
}
std::function<void(std::filesystem::path)> getFiles = [&](std::filesystem::path path)
{
for (auto const& dir_entry : std::filesystem::directory_iterator{ path })
{
if (std::filesystem::is_directory(dir_entry.path()))
{
if (recursive == 1)
{
getFiles(dir_entry.path());
}
if (include_path == 1)
{
if (absolute_path)
{
ret.push_back(std::filesystem::absolute(dir_entry.path()).string());
}
else
{
ret.push_back(std::filesystem::relative(dir_entry.path(), path0).string());
}
}
}
else
{
if (absolute_path)
{
ret.push_back(std::filesystem::absolute(dir_entry.path()).string());
}
else
{
ret.push_back(std::filesystem::relative(dir_entry.path(), path0).string());
}
}
}
};
getFiles(path0);
return ret;
}
std::string filefunc::getFileTime(const std::string& filename, const std::string& format)
{
if (!fileExist(filename)) { return ""; }
auto t = std::filesystem::last_write_time(filename);
auto t1 = std::chrono::time_point_cast<std::chrono::system_clock::duration>(t - std::filesystem::file_time_type::clock::now() + std::chrono::system_clock::now());
auto t2 = std::chrono::system_clock::to_time_t(t1);
char buf[64] = {};
strftime(buf, sizeof(buf), format.c_str(), localtime(&t2));
return buf;
}
void filefunc::changePath(const std::string& path)
{
if (!path.empty())
{
std::filesystem::current_path(path);
}
}
std::string filefunc::getCurrentPath()
{
return std::filesystem::current_path().string();
}
void filefunc::makePath(const std::string& path)
{
if (!path.empty())
{
std::filesystem::create_directories(path);
}
}
void filefunc::copyFile(const std::string& src, const std::string& dst)
{
std::filesystem::copy_file(src, dst, std::filesystem::copy_options::overwrite_existing);
}
void filefunc::removeFile(const std::string& filename)
{
std::filesystem::remove_all(filename);
}
std::string filefunc::getRelativePath(const std::string& filename, const std::string& basepath)
{
return std::filesystem::relative(filename, basepath).string();
}
std::string filefunc::getFileExt(const std::string& filename)
{
auto ext = std::filesystem::path(filename).extension().string();
if (!ext.empty() && ext[0] == '.')
{
ext = ext.substr(1);
}
return ext;
}
//find the last point as default, and find the first when mode is 1
std::string filefunc::getFileMainName(const std::string& filename)
{
auto name = std::filesystem::path(filename).parent_path().string();
if (!name.empty() && !is_path_char(name.back()))
{
name += get_path_char();
}
return name + std::filesystem::path(filename).stem().string();
}
std::string filefunc::getFilenameWithoutPath(const std::string& filename)
{
return std::filesystem::path(filename).filename().string();
}
std::string filefunc::getFileMainNameWithoutPath(const std::string& filename)
{
return std::filesystem::path(filename).stem().string();
}
std::string filefunc::changeFileExt(const std::string& filename, const std::string& ext)
{
auto e = ext;
if (!e.empty() && e[0] != '.')
{
e = "." + e;
}
return getFileMainName(filename) + e;
}
std::string filefunc::getParentPath(const std::string& filename, int utf8)
{
if (filename.size() > 0 && is_path_char(filename.back()))
{
return std::filesystem::path(filename).parent_path().parent_path().string();
}
else
{
return std::filesystem::path(filename).parent_path().string();
}
}
std::string filefunc::getFilePath(const std::string& filename, int utf8)
{
return getParentPath(filename, utf8);
}
std::string filefunc::toLegalFilename(const std::string& filename, int allow_path)
{
std::string f = filename, chars = " *<>?|:";
if (!allow_path)
{
chars += "/\\";
}
for (char i = 31; i >= 0; i--)
{
chars += i;
}
size_t pos = 0;
#ifdef _WIN32
if (allow_path && f.size() >= 2 && f[1] == ':')
{
pos = 2;
}
#endif
while ((pos = f.find_first_of(chars, pos)) != std::string::npos)
{
f[pos] = '_';
}
return f;
}
std::string filefunc::getAbsolutePath(const std::string& filename)
{
return std::filesystem::absolute(filename).string();
}
bool filefunc::compareNature(const std::string& a, const std::string& b)
{
if (a.empty() && b.empty()) { return false; }
if (a.empty()) { return true; }
if (b.empty()) { return false; }
auto is_digit = [](char c) -> bool
{
return c >= '0' && c <= '9';
};
if (is_digit(a[0]) && !is_digit(b[0])) { return true; }
if (!is_digit(a[0]) && is_digit(b[0])) { return false; }
if (!is_digit(a[0]) && !is_digit(b[0]))
{
if (std::toupper(a[0]) == std::toupper(b[0])) { return compareNature(a.substr(1), b.substr(1)); }
{
return (std::toupper(a[0]) < std::toupper(b[0]));
}
}
// Both strings begin with digit --> parse both numbers
std::istringstream issa(a);
std::istringstream issb(b);
int ia, ib;
issa >> ia;
issb >> ib;
if (ia != ib) { return ia < ib; }
// Numbers are the same --> remove numbers and recurse
std::string anew, bnew;
std::getline(issa, anew);
std::getline(issb, bnew);
return (compareNature(anew, bnew));
}