5
5
#include < exception>
6
6
#include < iostream>
7
7
#include < fstream>
8
+ #include < regex>
8
9
9
10
namespace fs = std::filesystem;
10
11
@@ -14,11 +15,13 @@ namespace tsm {
14
15
TimestampMapper::TimestampMapper (const std::filesystem::path& inputDir,
15
16
const std::filesystem::path& outputDir,
16
17
const std::string& datasetName,
18
+ const std::string& regexPattern,
17
19
const ds::DATASET_TYPE datasetType,
18
20
const bool regenIndices) :
19
21
m_inputDir{ sanitizeDirectoryPath (inputDir) },
20
22
m_outputDir{ sanitizeDirectoryPath (outputDir) },
21
23
m_datasetName{ datasetName },
24
+ m_regexPattern{ regexPattern },
22
25
m_datasetType{ datasetType },
23
26
m_regenIndices{ regenIndices },
24
27
m_filesToIndexPath{ m_inputDir / " files_to_index.txt" },
@@ -47,7 +50,7 @@ bool TimestampMapper::exec() {
47
50
}
48
51
49
52
std::cout << " Creating list of all .nc files in " << (m_indexFileExists ? m_filesToIndexPath : m_inputDir) << " ..." << std::endl;
50
- const auto & filePaths{ createFileList (m_indexFileExists ? m_filesToIndexPath : m_inputDir) };
53
+ const auto & filePaths{ createFileList (m_indexFileExists ? m_filesToIndexPath : m_inputDir, m_regexPattern ) };
51
54
if (filePaths.empty ()) {
52
55
std::cout << " No .nc files found." << " \n Exiting..." << std::endl;
53
56
return false ;
@@ -90,7 +93,7 @@ bool TimestampMapper::createDirectory(const std::filesystem::path& path) const n
90
93
}
91
94
92
95
/* **********************************************************************************/
93
- std::vector<fs::path> TimestampMapper::createFileList (const std::filesystem::path& inputDirOrIndexFile) const {
96
+ std::vector<fs::path> TimestampMapper::createFileList (const std::filesystem::path& inputDirOrIndexFile, const std::string& regex ) const {
94
97
std::vector<fs::path> paths;
95
98
96
99
// If file_to_index.txt exists, pull the file paths from there.
@@ -114,12 +117,24 @@ std::vector<fs::path> TimestampMapper::createFileList(const std::filesystem::pat
114
117
115
118
using recursive_directory_iterator = std::filesystem::recursive_directory_iterator;
116
119
const auto options{fs::directory_options::follow_directory_symlink};
120
+
121
+ try {
122
+ const std::regex r (regex);
117
123
118
- for (const auto & file : recursive_directory_iterator (inputDirOrIndexFile, options)) {
119
- if (fs::path (file).extension () == " .nc" ) {
120
- paths.emplace_back (file);
124
+ for (const auto & file : recursive_directory_iterator (inputDirOrIndexFile, options)) {
125
+ if (fs::path (file).extension () == " .nc" && std::regex_match (fs::path (file).string (), r)) {
126
+ paths.emplace_back (file);
127
+ }
121
128
}
122
129
}
130
+ catch (const std::regex_error& e) {
131
+ std::cerr << " Regex error: " << e.what () << std::endl;
132
+ std::exit (EXIT_FAILURE);
133
+ }
134
+ catch (...) {
135
+ std::cerr << " Caught unknown exception." << std::endl;
136
+ std::exit (EXIT_FAILURE);
137
+ }
123
138
124
139
return paths;
125
140
}
0 commit comments