diff options
author | Tim Mayberry <mojofunk@gmail.com> | 2014-06-19 12:31:19 +1000 |
---|---|---|
committer | Paul Davis <paul@linuxaudiosystems.com> | 2014-06-25 12:40:09 -0400 |
commit | d1dd5d3ee766791b60b8a60fc46e5086b4ffee90 (patch) | |
tree | 8888d364fa8f0224ecf84bf9519c7d0e21140eb8 /libs/pbd/file_utils.cc | |
parent | c1ff79e2e66ff552b061f3a5e71c308f68980866 (diff) |
Add PBD::get_directory_contents to pbd/file_utils.h
Diffstat (limited to 'libs/pbd/file_utils.cc')
-rw-r--r-- | libs/pbd/file_utils.cc | 40 |
1 files changed, 40 insertions, 0 deletions
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 @@ -65,6 +65,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) { if (!Glib::file_test (directory_path, Glib::FILE_TEST_IS_DIR)) return; |