diff --git a/Sofa/framework/Helper/src/sofa/helper/system/FileRepository.cpp b/Sofa/framework/Helper/src/sofa/helper/system/FileRepository.cpp index 35f5971dfc5..c7405547e59 100644 --- a/Sofa/framework/Helper/src/sofa/helper/system/FileRepository.cpp +++ b/Sofa/framework/Helper/src/sofa/helper/system/FileRepository.cpp @@ -304,6 +304,45 @@ bool FileRepository::findFileFromFile(std::string& filename, const std::string& return findFile(filename, SetDirectory::GetParentDir(basefile.c_str()), errlog); } +void FileRepository::findAllFilesInRepository(const std::string& path, std::vector& files, const std::vector& extensions, bool recursive) +{ + auto addToFiles = [&files, extensions](const std::filesystem::directory_entry& entry) + { + if (fs::is_regular_file(entry.path())) + { + if (extensions.empty() || std::find(extensions.begin(), extensions.end(), entry.path().extension()) != extensions.end()) + { + files.push_back(entry.path().string()); + } + } + }; + + for (std::vector::const_iterator it = vpath.begin(); it != vpath.end(); ++it) + { + // Get the full / absolute path (vpath + path) + const std::string fullPath = SetDirectory::GetRelativeFromDir(path.c_str(), it->c_str()); + fs::path p = fs::path(fullPath); + + if (fs::exists(p) && fs::is_directory(p)) + { + if (recursive) + { + for (auto& entry : fs::recursive_directory_iterator(p)) + { + addToFiles(entry); + } + } + else + { + for (auto& entry : fs::directory_iterator(p)) + { + addToFiles(entry); + } + } + } + } +} + void FileRepository::print() { for (std::vector::const_iterator it = vpath.begin(); it != vpath.end(); ++it) diff --git a/Sofa/framework/Helper/src/sofa/helper/system/FileRepository.h b/Sofa/framework/Helper/src/sofa/helper/system/FileRepository.h index e27531bbd0d..c72b655e474 100644 --- a/Sofa/framework/Helper/src/sofa/helper/system/FileRepository.h +++ b/Sofa/framework/Helper/src/sofa/helper/system/FileRepository.h @@ -135,13 +135,20 @@ class SOFA_HELPER_API FileRepository findFile(filename, basedir, errlog); return filename; } - + /// Find file using the stored set of paths. /// @param basefile override current directory by using the parent directory of the given file /// @param filename requested file as input, resolved file path as output /// @return true if the file was found in one of the directories, false otherwise bool findFileFromFile(std::string& filename, const std::string& basefile, std::ostream* errlog=&std::cerr); + /// Find all files in the repository under the given path. + /// @param path Path in vpath to search into. + /// @param files Output vector of found files. + /// @param extensions Extension filter (e.g. ".txt"), empty string means no filter. + /// @param recursive Indicates whether to search recursively into sub-directories. + void findAllFilesInRepository(const std::string& path, std::vector& files, const std::vector &extensions, bool recursive=true); + /// Print the list of path to std::cout void print();