diff --git a/libs/pbd/file_utils.cc b/libs/pbd/file_utils.cc index 544daa2e15..bdc03dc143 100644 --- a/libs/pbd/file_utils.cc +++ b/libs/pbd/file_utils.cc @@ -64,6 +64,46 @@ using namespace std; namespace PBD { +void +get_directory_contents (const std::string& directory_path, + vector& result, + bool files_only, + bool recurse) +{ + // perhaps we don't need this check assuming an exception is thrown + // as it would save checking that the path is a directory twice when + // recursing + if (!Glib::file_test (directory_path, Glib::FILE_TEST_IS_DIR)) return; + + try + { + Glib::Dir dir(directory_path); + Glib::DirIterator i = dir.begin(); + + while (i != dir.end()) { + + string fullpath = Glib::build_filename (directory_path, *i); + + bool is_dir = Glib::file_test (fullpath, Glib::FILE_TEST_IS_DIR); + + if (is_dir && recurse) { + get_directory_contents (fullpath, result, files_only, recurse); + } + + i++; + + if (is_dir && files_only) { + continue; + } + result.push_back (fullpath); + } + } + catch (Glib::FileError& err) + { + warning << err.what() << endmsg; + } +} + void get_files_in_directory (const std::string& directory_path, vector& result) { diff --git a/libs/pbd/pbd/file_utils.h b/libs/pbd/pbd/file_utils.h index 8a3b014ba5..151d177275 100644 --- a/libs/pbd/pbd/file_utils.h +++ b/libs/pbd/pbd/file_utils.h @@ -30,6 +30,22 @@ namespace PBD { +/** + * Get a contents of directory. + * @note paths in result will be absolute + * + * @param path An absolute path to a directory in the filename encoding + * @param result A vector of absolute paths to the directory entries in filename + * encoding. + * @param files_only Only include file entries in result + * @param recurse Recurse into child directories + */ +LIBPBD_API void +get_directory_contents (const std::string& path, + std::vector& result, + bool files_only = true, + bool recurse = false); + /** * Get a list of files in a directory. * @note You must join path with result to get the absolute path