Filesystem.h 2.59 KB
Newer Older
Your Name's avatar
Your Name committed
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

Your Name's avatar
Your Name committed
9
10
11
12
namespace migraphxSamples
{

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

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

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

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

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

/** 生成符合指定模式的文件名列表(支持递归遍历)
liucong's avatar
liucong committed
28
*
Your Name's avatar
Your Name committed
29
30
31
32
33
34
35
36
37
38
* pattern: 模式,比如"*.jpg","*.png","*.jpg,*.png"
* addPath:是否包含父路径
* 注意:
    1. 多个模式使用","分割,比如"*.jpg,*.png"
    2. 支持通配符'*','?' ,比如第一个字符是7的所有文件名:"7*.*", 以512结尾的所有jpg文件名:"*512.jpg"
    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);
Your Name's avatar
Your Name committed
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);
Your Name's avatar
Your Name committed
51
52

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

/** 获取路径的文件名和扩展名
liucong's avatar
liucong committed
56
 *
Your Name's avatar
Your Name committed
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);
Your Name's avatar
Your Name committed
63

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

/** 拷贝目录
liucong's avatar
liucong committed
68
 *
Your Name's avatar
Your Name committed
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);
Your Name's avatar
Your Name committed
75

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

#endif