Remove member filter argument from PathScanner methods and use normal filter for regex

Also use regfree to free memory of compiled patterns
This commit is contained in:
Tim Mayberry 2014-06-17 10:44:36 +10:00 committed by Paul Davis
parent ae22d60a42
commit 144da83bc8
2 changed files with 30 additions and 33 deletions

View file

@ -44,6 +44,14 @@ using PBD::closedir;
using namespace std; using namespace std;
using namespace PBD; using namespace PBD;
static
bool
regexp_filter (const string& str, void *arg)
{
regex_t* pattern = (regex_t*)arg;
return regexec (pattern, str.c_str(), 0, 0, 0) == 0;
}
vector<string> vector<string>
PathScanner::operator() (const string &dirpath, const string &regexp, PathScanner::operator() (const string &dirpath, const string &regexp,
bool match_fullpath, bool return_fullpath, bool match_fullpath, bool return_fullpath,
@ -52,6 +60,8 @@ PathScanner::operator() (const string &dirpath, const string &regexp,
{ {
int err; int err;
char msg[256]; char msg[256];
regex_t compiled_pattern;
vector<string> result;
if ((err = regcomp (&compiled_pattern, regexp.c_str(), if ((err = regcomp (&compiled_pattern, regexp.c_str(),
REG_EXTENDED|REG_NOSUB))) { REG_EXTENDED|REG_NOSUB))) {
@ -67,17 +77,20 @@ PathScanner::operator() (const string &dirpath, const string &regexp,
return vector<string>(); return vector<string>();
} }
return run_scan (dirpath, &PathScanner::regexp_filter, result = run_scan (dirpath,
(bool (*)(const string &, void *)) 0, regexp_filter,
0, &compiled_pattern,
match_fullpath, match_fullpath,
return_fullpath, return_fullpath,
limit, recurse); limit, recurse);
regfree (&compiled_pattern);
return result;
} }
vector<string> vector<string>
PathScanner::run_scan (const string &dirpath, PathScanner::run_scan (const string &dirpath,
bool (PathScanner::*memberfilter)(const string &),
bool (*filter)(const string &, void *), bool (*filter)(const string &, void *),
void *arg, void *arg,
bool match_fullpath, bool return_fullpath, bool match_fullpath, bool return_fullpath,
@ -85,14 +98,13 @@ PathScanner::run_scan (const string &dirpath,
bool recurse) bool recurse)
{ {
vector<string> result; vector<string> result;
run_scan_internal (result, dirpath, memberfilter, filter, arg, match_fullpath, return_fullpath, limit, recurse); run_scan_internal (result, dirpath, filter, arg, match_fullpath, return_fullpath, limit, recurse);
return result; return result;
} }
void void
PathScanner::run_scan_internal (vector<string>& result, PathScanner::run_scan_internal (vector<string>& result,
const string &dirpath, const string &dirpath,
bool (PathScanner::*memberfilter)(const string &),
bool (*filter)(const string &, void *), bool (*filter)(const string &, void *),
void *arg, void *arg,
bool match_fullpath, bool return_fullpath, bool match_fullpath, bool return_fullpath,
@ -135,7 +147,7 @@ PathScanner::run_scan_internal (vector<string>& result,
} }
if (statbuf.st_mode & S_IFDIR && recurse) { if (statbuf.st_mode & S_IFDIR && recurse) {
run_scan_internal (result, fullpath, memberfilter, filter, arg, match_fullpath, return_fullpath, limit, recurse); run_scan_internal (result, fullpath, filter, arg, match_fullpath, return_fullpath, limit, recurse);
} else { } else {
if (match_fullpath) { if (match_fullpath) {
@ -144,16 +156,8 @@ PathScanner::run_scan_internal (vector<string>& result,
search_str = finfo->d_name; search_str = finfo->d_name;
} }
/* handle either type of function ptr */ if (!filter(search_str, arg)) {
continue;
if (memberfilter) {
if (!(this->*memberfilter)(search_str)) {
continue;
}
} else {
if (!filter(search_str, arg)) {
continue;
}
} }
if (return_fullpath) { if (return_fullpath) {
@ -182,6 +186,7 @@ PathScanner::find_first (const string &dirpath,
vector<string> res; vector<string> res;
int err; int err;
char msg[256]; char msg[256];
regex_t compiled_pattern;
if ((err = regcomp (&compiled_pattern, regexp.c_str(), if ((err = regcomp (&compiled_pattern, regexp.c_str(),
REG_EXTENDED|REG_NOSUB))) { REG_EXTENDED|REG_NOSUB))) {
@ -195,13 +200,14 @@ PathScanner::find_first (const string &dirpath,
} }
run_scan_internal (res, dirpath, run_scan_internal (res, dirpath,
&PathScanner::regexp_filter, &regexp_filter,
(bool (*)(const string &, void *)) 0, &compiled_pattern,
0,
match_fullpath, match_fullpath,
return_fullpath, return_fullpath,
1); 1);
regfree (&compiled_pattern);
if (res.size() == 0) { if (res.size() == 0) {
return string(); return string();
} }
@ -221,7 +227,6 @@ PathScanner::find_first (const string &dirpath,
run_scan_internal (res, run_scan_internal (res,
dirpath, dirpath,
(bool (PathScanner::*)(const string &)) 0,
filter, filter,
0, 0,
match_fullpath, match_fullpath,

View file

@ -42,7 +42,6 @@ class LIBPBD_API PathScanner
long limit = -1, long limit = -1,
bool recurse = false) { bool recurse = false) {
return run_scan (dirpath, return run_scan (dirpath,
(bool (PathScanner::*)(const std::string &)) 0,
filter, filter,
arg, arg,
match_fullpath, match_fullpath,
@ -69,14 +68,8 @@ class LIBPBD_API PathScanner
bool return_fullpath = true); bool return_fullpath = true);
private: private:
regex_t compiled_pattern;
bool regexp_filter (const std::string &str) {
return regexec (&compiled_pattern, str.c_str(), 0, 0, 0) == 0;
}
std::vector<std::string> run_scan (const std::string &dirpath, std::vector<std::string> run_scan (const std::string &dirpath,
bool (PathScanner::*mfilter) (const std::string &),
bool (*filter)(const std::string &, void *), bool (*filter)(const std::string &, void *),
void *arg, void *arg,
bool match_fullpath, bool match_fullpath,
@ -86,7 +79,6 @@ class LIBPBD_API PathScanner
void run_scan_internal (std::vector<std::string>&, void run_scan_internal (std::vector<std::string>&,
const std::string &dirpath, const std::string &dirpath,
bool (PathScanner::*mfilter) (const std::string &),
bool (*filter)(const std::string &, void *), bool (*filter)(const std::string &, void *),
void *arg, void *arg,
bool match_fullpath, bool match_fullpath,