Move member functions from PathScanner to functions in pbd/file_utils.h
authorTim Mayberry <mojofunk@gmail.com>
Tue, 17 Jun 2014 02:16:37 +0000 (12:16 +1000)
committerTim Mayberry <mojofunk@gmail.com>
Tue, 17 Jun 2014 11:19:02 +0000 (21:19 +1000)
This allows us to remove PathScanner source file and keep PathScanner class
as header only until it is removed

libs/pbd/file_utils.cc
libs/pbd/pathscanner.cc [deleted file]
libs/pbd/pbd/file_utils.h
libs/pbd/pbd/pathscanner.h
libs/pbd/wscript

index 552012d2274f84fdceb49f219712401b767ba923..7e58f0fed5ebd061a9773beff0c1a94692cf988f 100644 (file)
@@ -1,5 +1,6 @@
 /*
-    Copyright (C) 2007 Tim Mayberry 
+    Copyright (C) 2007-2014 Tim Mayberry
+    Copyright (C) 1998-2014 Paul Davis
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
 /* close(), read(), write() */
 #ifdef COMPILER_MSVC
 #include <io.h> // Microsoft's nearest equivalent to <unistd.h>
+#include <ardourext/misc.h>
 #else
+#include <dirent.h>
 #include <unistd.h>
+#include <regex.h>
+#endif
+
+#ifdef PLATFORM_WINDOWS
+#define strtok_r strtok_s
 #endif
 
 #include "pbd/compose.h"
@@ -51,6 +59,7 @@
 #include "pbd/debug.h"
 #include "pbd/error.h"
 #include "pbd/pathscanner.h"
+#include "pbd/pathexpand.h"
 #include "pbd/stl_delete.h"
 
 #include "i18n.h"
@@ -161,6 +170,123 @@ find_file_in_search_path(const Searchpath& search_path,
        return true;
 }
 
