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.
34 #include <sys/types.h>
37 #include <glibmm/miscutils.h>
39 #include "pbd/error.h"
40 #include "pbd/pathexpand.h"
41 #include "pbd/pathscanner.h"
42 #include "pbd/stl_delete.h"
48 PathScanner::operator() (const string &dirpath, const string ®exp,
49 bool match_fullpath, bool return_fullpath,
50 long limit, bool recurse)
56 if ((err = regcomp (&compiled_pattern, regexp.c_str(),
57 REG_EXTENDED|REG_NOSUB))) {
59 regerror (err, &compiled_pattern,
62 error << "Cannot compile soundfile regexp for use ("
70 return run_scan (dirpath, &PathScanner::regexp_filter,
71 (bool (*)(const string &, void *)) 0,
79 PathScanner::run_scan (const string &dirpath,
80 bool (PathScanner::*memberfilter)(const string &),
81 bool (*filter)(const string &, void *),
83 bool match_fullpath, bool return_fullpath,
87 return run_scan_internal ((vector<string*>*) 0, dirpath, memberfilter, filter, arg, match_fullpath, return_fullpath, limit, recurse);
91 PathScanner::run_scan_internal (vector<string *> *result,
92 const string &dirpath,
93 bool (PathScanner::*memberfilter)(const string &),
94 bool (*filter)(const string &, void *),
96 bool match_fullpath, bool return_fullpath,
101 struct dirent *finfo;
102 char *pathcopy = strdup (search_path_expand (dirpath).c_str());
109 if ((thisdir = strtok (pathcopy, ":")) == 0 ||
110 strlen (thisdir) == 0) {
116 result = new vector<string *>;
121 if ((dir = opendir (thisdir)) == 0) {
125 while ((finfo = readdir (dir)) != 0) {
127 if ((finfo->d_name[0] == '.' && finfo->d_name[1] == '\0') ||
128 (finfo->d_name[0] == '.' && finfo->d_name[1] == '.' && finfo->d_name[2] == '\0')) {
132 fullpath = Glib::build_filename (thisdir, finfo->d_name);
135 if (stat (fullpath.c_str(), &statbuf) < 0) {
139 if (statbuf.st_mode & S_IFDIR && recurse) {
140 run_scan_internal (result, fullpath, memberfilter, filter, arg, match_fullpath, return_fullpath, limit, recurse);
143 if (match_fullpath) {
144 search_str = fullpath;
146 search_str = finfo->d_name;
149 /* handle either type of function ptr */
152 if (!(this->*memberfilter)(search_str)) {
156 if (!filter(search_str, arg)) {
161 if (return_fullpath) {
162 newstr = new string (fullpath);
164 newstr = new string (finfo->d_name);
167 result->push_back (newstr);
173 } while ((limit < 0 || (nfound < limit)) && (thisdir = strtok (0, ":")));
180 PathScanner::find_first (const string &dirpath,
181 const string ®exp,
183 bool return_fullpath)
185 vector<string *> *res;
190 if ((err = regcomp (&compiled_pattern, regexp.c_str(),
191 REG_EXTENDED|REG_NOSUB))) {
193 regerror (err, &compiled_pattern,
196 error << "Cannot compile soundfile regexp for use (" << msg << ")" << endmsg;
202 res = run_scan (dirpath,
203 &PathScanner::regexp_filter,
204 (bool (*)(const string &, void *)) 0,
210 if (res->size() == 0) {
221 PathScanner::find_first (const string &dirpath,
222 bool (*filter)(const string &, void *),
225 bool return_fullpath)
227 vector<string *> *res;
230 res = run_scan (dirpath,
231 (bool (PathScanner::*)(const string &)) 0,
237 if (res->size() == 0) {