Added hammock project to debug streaming issues
[pithos-ms-client] / trunk / hammock / src / net35 / ICSharpCode.SharpZipLib.Silverlight / Core / PathFilter.cs
1 // PathFilter.cs
2 //
3 // Copyright 2005 John Reilly
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 //
19 // Linking this library statically or dynamically with other modules is
20 // making a combined work based on this library.  Thus, the terms and
21 // conditions of the GNU General Public License cover the whole
22 // combination.
23 // 
24 // As a special exception, the copyright holders of this library give you
25 // permission to link this library with independent modules to produce an
26 // executable, regardless of the license terms of these independent
27 // modules, and to copy and distribute the resulting executable under
28 // terms of your choice, provided that you also meet, for each linked
29 // independent module, the terms and conditions of the license of that
30 // module.  An independent module is a module which is not derived from
31 // or based on this library.  If you modify this library, you may extend
32 // this exception to your version of the library, but you are not
33 // obligated to do so.  If you do not wish to do so, delete this
34 // exception statement from your version.
35
36 using System;
37 using System.IO;
38
39 namespace ICSharpCode.SharpZipLib.Silverlight.Core
40 {
41     /// <summary>
42     /// PathFilter filters directories and files using a form of <see cref="System.Text.RegularExpressions.Regex">regular expressions</see>
43     /// by full path name.
44     /// See <see cref="NameFilter">NameFilter</see> for more detail on filtering.
45     /// </summary>
46     public class PathFilter : IScanFilter
47     {
48         #region Constructors
49
50         /// <summary>
51         /// Initialise a new instance of <see cref="PathFilter"></see>.
52         /// </summary>
53         /// <param name="filter">The <see cref="NameFilter">filter</see> expression to apply.</param>
54         public PathFilter(string filter)
55         {
56             nameFilter_ = new NameFilter(filter);
57         }
58
59         #endregion
60
61         #region IScanFilter Members
62
63         /// <summary>
64         /// Test a name to see if it matches the filter.
65         /// </summary>
66         /// <param name="name">The name to test.</param>
67         /// <returns>True if the name matches, false otherwise.</returns>
68         public virtual bool IsMatch(string name)
69         {
70             var result = false;
71
72             if (name != null)
73             {
74                 var cooked = (name.Length > 0) ? Path.GetFullPath(name) : "";
75                 result = nameFilter_.IsMatch(cooked);
76             }
77             return result;
78         }
79
80         #endregion
81
82         #region Instance Fields
83
84         private readonly NameFilter nameFilter_;
85
86         #endregion
87     }
88
89     /// <summary>
90     /// ExtendedPathFilter filters based on name, file size, and the last write time of the file.
91     /// </summary>
92     /// <remarks>Provides an example of how to customise filtering.</remarks>
93     public class ExtendedPathFilter : PathFilter
94     {
95         #region Constructors
96
97         /// <summary>
98         /// Initialise a new instance of ExtendedPathFilter.
99         /// </summary>
100         /// <param name="filter">The filter to apply.</param>
101         /// <param name="minSize">The minimum file size to include.</param>
102         /// <param name="maxSize">The maximum file size to include.</param>
103         public ExtendedPathFilter(string filter,
104                                   long minSize, long maxSize)
105             : base(filter)
106         {
107             MinSize = minSize;
108             MaxSize = maxSize;
109         }
110
111         /// <summary>
112         /// Initialise a new instance of ExtendedPathFilter.
113         /// </summary>
114         /// <param name="filter">The filter to apply.</param>
115         /// <param name="minDate">The minimum <see cref="DateTime"/> to include.</param>
116         /// <param name="maxDate">The maximum <see cref="DateTime"/> to include.</param>
117         public ExtendedPathFilter(string filter,
118                                   DateTime minDate, DateTime maxDate)
119             : base(filter)
120         {
121             MinDate = minDate;
122             MaxDate = maxDate;
123         }
124
125         /// <summary>
126         /// Initialise a new instance of ExtendedPathFilter.
127         /// </summary>
128         /// <param name="filter">The filter to apply.</param>
129         /// <param name="minSize">The minimum file size to include.</param>
130         /// <param name="maxSize">The maximum file size to include.</param>
131         /// <param name="minDate">The minimum <see cref="DateTime"/> to include.</param>
132         /// <param name="maxDate">The maximum <see cref="DateTime"/> to include.</param>
133         public ExtendedPathFilter(string filter,
134                                   long minSize, long maxSize,
135                                   DateTime minDate, DateTime maxDate)
136             : base(filter)
137         {
138             MinSize = minSize;
139             MaxSize = maxSize;
140             MinDate = minDate;
141             MaxDate = maxDate;
142         }
143
144         #endregion
145
146         /// <summary>
147         /// Test a filename to see if it matches the filter.
148         /// </summary>
149         /// <param name="name">The filename to test.</param>
150         /// <returns>True if the filter matches, false otherwise.</returns>
151         public override bool IsMatch(string name)
152         {
153             var result = base.IsMatch(name);
154
155             if (result)
156             {
157                 var fileInfo = new FileInfo(name);
158                 result =
159                     (MinSize <= fileInfo.Length) &&
160                     (MaxSize >= fileInfo.Length) &&
161                     (MinDate <= fileInfo.LastWriteTime) &&
162                     (MaxDate >= fileInfo.LastWriteTime)
163                     ;
164             }
165             return result;
166         }
167
168         #region Properties
169
170         /// <summary>
171         /// Get/set the minimum size for a file that will match this filter.
172         /// </summary>
173         public long MinSize
174         {
175             get { return minSize_; }
176             set
177             {
178                 if ((value < 0) || (maxSize_ < value))
179                 {
180                     throw new ArgumentOutOfRangeException("value");
181                 }
182
183                 minSize_ = value;
184             }
185         }
186
187         /// <summary>
188         /// Get/set the maximum size for a file that will match this filter.
189         /// </summary>
190         public long MaxSize
191         {
192             get { return maxSize_; }
193             set
194             {
195                 if ((value < 0) || (minSize_ > value))
196                 {
197                     throw new ArgumentOutOfRangeException("value");
198                 }
199
200                 maxSize_ = value;
201             }
202         }
203
204         /// <summary>
205         /// Get/set the minimum <see cref="DateTime"/> value that will match for this filter.
206         /// </summary>
207         /// <remarks>Files with a LastWrite time less than this value are excluded by the filter.</remarks>
208         public DateTime MinDate
209         {
210             get { return minDate_; }
211
212             set
213             {
214                 if (value > maxDate_)
215                 {
216                     throw new ArgumentException("Exceeds MaxDate", "value");
217                 }
218
219                 minDate_ = value;
220             }
221         }
222
223         /// <summary>
224         /// Get/set the maximum <see cref="DateTime"/> value that will match for this filter.
225         /// </summary>
226         /// <remarks>Files with a LastWrite time greater than this value are excluded by the filter.</remarks>
227         public DateTime MaxDate
228         {
229             get { return maxDate_; }
230
231             set
232             {
233                 if (minDate_ > value)
234                 {
235                     throw new ArgumentException("Exceeds MinDate", "value");
236                 }
237
238                 maxDate_ = value;
239             }
240         }
241
242         #endregion
243
244         #region Instance Fields
245
246         private DateTime maxDate_ = DateTime.MaxValue;
247         private long maxSize_ = long.MaxValue;
248         private DateTime minDate_ = DateTime.MinValue;
249         private long minSize_;
250
251         #endregion
252     }
253
254     /// <summary>
255     /// NameAndSizeFilter filters based on name and file size.
256     /// </summary>
257     /// <remarks>A sample showing how filters might be extended.</remarks>
258     [Obsolete("Use ExtendedPathFilter instead")]
259     public class NameAndSizeFilter : PathFilter
260     {
261         /// <summary>
262         /// Initialise a new instance of NameAndSizeFilter.
263         /// </summary>
264         /// <param name="filter">The filter to apply.</param>
265         /// <param name="minSize">The minimum file size to include.</param>
266         /// <param name="maxSize">The maximum file size to include.</param>
267         public NameAndSizeFilter(string filter, long minSize, long maxSize)
268             : base(filter)
269         {
270             MinSize = minSize;
271             MaxSize = maxSize;
272         }
273
274         /// <summary>
275         /// Get/set the minimum size for a file that will match this filter.
276         /// </summary>
277         public long MinSize
278         {
279             get { return minSize_; }
280             set
281             {
282                 if ((value < 0) || (maxSize_ < value))
283                 {
284                     throw new ArgumentOutOfRangeException("value");
285                 }
286
287                 minSize_ = value;
288             }
289         }
290
291         /// <summary>
292         /// Get/set the maximum size for a file that will match this filter.
293         /// </summary>
294         public long MaxSize
295         {
296             get { return maxSize_; }
297             set
298             {
299                 if ((value < 0) || (minSize_ > value))
300                 {
301                     throw new ArgumentOutOfRangeException("value");
302                 }
303
304                 maxSize_ = value;
305             }
306         }
307
308         #region Instance Fields
309
310         private long maxSize_ = long.MaxValue;
311         private long minSize_;
312
313         #endregion
314
315         /// <summary>
316         /// Test a filename to see if it matches the filter.
317         /// </summary>
318         /// <param name="name">The filename to test.</param>
319         /// <returns>True if the filter matches, false otherwise.</returns>
320         public override bool IsMatch(string name)
321         {
322             var result = base.IsMatch(name);
323
324             if (result)
325             {
326                 var fileInfo = new FileInfo(name);
327                 var length = fileInfo.Length;
328                 result =
329                     (MinSize <= length) &&
330                     (MaxSize >= length);
331             }
332             return result;
333         }
334     }
335 }