aboutsummaryrefslogtreecommitdiff
path: root/src/tracking/path_matcher.cc
blob: c136565adcdb8449a9613e71334259336d7cb968 (plain)
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
#include "path_matcher.h"

#include <algorithm>

#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>

namespace tracking {

PathMatcher::PathMatcher(const std::string& source_file_path) {
	try {
		boost::filesystem::ifstream file(source_file_path);

		if ( file.is_open() ) {
			std::string current_line;

			while ( std::getline(file, current_line) ) {
				if ( current_line[0] != '#' ) { // i.e. not a comment
					try {
						this->patterns_.emplace_back(current_line);
					} catch ( const std::regex_error& ) { }
				}
			}
		}
	} catch ( boost::filesystem::filesystem_error& ) {
		// invalid source path is not relevant as we can easily fall back to declining
		// all candidate paths.
	}
}

bool PathMatcher::isMatching(const std::string& candidate) const {
	return std::any_of(
		this->patterns_.begin(),
		this->patterns_.end(),
		[&candidate](const std::regex& pattern) -> bool {
			return std::regex_match(candidate, pattern);
		}
	);
}

}