Change PBD::PathScanner API to return results by value to avoid inadvertent memory...
[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 #include "pbd/libpbd_visibility.h"
32
33 class LIBPBD_API PathScanner
34
35 {
36   public:
37         std::vector<std::string> operator() (const std::string &dirpath,
38                                              bool (*filter)(const std::string &, void *arg),
39                                              void *arg,
40                                              bool match_fullpath = true,
41                                              bool return_fullpath = true,
42                                              long limit = -1,
43                                              bool recurse = false) {
44                 return run_scan (dirpath,
45                                  (bool (PathScanner::*)(const std::string &)) 0, 
46                                  filter, 
47                                  arg,
48                                  match_fullpath,
49                                  return_fullpath, 
50                                  limit, recurse);
51         }
52
53         std::vector<std::string> operator() (const std::string &dirpath,
54                                              const std::string &regexp,
55                                              bool match_fullpath = true,
56                                              bool return_fullpath = true,
57                                              long limit = -1,
58                                              bool recurse = false);
59         
60         std::string find_first (const std::string &dirpath,
61                                 const std::string &regexp,
62                                 bool match_fullpath = true,
63                                 bool return_fullpath = true);
64         
65         std::string find_first (const std::string &dirpath,
66                                 bool (*filter)(const std::string &, void *),
67                                 void *arg,
68                                 bool match_fullpath = true,
69                                 bool return_fullpath = true);
70         
71   private:
72         regex_t compiled_pattern;
73         
74         bool regexp_filter (const std::string &str) {
75                 return regexec (&compiled_pattern, str.c_str(), 0, 0, 0) == 0;
76         }
77         
78         std::vector<std::string> run_scan (const std::string &dirpath,
79                                            bool (PathScanner::*mfilter) (const std::string &),
80                                            bool (*filter)(const std::string &, void *),
81                                            void *arg,
82                                            bool match_fullpath,
83                                            bool return_fullpath,
84                                            long limit,
85                                            bool recurse = false);
86
87         void run_scan_internal (std::vector<std::string>&,
88                                 const std::string &dirpath,
89                                 bool (PathScanner::*mfilter) (const std::string &),
90                                 bool (*filter)(const std::string &, void *),
91                                 void *arg,
92                                 bool match_fullpath,
93                                 bool return_fullpath,
94                                 long limit,
95                                 bool recurse = false);
96 };
97
98 #endif // __libmisc_pathscanner_h__