Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Sofa/framework/Helper/src/sofa/helper/system/FileRepository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>& files, const std::vector<std::string>& 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<std::string>::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<std::string>::const_iterator it = vpath.begin(); it != vpath.end(); ++it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>& files, const std::vector<std::string> &extensions, bool recursive=true);

/// Print the list of path to std::cout
void print();

Expand Down
Loading