Filesystem.h 2.59 KB
Newer Older
1
2
3
4
5
6
// 文件以及目录处理

#ifndef __FILE_SYSTEM_H__
#define __FILE_SYSTEM_H__

#include <string>
liucong's avatar
liucong committed
7
#include <vector>
liucong's avatar
liucong committed
8
9

namespace migraphxSamples {
10
11

// 路径是否存在
liucong's avatar
liucong committed
12
bool Exists(const std::string& path);
13
14

// 路径是否为目录
liucong's avatar
liucong committed
15
bool IsDirectory(const std::string& path);
16
17
18
19
20

// 是否是路径分隔符(Linux:‘/’,Windows:’\\’)
bool IsPathSeparator(char c);

// 路径拼接
liucong's avatar
liucong committed
21
std::string JoinPath(const std::string& base, const std::string& path);
22
23

// 创建多级目录,注意:创建多级目录的时候,目标目录是不能有文件存在的
liucong's avatar
liucong committed
24
bool CreateDirectories(const std::string& directoryPath);
25
26

/** 生成符合指定模式的文件名列表(支持递归遍历)
liucong's avatar
liucong committed
27
*
28
29
30
31
* pattern: 模式,比如"*.jpg","*.png","*.jpg,*.png"
* addPath:是否包含父路径
* 注意:
    1. 多个模式使用","分割,比如"*.jpg,*.png"
liucong's avatar
liucong committed
32
33
    2. 支持通配符'*','?' ,比如第一个字符是7的所有文件名:"7*.*",
以512结尾的所有jpg文件名:"*512.jpg"
34
35
36
37
38
    3. 使用"*.jpg",而不是".jpg"
    4. 空string表示返回所有结果
    5. 不能返回子目录名
*
*/
liucong's avatar
liucong committed
39
40
41
42
43
void GetFileNameList(const std::string& directory,
                     const std::string& pattern,
                     std::vector<std::string>& result,
                     bool recursive,
                     bool addPath);
44
45

// 与GetFileNameList的区别在于如果有子目录,在addPath为true的时候会返回子目录路径(目录名最后有"/")
liucong's avatar
liucong committed
46
47
48
49
50
void GetFileNameList2(const std::string& directory,
                      const std::string& pattern,
                      std::vector<std::string>& result,
                      bool recursive,
                      bool addPath);
51
52

// 删除文件或者目录,支持递归删除
liucong's avatar
liucong committed
53
void Remove(const std::string& directory, const std::string& extension = "");
54
55

/** 获取路径的文件名和扩展名
liucong's avatar
liucong committed
56
 *
57
 *  示例:path为D:/1/1.txt,则GetFileName()为1.txt,GetFileName_NoExtension()为1,GetExtension()为.txt,GetParentPath()为D:/1/
liucong's avatar
liucong committed
58
59
60
61
62
 */
std::string GetFileName(const std::string& path);
std::string GetFileName_NoExtension(const std::string& path);
std::string GetExtension(const std::string& path);
std::string GetParentPath(const std::string& path);
63

liucong's avatar
liucong committed
64
// 拷贝文件
liucong's avatar
liucong committed
65
bool CopyFile(const std::string srcPath, const std::string dstPath);
66
67

/** 拷贝目录
liucong's avatar
liucong committed
68
 *
69
70
71
72
73
 * 示例:CopyDirectories("D:/0/1/2/","E:/3/");实现把D:/0/1/2/目录拷贝到E:/3/目录中(即拷贝完成后的目录结构为E:/3/2/)
 * 注意:
    1.第一个参数的最后不能加”/”
    2.不能拷贝隐藏文件
*/
liucong's avatar
liucong committed
74
bool CopyDirectories(std::string srcPath, const std::string dstPath);
75

liucong's avatar
liucong committed
76
} // namespace migraphxSamples
77
78

#endif