3 // Copyright 2005 John Reilly
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.
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.
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.
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
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.
39 namespace ICSharpCode.SharpZipLib.Silverlight.Core
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.
46 public class PathFilter : IScanFilter
51 /// Initialise a new instance of <see cref="PathFilter"></see>.
53 /// <param name="filter">The <see cref="NameFilter">filter</see> expression to apply.</param>
54 public PathFilter(string filter)
56 nameFilter_ = new NameFilter(filter);
61 #region IScanFilter Members
64 /// Test a name to see if it matches the filter.
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)
74 var cooked = (name.Length > 0) ? Path.GetFullPath(name) : "";
75 result = nameFilter_.IsMatch(cooked);
82 #region Instance Fields
84 private readonly NameFilter nameFilter_;
90 /// ExtendedPathFilter filters based on name, file size, and the last write time of the file.
92 /// <remarks>Provides an example of how to customise filtering.</remarks>
93 public class ExtendedPathFilter : PathFilter
98 /// Initialise a new instance of ExtendedPathFilter.
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)
112 /// Initialise a new instance of ExtendedPathFilter.
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)
126 /// Initialise a new instance of ExtendedPathFilter.
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)
147 /// Test a filename to see if it matches the filter.
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)
153 var result = base.IsMatch(name);
157 var fileInfo = new FileInfo(name);
159 (MinSize <= fileInfo.Length) &&
160 (MaxSize >= fileInfo.Length) &&
161 (MinDate <= fileInfo.LastWriteTime) &&
162 (MaxDate >= fileInfo.LastWriteTime)
171 /// Get/set the minimum size for a file that will match this filter.
175 get { return minSize_; }
178 if ((value < 0) || (maxSize_ < value))
180 throw new ArgumentOutOfRangeException("value");
188 /// Get/set the maximum size for a file that will match this filter.
192 get { return maxSize_; }
195 if ((value < 0) || (minSize_ > value))
197 throw new ArgumentOutOfRangeException("value");
205 /// Get/set the minimum <see cref="DateTime"/> value that will match for this filter.
207 /// <remarks>Files with a LastWrite time less than this value are excluded by the filter.</remarks>
208 public DateTime MinDate
210 get { return minDate_; }
214 if (value > maxDate_)
216 throw new ArgumentException("Exceeds MaxDate", "value");
224 /// Get/set the maximum <see cref="DateTime"/> value that will match for this filter.
226 /// <remarks>Files with a LastWrite time greater than this value are excluded by the filter.</remarks>
227 public DateTime MaxDate
229 get { return maxDate_; }
233 if (minDate_ > value)
235 throw new ArgumentException("Exceeds MinDate", "value");
244 #region Instance Fields
246 private DateTime maxDate_ = DateTime.MaxValue;
247 private long maxSize_ = long.MaxValue;
248 private DateTime minDate_ = DateTime.MinValue;
249 private long minSize_;
255 /// NameAndSizeFilter filters based on name and file size.
257 /// <remarks>A sample showing how filters might be extended.</remarks>
258 [Obsolete("Use ExtendedPathFilter instead")]
259 public class NameAndSizeFilter : PathFilter
262 /// Initialise a new instance of NameAndSizeFilter.
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)
275 /// Get/set the minimum size for a file that will match this filter.
279 get { return minSize_; }
282 if ((value < 0) || (maxSize_ < value))
284 throw new ArgumentOutOfRangeException("value");
292 /// Get/set the maximum size for a file that will match this filter.
296 get { return maxSize_; }
299 if ((value < 0) || (minSize_ > value))
301 throw new ArgumentOutOfRangeException("value");
308 #region Instance Fields
310 private long maxSize_ = long.MaxValue;
311 private long minSize_;
316 /// Test a filename to see if it matches the filter.
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)
322 var result = base.IsMatch(name);
326 var fileInfo = new FileInfo(name);
327 var length = fileInfo.Length;
329 (MinSize <= length) &&