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 <pbd/error.h>
27 #include <pbd/pathscanner.h>
28 #include <pbd/stl_delete.h>
31 PathScanner::operator() (const string &dirpath, const string ®exp,
32 bool match_fullpath, bool return_fullpath,
39 if ((err = regcomp (&compiled_pattern, regexp.c_str(),
40 REG_EXTENDED|REG_NOSUB))) {
42 regerror (err, &compiled_pattern,
45 error << "Cannot compile soundfile regexp for use ("
53 return run_scan (dirpath, &PathScanner::regexp_filter,
54 (bool (*)(const string &, void *)) 0,
62 PathScanner::run_scan (const string &dirpath,
63 bool (PathScanner::*memberfilter)(const string &),
64 bool (*filter)(const string &, void *),
66 bool match_fullpath, bool return_fullpath,
70 vector<string *> *result = 0;
73 char *pathcopy = strdup (dirpath.c_str());
75 char fullpath[PATH_MAX+1];
80 if ((thisdir = strtok (pathcopy, ":")) == 0 ||
81 strlen (thisdir) == 0) {
86 result = new vector<string *>;
90 if ((dir = opendir (thisdir)) == 0) {
94 while ((finfo = readdir (dir)) != 0) {
96 snprintf (fullpath, sizeof(fullpath), "%s/%s",
97 thisdir, finfo->d_name);
100 search_str = fullpath;
102 search_str = finfo->d_name;
105 /* handle either type of function ptr */
108 if (!(this->*memberfilter)(search_str)) {
112 if (!filter(search_str, arg)) {
117 if (return_fullpath) {
118 newstr = new string (fullpath);
120 newstr = new string (finfo->d_name);
123 result->push_back (newstr);
129 } while ((limit < 0 || (nfound < limit)) && (thisdir = strtok (0, ":")));
136 PathScanner::find_first (const string &dirpath,
137 const string ®exp,
139 bool return_fullpath)
141 vector<string *> *res;
146 if ((err = regcomp (&compiled_pattern, regexp.c_str(),
147 REG_EXTENDED|REG_NOSUB))) {
149 regerror (err, &compiled_pattern,
152 error << "Cannot compile soundfile regexp for use (" << msg << ")" << endmsg;
158 res = run_scan (dirpath,
159 &PathScanner::regexp_filter,
160 (bool (*)(const string &, void *)) 0,
166 if (res->size() == 0) {
177 PathScanner::find_first (const string &dirpath,
178 bool (*filter)(const string &, void *),
181 bool return_fullpath)
183 vector<string *> *res;
186 res = run_scan (dirpath,
187 (bool (PathScanner::*)(const string &)) 0,
193 if (res->size() == 0) {