+static
+bool
+regexp_filter (const string& str, void *arg)
+{
+       regex_t* pattern = (regex_t*)arg;
+       return regexec (pattern, str.c_str(), 0, 0, 0) == 0;
+}
+
+void
+find_files_matching_regex (vector<string>& result,
+                           const std::string& dirpath,
+                           const std::string& regexp,
+                           bool match_fullpath, bool return_fullpath,
+                           long limit,
+                           bool recurse)
+{
+       int err;
+       char msg[256];
+       regex_t compiled_pattern;
+
+       if ((err = regcomp (&compiled_pattern, regexp.c_str(),
+                           REG_EXTENDED|REG_NOSUB))) {
+
+               regerror (err, &compiled_pattern,
+                         msg, sizeof (msg));
+
+               error << "Cannot compile soundfile regexp for use ("
+                     << msg
+                     << ")"
+                     << endmsg;
+
+               return;
+       }
+
+       find_files_matching_filter (result, dirpath,
+                                   regexp_filter, &compiled_pattern,
+                                   match_fullpath, return_fullpath,
+                                   limit, recurse);
+
+       regfree (&compiled_pattern);
+}
+
+void
+find_files_matching_filter (vector<string>& result,
+                            const string &dirpath,
+                            bool (*filter)(const string &, void *),
+                            void *arg,
+                            bool match_fullpath, bool return_fullpath,
+                            long limit,
+                            bool recurse)
+{
+       DIR *dir;
+       struct dirent *finfo;
+       char *pathcopy = strdup (search_path_expand (dirpath).c_str());
+       char *thisdir;
+       string fullpath;
+       string search_str;
+       long nfound = 0;
+       char *saveptr;
+
+       if ((thisdir = strtok_r (pathcopy, G_SEARCHPATH_SEPARATOR_S, &saveptr)) == 0 ||
+           strlen (thisdir) == 0) {
+               free (pathcopy);
+               return;
+       }
+
+       do {
+
+               if ((dir = opendir (thisdir)) == 0) {
+                       continue;
+               }
+
+               while ((finfo = readdir (dir)) != 0) {
+
+                       if ((finfo->d_name[0] == '.' && finfo->d_name[1] == '\0') ||
+                           (finfo->d_name[0] == '.' && finfo->d_name[1] == '.' && finfo->d_name[2] == '\0')) {
+                               continue;
+                       }
+
+                       fullpath = Glib::build_filename (thisdir, finfo->d_name);
+
+                       struct stat statbuf;
+                       if (stat (fullpath.c_str(), &statbuf) < 0) {
+                               continue;
+                       }
+
+                       if (statbuf.st_mode & S_IFDIR && recurse) {
+                               find_files_matching_filter (result, fullpath, filter, arg, match_fullpath, return_fullpath, limit, recurse);
+                       } else {
+
+                               if (match_fullpath) {
+                                       search_str = fullpath;
+                               } else {
+                                       search_str = finfo->d_name;
+                               }
+
+                               if (!filter(search_str, arg)) {
+                                       continue;
+                               }
+
+                               if (return_fullpath) {
+                                       result.push_back(fullpath);
+                               } else {
+                                       result.push_back(finfo->d_name);
+                               }
+
+                               nfound++;
+                       }
+               }
+               closedir (dir);
+
+       } while ((limit < 0 || (nfound < limit)) && (thisdir = strtok_r (0, G_SEARCHPATH_SEPARATOR_S, &saveptr)));
+
+       free (pathcopy);
+       return;
+}
+
 bool
 copy_file(const std::string & from_path, const std::string & to_path)
 {
diff --git a/libs/pbd/pathscanner.cc b/libs/pbd/pathscanner.cc
deleted file mode 100644 (file)
index a320ec8..0000000
+++ /dev/null
@@ -1,162 +0,0 @@
-/*
-    Copyright (C) 1998-99 Paul Barton-Davis 
-
-    This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
-    $Id$
-*/
-
-#ifdef COMPILER_MSVC
-#include <stdlib.h>
-#include <stdio.h>
-using PBD::readdir;
-using PBD::opendir;
-using PBD::closedir;
-#define strtok_r strtok_s // @john: this should probably go to msvc_extra_headers/ardourext/misc.h.input instead of the current define there
-#else
-#include <dirent.h>
-#include <cstdlib>
-#include <cstdio>
-#endif
-#include <cstring>
-#include <vector>
-#include <sys/types.h>
-#include <sys/stat.h>
-
-#include <glibmm/miscutils.h>
-
-#include "pbd/error.h"
-#include "pbd/pathexpand.h"
-#include "pbd/pathscanner.h"
-
-using namespace std;
-using namespace PBD;
-
-static
-bool
-regexp_filter (const string& str, void *arg)
-{
-       regex_t* pattern = (regex_t*)arg;
-       return regexec (pattern, str.c_str(), 0, 0, 0) == 0;
-}
-
-void
-PathScanner::find_files_matching_regex (vector<string>& result,
-               const std::string& dirpath,
-               const std::string& regexp,
-               bool match_fullpath, bool return_fullpath,
-               long limit,
-               bool recurse)
-{
-       int err;
-       char msg[256];
-       regex_t compiled_pattern;
-
-       if ((err = regcomp (&compiled_pattern, regexp.c_str(),
-                           REG_EXTENDED|REG_NOSUB))) {
-               
-               regerror (err, &compiled_pattern,
-                         msg, sizeof (msg));
-               
-               error << "Cannot compile soundfile regexp for use (" 
-                     << msg 
-                     << ")" 
-                     << endmsg;
-               
-               return;
-       }
-
-       find_files_matching_filter (result, dirpath,
-                                   regexp_filter, &compiled_pattern,
-                                   match_fullpath, return_fullpath,
-                                   limit, recurse);
-
-       regfree (&compiled_pattern);
-}
-       
-void
-PathScanner::find_files_matching_filter (vector<string>& result,
-                                        const string &dirpath,
-                                        bool (*filter)(const string &, void *),
-                                        void *arg,
-                                        bool match_fullpath, bool return_fullpath,
-                                        long limit,
-                                        bool recurse)
-{
-       DIR *dir;
-       struct dirent *finfo;
-       char *pathcopy = strdup (search_path_expand (dirpath).c_str());
-       char *thisdir;
-       string fullpath;
-       string search_str;
-       long nfound = 0;
-       char *saveptr;
-
-       if ((thisdir = strtok_r (pathcopy, G_SEARCHPATH_SEPARATOR_S, &saveptr)) == 0 ||
-           strlen (thisdir) == 0) {
-               free (pathcopy);
-               return;
-       }
-
-       do {
-
-               if ((dir = opendir (thisdir)) == 0) {
-                       continue;
-               }
-               
-               while ((finfo = readdir (dir)) != 0) {
-
-                       if ((finfo->d_name[0] == '.' && finfo->d_name[1] == '\0') ||
-                           (finfo->d_name[0] == '.' && finfo->d_name[1] == '.' && finfo->d_name[2] == '\0')) {
-                               continue;
-                       }
-                        
-                        fullpath = Glib::build_filename (thisdir, finfo->d_name);
-
-                       struct stat statbuf;
-                       if (stat (fullpath.c_str(), &statbuf) < 0) {
-                               continue;
-                       }
-
-                       if (statbuf.st_mode & S_IFDIR && recurse) {
-                               find_files_matching_filter (result, fullpath, filter, arg, match_fullpath, return_fullpath, limit, recurse);
-                       } else {
-                               
-                               if (match_fullpath) {
-                                       search_str = fullpath;
-                               } else {
-                                       search_str = finfo->d_name;
-                               }
-                               
-                               if (!filter(search_str, arg)) {
-                                       continue;
-                               }
-
-                               if (return_fullpath) {
-                                       result.push_back(fullpath);
-                               } else {
-                                       result.push_back(finfo->d_name);
-                               } 
-                               
-                               nfound++;
-                       }
-               }
-               closedir (dir);
-               
-       } while ((limit < 0 || (nfound < limit)) && (thisdir = strtok_r (0, G_SEARCHPATH_SEPARATOR_S, &saveptr)));
-
-       free (pathcopy);
-       return;
-}
index 01ff8606a722b0eda0d041812f569ed5ad60afe9..51fb2c1ad1619027804193cde76ec1e735140718 100644 (file)
@@ -92,6 +92,32 @@ find_file_in_search_path (const Searchpath& search_path,
                           const std::string& filename,
                           std::string& result);
 
+
+/**
+ * @return files in dirpath that match a regular expression
+ */
+LIBPBD_API void
+find_files_matching_regex (std::vector<std::string>& results,
+                           const std::string& dirpath,
+                           const std::string& regexp,
+                           bool match_fullpath,
+                           bool return_fullpath,
+                           long limit,
+                           bool recurse = false);
+
+/**
+ * @return files in dirpath that match a supplied filter(functor)
+ */
+LIBPBD_API void
+find_files_matching_filter (std::vector<std::string>&,
+                            const std::string &dirpath,
+                            bool (*filter)(const std::string &, void *),
+                            void *arg,
+                            bool match_fullpath,
+                            bool return_fullpath,
+                            long limit,
+                            bool recurse = false);
+
 /**
  * Attempt to copy the contents of the file from_path to a new file
  * at path to_path. If to_path exists it is overwritten.
index d9522affe307f0402cd5d32ccbcc127c3200d404..e4d2f6aa0efcc2b4b796acdaa416bd15af2571a2 100644 (file)
@@ -30,6 +30,8 @@
 
 #include "pbd/libpbd_visibility.h"
 
+#include "pbd/file_utils.h"
+
 class LIBPBD_API PathScanner
 
 {
@@ -42,10 +44,10 @@ class LIBPBD_API PathScanner
                                             long limit = -1,
                                             bool recurse = false) {
                std::vector<std::string> result;
-               find_files_matching_filter (result, dirpath,
-                                           filter, arg,
-                                           match_fullpath, return_fullpath,
-                                           limit, recurse);
+               PBD::find_files_matching_filter (result, dirpath,
+                                                filter, arg,
+                                                match_fullpath, return_fullpath,
+                                                limit, recurse);
                return result;
        }
 
@@ -58,34 +60,15 @@ class LIBPBD_API PathScanner
        {
                std::vector<std::string> result;
 
-               find_files_matching_regex (result,
-                                          dirpath,
-                                          regexp,
-                                          match_fullpath,
-                                          return_fullpath,
-                                          limit, recurse);
+               PBD::find_files_matching_regex (result,
+                                               dirpath,
+                                               regexp,
+                                               match_fullpath,
+                                               return_fullpath,
+                                               limit, recurse);
 
                return result;
        }
-
-  private:
-
-       void find_files_matching_regex (std::vector<std::string>& results,
-                       const std::string& dirpath,
-                       const std::string& regexp,
-                       bool match_fullpath,
-                       bool return_fullpath,
-                       long limit,
-                       bool recurse = false);
-
-       void find_files_matching_filter (std::vector<std::string>&,
-                                       const std::string &dirpath,
-                                       bool (*filter)(const std::string &, void *),
-                                       void *arg,
-                                       bool match_fullpath,
-                                       bool return_fullpath,
-                                       long limit,
-                                       bool recurse = false);
 };
 
 #endif // __libmisc_pathscanner_h__
index 2ba79d0c34d6fdd8ec2fbcf59fcadcadd9774c0a..35f4b4c5c75ce62be6d3516c2e79992e7c864132 100644 (file)
@@ -57,7 +57,6 @@ libpbd_sources = [
     'mountpoint.cc',
     'openuri.cc',
     'pathexpand.cc',
-    'pathscanner.cc',
     'pbd.cc',
     'pool.cc',
     'property_list.cc',