Add PBD::get_directory_contents to pbd/file_utils.h
authorTim Mayberry <mojofunk@gmail.com>
Thu, 19 Jun 2014 02:31:19 +0000 (12:31 +1000)
committerPaul Davis <paul@linuxaudiosystems.com>
Wed, 25 Jun 2014 16:40:09 +0000 (12:40 -0400)
libs/pbd/file_utils.cc
libs/pbd/pbd/file_utils.h

index 544daa2e152ca6cb84f00e2bcf30164e9200938c..bdc03dc143a6a4adcd21ca203c1381ea9d7ef29b 100644 (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)
 {
index 8a3b014ba59a708cce8509880db71a64146f9c7d..151d177275f414a7c156e145a891abc675dda45f 100644 (file)
 
 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