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"
37 PathScanner::operator() (const string &dirpath, const string ®exp,
38 bool match_fullpath, bool return_fullpath,
39 long limit, bool recurse)
45 if ((err = regcomp (&compiled_pattern, regexp.c_str(),
46 REG_EXTENDED|REG_NOSUB))) {
48 regerror (err, &compiled_pattern,
51 error << "Cannot compile soundfile regexp for use ("
59 return run_scan (dirpath, &PathScanner::regexp_filter,
60 (bool (*)(const string &, void *)) 0,
68 PathScanner::run_scan (const string &dirpath,
69 bool (PathScanner::*memberfilter)(const string &),
70 bool (*filter)(const string &, void *),
72 bool match_fullpath, bool return_fullpath,
76 return run_scan_internal ((vector<string*>*) 0, dirpath, memberfilter, filter, arg, match_fullpath, return_fullpath, limit, recurse);
80 PathScanner::run_scan_internal (vector<string *> *result,
81 const string &dirpath,
82 bool (PathScanner::*memberfilter)(const string &),
83 bool (*filter)(const string &, void *),
85 bool match_fullpath, bool return_fullpath,
91 char *pathcopy = strdup (dirpath.c_str());
93 char fullpath[PATH_MAX+1];
98 if ((thisdir = strtok (pathcopy, ":")) == 0 ||
99 strlen (thisdir) == 0) {
105 result = new vector<string *>;
110 if ((dir = opendir (thisdir)) == 0) {
114 while ((finfo = readdir (dir)) != 0) {
116 if ((finfo->d_name[0] == '.' && finfo->d_name[1] == '\0') ||
117 (finfo->d_name[0] == '.' && finfo->d_name[1] == '.' && finfo->d_name[2] == '\0')) {
121 snprintf (fullpath, sizeof(fullpath), "%s/%s",
122 thisdir, finfo->d_name);
125 if (stat (fullpath, &statbuf) < 0) {
129 if (statbuf.st_mode & S_IFDIR && recurse) {
130 run_scan_internal (result, fullpath, memberfilter, filter, arg, match_fullpath, return_fullpath, limit, recurse);
133 if (match_fullpath) {
134 search_str = fullpath;
136 search_str = finfo->d_name;
139 /* handle either type of function ptr */
142 if (!(this->*memberfilter)(search_str)) {
146 if (!filter(search_str, arg)) {
151 if (return_fullpath) {
152 newstr = new string (fullpath);
154 newstr = new string (finfo->d_name);
157 result->push_back (newstr);
163 } while ((limit < 0 || (nfound < limit)) && (thisdir = strtok (0, ":")));
170 PathScanner::find_first (const string &dirpath,
171 const string ®exp,
173 bool return_fullpath)
175 vector<string *> *res;
180 if ((err = regcomp (&compiled_pattern, regexp.c_str(),
181 REG_EXTENDED|REG_NOSUB))) {
183 regerror (err, &compiled_pattern,
186 error << "Cannot compile soundfile regexp for use (" << msg << ")" << endmsg;
192 res = run_scan (dirpath,
193 &PathScanner::regexp_filter,
194 (bool (*)(const string &, void *)) 0,
200 if (res->size() == 0) {
211 PathScanner::find_first (const string &dirpath,
212 bool (*filter)(const string &, void *),
215 bool return_fullpath)
217 vector<string *> *res;
220 res = run_scan (dirpath,
221 (bool (PathScanner::*)(const string &)) 0,
227 if (res->size() == 0) {