1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html *
// * Copyright (C) 2008-2010 ZenJu (zhnmju123 AT gmx.de) *
// **************************************************************************
//
#ifndef FFS_FILTER_H_INCLUDED
#define FFS_FILTER_H_INCLUDED
#include "../shared/zstring.h"
#include <set>
#include <boost/shared_ptr.hpp>
#include <wx/stream.h>
namespace FreeFileSync
{
//------------------------------------------------------------------
/* class hierarchy:
BaseFilter (interface)
/|\
_________|_____________
| | |
NullFilter NameFilter CombinedFilter
*/
/*
Semantics of BaseFilter:
1. using it creates a NEW folder hierarchy! -> must be respected by <Automatic>-mode!
2. it applies equally to both sides => it always matches either both sides or none! => can be used while traversing a single folder!
*/
class BaseFilter //interface for filtering
{
public:
virtual ~BaseFilter() {}
//filtering
virtual bool passFileFilter(const DefaultChar* relFilename) const = 0;
virtual bool passDirFilter(const DefaultChar* relDirname, bool* subObjMightMatch) const = 0;
//subObjMightMatch: file/dir in subdirectories could(!) match
//note: variable is only set if passDirFilter returns false!
virtual bool isNull() const = 0; //filter is equivalent to NullFilter, but may be technically slower
//comparison
bool operator<(const BaseFilter& other) const;
bool operator==(const BaseFilter& other) const;
bool operator!=(const BaseFilter& other) const;
typedef boost::shared_ptr<const BaseFilter> FilterRef; //always bound by design!
//serialization
void saveFilter(wxOutputStream& stream) const; //serialize derived object
static FilterRef loadFilter(wxInputStream& stream); //CAVEAT!!! adapt this method for each new derivation!!!
private:
virtual Zstring uniqueClassIdentifier() const = 0; //get identifier, used for serialization
virtual void save(wxOutputStream& stream) const = 0; //serialization
virtual bool cmpLessSameType(const BaseFilter& other) const = 0; //typeid(*this) == typeid(other) in this context!
};
class NullFilter : public BaseFilter //no filtering at all
{
public:
static FilterRef load(wxInputStream& stream); //"serial constructor"
virtual bool passFileFilter(const DefaultChar* relFilename) const;
virtual bool passDirFilter(const DefaultChar* relDirname, bool* subObjMightMatch) const;
virtual bool isNull() const;
private:
virtual Zstring uniqueClassIdentifier() const;
virtual void save(wxOutputStream& stream) const {}
virtual bool cmpLessSameType(const BaseFilter& other) const;
};
class NameFilter : public BaseFilter //standard filter by filename
{
public:
NameFilter(const Zstring& includeFilter, const Zstring& excludeFilter);
static FilterRef load(wxInputStream& stream); //"serial constructor"
virtual bool passFileFilter(const DefaultChar* relFilename) const;
virtual bool passDirFilter(const DefaultChar* relDirname, bool* subObjMightMatch) const;
virtual bool isNull() const;
private:
virtual Zstring uniqueClassIdentifier() const;
virtual void save(wxOutputStream& stream) const;
virtual bool cmpLessSameType(const BaseFilter& other) const;
std::set<Zstring> filterFileIn;
std::set<Zstring> filterFolderIn;
std::set<Zstring> filterFileEx;
std::set<Zstring> filterFolderEx;
const Zstring includeFilterTmp; //save constructor arguments for serialization
const Zstring excludeFilterTmp; //
};
class CombinedFilter : public BaseFilter //combine two filters to match if and only if both match
{
public:
CombinedFilter(const FilterRef& first, const FilterRef& second) : first_(first), second_(second) {}
static FilterRef load(wxInputStream& stream); //"serial constructor"
virtual bool passFileFilter(const DefaultChar* relFilename) const;
virtual bool passDirFilter(const DefaultChar* relDirname, bool* subObjMightMatch) const;
virtual bool isNull() const;
private:
virtual Zstring uniqueClassIdentifier() const;
virtual void save(wxOutputStream& stream) const;
virtual bool cmpLessSameType(const BaseFilter& other) const;
const FilterRef first_;
const FilterRef second_;
};
//small helper method: remove Null-filters
BaseFilter::FilterRef combineFilters(const BaseFilter::FilterRef& first,
const BaseFilter::FilterRef& second);
//---------------Inline Implementation---------------------------------------------------
inline
BaseFilter::FilterRef NullFilter::load(wxInputStream& stream) //"serial constructor"
{
return FilterRef(new NullFilter);
}
inline
bool NullFilter::passFileFilter(const DefaultChar* relFilename) const
{
return true;
}
inline
bool NullFilter::passDirFilter(const DefaultChar* relDirname, bool* subObjMightMatch) const
{
assert(subObjMightMatch == NULL || *subObjMightMatch == true); //check correct usage
return true;
}
inline
bool NullFilter::isNull() const
{
return true;
}
inline
bool NullFilter::cmpLessSameType(const BaseFilter& other) const
{
//typeid(*this) == typeid(other) in this context!
assert(typeid(*this) == typeid(other));
return false;
}
inline
Zstring NullFilter::uniqueClassIdentifier() const
{
return DefaultStr("NullFilter");
}
inline
bool CombinedFilter::passFileFilter(const DefaultChar* relFilename) const
{
return first_->passFileFilter(relFilename) && //short-circuit behavior
second_->passFileFilter(relFilename);
}
inline
bool CombinedFilter::passDirFilter(const DefaultChar* relDirname, bool* subObjMightMatch) const
{
return first_->passDirFilter(relDirname, subObjMightMatch) && //short-circuit behavior: subObjMightMatch handled correctly!
second_->passDirFilter(relDirname, subObjMightMatch);
}
inline
bool CombinedFilter::isNull() const
{
return first_->isNull() && second_->isNull();
}
inline
bool CombinedFilter::cmpLessSameType(const BaseFilter& other) const
{
//typeid(*this) == typeid(other) in this context!
assert(typeid(*this) == typeid(other));
const CombinedFilter& otherCombFilt = static_cast<const CombinedFilter&>(other);
if (*first_ != *otherCombFilt.first_)
return *first_ < *otherCombFilt.first_;
return *second_ < *otherCombFilt.second_;
}
inline
Zstring CombinedFilter::uniqueClassIdentifier() const
{
return DefaultStr("CombinedFilter");
}
inline
void CombinedFilter::save(wxOutputStream& stream) const
{
first_->saveFilter(stream);
second_->saveFilter(stream);
}
inline
BaseFilter::FilterRef CombinedFilter::load(wxInputStream& stream) //"constructor"
{
FilterRef first = loadFilter(stream);
FilterRef second = loadFilter(stream);
return combineFilters(first, second);
}
inline
BaseFilter::FilterRef combineFilters(const BaseFilter::FilterRef& first,
const BaseFilter::FilterRef& second)
{
if (first->isNull())
{
if (second->isNull())
return BaseFilter::FilterRef(new NullFilter);
else
return second;
}
else
{
if (second->isNull())
return first;
else
return BaseFilter::FilterRef(new CombinedFilter(first, second));
}
}
}
#endif // FFS_FILTER_H_INCLUDED
|