Make UEFI boot-platform build again
[haiku.git] / headers / private / shared / EntryFilter.h
blobb7511765cea9b55d13df67a754852942d0a224be
1 /*
2 * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5 #ifndef _ENTRY_FILTER_H
6 #define _ENTRY_FILTER_H
8 #include <stddef.h>
9 #include <stdlib.h>
10 #include <string.h>
12 #include <new>
14 #if defined(__BEOS__) && !defined(__HAIKU__)
15 // BeOS doesn't have <fnmatch.h>, but libroot.so features fnmatch() anyway
16 extern "C" int fnmatch(const char *pattern, const char *string, int flags);
17 #else
18 # include <fnmatch.h>
19 #endif
21 namespace BPrivate {
24 class BasicEntryFilter {
25 public:
26 BasicEntryFilter()
27 : fPattern(NULL),
28 fIsFileName(false)
32 ~BasicEntryFilter()
34 free(fPattern);
37 bool SetTo(const char* pattern, bool isFileName)
39 free(fPattern);
41 fPattern = strdup(pattern);
42 if (fPattern == NULL)
43 return false;
45 fIsFileName = isFileName;
47 return true;
50 bool Filter(const char* path, const char* name) const
52 if (fPattern != NULL) {
53 if (fnmatch(fPattern, (fIsFileName ? name : path), 0) == 0)
54 return true;
57 return false;
60 void SetNextFilter(BasicEntryFilter* next)
62 fNextFilter = next;
65 BasicEntryFilter* NextFilter() const
67 return fNextFilter;
70 private:
71 char* fPattern;
72 bool fIsFileName;
73 BasicEntryFilter* fNextFilter;
77 class EntryFilter {
78 public:
79 EntryFilter()
80 : fIncludeFilters(NULL),
81 fExcludeFilters(NULL)
85 ~EntryFilter()
87 while (BasicEntryFilter* filter = fIncludeFilters) {
88 fIncludeFilters = filter->NextFilter();
89 delete filter;
92 while (BasicEntryFilter* filter = fExcludeFilters) {
93 fExcludeFilters = filter->NextFilter();
94 delete filter;
98 void AddIncludeFilter(BasicEntryFilter* filter)
100 _AddFilter(fIncludeFilters, filter);
103 bool AddIncludeFilter(const char* pattern, bool isFilePattern)
105 return _AddFilter(fIncludeFilters, pattern, isFilePattern);
108 void AddExcludeFilter(BasicEntryFilter* filter)
110 _AddFilter(fExcludeFilters, filter);
113 bool AddExcludeFilter(const char* pattern, bool isFilePattern)
115 return _AddFilter(fExcludeFilters, pattern, isFilePattern);
118 bool Filter(const char* path) const
120 if (fExcludeFilters == NULL && fIncludeFilters)
121 return true;
123 // get leaf name
124 const char* name = strrchr(path, '/');
125 name = (name != NULL ? name + 1 : path);
127 // exclude filters
128 if (_Filter(fExcludeFilters, path, name))
129 return false;
131 // include filters -- if none are given, everything matches
132 return fIncludeFilters == NULL || _Filter(fIncludeFilters, path, name);
135 private:
136 static void _AddFilter(BasicEntryFilter*& filterList,
137 BasicEntryFilter* filter)
139 filter->SetNextFilter(filterList);
140 filterList = filter;
143 static bool _AddFilter(BasicEntryFilter*& filterList, const char* pattern,
144 bool isFilePattern)
146 BasicEntryFilter* filter = new(std::nothrow) BasicEntryFilter;
147 if (filter == NULL)
148 return false;
150 if (!filter->SetTo(pattern, isFilePattern)) {
151 delete filter;
152 return false;
155 _AddFilter(filterList, filter);
157 return true;
160 static bool _Filter(const BasicEntryFilter* const& filterList,
161 const char* path, const char* name)
163 const BasicEntryFilter* filter = filterList;
164 while (filter) {
165 if (filter->Filter(path, name))
166 return true;
167 filter = filter->NextFilter();
170 return false;
173 private:
174 BasicEntryFilter* fIncludeFilters;
175 BasicEntryFilter* fExcludeFilters;
178 } // namespace BPrivate
181 #endif // _ENTRY_FILTER_H