Refactor FindFirstFile-FindNextFile code blocks

Fix #14853, fix #14847, fix 060396c#r139323315, close #14857
This commit is contained in:
Alan Kilborn 2024-03-13 20:08:36 -04:00 committed by Don Ho
parent 9d848b6a47
commit 2e4ad67dff
7 changed files with 68 additions and 128 deletions

View File

@ -950,7 +950,7 @@ bool str2Clipboard(const generic_string &str2cpy, HWND hwnd)
return true; return true;
} }
bool buf2Clipborad(const std::vector<Buffer*>& buffers, bool isFullPath, HWND hwnd) bool buf2Clipboard(const std::vector<Buffer*>& buffers, bool isFullPath, HWND hwnd)
{ {
const generic_string crlf = _T("\r\n"); const generic_string crlf = _T("\r\n");
generic_string selection; generic_string selection;
@ -1342,21 +1342,19 @@ void getFilesInFolder(std::vector<generic_string>& files, const generic_string&
WIN32_FIND_DATA foundData; WIN32_FIND_DATA foundData;
HANDLE hFindFile = ::FindFirstFile(filter.c_str(), &foundData); HANDLE hFindFile = ::FindFirstFile(filter.c_str(), &foundData);
if (hFindFile != INVALID_HANDLE_VALUE)
if (hFindFile != INVALID_HANDLE_VALUE && !(foundData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{ {
generic_string foundFullPath = inFolder; do
pathAppend(foundFullPath, foundData.cFileName);
files.push_back(foundFullPath);
while (::FindNextFile(hFindFile, &foundData))
{ {
generic_string foundFullPath2 = inFolder; if (!(foundData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
pathAppend(foundFullPath2, foundData.cFileName); {
files.push_back(foundFullPath2); generic_string foundFullPath = inFolder;
} pathAppend(foundFullPath, foundData.cFileName);
files.push_back(foundFullPath);
}
} while (::FindNextFile(hFindFile, &foundData));
::FindClose(hFindFile);
} }
::FindClose(hFindFile);
} }
// remove any leading or trailing spaces from str // remove any leading or trailing spaces from str

View File

@ -167,7 +167,7 @@ int OrdinalIgnoreCaseCompareStrings(LPCTSTR sz1, LPCTSTR sz2);
bool str2Clipboard(const generic_string &str2cpy, HWND hwnd); bool str2Clipboard(const generic_string &str2cpy, HWND hwnd);
class Buffer; class Buffer;
bool buf2Clipborad(const std::vector<Buffer*>& buffers, bool isFullPath, HWND hwnd); bool buf2Clipboard(const std::vector<Buffer*>& buffers, bool isFullPath, HWND hwnd);
generic_string GetLastErrorAsString(DWORD errorCode = 0); generic_string GetLastErrorAsString(DWORD errorCode = 0);

View File

@ -285,7 +285,7 @@ enum Platform { PF_UNKNOWN, PF_X86, PF_X64, PF_IA64, PF_ARM64 };
#define NPPM_RELOADFILE (NPPMSG + 36) #define NPPM_RELOADFILE (NPPMSG + 36)
// BOOL NPPM_RELOADFILE(BOOL withAlert, TCHAR *filePathName2Reload) // BOOL NPPM_RELOADFILE(BOOL withAlert, TCHAR *filePathName2Reload)
// Reload the document which matches with the given filePathName2switch. // Reload the document which matches with the given filePathName2Reload.
// wParam: 0 (not used) // wParam: 0 (not used)
// lParam[in]: filePathName2Reload is the full file path of document to reload // lParam[in]: filePathName2Reload is the full file path of document to reload
// Return TRUE if reloading file succeeds, otherwise FALSE // Return TRUE if reloading file succeeds, otherwise FALSE

View File

@ -1821,74 +1821,47 @@ void Notepad_plus::removeDuplicateLines()
void Notepad_plus::getMatchedFileNames(const TCHAR *dir, size_t level, const vector<generic_string> & patterns, vector<generic_string> & fileNames, bool isRecursive, bool isInHiddenDir) void Notepad_plus::getMatchedFileNames(const TCHAR *dir, size_t level, const vector<generic_string> & patterns, vector<generic_string> & fileNames, bool isRecursive, bool isInHiddenDir)
{ {
level++; level++;
generic_string dirFilter(dir); generic_string dirFilter(dir);
dirFilter += TEXT("*.*"); dirFilter += TEXT("*.*");
WIN32_FIND_DATA foundData; WIN32_FIND_DATA foundData;
HANDLE hFindFile = ::FindFirstFile(dirFilter.c_str(), &foundData);
HANDLE hFile = ::FindFirstFile(dirFilter.c_str(), &foundData); if (hFindFile != INVALID_HANDLE_VALUE)
if (hFile != INVALID_HANDLE_VALUE)
{ {
do
if (foundData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{ {
if (!isInHiddenDir && (foundData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)) if (foundData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{ {
// do nothing if (!isInHiddenDir && (foundData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
}
else if (isRecursive)
{
if ((OrdinalIgnoreCaseCompareStrings(foundData.cFileName, TEXT(".")) != 0) && (OrdinalIgnoreCaseCompareStrings(foundData.cFileName, TEXT("..")) != 0) &&
!matchInExcludeDirList(foundData.cFileName, patterns, level))
{ {
generic_string pathDir(dir); // do nothing
pathDir += foundData.cFileName; }
pathDir += TEXT("\\"); else if (isRecursive)
getMatchedFileNames(pathDir.c_str(), level, patterns, fileNames, isRecursive, isInHiddenDir); {
if ((OrdinalIgnoreCaseCompareStrings(foundData.cFileName, TEXT(".")) != 0) &&
(OrdinalIgnoreCaseCompareStrings(foundData.cFileName, TEXT("..")) != 0) &&
!matchInExcludeDirList(foundData.cFileName, patterns, level))
{
generic_string pathDir(dir);
pathDir += foundData.cFileName;
pathDir += TEXT("\\");
getMatchedFileNames(pathDir.c_str(), level, patterns, fileNames, isRecursive, isInHiddenDir);
}
} }
} }
} else
else
{
if (matchInList(foundData.cFileName, patterns))
{ {
generic_string pathFile(dir); if (matchInList(foundData.cFileName, patterns))
pathFile += foundData.cFileName;
fileNames.push_back(pathFile.c_str());
}
}
}
while (::FindNextFile(hFile, &foundData))
{
if (foundData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (!isInHiddenDir && (foundData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
{
// do nothing
}
else if (isRecursive)
{
if ((OrdinalIgnoreCaseCompareStrings(foundData.cFileName, TEXT(".")) != 0) && (OrdinalIgnoreCaseCompareStrings(foundData.cFileName, TEXT("..")) != 0) &&
!matchInExcludeDirList(foundData.cFileName, patterns, level))
{ {
generic_string pathDir(dir); generic_string pathFile(dir);
pathDir += foundData.cFileName; pathFile += foundData.cFileName;
pathDir += TEXT("\\"); fileNames.push_back(pathFile.c_str());
getMatchedFileNames(pathDir.c_str(), level, patterns, fileNames, isRecursive, isInHiddenDir);
} }
} }
} } while (::FindNextFile(hFindFile, &foundData));
else ::FindClose(hFindFile);
{
if (matchInList(foundData.cFileName, patterns))
{
generic_string pathFile(dir);
pathFile += foundData.cFileName;
fileNames.push_back(pathFile.c_str());
}
}
} }
::FindClose(hFile);
} }
bool Notepad_plus::createFilelistForFiles(vector<generic_string> & fileNames) bool Notepad_plus::createFilelistForFiles(vector<generic_string> & fileNames)

View File

@ -255,7 +255,7 @@ void Notepad_plus::command(int id)
auto files = _pDocumentListPanel->getSelectedFiles(false); auto files = _pDocumentListPanel->getSelectedFiles(false);
for (auto&& sel : files) for (auto&& sel : files)
buffers.push_back(MainFileManager.getBufferByID(sel._bufID)); buffers.push_back(MainFileManager.getBufferByID(sel._bufID));
buf2Clipborad(buffers, id == IDM_DOCLIST_COPYPATHS, _pDocumentListPanel->getHSelf()); buf2Clipboard(buffers, id == IDM_DOCLIST_COPYPATHS, _pDocumentListPanel->getHSelf());
} }
break; break;
@ -1289,7 +1289,7 @@ void Notepad_plus::command(int id)
buffers.push_back(buf); buffers.push_back(buf);
} }
} }
buf2Clipborad({ buffers.begin(), buffers.end() }, id == IDM_EDIT_COPY_ALL_PATHS, _pPublicInterface->getHSelf()); buf2Clipboard({ buffers.begin(), buffers.end() }, id == IDM_EDIT_COPY_ALL_PATHS, _pPublicInterface->getHSelf());
} }
break; break;

View File

@ -884,75 +884,44 @@ void FileBrowser::getDirectoryStructure(const TCHAR *dir, const std::vector<gene
dirFilter += TEXT("*.*"); dirFilter += TEXT("*.*");
WIN32_FIND_DATA foundData; WIN32_FIND_DATA foundData;
HANDLE hFile = ::FindFirstFile(dirFilter.c_str(), &foundData); HANDLE hFindFile = ::FindFirstFile(dirFilter.c_str(), &foundData);
if (hFindFile != INVALID_HANDLE_VALUE)
if (hFile != INVALID_HANDLE_VALUE)
{ {
do
if (foundData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{ {
if (!isInHiddenDir && (foundData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)) if (foundData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{ {
// do nothing if (!isInHiddenDir && (foundData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
}
else if (isRecursive)
{
if ((OrdinalIgnoreCaseCompareStrings(foundData.cFileName, TEXT(".")) != 0) && (OrdinalIgnoreCaseCompareStrings(foundData.cFileName, TEXT("..")) != 0))
{ {
generic_string pathDir(dir); // do nothing
if (pathDir[pathDir.length() - 1] != '\\') }
else if (isRecursive)
{
if ((OrdinalIgnoreCaseCompareStrings(foundData.cFileName, TEXT(".")) != 0) &&
(OrdinalIgnoreCaseCompareStrings(foundData.cFileName, TEXT("..")) != 0))
{
generic_string pathDir(dir);
if (pathDir[pathDir.length() - 1] != '\\')
pathDir += TEXT("\\");
pathDir += foundData.cFileName;
pathDir += TEXT("\\"); pathDir += TEXT("\\");
pathDir += foundData.cFileName;
pathDir += TEXT("\\");
FolderInfo subDirectoryStructure(foundData.cFileName, &directoryStructure); FolderInfo subDirectoryStructure(foundData.cFileName, &directoryStructure);
getDirectoryStructure(pathDir.c_str(), patterns, subDirectoryStructure, isRecursive, isInHiddenDir); getDirectoryStructure(pathDir.c_str(), patterns, subDirectoryStructure, isRecursive, isInHiddenDir);
directoryStructure.addSubFolder(subDirectoryStructure); directoryStructure.addSubFolder(subDirectoryStructure);
}
} }
} }
} else
else
{
if (matchInList(foundData.cFileName, patterns))
{ {
directoryStructure.addFile(foundData.cFileName); if (matchInList(foundData.cFileName, patterns))
}
}
}
while (::FindNextFile(hFile, &foundData))
{
if (foundData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (!isInHiddenDir && (foundData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
{
// do nothing
}
else if (isRecursive)
{
if ((OrdinalIgnoreCaseCompareStrings(foundData.cFileName, TEXT(".")) != 0) && (OrdinalIgnoreCaseCompareStrings(foundData.cFileName, TEXT("..")) != 0))
{ {
generic_string pathDir(dir); directoryStructure.addFile(foundData.cFileName);
if (pathDir[pathDir.length() - 1] != '\\')
pathDir += TEXT("\\");
pathDir += foundData.cFileName;
pathDir += TEXT("\\");
FolderInfo subDirectoryStructure(foundData.cFileName, &directoryStructure);
getDirectoryStructure(pathDir.c_str(), patterns, subDirectoryStructure, isRecursive, isInHiddenDir);
directoryStructure.addSubFolder(subDirectoryStructure);
} }
} }
} } while (::FindNextFile(hFindFile, &foundData));
else ::FindClose(hFindFile);
{
if (matchInList(foundData.cFileName, patterns))
{
directoryStructure.addFile(foundData.cFileName);
}
}
} }
::FindClose(hFile);
} }
void FileBrowser::addRootFolder(generic_string rootFolderPath) void FileBrowser::addRootFolder(generic_string rootFolderPath)

View File

@ -1093,7 +1093,7 @@ void WindowsDlg::putItemsToClipboard(bool isFullPath)
} }
while (i >= 0); while (i >= 0);
buf2Clipborad(buffers, isFullPath, _hList); buf2Clipboard(buffers, isFullPath, _hList);
} }
Buffer* WindowsDlg::getBuffer(int index) const Buffer* WindowsDlg::getBuffer(int index) const