diff --git a/libs/pbd/pathscanner.cc b/libs/pbd/pathscanner.cc index bd010e66c5..05e90b599b 100644 --- a/libs/pbd/pathscanner.cc +++ b/libs/pbd/pathscanner.cc @@ -44,6 +44,14 @@ using PBD::closedir; using namespace std; 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 PathScanner::operator() (const string &dirpath, const string ®exp, bool match_fullpath, bool return_fullpath, @@ -52,6 +60,8 @@ PathScanner::operator() (const string &dirpath, const string ®exp, { int err; char msg[256]; + regex_t compiled_pattern; + vector result; if ((err = regcomp (&compiled_pattern, regexp.c_str(), REG_EXTENDED|REG_NOSUB))) { @@ -67,17 +77,20 @@ PathScanner::operator() (const string &dirpath, const string ®exp, return vector(); } - return run_scan (dirpath, &PathScanner::regexp_filter, - (bool (*)(const string &, void *)) 0, - 0, - match_fullpath, - return_fullpath, - limit, recurse); + result = run_scan (dirpath, + regexp_filter, + &compiled_pattern, + match_fullpath, + return_fullpath, + limit, recurse); + + regfree (&compiled_pattern); + + return result; } vector PathScanner::run_scan (const string &dirpath, - bool (PathScanner::*memberfilter)(const string &), bool (*filter)(const string &, void *), void *arg, bool match_fullpath, bool return_fullpath, @@ -85,14 +98,13 @@ PathScanner::run_scan (const string &dirpath, bool recurse) { vector 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; } void PathScanner::run_scan_internal (vector& result, const string &dirpath, - bool (PathScanner::*memberfilter)(const string &), bool (*filter)(const string &, void *), void *arg, bool match_fullpath, bool return_fullpath, @@ -135,7 +147,7 @@ PathScanner::run_scan_internal (vector& result, } 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 { if (match_fullpath) { @@ -144,16 +156,8 @@ PathScanner::run_scan_internal (vector& result, search_str = finfo->d_name; } - /* handle either type of function ptr */ - - if (memberfilter) { - if (!(this->*memberfilter)(search_str)) { - continue; - } - } else { - if (!filter(search_str, arg)) { - continue; - } + if (!filter(search_str, arg)) { + continue; } if (return_fullpath) { @@ -182,6 +186,7 @@ PathScanner::find_first (const string &dirpath, vector res; int err; char msg[256]; + regex_t compiled_pattern; if ((err = regcomp (&compiled_pattern, regexp.c_str(), REG_EXTENDED|REG_NOSUB))) { @@ -195,13 +200,14 @@ PathScanner::find_first (const string &dirpath, } run_scan_internal (res, dirpath, - &PathScanner::regexp_filter, - (bool (*)(const string &, void *)) 0, - 0, + ®exp_filter, + &compiled_pattern, match_fullpath, return_fullpath, 1); + regfree (&compiled_pattern); + if (res.size() == 0) { return string(); } @@ -221,7 +227,6 @@ PathScanner::find_first (const string &dirpath, run_scan_internal (res, dirpath, - (bool (PathScanner::*)(const string &)) 0, filter, 0, match_fullpath, diff --git a/libs/pbd/pbd/pathscanner.h b/libs/pbd/pbd/pathscanner.h index 990aa44c77..de23d7c645 100644 --- a/libs/pbd/pbd/pathscanner.h +++ b/libs/pbd/pbd/pathscanner.h @@ -42,7 +42,6 @@ class LIBPBD_API PathScanner long limit = -1, bool recurse = false) { return run_scan (dirpath, - (bool (PathScanner::*)(const std::string &)) 0, filter, arg, match_fullpath, @@ -69,14 +68,8 @@ class LIBPBD_API PathScanner bool return_fullpath = true); 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 run_scan (const std::string &dirpath, - bool (PathScanner::*mfilter) (const std::string &), bool (*filter)(const std::string &, void *), void *arg, bool match_fullpath, @@ -86,7 +79,6 @@ class LIBPBD_API PathScanner void run_scan_internal (std::vector&, const std::string &dirpath, - bool (PathScanner::*mfilter) (const std::string &), bool (*filter)(const std::string &, void *), void *arg, bool match_fullpath,