Merging from trunk
[ardour.git] / libs / pbd3 / pathscanner.cc
1 /*
2     Copyright (C) 1998-99 Paul Barton-Davis 
3
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.
8
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.
13
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.
17
18     $Id$
19 */
20
21 #include <cstdlib>
22 #include <cstdio>
23 #include <vector>
24 #include <dirent.h>
25
26 #include <pbd/error.h>
27 #include <pbd/pathscanner.h>
28 #include <pbd/stl_delete.h>
29
30 using namespace PBD;
31
32 vector<string *> *
33 PathScanner::operator() (const string &dirpath, const string &regexp,
34                          bool match_fullpath, bool return_fullpath, 
35                          long limit)
36
37 {
38         int err;
39         char msg[256];
40
41         if ((err = regcomp (&compiled_pattern, regexp.c_str(),
42                             REG_EXTENDED|REG_NOSUB))) {
43                 
44                 regerror (err, &compiled_pattern,
45                           msg, sizeof (msg));
46                 
47                 error << "Cannot compile soundfile regexp for use (" 
48                       << msg 
49                       << ")" 
50                       << endmsg;
51                 
52                 return 0;
53         }
54         
55         return run_scan (dirpath, &PathScanner::regexp_filter, 
56                          (bool (*)(const string &, void *)) 0,
57                          0,
58                          match_fullpath,
59                          return_fullpath,
60                          limit);
61 }       
62
63 vector<string *> *
64 PathScanner::run_scan (const string &dirpath, 
65                        bool (PathScanner::*memberfilter)(const string &),
66                        bool (*filter)(const string &, void *),
67                        void *arg,
68                        bool match_fullpath, bool return_fullpath,
69                        long limit)
70
71 {
72         vector<string *> *result = 0;
73         DIR *dir;
74         struct dirent *finfo;
75         char *pathcopy = strdup (dirpath.c_str());
76         char *thisdir;
77         char fullpath[PATH_MAX+1];
78         string search_str;
79         string *newstr;
80         long nfound = 0;
81
82         if ((thisdir = strtok (pathcopy, ":")) == 0 ||
83             strlen (thisdir) == 0) {
84                 free (pathcopy);
85                 return 0;
86         }
87
88         result = new vector<string *>;
89
90         do {
91
92                 if ((dir = opendir (thisdir)) == 0) {
93                         continue;
94                 }
95                 
96                 while ((finfo = readdir (dir)) != 0) {
97
98                         snprintf (fullpath, sizeof(fullpath), "%s/%s",
99                                   thisdir, finfo->d_name);
100
101                         if (match_fullpath) {
102                                 search_str = fullpath;
103                         } else {
104                                 search_str = finfo->d_name;
105                         }
106
107                         /* handle either type of function ptr */
108
109                         if (memberfilter) {
110                                 if (!(this->*memberfilter)(search_str)) {
111                                         continue;
112                                 } 
113                         } else {
114                                 if (!filter(search_str, arg)) {
115                                         continue;
116                                 }
117                         }
118
119                         if (return_fullpath) {
120                                 newstr = new string (fullpath);
121                         } else {
122                                 newstr = new string (finfo->d_name);
123                         } 
124
125                         result->push_back (newstr);
126                         nfound++;
127                 }
128
129                 closedir (dir);
130                 
131         } while ((limit < 0 || (nfound < limit)) && (thisdir = strtok (0, ":")));
132
133         free (pathcopy);
134         return result;
135 }
136
137 string *
138 PathScanner::find_first (const string &dirpath,
139                          const string &regexp,
140                          bool match_fullpath,
141                          bool return_fullpath)
142 {
143         vector<string *> *res;
144         string *ret;
145         int err;
146         char msg[256];
147
148         if ((err = regcomp (&compiled_pattern, regexp.c_str(),
149                             REG_EXTENDED|REG_NOSUB))) {
150                 
151                 regerror (err, &compiled_pattern,
152                           msg, sizeof (msg));
153                 
154                 error << "Cannot compile soundfile regexp for use (" << msg << ")" << endmsg;
155
156                 
157                 return 0;
158         }
159         
160         res = run_scan (dirpath, 
161                         &PathScanner::regexp_filter,
162                         (bool (*)(const string &, void *)) 0,
163                         0,
164                         match_fullpath,
165                         return_fullpath, 
166                         1);
167         
168         if (res->size() == 0) {
169                 ret = 0;
170         } else {
171                 ret = res->front();
172         }
173         vector_delete (res);
174         delete res;
175         return ret;
176 }
177
178 string *
179 PathScanner::find_first (const string &dirpath,
180                          bool (*filter)(const string &, void *),
181                          void *arg,
182                          bool match_fullpath,
183                          bool return_fullpath)
184 {
185         vector<string *> *res;
186         string *ret;
187
188         res = run_scan (dirpath, 
189                         (bool (PathScanner::*)(const string &)) 0,
190                         filter,
191                         0,
192                         match_fullpath,
193                         return_fullpath, 1);
194         
195         if (res->size() == 0) {
196                 ret = 0;
197         } else {
198                 ret = res->front();
199         }
200         vector_delete (res);
201         delete res;
202         return ret;
203 }