From 67776333295eaf760e56bbfdf4af7f2802aeb6d7 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 30 Dec 2023 00:23:28 +0200 Subject: [PATCH] Refactor FS::list_files to not use an empty regex --- source/fs/dir.cpp | 5 ----- source/fs/unix/dir.cpp | 18 ++++++++++++++---- source/fs/windows/dir.cpp | 18 ++++++++++++++---- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/source/fs/dir.cpp b/source/fs/dir.cpp index 0f617e5..e043894 100644 --- a/source/fs/dir.cpp +++ b/source/fs/dir.cpp @@ -101,11 +101,6 @@ void rmpath(const Path &path) rmdir(path); } -vector list_files(const Path &path) -{ - return list_filtered(path, string()); -} - Path get_sys_conf_dir() { const char *argv0 = Application::get_argv0(); diff --git a/source/fs/unix/dir.cpp b/source/fs/unix/dir.cpp index a13cbbb..a2ac80e 100644 --- a/source/fs/unix/dir.cpp +++ b/source/fs/unix/dir.cpp @@ -22,10 +22,9 @@ void rmdir(const Path &path) throw system_error("rmdir"); } -vector list_filtered(const Path &path, const string &filter) +template +vector list_files_internal(const Path &path, const F &filter) { - Regex r_filter(filter); - vector result; DIR *dir = opendir(path.str().c_str()); if(!dir) @@ -36,7 +35,7 @@ vector list_filtered(const Path &path, const string &filter) const char *fn = de->d_name; if(fn[0]=='.' && (fn[1]==0 || (fn[1]=='.' && fn[2]==0))) continue; - if(r_filter.match(fn)) + if(filter(fn)) result.push_back(fn); } closedir(dir); @@ -44,6 +43,17 @@ vector list_filtered(const Path &path, const string &filter) return result; } +vector list_files(const Path &path) +{ + return list_files_internal(path, [](const char *){ return true; }); +} + +vector list_filtered(const Path &path, const string &filter) +{ + Regex r_filter(filter); + return list_files_internal(path, [&r_filter](const char *fn){ return r_filter.match(fn); }); +} + Path getcwd() { char buf[1024]; diff --git a/source/fs/windows/dir.cpp b/source/fs/windows/dir.cpp index 2ba2f1e..c69820b 100644 --- a/source/fs/windows/dir.cpp +++ b/source/fs/windows/dir.cpp @@ -21,10 +21,9 @@ void rmdir(const Path &path) throw system_error("RemoveDirectory"); } -vector list_filtered(const Path &path, const string &filter) +template +vector list_files_internal(const Path &path, const F &filter) { - Regex r_filter(filter); - vector result; WIN32_FIND_DATA entry; string pattern = path.str()+"\\*"; @@ -43,7 +42,7 @@ vector list_filtered(const Path &path, const string &filter) const char *fn = entry.cFileName; if(fn[0]=='.' && (fn[1]==0 || (fn[1]=='.' && fn[2]==0))) continue; - if(r_filter.match(fn)) + if(filter(fn)) result.push_back(fn); } @@ -55,6 +54,17 @@ vector list_filtered(const Path &path, const string &filter) return result; } +vector list_files(const Path &path) +{ + return list_files_internal(path, [](const char *){ return true; }); +} + +vector list_filtered(const Path &path, const string &filter) +{ + Regex r_filter(filter); + return list_files_internal(path, [&r_filter](const char *fn){ return r_filter.match(fn); }); +} + /* Windows documentation says Get/SetCurrentDirectory use a global buffer and are not thread safe. */ static Mutex &cwd_mutex() -- 2.45.2