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>
33 PathScanner::operator() (const string &dirpath, const string ®exp,
34 bool match_fullpath, bool return_fullpath,
41 if ((err = regcomp (&compiled_pattern, regexp.c_str(),
42 REG_EXTENDED|REG_NOSUB))) {
44 regerror (err, &compiled_pattern,
47 error << "Cannot compile soundfile regexp for use ("
55 return run_scan (dirpath, &PathScanner::regexp_filter,
56 (bool (*)(const string &, void *)) 0,
64 PathScanner::run_scan (const string &dirpath,
65 bool (PathScanner::*memberfilter)(const string &),
66 bool (*filter)(const string &, void *),
68 bool match_fullpath, bool return_fullpath,
72 vector<string *> *result = 0;
75 char *pathcopy = strdup (dirpath.c_str());
77 char fullpath[PATH_MAX+1];
82 if ((thisdir = strtok (pathcopy, ":")) == 0 ||
83 strlen (thisdir) == 0) {
88 result = new vector<string *>;
92 if ((dir = opendir (thisdir)) == 0) {
96 while ((finfo = readdir (dir)) != 0) {
98 snprintf (fullpath, sizeof(fullpath), "%s/%s",
99 thisdir, finfo->d_name);
101 if (match_fullpath) {
102 search_str = fullpath;
104 search_str = finfo->d_name;
107 /* handle either type of function ptr */
110 if (!(this->*memberfilter)(search_str)) {
114 if (!filter(search_str, arg)) {
119 if (return_fullpath) {
120 newstr = new string (fullpath);
122 newstr = new string (finfo->d_name);
125 result->push_back (newstr);
131 } while ((limit < 0 || (nfound < limit)) && (thisdir = strtok (0, ":")));
138 PathScanner::find_first (const string &dirpath,
139 const string ®exp,
141 bool return_fullpath)
143 vector<string *> *res;
148 if ((err = regcomp (&compiled_pattern, regexp.c_str(),
149 REG_EXTENDED|REG_NOSUB))) {
151 regerror (err, &compiled_pattern,
154 error << "Cannot compile soundfile regexp for use (" << msg << ")" << endmsg;
160 res = run_scan (dirpath,
161 &PathScanner::regexp_filter,
162 (bool (*)(const string &, void *)) 0,
168 if (res->size() == 0) {
179 PathScanner::find_first (const string &dirpath,
180 bool (*filter)(const string &, void *),
183 bool return_fullpath)
185 vector<string *> *res;
188 res = run_scan (dirpath,
189 (bool (PathScanner::*)(const string &)) 0,
195 if (res->size() == 0) {