Summary

Class:ICSharpCode.SharpZipLib.Core.ExtendedPathFilter
Assembly:ICSharpCode.SharpZipLib
File(s):C:\Users\Neil\Documents\Visual Studio 2015\Projects\icsharpcode\SZL_master\ICSharpCode.SharpZipLib\Core\PathFilter.cs
Covered lines:0
Uncovered lines:47
Coverable lines:47
Total lines:280
Line coverage:0%
Branch coverage:0%

Metrics

MethodCyclomatic ComplexitySequence CoverageBranch Coverage
.ctor(...)100
.ctor(...)100
.ctor(...)100
IsMatch(...)500

File(s)

C:\Users\Neil\Documents\Visual Studio 2015\Projects\icsharpcode\SZL_master\ICSharpCode.SharpZipLib\Core\PathFilter.cs

#LineLine coverage
 1using System;
 2using System.IO;
 3
 4namespace ICSharpCode.SharpZipLib.Core
 5{
 6  /// <summary>
 7  /// PathFilter filters directories and files using a form of <see cref="System.Text.RegularExpressions.Regex">regular 
 8  /// by full path name.
 9  /// See <see cref="NameFilter">NameFilter</see> for more detail on filtering.
 10  /// </summary>
 11  public class PathFilter : IScanFilter
 12  {
 13    #region Constructors
 14    /// <summary>
 15    /// Initialise a new instance of <see cref="PathFilter"></see>.
 16    /// </summary>
 17    /// <param name="filter">The <see cref="NameFilter">filter</see> expression to apply.</param>
 18    public PathFilter(string filter)
 19    {
 20      nameFilter_ = new NameFilter(filter);
 21    }
 22    #endregion
 23
 24    #region IScanFilter Members
 25    /// <summary>
 26    /// Test a name to see if it matches the filter.
 27    /// </summary>
 28    /// <param name="name">The name to test.</param>
 29    /// <returns>True if the name matches, false otherwise.</returns>
 30    /// <remarks><see cref="Path.GetFullPath(string)"/> is used to get the full path before matching.</remarks>
 31    public virtual bool IsMatch(string name)
 32    {
 33      bool result = false;
 34
 35      if (name != null) {
 36        string cooked = (name.Length > 0) ? Path.GetFullPath(name) : "";
 37        result = nameFilter_.IsMatch(cooked);
 38      }
 39      return result;
 40    }
 41
 42    readonly
 43    #endregion
 44
 45    #region Instance Fields
 46    NameFilter nameFilter_;
 47    #endregion
 48  }
 49
 50  /// <summary>
 51  /// ExtendedPathFilter filters based on name, file size, and the last write time of the file.
 52  /// </summary>
 53  /// <remarks>Provides an example of how to customise filtering.</remarks>
 54  public class ExtendedPathFilter : PathFilter
 55  {
 56    #region Constructors
 57    /// <summary>
 58    /// Initialise a new instance of ExtendedPathFilter.
 59    /// </summary>
 60    /// <param name="filter">The filter to apply.</param>
 61    /// <param name="minSize">The minimum file size to include.</param>
 62    /// <param name="maxSize">The maximum file size to include.</param>
 63    public ExtendedPathFilter(string filter,
 64      long minSize, long maxSize)
 065      : base(filter)
 66    {
 067      MinSize = minSize;
 068      MaxSize = maxSize;
 069    }
 70
 71    /// <summary>
 72    /// Initialise a new instance of ExtendedPathFilter.
 73    /// </summary>
 74    /// <param name="filter">The filter to apply.</param>
 75    /// <param name="minDate">The minimum <see cref="DateTime"/> to include.</param>
 76    /// <param name="maxDate">The maximum <see cref="DateTime"/> to include.</param>
 77    public ExtendedPathFilter(string filter,
 78      DateTime minDate, DateTime maxDate)
 079      : base(filter)
 80    {
 081      MinDate = minDate;
 082      MaxDate = maxDate;
 083    }
 84
 85    /// <summary>
 86    /// Initialise a new instance of ExtendedPathFilter.
 87    /// </summary>
 88    /// <param name="filter">The filter to apply.</param>
 89    /// <param name="minSize">The minimum file size to include.</param>
 90    /// <param name="maxSize">The maximum file size to include.</param>
 91    /// <param name="minDate">The minimum <see cref="DateTime"/> to include.</param>
 92    /// <param name="maxDate">The maximum <see cref="DateTime"/> to include.</param>
 93    public ExtendedPathFilter(string filter,
 94      long minSize, long maxSize,
 95      DateTime minDate, DateTime maxDate)
 096      : base(filter)
 97    {
 098      MinSize = minSize;
 099      MaxSize = maxSize;
 0100      MinDate = minDate;
 0101      MaxDate = maxDate;
 0102    }
 103    #endregion
 104
 105    #region IScanFilter Members
 106    /// <summary>
 107    /// Test a filename to see if it matches the filter.
 108    /// </summary>
 109    /// <param name="name">The filename to test.</param>
 110    /// <returns>True if the filter matches, false otherwise.</returns>
 111    /// <exception cref="System.IO.FileNotFoundException">The <see paramref="fileName"/> doesnt exist</exception>
 112    public override bool IsMatch(string name)
 113    {
 0114      bool result = base.IsMatch(name);
 115
 0116       if (result) {
 0117        var fileInfo = new FileInfo(name);
 0118        result =
 0119          (MinSize <= fileInfo.Length) &&
 0120          (MaxSize >= fileInfo.Length) &&
 0121          (MinDate <= fileInfo.LastWriteTime) &&
 0122          (MaxDate >= fileInfo.LastWriteTime)
 0123          ;
 124      }
 0125      return result;
 126    }
 127    #endregion
 128
 129    #region Properties
 130    /// <summary>
 131    /// Get/set the minimum size/length for a file that will match this filter.
 132    /// </summary>
 133    /// <remarks>The default value is zero.</remarks>
 134    /// <exception cref="ArgumentOutOfRangeException">value is less than zero; greater than <see cref="MaxSize"/></excep
 135    public long MinSize {
 0136      get { return minSize_; }
 137      set {
 0138         if ((value < 0) || (maxSize_ < value)) {
 0139          throw new ArgumentOutOfRangeException(nameof(value));
 140        }
 141
 0142        minSize_ = value;
 0143      }
 144    }
 145
 146    /// <summary>
 147    /// Get/set the maximum size/length for a file that will match this filter.
 148    /// </summary>
 149    /// <remarks>The default value is <see cref="System.Int64.MaxValue"/></remarks>
 150    /// <exception cref="ArgumentOutOfRangeException">value is less than zero or less than <see cref="MinSize"/></except
 151    public long MaxSize {
 0152      get { return maxSize_; }
 153      set {
 0154         if ((value < 0) || (minSize_ > value)) {
 0155          throw new ArgumentOutOfRangeException(nameof(value));
 156        }
 157
 0158        maxSize_ = value;
 0159      }
 160    }
 161
 162    /// <summary>
 163    /// Get/set the minimum <see cref="DateTime"/> value that will match for this filter.
 164    /// </summary>
 165    /// <remarks>Files with a LastWrite time less than this value are excluded by the filter.</remarks>
 166    public DateTime MinDate {
 167      get {
 0168        return minDate_;
 169      }
 170
 171      set {
 0172         if (value > maxDate_) {
 0173          throw new ArgumentOutOfRangeException(nameof(value), "Exceeds MaxDate");
 174        }
 175
 0176        minDate_ = value;
 0177      }
 178    }
 179
 180    /// <summary>
 181    /// Get/set the maximum <see cref="DateTime"/> value that will match for this filter.
 182    /// </summary>
 183    /// <remarks>Files with a LastWrite time greater than this value are excluded by the filter.</remarks>
 184    public DateTime MaxDate {
 185      get {
 0186        return maxDate_;
 187      }
 188
 189      set {
 0190         if (minDate_ > value) {
 0191          throw new ArgumentOutOfRangeException(nameof(value), "Exceeds MinDate");
 192        }
 193
 0194        maxDate_ = value;
 0195      }
 196    }
 197    #endregion
 198
 199    #region Instance Fields
 200    long minSize_;
 0201    long maxSize_ = long.MaxValue;
 0202    DateTime minDate_ = DateTime.MinValue;
 0203    DateTime maxDate_ = DateTime.MaxValue;
 204    #endregion
 205  }
 206
 207  /// <summary>
 208  /// NameAndSizeFilter filters based on name and file size.
 209  /// </summary>
 210  /// <remarks>A sample showing how filters might be extended.</remarks>
 211  [Obsolete("Use ExtendedPathFilter instead")]
 212  public class NameAndSizeFilter : PathFilter
 213  {
 214
 215    /// <summary>
 216    /// Initialise a new instance of NameAndSizeFilter.
 217    /// </summary>
 218    /// <param name="filter">The filter to apply.</param>
 219    /// <param name="minSize">The minimum file size to include.</param>
 220    /// <param name="maxSize">The maximum file size to include.</param>
 221    public NameAndSizeFilter(string filter, long minSize, long maxSize)
 222      : base(filter)
 223    {
 224      MinSize = minSize;
 225      MaxSize = maxSize;
 226    }
 227
 228    /// <summary>
 229    /// Test a filename to see if it matches the filter.
 230    /// </summary>
 231    /// <param name="name">The filename to test.</param>
 232    /// <returns>True if the filter matches, false otherwise.</returns>
 233    public override bool IsMatch(string name)
 234    {
 235      bool result = base.IsMatch(name);
 236
 237      if (result) {
 238        var fileInfo = new FileInfo(name);
 239        long length = fileInfo.Length;
 240        result =
 241          (MinSize <= length) &&
 242          (MaxSize >= length);
 243      }
 244      return result;
 245    }
 246
 247    /// <summary>
 248    /// Get/set the minimum size for a file that will match this filter.
 249    /// </summary>
 250    public long MinSize {
 251      get { return minSize_; }
 252      set {
 253        if ((value < 0) || (maxSize_ < value)) {
 254          throw new ArgumentOutOfRangeException(nameof(value));
 255        }
 256
 257        minSize_ = value;
 258      }
 259    }
 260
 261    /// <summary>
 262    /// Get/set the maximum size for a file that will match this filter.
 263    /// </summary>
 264    public long MaxSize {
 265      get { return maxSize_; }
 266      set {
 267        if ((value < 0) || (minSize_ > value)) {
 268          throw new ArgumentOutOfRangeException(nameof(value));
 269        }
 270
 271        maxSize_ = value;
 272      }
 273    }
 274
 275    #region Instance Fields
 276    long minSize_;
 277    long maxSize_ = long.MaxValue;
 278    #endregion
 279  }
 280}