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