13
0

Add PathScanner::find_files_matching_regex and move regexp usage to one place

This commit is contained in:
Tim Mayberry 2014-06-17 11:15:10 +10:00
parent f220d01132
commit f54092dada
2 changed files with 41 additions and 36 deletions

View File

@ -52,16 +52,17 @@ regexp_filter (const string& str, void *arg)
return regexec (pattern, str.c_str(), 0, 0, 0) == 0; return regexec (pattern, str.c_str(), 0, 0, 0) == 0;
} }
vector<string> void
PathScanner::operator() (const string &dirpath, const string &regexp, PathScanner::find_files_matching_regex (vector<string>& result,
bool match_fullpath, bool return_fullpath, const std::string& dirpath,
long limit, bool recurse) const std::string& regexp,
bool match_fullpath, bool return_fullpath,
long limit,
bool recurse)
{ {
int err; int err;
char msg[256]; char msg[256];
regex_t compiled_pattern; 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))) {
@ -74,17 +75,33 @@ PathScanner::operator() (const string &dirpath, const string &regexp,
<< ")" << ")"
<< endmsg; << endmsg;
return vector<string>(); return;
} }
result = run_scan (dirpath, result = run_scan (dirpath,
regexp_filter, regexp_filter,
&compiled_pattern, &compiled_pattern,
match_fullpath, match_fullpath,
return_fullpath, return_fullpath,
limit, recurse); limit, recurse);
regfree (&compiled_pattern); regfree (&compiled_pattern);
}
vector<string>
PathScanner::operator() (const string &dirpath, const string &regexp,
bool match_fullpath, bool return_fullpath,
long limit, bool recurse)
{
vector<string> result;
find_files_matching_regex (result,
dirpath,
regexp,
match_fullpath,
return_fullpath,
limit, recurse);
return result; return result;
} }
@ -184,30 +201,10 @@ PathScanner::find_first (const string &dirpath,
bool return_fullpath) bool return_fullpath)
{ {
vector<string> res; vector<string> res;
int err;
char msg[256];
regex_t compiled_pattern;
if ((err = regcomp (&compiled_pattern, regexp.c_str(), find_files_matching_regex (res, dirpath, regexp,
REG_EXTENDED|REG_NOSUB))) { match_fullpath, return_fullpath, 1);
regerror (err, &compiled_pattern,
msg, sizeof (msg));
error << "Cannot compile soundfile regexp for use (" << msg << ")" << endmsg;
return 0;
}
run_scan_internal (res, dirpath,
&regexp_filter,
&compiled_pattern,
match_fullpath,
return_fullpath,
1);
regfree (&compiled_pattern);
if (res.size() == 0) { if (res.size() == 0) {
return string(); return string();
} }

View File

@ -69,6 +69,14 @@ class LIBPBD_API PathScanner
private: private:
void find_files_matching_regex (std::vector<std::string>& results,
const std::string& dirpath,
const std::string& regexp,
bool match_fullpath,
bool return_fullpath,
long limit,
bool recurse = false);
std::vector<std::string> run_scan (const std::string &dirpath, std::vector<std::string> run_scan (const std::string &dirpath,
bool (*filter)(const std::string &, void *), bool (*filter)(const std::string &, void *),
void *arg, void *arg,