-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirectoryrange.h
189 lines (152 loc) · 3.08 KB
/
directoryrange.h
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
#ifndef _DIRECTORY_RANGE_H_
#define _DIRECTORY_RANGE_H_
#include <cstdint>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <string>
#include <vector>
#include <dirent.h>
#ifdef _WIN32
#define SLASH "\\"
#define SLASH_CHAR '\\'
#else
#define SLASH "/"
#define SLASH_CHAR '/'
#endif
class DirectoryRange
{
private:
DIR * dp;
dirent *ep;
std::string _path;
public:
DirectoryRange(const std::string & _path, const std::string & file);
DirectoryRange(const std::string & _path);
virtual ~DirectoryRange();
bool empty() const;
bool isDirectory() const;
bool isFile() const;
bool isExtension(const std::string & ext) const;
virtual void popFront();
virtual const std::string & directory() const;
virtual std::string fullname() const;
virtual int depth() const { return 0; }
std::string path() const;
std::string syspath() const;
std::string filename() const;
std::string extension() const;
};
class RecursiveDirectoryRange : public DirectoryRange
{
typedef DirectoryRange super;
RecursiveDirectoryRange * child;
public:
RecursiveDirectoryRange(const std::string & _path, const std::string & file);
RecursiveDirectoryRange(const std::string & _path);
~RecursiveDirectoryRange();
void popFront();
int depth() const
{
if(child)
{
return child->RecursiveDirectoryRange::depth() + 1;
}
return 0;
}
#define recursive(function)\
function() const\
{\
return child? child->RecursiveDirectoryRange::function() : super::function();\
}
const std::string & recursive(directory)
std::string recursive(fullname)
#undef recursive
};
template<class T>
class FileRange : public T
{
typedef T super;
public:
FileRange(const std::string & path) :
super(path) {}
void popFront()
{
for(super::popFront(); !super::empty() && !super::isFile(); super::popFront()) ;
}
};
template<class T>
class FileTypeRange : public T
{
typedef T super;
const std::string ext;
void pop()
{
for(; !super::empty(); super::popFront())
{
if(!super::isFile())
{
continue;
}
if(super::isExtension(ext))
{
break;
}
}
}
public:
FileTypeRange(const std::string & dir, const std::string & file, const std::string & ext) :
super(dir, file),
ext(ext)
{
pop();
}
FileTypeRange(const std::string & path, const std::string & ext) :
super(path),
ext(ext)
{
pop();
}
void popFront()
{
super::popFront();
pop();
}
};
template<class T>
class FileTypesRange : public T
{
typedef T super;
const std::vector<std::string> extensions;
void pop()
{
for(; !super::empty(); super::popFront())
{
if(!super::isFile())
{
continue;
}
for(auto i = extensions.begin(); i != extensions.end(); ++i)
{
if(super::isExtension(*i))
{
return;
}
}
}
return;
}
public:
FileTypesRange(std::string path, std::initializer_list<std::string> args) :
super(path),
extensions(args)
{
pop();
}
void popFront()
{
super::popFront();
pop();
}
};
#endif // _DIRECTORY_RANGE_H_