Unified SystemToolkit list directory with dialog file patterns

This commit is contained in:
Bruno Herbelin
2021-12-05 18:39:58 +01:00
parent e5334aae0a
commit 923d84f378
3 changed files with 10 additions and 7 deletions

View File

@@ -43,6 +43,7 @@ using namespace std;
#include <sys/stat.h> #include <sys/stat.h>
#include <pwd.h> #include <pwd.h>
#include <dirent.h> #include <dirent.h>
#include <fnmatch.h>
#define PATH_SEP '/' #define PATH_SEP '/'
#endif #endif
@@ -298,7 +299,7 @@ std::string SystemToolkit::path_directory(const std::string& path)
return directorypath; return directorypath;
} }
list<string> SystemToolkit::list_directory(const string& path, const list<string>& extensions) list<string> SystemToolkit::list_directory(const string& path, const list<string>& patterns)
{ {
list<string> ls; list<string> ls;
@@ -309,10 +310,12 @@ list<string> SystemToolkit::list_directory(const string& path, const list<string
while ((ent = readdir (dir)) != NULL) { while ((ent = readdir (dir)) != NULL) {
if ( ent->d_type == DT_REG) if ( ent->d_type == DT_REG)
{ {
string filename = string(ent->d_name); int found = FNM_NOMATCH;
string ext = extension_filename(filename); for (auto it = patterns.cbegin(); it != patterns.cend() && found == FNM_NOMATCH; ++it)
if ( extensions.empty() || find(extensions.cbegin(), extensions.cend(), ext) != extensions.cend()) // test pattern in CASE insensitive
ls.push_back( full_filename(path, filename) ); found = fnmatch( it->c_str(), ent->d_name, FNM_CASEFOLD );
if (found != FNM_NOMATCH)
ls.push_back( full_filename(path, ent->d_name) );
} }
} }
closedir (dir); closedir (dir);

View File

@@ -49,7 +49,7 @@ namespace SystemToolkit
std::string path_directory(const std::string& path); std::string path_directory(const std::string& path);
// list all files of a directory mathing the given filter extension (if any) // list all files of a directory mathing the given filter extension (if any)
std::list<std::string> list_directory(const std::string& path, const std::list<std::string> &extensions); std::list<std::string> list_directory(const std::string& path, const std::list<std::string> &patterns);
// builds a path relative to 'relativeTo' to reach file at 'absolutePath' (e.g. /a/b/c/d rel to /a/b/e -> ../c/d) // builds a path relative to 'relativeTo' to reach file at 'absolutePath' (e.g. /a/b/c/d rel to /a/b/e -> ../c/d)
std::string path_relative_to_path(const std::string& absolutePath, const std::string& relativeTo); std::string path_relative_to_path(const std::string& absolutePath, const std::string& relativeTo);

View File

@@ -4415,7 +4415,7 @@ void Navigator::RenderMainPannelVimix()
// selection MODE 1 : LIST FOLDER // selection MODE 1 : LIST FOLDER
else if ( selection_session_mode == 1) { else if ( selection_session_mode == 1) {
// show list of vimix files in folder // show list of vimix files in folder
sessions_list = SystemToolkit::list_directory( Settings::application.recentFolders.path, {"mix", "MIX"}); sessions_list = SystemToolkit::list_directory( Settings::application.recentFolders.path, { VIMIX_FILES_PATTERN });
} }
// indicate the list changed (do not change at every frame) // indicate the list changed (do not change at every frame)
selection_session_mode_changed = false; selection_session_mode_changed = false;