-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.cpp
More file actions
executable file
·547 lines (467 loc) · 14.9 KB
/
File.cpp
File metadata and controls
executable file
·547 lines (467 loc) · 14.9 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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#include "File.h"
#include <fstream>
void sepReplace(std::string &sPath)
{
int iPos = 0;
while (sPath.find('\\') != sPath.npos) {
iPos = sPath.find('\\', iPos);
sPath = sPath.replace(iPos, 1, "/");
}
if(sPath.back() == '/')
sPath.pop_back();
}
int isFileOrDir(const std::string &sPath)
{
intptr_t handle;
_finddata_t findData;
handle = _findfirst(sPath.c_str(), &findData);
if(findData.attrib & _A_SUBDIR)
return 2;
else
return 1;
_findclose(handle);
}
void getMultiLayerDirFromStr(const std::string &str, std::vector<std::string> &vSubDirs)
{
std::string sOriStrCp = str;
// 将原始字符串的"\\"替换成"/"
sepReplace(sOriStrCp);
// 将字符串进行分割
std::vector<std::string> vTokenRes;
strToken(sOriStrCp, '/', vTokenRes);
// 将分割后的结果,按照逐步累加的方式,存储到vSubDirs中
vSubDirs.push_back(vTokenRes.front());
for(int i = 1; i < (int)vTokenRes.size(); i++)
{
vSubDirs.push_back(vSubDirs[i - 1] + "/" + vTokenRes[i]);
}
}
void strToken(const std::string& str, const char cSep, std::vector<std::string>& vTokenRes)
{
int iPos = 0;
while (str.find(cSep, iPos) != str.npos) {
vTokenRes.push_back(str.substr(iPos, str.find(cSep, iPos)));
iPos = str.find(cSep, iPos) + 1;
}
vTokenRes.push_back(str.substr(iPos));
}
File::File(const std::string& sFilePath):
m_sFilePath(sFilePath)
{
m_iIsFileOrDir = isFileOrDir(m_sFilePath);
}
File::~File()
{
}
void File::setFilePath(const std::string &sFilePath)
{
this->m_sFilePath = sFilePath;
sepReplace(m_sFilePath);
}
std::string File::fileName()
{
return m_sFilePath.substr(m_sFilePath.find_last_of('/') + 1);
}
const std::string& File::filePath()
{
return m_sFilePath;
}
bool File::isFile()
{
return m_iIsFileOrDir == 1;
}
bool File::isDir()
{
return m_iIsFileOrDir == 2;
}
Dir File::parentDir()
{
Dir pD;
pD.setFilePath(this->m_sFilePath.substr(0, m_sFilePath.find_last_of('/')));
return pD;
}
bool File::touch(const std::string &sFileName)
{
std::string sDirPath = this->parentDir().filePath();
std::string sNewFilePath;
sNewFilePath = sDirPath + std::string("/") + std::string(sFileName);
/* _access函数说明:(参考链接:https://blog.csdn.net/monk1992/article/details/81906013)
* 头文件:<io.h>
* 函数原型:int _access(const char *pathname, int mode);
* 参数:pathname 为文件路径或目录路径 mode 为访问权限(在不同系统中可能用不能的宏定义重新定义)
* 返回值:如果文件具有指定的访问权限,则函数返回0;如果文件不存在或者不能访问指定的权限,则返回-1.
* 备注:当pathname为文件时,_access函数判断文件是否存在,并判断文件是否可以用mode值指定的模式进行访问。当pathname为目录时,_access只判断指定目录是否存在,在Windows NT和Windows 2000中,所有的目录都只有读写权限。
* mode的值和含义如下所示:
* 00——只检查文件是否存在
* 02——写权限
* 04——读权限
* 06——读写权限
*/
if(0 != _access(sDirPath.c_str(), 2))
{
printf("***ERROR***: Dest dir has no writing access.\n");
return false;
}
std::ofstream os(sNewFilePath, std::ios::out);
if(os.is_open())
{
os.close();
return true;
}
else
{
os.close();
return false;
}
}
bool File::touch(const std::string &sTarDirName, const std::string &sNewFileName)
{
std::string sParent = sTarDirName;
sepReplace(sParent);
if(0 != _access(sParent.c_str(), 2))
{
printf("***ERROR***: Dest dir has no writing access.\n");
return false;
}
std::string sNewFilePath = sParent;
// 如果sNewFileName是包含多级目录的文件,如:DirA\\DirB\\DirB\\FileA.txt;
// 则需要将该文件路径拆分开,分别创建多级目录,再创建文件
// if(sNewFileName.find('/') != sNewFileName.npos || sNewFileName.find('\\') != sNewFileName.npos)
// {
// std::vector<std::string> sSubDirs;
// getMultiLayerDirFromStr(sNewFileName, sSubDirs);
// for(int i = 0; i < (int)sSubDirs.size() - 1; i++)
// {
// Dir::mkdir(sTarDirName, sSubDirs[i]);
// }
// }
sNewFilePath.append("/").append(sNewFileName);
std::ofstream os(sNewFilePath, std::ios::out);
if(os.is_open())
{
os.close();
return true;
}
else
{
os.close();
return false;
}
}
bool File::copy(const std::string &sTarDirPath)
{
if(0 != _access(sTarDirPath.c_str(), 2))
{
printf("***ERROR***: Dest dir has no writing access.\n");
return false;
}
/* 将本文件复制到pTarDirPath下 */
// 1. 读文件,二进制方式打开
std::ifstream inFile(m_sFilePath, std::ios::in | std::ios::binary);
if(! inFile.is_open())
{
printf("***ERROR***: File open failure.\n");
inFile.close();
return false;
}
// 2. 在目标文件夹下创建新当前同名文件
// 可以做个判断该文件是否存在
bool touchRes = File::touch(sTarDirPath, this->fileName().c_str());
if(touchRes == false)
{
printf("***ERROR***: New file created to target dir failed.\n");
inFile.close();
return false;
}
std::string sNewFilePath = std::string(sTarDirPath) + "/" + this->fileName();
// 3. 打开创建的该新文件,并将数据复制到该文件中
std::ofstream outFile(sNewFilePath, std::ios::out | std::ios::binary);
if(! outFile.is_open())
{
printf("***ERROR***: Open copied new file failed.\n");
inFile.close();
outFile.close();
return false;
}
outFile << inFile.rdbuf(); // 这句话网上也是这样用的,说速度很快
inFile.close();
outFile.close();
return true;
}
bool File::copy(const std::string &sTarDirName, const std::string &sFilePath)
{
if(0 != _access(sTarDirName.c_str(), 2))
{
printf("***ERROR***: Dest dir has no writing access.\n");
return false;
}
std::ifstream inFile(sFilePath, std::ios::in | std::ios::binary);
if(! inFile.is_open())
{
printf("***ERROR***: Open file failure.\n");
inFile.close();
return false;
}
// 在目标文件夹下创建新文件
std::string sTarDir = sTarDirName;
sepReplace(sTarDir);
File oriFile(sFilePath);
bool touchRes = File::touch(sTarDir, oriFile.fileName());
if(touchRes == false)
{
printf("***ERROR***: Touch new file failure.\n");
inFile.close();
return false;
}
std::string sNewFilePath = sTarDir + "/" + oriFile.fileName();
// 3. 打开创建的该新文件,并将数据复制到该文件中
std::ofstream outFile(sNewFilePath, std::ios::out | std::ios::binary);
if(! outFile.is_open())
{
printf("***ERROR***: Open copied new file failed.\n");
inFile.close();
outFile.close();
return false;
}
outFile << inFile.rdbuf(); // 这句话网上也是这样用的,说速度很快
inFile.close();
outFile.close();
return true;
}
Dir::Dir(const std::string& sDirPath)
{
m_sFilePath = sDirPath;
sepReplace(m_sFilePath);
m_iIsFileOrDir = isFileOrDir(m_sFilePath);
}
Dir::~Dir()
{
while (! m_lstFileList.empty()) {
auto file = m_lstFileList.front();
if(file != nullptr)
{
delete file;
m_lstFileList.pop_front();
}
}
}
bool Dir::mkdir(const std::string &sNewDirName)
{
if(0 != _access(m_sFilePath.c_str(), 2))
{
printf("***ERROR***: Curent dir has no writing access.\n");
return false;
}
// 如果sNewDirName是多级子目录组成的,则对其进行拆分处理
std::vector<std::string> vSubDirs;
getMultiLayerDirFromStr(sNewDirName, vSubDirs);
for(int i = 0; i < (int)vSubDirs.size(); i++)
{
std::string sSubDirPath = sNewDirName + vSubDirs[i];
_mkdir(sSubDirPath.c_str());
}
// std::string sNewDirPath;
// sNewDirPath = m_sFilePath + std::string("/") + sNewDirName;
// int iMkdirRes = _mkdir(sNewDirPath.c_str());
return true;
}
bool Dir::mkdir(const std::string &sTarDirPath, const std::string &sNewDirName)
{
std::string sParentDirPath = sTarDirPath;
sepReplace(sParentDirPath);
if(0 != _access(sParentDirPath.c_str(), 2))
{
printf("***ERROR***: Dest dir has no writing access.\n");
return false;
}
// 如果sNewDirName是多级子目录组成的,则对其进行拆分处理
std::vector<std::string> vSubDirs;
getMultiLayerDirFromStr(sNewDirName, vSubDirs);
for(int i = 0; i < (int)vSubDirs.size(); i++)
{
std::string sSubDirPath = sTarDirPath + "/" + vSubDirs[i];
_mkdir(sSubDirPath.c_str());
}
// std::string sNewDirPath;
// sNewDirPath = sParentDirPath + std::string("/") + sNewDirName;
// int iMkdirRes = _mkdir(sNewDirPath.c_str());
return true;
}
const FileList& Dir::entry(bool bDeepTraverse)
{
if(bDeepTraverse == false)
listFiles(m_sFilePath, m_lstFileList);
else
listFiles_Deep(m_sFilePath, m_lstFileList);
return m_lstFileList;
}
FileList Dir::entry_static(const std::string& sDirPath, bool bDeepTraverse)
{
FileList list;
if(bDeepTraverse == false)
listFiles(sDirPath, list);
else
listFiles_Deep(sDirPath, list);
return list;
}
bool Dir::copy(const std::string &sTarDirPath)
{
// 判断目标文件夹是否有写权限
if(0 != _access(sTarDirPath.c_str(), 2))
{
printf("***ERROR***: Dest dir path has no writing access.\n");
return false;
}
auto curFiles = this->entry(true);
for(auto file: curFiles)
{
if(file->isFile())
{
// 将当前文件的文件路径减去当前文件夹的路径,得到当前文件相对当前文件夹的相对路径
std::string sRelPath = getRelativePath(*file, *this);
// 相对路径的前缀
std::string sPrefix;
if(sRelPath.find('/') != sRelPath.npos)
sPrefix = sRelPath.substr(0, sRelPath.find_last_of('/'));
else
sPrefix = "";
// 在目标目录下创建相对路径的前缀
if(sPrefix == "")
{
File::copy(sTarDirPath, file->filePath());
}
else
{
Dir::mkdir(sTarDirPath, sPrefix);
File::copy(sTarDirPath + "/" + sPrefix, file->filePath()); // ?????????????????????????? TODO
}
}
}
// 内存回收 —— curFiles是在entry_static中创建的,因此需要手动回收
while (! curFiles.empty()) {
auto file = curFiles.front();
if(file != nullptr)
{
delete file;
curFiles.pop_front();
}
}
return true;
}
bool Dir::copy(const std::string &sSrcDirPath, const std::string &sTarDirPath)
{
// 判断对目标文件夹是否有写权限
if(0 != _access(sTarDirPath.c_str(), 2))
{
printf("***ERROR***: Dest dir path has no writing access.\n");
return false;
}
Dir srcDir(sSrcDirPath);
// 遍历当前文件夹,得到所有的文件
auto curFiles = Dir::entry_static(sSrcDirPath, true);
for(auto file: curFiles)
{
if(file->isFile())
{
// 将当前文件的文件路径减去当前文件夹的路径,得到当前文件相对当前文件夹的相对路径
std::string sRelPath = getRelativePath(*file, srcDir);
// 相对路径的前缀
std::string sPrefix;
if(sRelPath.find('/') != sRelPath.npos)
sPrefix = sRelPath.substr(0, sRelPath.find_last_of('/'));
else
sPrefix = "";
// 在目标目录下创建相对路径的前缀
if(sPrefix == "")
{
File::copy(sTarDirPath, file->filePath());
}
else
{
Dir::mkdir(sTarDirPath, sPrefix);
File::copy(sTarDirPath + "/" + sPrefix, file->filePath()); // ?????????????????????????? TODO
}
}
}
// 内存回收 —— curFiles是在entry_static中创建的,因此需要手动回收
while (! curFiles.empty()) {
auto file = curFiles.front();
if(file != nullptr)
{
delete file;
curFiles.pop_front();
}
}
return true;
}
void Dir::listFiles(const std::string& sDirPath, FileList& list)
{
intptr_t handle;
_finddata_t findData;
std::string sDir = sDirPath;
sepReplace(sDir);
std::string sAddWildcardCharacter = sDir + "/*.*"; // 必须加上这种通配符,否则只循环一次
// std::string sAddWildcardCharacter = m_sFilePath + "/"; // 这种目录后加斜杠的不行,会find失败
handle = _findfirst(sAddWildcardCharacter.c_str(), &findData);
if(-1 == handle)
{
printf("***ERROR***: Find first file failure.\n");
return;
}
do {
if(findData.attrib & _A_SUBDIR)
{
if (strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0)
continue;
Dir* pDir = new Dir(sDir + "/" + findData.name);
list.push_back(pDir);
}
else
{
File* pFile = new File(sDir + "/" + findData.name);
list.push_back(pFile);
}
} while (_findnext(handle, &findData) == 0);
_findclose(handle);
}
void Dir::listFiles_Deep(const std::string &sDirPath, FileList& list)
{
intptr_t handle;
_finddata_t findData;
std::string sDir = sDirPath;
sepReplace(sDir);
std::string sAddWildcardCharacter = sDir + "/*.*"; // 必须加上这种通配符,否则只循环一次
// std::string sAddWildcardCharacter = m_sFilePath + "/"; // 这种目录后加斜杠的不行,会find失败
handle = _findfirst(sAddWildcardCharacter.c_str(), &findData);
if(-1 == handle)
{
printf("***ERROR***: Find first file failure.\n");
return;
}
do {
if(findData.attrib & _A_SUBDIR)
{
if (strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0)
continue;
Dir* pDir = new Dir(sDir + "/" + findData.name);
list.push_back(pDir);
if(pDir->fileName().back() == '.')
continue;
listFiles_Deep(sDir + "/" + findData.name, list);
}
else
{
File* pFile = new File(sDir + "/" + findData.name);
list.push_back(pFile);
}
} while (_findnext(handle, &findData) == 0);
_findclose(handle);
}
std::string getRelativePath(File &file, Dir &dir)
{
std::string sFilePath = file.filePath();
std::string sDirPath = dir.filePath();
if(sFilePath.find(sDirPath) == std::string::npos)
return "";
return sFilePath.substr(sDirPath.length() + 1);
}