From 8de73ad774d720b299bbbbc15eefb3dd2e8ff045 Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Sat, 6 Apr 2024 12:47:11 +0300 Subject: [PATCH] Refactor file find Fixes #352 --- src/file_find.cc | 30 +++++++++++++++++------------- src/file_find.h | 2 +- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/file_find.cc b/src/file_find.cc index 3a5a832..1d7da82 100644 --- a/src/file_find.cc +++ b/src/file_find.cc @@ -16,11 +16,17 @@ bool fileFindFirst(const char* path, DirectoryFileFindData* findData) return false; } #else - strcpy(findData->path, path); - char drive[COMPAT_MAX_DRIVE]; 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]; compat_makepath(basePath, drive, dir, nullptr, nullptr); @@ -32,9 +38,10 @@ bool fileFindFirst(const char* path, DirectoryFileFindData* findData) findData->entry = readdir(findData->dir); while (findData->entry != nullptr) { - char entryPath[COMPAT_MAX_PATH]; - compat_makepath(entryPath, drive, dir, fileFindGetName(findData), nullptr); - if (fpattern_match(findData->path, entryPath)) { + char entryName[COMPAT_MAX_FNAME]; + strcpy(entryName, fileFindGetName(findData)); + compat_strlwr(entryName); + if (fpattern_match(findData->pattern, entryName)) { break; } findData->entry = readdir(findData->dir); @@ -58,15 +65,12 @@ bool fileFindNext(DirectoryFileFindData* findData) return false; } #else - char drive[COMPAT_MAX_DRIVE]; - char dir[COMPAT_MAX_DIR]; - compat_splitpath(findData->path, drive, dir, nullptr, nullptr); - findData->entry = readdir(findData->dir); while (findData->entry != nullptr) { - char entryPath[COMPAT_MAX_PATH]; - compat_makepath(entryPath, drive, dir, fileFindGetName(findData), nullptr); - if (fpattern_match(findData->path, entryPath)) { + char entryName[COMPAT_MAX_FNAME]; + strcpy(entryName, fileFindGetName(findData)); + compat_strlwr(entryName); + if (fpattern_match(findData->pattern, entryName)) { break; } findData->entry = readdir(findData->dir); diff --git a/src/file_find.h b/src/file_find.h index 09b72b9..28e8cfd 100644 --- a/src/file_find.h +++ b/src/file_find.h @@ -37,7 +37,7 @@ typedef struct DirectoryFileFindData { #else DIR* dir; struct dirent* entry; - char path[COMPAT_MAX_PATH]; + char pattern[COMPAT_MAX_FNAME]; #endif } DirectoryFileFindData;