Add PBD::get_directory_contents to pbd/file_utils.h

This commit is contained in:
Tim Mayberry 2014-06-19 12:31:19 +10:00 committed by Paul Davis
parent c1ff79e2e6
commit d1dd5d3ee7
2 changed files with 56 additions and 0 deletions

View File

@ -64,6 +64,46 @@ using namespace std;
namespace PBD {
void
get_directory_contents (const std::string& directory_path,
vector<string>& 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<string>& result)
{

View File

@ -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<std::string>& 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