forked from PolusAI/filepattern
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringpattern.cpp
More file actions
49 lines (40 loc) · 1.53 KB
/
Copy pathstringpattern.cpp
File metadata and controls
49 lines (40 loc) · 1.53 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
#include "stringpattern.hpp"
#include <fstream>
StringPattern::StringPattern(const std::string& file_name, const std::string& file_pattern, bool suppress_warnings, bool sorted) {
if (!fs::exists(file_name)) {
throw std::invalid_argument("Path \"" + file_name + "\" does not exist.");
}
this->setSuppressWarnings(suppress_warnings);
this->file_name_ = file_name; // store path to target directory
this->setFilePattern(file_pattern); // cast input string to regex
this->setRegexFilePattern("");
this->setIsSorted(sorted);
this->readFile(); // read file into memory
this->matchFiles(); // match files to pattern
if (isSorted()) {
this->sortFiles();
}
}
void StringPattern::readFile(){
std::string str;
std::ifstream in(this->file_name_);
if(!in.is_open()) {
throw std::runtime_error("File \"" + this->file_name_ + "\" not found.");
}
// read filenames into memory
while(std::getline(in, str)){
if(str.size()) this->files_.push_back(str);
}
}
void StringPattern::matchFiles(){
filePatternToRegex(); // get regex equivalent of filepattern
//string file_path;
std::regex pattern_regex = std::regex(this->getRegexFilePattern()); // convert to regex
std::smatch sm; // store matching groups
for (const auto& file_path : this->files_) {
// Get the current file
if(std::regex_match(file_path, sm, pattern_regex)){
this->valid_files_.push_back(getVariableMap(file_path, sm)); // write to txt file
}
}
}