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