forked from burner/sweet.hpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesystem.hpp
152 lines (125 loc) · 2.95 KB
/
filesystem.hpp
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
#pragma once
#include <string>
#include <regex>
#include <memory>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
namespace sweet {
inline std::string getCurrentPath() {
char buf[PATH_MAX];
getcwd(buf, PATH_MAX);
return std::string(buf);
}
struct DirDeletor {
inline void operator()(DIR* dir) {
closedir(dir);
}
};
class File {
class FileSystemIterator {
public:
inline FileSystemIterator() : entry(nullptr), dir(nullptr) {}
inline FileSystemIterator(const File& f) : entry(nullptr), path(f.path+'/') {
if(f.isDir()) {
this->dir = std::shared_ptr<DIR>(opendir(f.path.c_str()), DirDeletor());
this->entry = readdir(this->dir.get());
}
}
inline File operator*() const {
if(this->entry) {
return File(this->path + this->entry->d_name);
} else {
return File("");
}
}
inline File operator->() const {
return this->operator*();
}
inline FileSystemIterator operator++() {
auto ret(*this);
if(this->dir) {
this->entry = readdir(this->dir.get());
}
return ret;
}
inline bool operator==(const FileSystemIterator& o) const {
return this->entry == o.entry;
}
inline bool operator!=(const FileSystemIterator& o) const {
return this->entry != o.entry;
}
private:
struct dirent* entry;
std::shared_ptr<DIR> dir;
std::string path;
};
public:
typedef FileSystemIterator iterator;
inline File() : path(getCurrentPath()) {}
inline File(const std::string& p) : path(p) {}
inline bool isDir() const {
struct stat st;
stat(path.c_str(), &st);
return S_ISDIR(st.st_mode);
}
inline bool isFile() const {
struct stat st;
stat(path.c_str(), &st);
return S_ISREG(st.st_mode);
}
inline iterator begin() {
return FileSystemIterator(*this);
}
inline iterator end() {
return FileSystemIterator();
}
std::string path;
};
enum class FileSystemFilterType {
Invalid,
File,
Directory,
FileFolder
};
struct FileSystemFilterIncrement {
inline FileSystemFilterIncrement(FileSystemFilterType t = FileSystemFilterType::Invalid) :
type(t) {}
inline FileSystemFilterIncrement(FileSystemFilterType t, const std::string& re) :
type(t), regex(re), regexStr(re) {
}
inline FileSystemFilterIncrement(const std::string& re) :
type(FileSystemFilterType::Invalid), regex(re), regexStr(re) {
}
inline File::iterator operator()(File::iterator cur, File::iterator end) {
++cur;
while(cur != end) {
switch(this->type) {
case FileSystemFilterType::FileFolder:
case FileSystemFilterType::Invalid: break;
case FileSystemFilterType::File:
if(!(*cur).isFile()) {
goto inc;
}
break;
case FileSystemFilterType::Directory:
if(!(*cur).isDir()) {
goto inc;
}
break;
default:
return File::iterator();
}
if(this->regexStr.empty() || std::regex_match((*cur).path, this->regex)) {
break;
}
inc:
++cur;
}
return cur;
}
FileSystemFilterType type;
std::regex regex;
std::string regexStr;
};
}