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;
}
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");
generic_string selection;
@ -1342,21 +1342,19 @@ void getFilesInFolder(std::vector<generic_string>& files, const generic_string&
WIN32_FIND_DATA foundData;
HANDLE hFindFile = ::FindFirstFile(filter.c_str(), &foundData);
if (hFindFile != INVALID_HANDLE_VALUE && !(foundData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
if (hFindFile != INVALID_HANDLE_VALUE)
{
generic_string foundFullPath = inFolder;
pathAppend(foundFullPath, foundData.cFileName);
files.push_back(foundFullPath);
while (::FindNextFile(hFindFile, &foundData))
do
{
generic_string foundFullPath2 = inFolder;
pathAppend(foundFullPath2, foundData.cFileName);
files.push_back(foundFullPath2);
}
if (!(foundData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
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

View File

@ -167,7 +167,7 @@ int OrdinalIgnoreCaseCompareStrings(LPCTSTR sz1, LPCTSTR sz2);
bool str2Clipboard(const generic_string &str2cpy, HWND hwnd);
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);

View File

@ -285,7 +285,7 @@ enum Platform { PF_UNKNOWN, PF_X86, PF_X64, PF_IA64, PF_ARM64 };
#define NPPM_RELOADFILE (NPPMSG + 36)
// 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)
// lParam[in]: filePathName2Reload is the full file path of document to reload
// 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)
{
level++;
generic_string dirFilter(dir);
dirFilter += TEXT("*.*");
WIN32_FIND_DATA foundData;
HANDLE hFile = ::FindFirstFile(dirFilter.c_str(), &foundData);
if (hFile != INVALID_HANDLE_VALUE)
HANDLE hFindFile = ::FindFirstFile(dirFilter.c_str(), &foundData);
if (hFindFile != INVALID_HANDLE_VALUE)
{
if (foundData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
do
{
if (!isInHiddenDir && (foundData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
if (foundData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
// do nothing
}
else if (isRecursive)
{
if ((OrdinalIgnoreCaseCompareStrings(foundData.cFileName, TEXT(".")) != 0) && (OrdinalIgnoreCaseCompareStrings(foundData.cFileName, TEXT("..")) != 0) &&
!matchInExcludeDirList(foundData.cFileName, patterns, level))
if (!isInHiddenDir && (foundData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
{
generic_string pathDir(dir);
pathDir += foundData.cFileName;
pathDir += TEXT("\\");
getMatchedFileNames(pathDir.c_str(), level, patterns, fileNames, isRecursive, isInHiddenDir);
// 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);
pathDir += foundData.cFileName;
pathDir += TEXT("\\");
getMatchedFileNames(pathDir.c_str(), level, patterns, fileNames, isRecursive, isInHiddenDir);
}
}
}
}
else
{
if (matchInList(foundData.cFileName, patterns))
else
{
generic_string pathFile(dir);
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))
if (matchInList(foundData.cFileName, patterns))
{
generic_string pathDir(dir);
pathDir += foundData.cFileName;
pathDir += TEXT("\\");
getMatchedFileNames(pathDir.c_str(), level, patterns, fileNames, isRecursive, isInHiddenDir);
generic_string pathFile(dir);
pathFile += foundData.cFileName;
fileNames.push_back(pathFile.c_str());
}
}
}
else
{
if (matchInList(foundData.cFileName, patterns))
{
generic_string pathFile(dir);
pathFile += foundData.cFileName;
fileNames.push_back(pathFile.c_str());
}
}
} while (::FindNextFile(hFindFile, &foundData));
::FindClose(hFindFile);
}
::FindClose(hFile);
}
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);
for (auto&& sel : files)
buffers.push_back(MainFileManager.getBufferByID(sel._bufID));
buf2Clipborad(buffers, id == IDM_DOCLIST_COPYPATHS, _pDocumentListPanel->getHSelf());
buf2Clipboard(buffers, id == IDM_DOCLIST_COPYPATHS, _pDocumentListPanel->getHSelf());
}
break;
@ -1289,7 +1289,7 @@ void Notepad_plus::command(int id)
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;

View File

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

View File

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