forked from PolusAI/filepattern
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilepattern.cpp
More file actions
199 lines (151 loc) · 6.41 KB
/
Copy pathfilepattern.cpp
File metadata and controls
199 lines (151 loc) · 6.41 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
#include "filepattern.hpp"
#include "../util/util.hpp"
#include <chrono>
FilePatternObject::FilePatternObject(const std::string& path, const std::string& file_pattern, bool recursive, bool suppress_warnings, bool sorted) {
this->setSuppressWarnings(suppress_warnings);
if(file_pattern == ""){
this->getPathFromPattern(path); // set path and file_pattern
try {
this->recursive_iterator_ = fs::recursive_directory_iterator(fs::path(this->getPath()));
this->recursive_ = true;
this->setJustPath(true);
} catch (const std::runtime_error& e) {
std::string error = "No directory found. Invalid path \"" + path + "\".";
throw std::runtime_error(error);
}
} else {
this->recursive_ = recursive; // Iterate over subdirectories
this->setCaptureDirectoryNames(false);
// check if filepattern contains directory capturing
if (s::isPath(file_pattern)) {
this->setCaptureDirectoryNames(true);
this->recursive_ = true; // need to be recursive to capture directory names
this->setFilePattern(s::escapeForwardSlashes(file_pattern));
} else {
this->setFilePattern(file_pattern); // cast input string to regex
}
this->setJustPath(false);
this->setPath(path); // store path to target directory
try {
if(this->recursive_){
this->recursive_iterator_ = fs::recursive_directory_iterator(fs::path(this->getPath()));
} else{
this->iterator_ = fs::directory_iterator(fs::path(this->getPath())); // store iterator for target directory
}
} catch (const std::runtime_error& e) {
std::string error = "No directory found. Invalid path \"" + path + "\".";
throw std::runtime_error(error);
}
}
this->setRegexFilePattern(""); // Regex version of pattern
this->matchFiles();
this->setIsSorted(sorted);
if (isSorted()) {
this->sortFiles();
}
}
void FilePatternObject::matchFilesOneDir(){
Map mapping;
std::vector<std::string> parsed_regex;
std::string s;
std::string file, file_path;
Tuple member;
// Iterate over every file in directory
// check if bracket expression was not properly parsed
if (this->getRegexFilePattern().find('{') != std::string::npos || this->getRegexFilePattern().find('}') != std::string::npos) {
auto start = this->getRegexFilePattern().find('{');
auto end = this->getRegexFilePattern().find('}');
auto length = end - start;
throw std::invalid_argument("Invalid pattern found in bracket expressions in filepattern: \"" + this->getRegexFilePattern().substr(start, length+1) + "\"");
}
std::regex pattern_regex = std::regex(this->getRegexFilePattern());
std::smatch sm;
for (const auto& entry : this->iterator_) {
// Get the current file
file_path = entry.path().string();
replace(file_path.begin(), file_path.end(), '\\', '/');
file = s::getBaseName(file_path);
if(regex_match(file, sm, pattern_regex)){
this->valid_files_.push_back(getVariableMap(file_path, sm)); // write to txt file
}
}
}
void FilePatternObject::matchFilesMultDir(){
std::regex pattern_regex = std::regex(this->getRegexFilePattern());
Tuple tup;
std::smatch sm;
std::string file, file_path;
bool is_pushed = false;
// Iterate over directories and subdirectories
for (const auto& entry : this->recursive_iterator_) {
file_path = entry.path().string();
replace(file_path.begin(), file_path.end(), '\\', '/'); // escape slashes for regex
if(this->getJustPath() || this->captureDirectoryNames()){
file = s::eraseSubStr(file_path, this->getPath());
} else {
file = s::getBaseName(file_path);
}
if(regex_match(file, sm, pattern_regex)){
if(this->getJustPath() || this->captureDirectoryNames()) {
tup = getVariableMap(file_path, sm);
} else {
tup = getVariableMapMultDir(file_path, sm);
}
if(std::get<0>(tup).size() > 0){
this->valid_files_.push_back(tup);
is_pushed = true;
} else {
is_pushed = false;
}
}
}
if (!is_pushed && std::get<1>(tup).size() > 0) {
this->valid_files_.push_back(tup);
}
}
void FilePatternObject::matchFiles() {
filePatternToRegex(); // Get regex of filepattern
if(this->recursive_){
this->matchFilesMultDir();
} else {
this->matchFilesOneDir();
}
}
ArrayPattern::ArrayPattern(const std::vector<std::string>& file_array, const std::string& pattern, bool suppress_warnings, bool sorted){
this->setSuppressWarnings(suppress_warnings);
this->setFilePattern(pattern);
this->setRegexFilePattern(""); // Regex version of pattern
this->matchFiles(file_array);
this->setIsSorted(sorted);
if (isSorted()) {
this->sortFiles();
}
};
void ArrayPattern::matchFiles(const std::vector<std::string>& file_array) {
filePatternToRegex(); // Get regex of filepattern
Map mapping;
std::vector<std::string> parsed_regex;
std::string s;
std::string file, file_path;
Tuple member;
// Iterate over every file in directory
// check if bracket expression was not properly parsed
if (this->getRegexFilePattern().find('{') != std::string::npos || this->getRegexFilePattern().find('}') != std::string::npos) {
auto start = this->getRegexFilePattern().find('{');
auto end = this->getRegexFilePattern().find('}');
auto length = end - start;
throw std::invalid_argument("Invalid pattern found in bracket expressions in filepattern: \"" + this->getRegexFilePattern().substr(start, length+1) + "\"");
}
std::regex pattern_regex = std::regex(this->getRegexFilePattern());
std::smatch sm;
// iterate over file array and add valid files
for (const auto& path : file_array) {
// Get the current file
auto file_path = path;
std::replace(file_path.begin(), file_path.end(), '\\', '/');
file = s::getBaseName(file_path);
if(regex_match(file, sm, pattern_regex)){
this->valid_files_.push_back(getVariableMap(file_path, sm)); // write to txt file
}
}
};