2 Copyright (C) 1998-99 Paul Barton-Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <sys/types.h>
29 #include <pbd/error.h>
30 #include <pbd/pathscanner.h>
31 #include <pbd/stl_delete.h>
36 PathScanner::operator() (const string &dirpath, const string ®exp,
37 bool match_fullpath, bool return_fullpath,
38 long limit, bool recurse)
44 if ((err = regcomp (&compiled_pattern, regexp.c_str(),
45 REG_EXTENDED|REG_NOSUB))) {
47 regerror (err, &compiled_pattern,
50 error << "Cannot compile soundfile regexp for use ("
58 return run_scan (dirpath, &PathScanner::regexp_filter,
59 (bool (*)(const string &, void *)) 0,
67 PathScanner::run_scan (const string &dirpath,
68 bool (PathScanner::*memberfilter)(const string &),
69 bool (*filter)(const string &, void *),
71 bool match_fullpath, bool return_fullpath,
75 return run_scan_internal ((vector<string*>*) 0, dirpath, memberfilter, filter, arg, match_fullpath, return_fullpath, limit, recurse);
79 PathScanner::run_scan_internal (vector<string *> *result,
80 const string &dirpath,
81 bool (PathScanner::*memberfilter)(const string &),
82 bool (*filter)(const string &, void *),
84 bool match_fullpath, bool return_fullpath,
90 char *pathcopy = strdup (dirpath.c_str());
92 char fullpath[PATH_MAX+1];
97 if ((thisdir = strtok (pathcopy, ":")) == 0 ||
98 strlen (thisdir) == 0) {
104 result = new vector<string *>;
109 if ((dir = opendir (thisdir)) == 0) {
113 while ((finfo = readdir (dir)) != 0) {
115 if ((finfo->d_name[0] == '.' && finfo->d_name[1] == '\0') ||
116 (finfo->d_name[0] == '.' && finfo->d_name[1] == '.' && finfo->d_name[2] == '\0')) {
120 snprintf (fullpath, sizeof(fullpath), "%s/%s",
121 thisdir, finfo->d_name);
124 if (stat (fullpath, &statbuf) < 0) {
128 if (statbuf.st_mode & S_IFDIR && recurse) {
129 run_scan_internal (result, fullpath, memberfilter, filter, arg, match_fullpath, return_fullpath, limit, recurse);
132 if (match_fullpath) {
133 search_str = fullpath;
135 search_str = finfo->d_name;
138 /* handle either type of function ptr */
141 if (!(this->*memberfilter)(search_str)) {
145 if (!filter(search_str, arg)) {
150 if (return_fullpath) {
151 newstr = new string (fullpath);
153 newstr = new string (finfo->d_name);
156 result->push_back (newstr);
162 } while ((limit < 0 || (nfound < limit)) && (thisdir = strtok (0, ":")));
169 PathScanner::find_first (const string &dirpath,
170 const string ®exp,
172 bool return_fullpath)
174 vector<string *> *res;
179 if ((err = regcomp (&compiled_pattern, regexp.c_str(),
180 REG_EXTENDED|REG_NOSUB))) {
182 regerror (err, &compiled_pattern,
185 error << "Cannot compile soundfile regexp for use (" << msg << ")" << endmsg;
191 res = run_scan (dirpath,
192 &PathScanner::regexp_filter,
193 (bool (*)(const string &, void *)) 0,
199 if (res->size() == 0) {
210 PathScanner::find_first (const string &dirpath,
211 bool (*filter)(const string &, void *),
214 bool return_fullpath)
216 vector<string *> *res;
219 res = run_scan (dirpath,
220 (bool (PathScanner::*)(const string &)) 0,
226 if (res->size() == 0) {