Refactor file find

Fixes #352
This commit is contained in:
Alexander Batalov 2024-04-06 12:47:11 +03:00
parent 3ceacf1001
commit 8de73ad774
2 changed files with 18 additions and 14 deletions

View File

@ -16,11 +16,17 @@ bool fileFindFirst(const char* path, DirectoryFileFindData* findData)
return false; return false;
} }
#else #else
strcpy(findData->path, path);
char drive[COMPAT_MAX_DRIVE]; char drive[COMPAT_MAX_DRIVE];
char dir[COMPAT_MAX_DIR]; char dir[COMPAT_MAX_DIR];
compat_splitpath(path, drive, dir, nullptr, nullptr); char fname[COMPAT_MAX_FNAME];
char ext[COMPAT_MAX_EXT];
compat_splitpath(path, drive, dir, fname, ext);
// Reassemble file name and extension to serve as a pattern. It has to be
// lowercased because underlying `fpattern` implementation uses lowercased
// char-by-char matching.
compat_makepath(findData->pattern, nullptr, nullptr, fname, ext);
compat_strlwr(findData->pattern);
char basePath[COMPAT_MAX_PATH]; char basePath[COMPAT_MAX_PATH];
compat_makepath(basePath, drive, dir, nullptr, nullptr); compat_makepath(basePath, drive, dir, nullptr, nullptr);
@ -32,9 +38,10 @@ bool fileFindFirst(const char* path, DirectoryFileFindData* findData)
findData->entry = readdir(findData->dir); findData->entry = readdir(findData->dir);
while (findData->entry != nullptr) { while (findData->entry != nullptr) {
char entryPath[COMPAT_MAX_PATH]; char entryName[COMPAT_MAX_FNAME];
compat_makepath(entryPath, drive, dir, fileFindGetName(findData), nullptr); strcpy(entryName, fileFindGetName(findData));
if (fpattern_match(findData->path, entryPath)) { compat_strlwr(entryName);
if (fpattern_match(findData->pattern, entryName)) {
break; break;
} }
findData->entry = readdir(findData->dir); findData->entry = readdir(findData->dir);
@ -58,15 +65,12 @@ bool fileFindNext(DirectoryFileFindData* findData)
return false; return false;
} }
#else #else
char drive[COMPAT_MAX_DRIVE];
char dir[COMPAT_MAX_DIR];
compat_splitpath(findData->path, drive, dir, nullptr, nullptr);
findData->entry = readdir(findData->dir); findData->entry = readdir(findData->dir);
while (findData->entry != nullptr) { while (findData->entry != nullptr) {
char entryPath[COMPAT_MAX_PATH]; char entryName[COMPAT_MAX_FNAME];
compat_makepath(entryPath, drive, dir, fileFindGetName(findData), nullptr); strcpy(entryName, fileFindGetName(findData));
if (fpattern_match(findData->path, entryPath)) { compat_strlwr(entryName);
if (fpattern_match(findData->pattern, entryName)) {
break; break;
} }
findData->entry = readdir(findData->dir); findData->entry = readdir(findData->dir);

View File

@ -37,7 +37,7 @@ typedef struct DirectoryFileFindData {
#else #else
DIR* dir; DIR* dir;
struct dirent* entry; struct dirent* entry;
char path[COMPAT_MAX_PATH]; char pattern[COMPAT_MAX_FNAME];
#endif #endif
} DirectoryFileFindData; } DirectoryFileFindData;