Merge branch 'master' into windows
[ardour.git] / libs / pbd / pbd / pathscanner.h
1 /*
2     Copyright (C) 2000-2007 Paul 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 */
19
20 #ifndef __libmisc_pathscanner_h__
21 #define __libmisc_pathscanner_h__
22
23 #include <vector>
24 #include <string>
25 #ifdef COMPILER_MSVC
26 #include <ardourext/misc.h>
27 #else
28 #include <regex.h>
29 #endif
30
31 class PathScanner
32
33 {
34   public:
35         std::vector<std::string *> *operator() (const std::string &dirpath,
36                                                 bool (*filter)(const std::string &, void *arg),
37                                                 void *arg, 
38                                                 bool match_fullpath = true,
39                                                 bool return_fullpath = true,
40                                                 long limit = -1,
41                                                 bool recurse = false) {
42                 return run_scan (dirpath,
43                                  (bool (PathScanner::*)(const std::string &)) 0, 
44                                  filter, 
45                                  arg,
46                                  match_fullpath,
47                                  return_fullpath, 
48                                  limit, recurse);
49         }
50
51         std::vector<std::string *> *operator() (const std::string &dirpath,
52                                                 const std::string &regexp,
53                                                 bool match_fullpath = true,
54                                                 bool return_fullpath = true,
55                                                 long limit = -1,
56                                                 bool recurse = false);
57         
58         std::string *find_first (const std::string &dirpath,
59                                  const std::string &regexp,
60                                  bool match_fullpath = true,
61                                  bool return_fullpath = true);
62         
63         std::string *find_first (const std::string &dirpath,
64                                  bool (*filter)(const std::string &, void *),
65                                  void *arg,
66                                  bool match_fullpath = true,
67                                  bool return_fullpath = true);
68         
69   private:
70         regex_t compiled_pattern;
71         
72         bool regexp_filter (const std::string &str) {
73                 return regexec (&compiled_pattern, str.c_str(), 0, 0, 0) == 0;
74         }
75         
76         std::vector<std::string *> *run_scan (const std::string &dirpath,
77                                               bool (PathScanner::*mfilter) (const std::string &),
78                                               bool (*filter)(const std::string &, void *),
79                                               void *arg,
80                                               bool match_fullpath,
81                                               bool return_fullpath,
82                                               long limit,
83                                               bool recurse = false);
84
85         std::vector<std::string *> *run_scan_internal (std::vector<std::string*>*, 
86                                                        const std::string &dirpath,
87                                                        bool (PathScanner::*mfilter) (const std::string &),
88                                                        bool (*filter)(const std::string &, void *),
89                                                        void *arg,
90                                                        bool match_fullpath,
91                                                        bool return_fullpath,
92                                                        long limit,
93                                                        bool recurse = false);
94 };
95
96 #endif // __libmisc_pathscanner_h__