Add function to file_utils intended to be used internally
authorTim Mayberry <mojofunk@gmail.com>
Sun, 22 Jun 2014 03:18:14 +0000 (13:18 +1000)
committerPaul Davis <paul@linuxaudiosystems.com>
Wed, 25 Jun 2014 16:40:10 +0000 (12:40 -0400)
libs/pbd/file_utils.cc

index f0c44333765479f47999b66bb7e9c9898ab97865..95a54c9a8bef52c516904791bca20cbd8b2a2cbf 100644 (file)
@@ -62,6 +62,77 @@ using namespace std;
 
 namespace PBD {
 
+void
+run_functor_for_paths (vector<string>& result,
+                       const Searchpath& paths,
+                       bool (*functor)(const string &, void *),
+                       void *arg,
+                       bool pass_files_only,
+                       bool pass_fullpath, bool return_fullpath,
+                       bool recurse)
+{
+       for (vector<string>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
+               string expanded_path = path_expand (*i);
+               DEBUG_TRACE (DEBUG::FileUtils,
+                               string_compose("Find files in expanded path: %1\n", expanded_path));
+
+               if (!Glib::file_test (expanded_path, Glib::FILE_TEST_IS_DIR)) continue;
+
+               try
+               {
+                       Glib::Dir dir(expanded_path);
+
+                       for (Glib::DirIterator di = dir.begin(); di != dir.end(); di++) {
+
+                               string fullpath = Glib::build_filename (expanded_path, *di);
+                               string basename = *di;
+
+                               bool is_dir = Glib::file_test (fullpath, Glib::FILE_TEST_IS_DIR);
+
+                               if (is_dir && recurse) {
+                                       DEBUG_TRACE (DEBUG::FileUtils,
+                                                       string_compose("Descending into directory:  %1\n",
+                                                               fullpath));
+                                       run_functor_for_paths (result, fullpath, functor, arg, pass_files_only,
+                                                              pass_fullpath, return_fullpath, recurse);
+                               }
+
+                               if (is_dir && pass_files_only) {
+                                       continue;
+                               }
+
+                               string functor_str;
+
+                               if (pass_fullpath) {
+                                       functor_str = fullpath;
+                               } else {
+                                       functor_str = basename;
+                               }
+
+                               DEBUG_TRACE (DEBUG::FileUtils,
+                                               string_compose("Run Functor using string: %1\n", functor_str));
+
+                               if (!functor(functor_str, arg)) {
+                                       continue;
+                               }
+
+                               DEBUG_TRACE (DEBUG::FileUtils,
+                                               string_compose("Found file %1 matching functor\n", functor_str));
+
+                               if (return_fullpath) {
+                                       result.push_back(fullpath);
+                               } else {
+                                       result.push_back(basename);
+                               }
+                       }
+               }
+               catch (Glib::FileError& err)
+               {
+                       warning << err.what() << endmsg;
+               }
+       }
+}
+
 void
 get_directory_contents (const std::string& directory_path,
                        vector<string>& result,