2 * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
5 #ifndef _ENTRY_FILTER_H
6 #define _ENTRY_FILTER_H
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
);
24 class BasicEntryFilter
{
37 bool SetTo(const char* pattern
, bool isFileName
)
41 fPattern
= strdup(pattern
);
45 fIsFileName
= isFileName
;
50 bool Filter(const char* path
, const char* name
) const
52 if (fPattern
!= NULL
) {
53 if (fnmatch(fPattern
, (fIsFileName
? name
: path
), 0) == 0)
60 void SetNextFilter(BasicEntryFilter
* next
)
65 BasicEntryFilter
* NextFilter() const
73 BasicEntryFilter
* fNextFilter
;
80 : fIncludeFilters(NULL
),
87 while (BasicEntryFilter
* filter
= fIncludeFilters
) {
88 fIncludeFilters
= filter
->NextFilter();
92 while (BasicEntryFilter
* filter
= fExcludeFilters
) {
93 fExcludeFilters
= filter
->NextFilter();
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
)
124 const char* name
= strrchr(path
, '/');
125 name
= (name
!= NULL
? name
+ 1 : path
);
128 if (_Filter(fExcludeFilters
, path
, name
))
131 // include filters -- if none are given, everything matches
132 return fIncludeFilters
== NULL
|| _Filter(fIncludeFilters
, path
, name
);
136 static void _AddFilter(BasicEntryFilter
*& filterList
,
137 BasicEntryFilter
* filter
)
139 filter
->SetNextFilter(filterList
);
143 static bool _AddFilter(BasicEntryFilter
*& filterList
, const char* pattern
,
146 BasicEntryFilter
* filter
= new(std::nothrow
) BasicEntryFilter
;
150 if (!filter
->SetTo(pattern
, isFilePattern
)) {
155 _AddFilter(filterList
, filter
);
160 static bool _Filter(const BasicEntryFilter
* const& filterList
,
161 const char* path
, const char* name
)
163 const BasicEntryFilter
* filter
= filterList
;
165 if (filter
->Filter(path
, name
))
167 filter
= filter
->NextFilter();
174 BasicEntryFilter
* fIncludeFilters
;
175 BasicEntryFilter
* fExcludeFilters
;
178 } // namespace BPrivate
181 #endif // _ENTRY_FILTER_H