Skip to content

Commit

Permalink
improve performance when filtering and renaming, and fix a memory leak.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefankueng committed Aug 3, 2020
1 parent 38e8c2e commit 60de377
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 43 deletions.
22 changes: 6 additions & 16 deletions StExBar/src/Filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "stdafx.h"
#include "SRBand.h"
#include "StringUtils.h"
#include "OnOutOfScope.h"
#include <regex>

bool CDeskBand::Filter(LPTSTR filter)
Expand Down Expand Up @@ -190,21 +191,21 @@ bool CDeskBand::Filter(LPTSTR filter)

bool CDeskBand::CheckDisplayName(IShellFolder* shellFolder, LPITEMIDLIST pidl, LPCTSTR filter, bool bUseRegex)
{
if (filter == nullptr || filter[0] == 0)
return true;
STRRET str;
if (SUCCEEDED(shellFolder->GetDisplayNameOf(pidl,
// SHGDN_FORPARSING needed to get the extensions even if they're not shown
SHGDN_INFOLDER | SHGDN_FORPARSING,
&str)))
{
wchar_t dispname[MAX_PATH];
StrRetToBuf(&str, pidl, dispname, _countof(dispname));

OnOutOfScope(CoTaskMemFree(str.pOleStr));
if (bUseRegex)
{
try
{
const std::wregex regCheck(&filter[1], std::regex_constants::icase | std::regex_constants::ECMAScript);
std::wstring s = dispname;
std::wstring s = str.pOleStr;

return std::regex_search(s, regCheck);
}
Expand All @@ -214,19 +215,8 @@ bool CDeskBand::CheckDisplayName(IShellFolder* shellFolder, LPITEMIDLIST pidl, L
}
else
{
// we now have the display name of the item
// i.e. the way the item is shown
// since the windows file system is case-insensitive
// we have to force the display name to lowercase
// so the filter matches case-insensitive too
wchar_t* pString = dispname;
while (*pString)
{
*pString = towlower(*pString);
pString++;
}
// check if the item name matches the text of the edit control
return (wcsstr(dispname, filter) != NULL);
return (StrStrIW(str.pOleStr, filter) != NULL);
}
}
return false;
Expand Down
38 changes: 11 additions & 27 deletions StExBar/src/Rename.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
#include "stdafx.h"
#include "SRBand.h"
#include "resource.h"
#include <regex>
#include "RenameDlg.h"
#include "Pidl.h"
#include "NumberReplacer.h"
#include "OnOutOfScope.h"
#include <regex>

void CDeskBand::Rename(HWND hwnd, const std::map<std::wstring, ULONG>& items)
{
Expand Down Expand Up @@ -97,36 +98,21 @@ void CDeskBand::Rename(HWND hwnd, const std::map<std::wstring, ULONG>& items)
{
LPITEMIDLIST pidl;
ULONG fetched = 0;
ULONG attribs = 0;
do
{
pidl = NULL;
if (SUCCEEDED(pEnum->Next(1, &pidl, &fetched)))
{
if (fetched)
{
// the pidl we get here is relative!
attribs = SFGAO_FILESYSTEM | SFGAO_FOLDER;
if (SUCCEEDED(pShellFolder->GetAttributesOf(1, (LPCITEMIDLIST*)&pidl, &attribs)))
STRRET str;
if (SUCCEEDED(pShellFolder->GetDisplayNameOf(pidl,
// SHGDN_FORPARSING needed to get the extensions even if they're not shown
SHGDN_INFOLDER | SHGDN_FORPARSING,
&str)))
{
if (attribs & SFGAO_FILESYSTEM)
{
// create an absolute pidl with the pidl we got above
LPITEMIDLIST abspidl = CPidl::Append(folderpidl, pidl);
if (abspidl)
{
if (SHGetPathFromIDList(abspidl, buf))
{
std::wstring p = buf;
size_t pos = p.find_last_of('\\');
if (pos != std::wstring::npos)
{
m_filelist.insert(p.substr(pos + 1));
}
}
CoTaskMemFree(abspidl);
}
}
m_filelist.insert(str.pOleStr);
CoTaskMemFree(str.pOleStr);
}
}
CoTaskMemFree(pidl);
Expand Down Expand Up @@ -215,13 +201,11 @@ void CDeskBand::Rename(HWND hwnd, const std::map<std::wstring, ULONG>& items)
SHGDN_INFOLDER | SHGDN_FORPARSING,
&str)))
{
wchar_t dispname[MAX_PATH];
StrRetToBuf(&str, pidl, dispname, _countof(dispname));

OnOutOfScope(CoTaskMemFree(str.pOleStr));
std::wstring replaced;
try
{
std::wstring sDispName = dispname;
std::wstring sDispName = str.pOleStr;
// check if the item is in the list of selected items
if (m_filelist.find(sDispName) != m_filelist.end())
{
Expand Down

0 comments on commit 60de377

Please sign in to comment.