Skip to content

Commit

Permalink
Fix issue #1646, #1644: Filtering qualified files
Browse files Browse the repository at this point in the history
  • Loading branch information
sdottaka committed Jan 3, 2023
1 parent 2ed3328 commit 96fd846
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 18 deletions.
12 changes: 9 additions & 3 deletions Src/DirScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -844,9 +844,15 @@ static void CompareDiffItem(FolderCmp &fc, DIFFITEM &di)
{
// 1. Test against filters
if (pCtxt->m_piFilterGlobal==nullptr ||
(nDirs == 2 && pCtxt->m_piFilterGlobal->includeFile(di.diffFileInfo[0].filename, di.diffFileInfo[1].filename)) ||
(nDirs == 3 && pCtxt->m_piFilterGlobal->includeFile(di.diffFileInfo[0].filename, di.diffFileInfo[1].filename, di.diffFileInfo[2].filename))
)
(nDirs == 2 && pCtxt->m_piFilterGlobal->includeFile(
paths::ConcatPath(di.diffFileInfo[0].path, di.diffFileInfo[0].filename),
paths::ConcatPath(di.diffFileInfo[1].path, di.diffFileInfo[1].filename)
)) ||
(nDirs == 3 && pCtxt->m_piFilterGlobal->includeFile(
paths::ConcatPath(di.diffFileInfo[0].path, di.diffFileInfo[0].filename),
paths::ConcatPath(di.diffFileInfo[1].path, di.diffFileInfo[1].filename),
paths::ConcatPath(di.diffFileInfo[2].path, di.diffFileInfo[2].filename)
)))
{
di.diffcode.diffcode |= DIFFCODE::INCLUDED;
di.diffcode.diffcode |= fc.prepAndCompareFiles(di);
Expand Down
7 changes: 5 additions & 2 deletions Src/FileFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ struct FileFilterElement
Poco::RegularExpression regexp; /**< Compiled regular expression */
std::string _regex; /**< Regular expression string to set to Poco::RegularExpression */
int _reOpts; /**< Options to set to Poco::RegularExpression */
FileFilterElement(const std::string& regex, int reOpts) : regexp(regex, reOpts), _regex(regex), _reOpts(reOpts)
bool _fileNameOnly; /**< If true, indicates that the filter matches only filenames */
FileFilterElement(const std::string& regex, int reOpts, bool fileFilter) :
regexp(regex, reOpts), _regex(regex), _reOpts(reOpts), _fileNameOnly(fileFilter && regex.find("\\\\", 0) == String::npos)
{
}
FileFilterElement(const FileFilterElement* element) : regexp(element->_regex, element->_reOpts), _regex(element->_regex), _reOpts(element->_reOpts)
FileFilterElement(const FileFilterElement* element) :
regexp(element->_regex, element->_reOpts), _regex(element->_regex), _reOpts(element->_reOpts), _fileNameOnly(element->_fileNameOnly)
{
}
};
Expand Down
20 changes: 11 additions & 9 deletions Src/FileFilterMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ using Poco::Glob;
using Poco::icompare;
using Poco::RegularExpression;

static void AddFilterPattern(vector<FileFilterElementPtr> *filterList, String & str);
static void AddFilterPattern(vector<FileFilterElementPtr> *filterList, String & str, bool fileFilter);

