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.
27 #include <pbd/error.h>
28 #include <pbd/pathscanner.h>
29 #include <pbd/stl_delete.h>
34 PathScanner::operator() (const string &dirpath, const string ®exp,
35 bool match_fullpath, bool return_fullpath,
42 if ((err = regcomp (&compiled_pattern, regexp.c_str(),
43 REG_EXTENDED|REG_NOSUB))) {
45 regerror (err, &compiled_pattern,
48 error << "Cannot compile soundfile regexp for use ("
56 return run_scan (dirpath, &PathScanner::regexp_filter,
57 (bool (*)(const string &, void *)) 0,
65 PathScanner::run_scan (const string &dirpath,
66 bool (PathScanner::*memberfilter)(const string &),
67 bool (*filter)(const string &, void *),
69 bool match_fullpath, bool return_fullpath,
73 vector<string *> *result = 0;
76 char *pathcopy = strdup (dirpath.c_str());
78 char fullpath[PATH_MAX+1];
83 if ((thisdir = strtok (pathcopy, ":")) == 0 ||
84 strlen (thisdir) == 0) {
89 result = new vector<string *>;
93 if ((dir = opendir (thisdir)) == 0) {
97 while ((finfo = readdir (dir)) != 0) {
99 snprintf (fullpath, sizeof(fullpath), "%s/%s",
100 thisdir, finfo->d_name);
102 if (match_fullpath) {
103 search_str = fullpath;
105 search_str = finfo->d_name;
108 /* handle either type of function ptr */
111 if (!(this->*memberfilter)(search_str)) {
115 if (!filter(search_str, arg)) {
120 if (return_fullpath) {
121 newstr = new string (fullpath);
123 newstr = new string (finfo->d_name);
126 result->push_back (newstr);
132 } while ((limit < 0 || (nfound < limit)) && (thisdir = strtok (0, ":")));
139 PathScanner::find_first (const string &dirpath,
140 const string ®exp,
142 bool return_fullpath)
144 vector<string *> *res;
149 if ((err = regcomp (&compiled_pattern, regexp.c_str(),
150 REG_EXTENDED|REG_NOSUB))) {
152 regerror (err, &compiled_pattern,
155 error << "Cannot compile soundfile regexp for use (" << msg << ")" << endmsg;
161 res = run_scan (dirpath,
162 &PathScanner::regexp_filter,
163 (bool (*)(const string &, void *)) 0,
169 if (res->size() == 0) {
180 PathScanner::find_first (const string &dirpath,
181 bool (*filter)(const string &, void *),
184 bool return_fullpath)
186 vector<string *> *res;
189 res = run_scan (dirpath,
190 (bool (PathScanner::*)(const string &)) 0,
196 if (res->size() == 0) {