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 #define strtok_r strtok_s // @john: this should probably go to msvc_extra_headers/ardourext/misc.h.input instead of the current define there
35 #include <sys/types.h>
38 #include <glibmm/miscutils.h>
40 #include "pbd/error.h"
41 #include "pbd/pathexpand.h"
42 #include "pbd/pathscanner.h"
49 regexp_filter (const string& str, void *arg)
51 regex_t* pattern = (regex_t*)arg;
52 return regexec (pattern, str.c_str(), 0, 0, 0) == 0;
56 PathScanner::find_files_matching_regex (vector<string>& result,
57 const std::string& dirpath,
58 const std::string& regexp,
59 bool match_fullpath, bool return_fullpath,
65 regex_t compiled_pattern;
67 if ((err = regcomp (&compiled_pattern, regexp.c_str(),
68 REG_EXTENDED|REG_NOSUB))) {
70 regerror (err, &compiled_pattern,
73 error << "Cannot compile soundfile regexp for use ("
81 run_scan_internal (result, dirpath,
82 regexp_filter, &compiled_pattern,
83 match_fullpath, return_fullpath,
86 regfree (&compiled_pattern);
90 PathScanner::operator() (const string &dirpath, const string ®exp,
91 bool match_fullpath, bool return_fullpath,
92 long limit, bool recurse)
95 vector<string> result;
97 find_files_matching_regex (result,
108 PathScanner::run_scan_internal (vector<string>& result,
109 const string &dirpath,
110 bool (*filter)(const string &, void *),
112 bool match_fullpath, bool return_fullpath,
117 struct dirent *finfo;
118 char *pathcopy = strdup (search_path_expand (dirpath).c_str());
125 if ((thisdir = strtok_r (pathcopy, G_SEARCHPATH_SEPARATOR_S, &saveptr)) == 0 ||
126 strlen (thisdir) == 0) {
133 if ((dir = opendir (thisdir)) == 0) {
137 while ((finfo = readdir (dir)) != 0) {
139 if ((finfo->d_name[0] == '.' && finfo->d_name[1] == '\0') ||
140 (finfo->d_name[0] == '.' && finfo->d_name[1] == '.' && finfo->d_name[2] == '\0')) {
144 fullpath = Glib::build_filename (thisdir, finfo->d_name);
147 if (stat (fullpath.c_str(), &statbuf) < 0) {
151 if (statbuf.st_mode & S_IFDIR && recurse) {
152 run_scan_internal (result, fullpath, filter, arg, match_fullpath, return_fullpath, limit, recurse);
155 if (match_fullpath) {
156 search_str = fullpath;
158 search_str = finfo->d_name;
161 if (!filter(search_str, arg)) {
165 if (return_fullpath) {
166 result.push_back(fullpath);
168 result.push_back(finfo->d_name);
176 } while ((limit < 0 || (nfound < limit)) && (thisdir = strtok_r (0, G_SEARCHPATH_SEPARATOR_S, &saveptr)));
183 PathScanner::find_first (const string &dirpath,
184 const string ®exp,
186 bool return_fullpath)
190 find_files_matching_regex (res, dirpath, regexp,
191 match_fullpath, return_fullpath, 1);
193 if (res.size() == 0) {
201 PathScanner::find_first (const string &dirpath,
202 bool (*filter)(const string &, void *),
205 bool return_fullpath)
210 run_scan_internal (res,
217 if (res.size() == 0) {