/**
* @brief Destructor, frees all filters.
Expand Down Expand Up @@ -119,7 +119,7 @@ void FileFilterMgr::DeleteAllFilters()
* @param [in] filterList List where pattern is added.
* @param [in] str Temporary variable (ie, it may be altered)
*/
static void AddFilterPattern(vector<FileFilterElementPtr> *filterList, String & str)
static void AddFilterPattern(vector<FileFilterElementPtr> *filterList, String & str, bool fileFilter)
{
const String& commentLeader = _T("##"); // Starts comment
str = strutils::trim_ws_begin(str);
Expand All @@ -145,7 +145,7 @@ static void AddFilterPattern(vector<FileFilterElementPtr> *filterList, String &
re_opts |= RegularExpression::RE_UTF8;
try
{
filterList->push_back(FileFilterElementPtr(new FileFilterElement(regexString, re_opts)));
filterList->push_back(FileFilterElementPtr(new FileFilterElement(regexString, re_opts, fileFilter)));
}
catch (...)
{
Expand Down Expand Up @@ -218,25 +218,25 @@ FileFilter * FileFilterMgr::LoadFilterFile(const String& szFilepath, int & error
{
// file filter
String str = sLine.substr(2);
AddFilterPattern(&pfilter->filefilters, str);
AddFilterPattern(&pfilter->filefilters, str, true);
}
else if (0 == sLine.compare(0, 2, _T("d:"), 2))
{
// directory filter
String str = sLine.substr(2);
AddFilterPattern(&pfilter->dirfilters, str);
AddFilterPattern(&pfilter->dirfilters, str, false);
}
else if (0 == sLine.compare(0, 3, _T("f!:"), 3))
{
// file filter
String str = sLine.substr(3);
AddFilterPattern(&pfilter->filefiltersExclude, str);
AddFilterPattern(&pfilter->filefiltersExclude, str, true);
}
else if (0 == sLine.compare(0, 3, _T("d!:"), 3))
{
// directory filter
String str = sLine.substr(3);
AddFilterPattern(&pfilter->dirfiltersExclude, str);
AddFilterPattern(&pfilter->dirfiltersExclude, str, false);
}
} while (bLinesLeft);

Expand Down Expand Up @@ -289,15 +289,17 @@ bool TestAgainstRegList(const vector<FileFilterElementPtr> *filterList, const St
if (filterList->size() == 0)
return false;

std::string compString;
std::string compString, compStringFileName;
ucr::toUTF8(szTest, compString);
vector<FileFilterElementPtr>::const_iterator iter = filterList->begin();
while (iter != filterList->end())
{
RegularExpression::Match match;
try
{
if ((*iter)->regexp.match(compString, 0, match) > 0)
if ((*iter)->_fileNameOnly && compStringFileName.empty())
ucr::toUTF8(paths::FindFileName(szTest), compStringFileName);
if ((*iter)->regexp.match((*iter)->_fileNameOnly ? compStringFileName : compString, 0, match) > 0)
return true;
}
catch (...)
Expand Down
9 changes: 6 additions & 3 deletions Src/FolderCmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,12 @@ int FolderCmp::prepAndCompareFiles(DIFFITEM &di)
nCompMethod = CMP_BINARY_CONTENT;
}
else if (m_pCtxt->m_bEnableImageCompare && (
di.diffFileInfo[0].size != DirItem::FILE_SIZE_NONE && m_pCtxt->m_pImgfileFilter->includeFile(di.diffFileInfo[0].filename) ||
di.diffFileInfo[1].size != DirItem::FILE_SIZE_NONE && m_pCtxt->m_pImgfileFilter->includeFile(di.diffFileInfo[1].filename) ||
nDirs > 2 && di.diffFileInfo[2].size != DirItem::FILE_SIZE_NONE && m_pCtxt->m_pImgfileFilter->includeFile(di.diffFileInfo[2].filename)))
di.diffFileInfo[0].size != DirItem::FILE_SIZE_NONE && m_pCtxt->m_pImgfileFilter->includeFile(
paths::ConcatPath(di.diffFileInfo[0].path, di.diffFileInfo[0].filename)) ||
di.diffFileInfo[1].size != DirItem::FILE_SIZE_NONE && m_pCtxt->m_pImgfileFilter->includeFile(
paths::ConcatPath(di.diffFileInfo[1].path, di.diffFileInfo[1].filename)) ||
nDirs > 2 && di.diffFileInfo[2].size != DirItem::FILE_SIZE_NONE && m_pCtxt->m_pImgfileFilter->includeFile(
paths::ConcatPath(di.diffFileInfo[2].path, di.diffFileInfo[2].filename))))
{
nCompMethod = CMP_IMAGE_CONTENT;
}
Expand Down
2 changes: 1 addition & 1 deletion Src/Plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ void PluginInfo::LoadFilterString()
re_opts |= RegularExpression::RE_UTF8;
try
{
m_filters.emplace_back(std::make_shared<FileFilterElement>(regexString, re_opts));
m_filters.emplace_back(std::make_shared<FileFilterElement>(regexString, re_opts, true));
}
catch (...)
{
Expand Down

0 comments on commit 96fd846

Please sign in to comment.