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 find_files_matching_filter (result, dirpath,
82 regexp_filter, &compiled_pattern,
83 match_fullpath, return_fullpath,
86 regfree (&compiled_pattern);
90 PathScanner::find_files_matching_filter (vector<string>& result,
91 const string &dirpath,
92 bool (*filter)(const string &, void *),
94 bool match_fullpath, bool return_fullpath,
100 char *pathcopy = strdup (search_path_expand (dirpath).c_str());
107 if ((thisdir = strtok_r (pathcopy, G_SEARCHPATH_SEPARATOR_S, &saveptr)) == 0 ||
108 strlen (thisdir) == 0) {
115 if ((dir = opendir (thisdir)) == 0) {
119 while ((finfo = readdir (dir)) != 0) {
121 if ((finfo->d_name[0] == '.' && finfo->d_name[1] == '\0') ||
122 (finfo->d_name[0] == '.' && finfo->d_name[1] == '.' && finfo->d_name[2] == '\0')) {
126 fullpath = Glib::build_filename (thisdir, finfo->d_name);
129 if (stat (fullpath.c_str(), &statbuf) < 0) {
133 if (statbuf.st_mode & S_IFDIR && recurse) {
134 find_files_matching_filter (result, fullpath, filter, arg, match_fullpath, return_fullpath, limit, recurse);
137 if (match_fullpath) {
138 search_str = fullpath;
140 search_str = finfo->d_name;
143 if (!filter(search_str, arg)) {
147 if (return_fullpath) {
148 result.push_back(fullpath);
150 result.push_back(finfo->d_name);
158 } while ((limit < 0 || (nfound < limit)) && (thisdir = strtok_r (0, G_SEARCHPATH_SEPARATOR_S, &saveptr)));