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.
28 #include "pbd/error.h"
29 #include "pbd/pathscanner.h"
30 #include "pbd/stl_delete.h"
35 PathScanner::operator() (const string &dirpath, const string ®exp,
36 bool match_fullpath, bool return_fullpath,
43 if ((err = regcomp (&compiled_pattern, regexp.c_str(),
44 REG_EXTENDED|REG_NOSUB))) {
46 regerror (err, &compiled_pattern,
49 error << "Cannot compile soundfile regexp for use ("
57 return run_scan (dirpath, &PathScanner::regexp_filter,
58 (bool (*)(const string &, void *)) 0,
66 PathScanner::run_scan (const string &dirpath,
67 bool (PathScanner::*memberfilter)(const string &),
68 bool (*filter)(const string &, void *),
70 bool match_fullpath, bool return_fullpath,
74 vector<string *> *result = 0;
77 char *pathcopy = strdup (dirpath.c_str());
79 char fullpath[PATH_MAX+1];
84 if ((thisdir = strtok (pathcopy, ":")) == 0 ||
85 strlen (thisdir) == 0) {
90 result = new vector<string *>;
94 if ((dir = opendir (thisdir)) == 0) {
98 while ((finfo = readdir (dir)) != 0) {
100 snprintf (fullpath, sizeof(fullpath), "%s/%s",
101 thisdir, finfo->d_name);
103 if (match_fullpath) {
104 search_str = fullpath;
106 search_str = finfo->d_name;
109 /* handle either type of function ptr */
112 if (!(this->*memberfilter)(search_str)) {
116 if (!filter(search_str, arg)) {
121 if (return_fullpath) {
122 newstr = new string (fullpath);
124 newstr = new string (finfo->d_name);
127 result->push_back (newstr);
133 } while ((limit < 0 || (nfound < limit)) && (thisdir = strtok (0, ":")));
140 PathScanner::find_first (const string &dirpath,
141 const string ®exp,
143 bool return_fullpath)
145 vector<string *> *res;
150 if ((err = regcomp (&compiled_pattern, regexp.c_str(),
151 REG_EXTENDED|REG_NOSUB))) {
153 regerror (err, &compiled_pattern,
156 error << "Cannot compile soundfile regexp for use (" << msg << ")" << endmsg;
162 res = run_scan (dirpath,
163 &PathScanner::regexp_filter,
164 (bool (*)(const string &, void *)) 0,
170 if (res->size() == 0) {
181 PathScanner::find_first (const string &dirpath,
182 bool (*filter)(const string &, void *),
185 bool return_fullpath)
187 vector<string *> *res;
190 res = run_scan (dirpath,
191 (bool (PathScanner::*)(const string &)) 0,
197 if (res->size() == 0) {