Copy 0.84 to trunk.
[mono.git] / mcs / class / ICSharpCode.SharpZipLib / ICSharpCode.SharpZipLib / Core / PathFilter.cs
1 // PathFilter.cs\r
2 //\r
3 // Copyright 2005 John Reilly\r
4 //\r
5 // This program is free software; you can redistribute it and/or\r
6 // modify it under the terms of the GNU General Public License\r
7 // as published by the Free Software Foundation; either version 2\r
8 // of the License, or (at your option) any later version.\r
9 //\r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 //\r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, write to the Free Software\r
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\r
18 //\r
19 // Linking this library statically or dynamically with other modules is\r
20 // making a combined work based on this library.  Thus, the terms and\r
21 // conditions of the GNU General Public License cover the whole\r
22 // combination.\r
23 // \r
24 // As a special exception, the copyright holders of this library give you\r
25 // permission to link this library with independent modules to produce an\r
26 // executable, regardless of the license terms of these independent\r
27 // modules, and to copy and distribute the resulting executable under\r
28 // terms of your choice, provided that you also meet, for each linked\r
29 // independent module, the terms and conditions of the license of that\r
30 // module.  An independent module is a module which is not derived from\r
31 // or based on this library.  If you modify this library, you may extend\r
32 // this exception to your version of the library, but you are not\r
33 // obligated to do so.  If you do not wish to do so, delete this\r
34 // exception statement from your version.\r
35 \r
36 \r
37 using System;\r
38 using System.IO;\r
39 \r
40 namespace ICSharpCode.SharpZipLib.Core\r
41 {\r
42         /// <summary>\r
43         /// Scanning filters support these operations.\r
44         /// </summary>\r
45         public interface IScanFilter\r
46         {\r
47                 /// <summary>\r
48                 /// Test a name to see if is 'matches' the filter.\r
49                 /// </summary>\r
50                 /// <param name="name">The name to test.</param>\r
51                 /// <returns>Returns true if the name matches the filter, false if it does not match.</returns>\r
52                 bool IsMatch(string name);\r
53         }\r
54         \r
55         /// <summary>\r
56         /// PathFilter filters directories and files by full path name.\r
57         /// </summary>\r
58         public class PathFilter : IScanFilter\r
59         {\r
60                 /// <summary>\r
61                 /// Initialise a new instance of <see cref="PathFilter"></see>.\r
62                 /// </summary>\r
63                 /// <param name="filter">The <see cref="NameFilter"></see>filter expression to apply.</param>\r
64                 public PathFilter(string filter)\r
65                 {\r
66                         nameFilter = new NameFilter(filter);\r
67                 }\r
68 \r
69                 /// <summary>\r
70                 /// Test a name to see if it matches the filter.\r
71                 /// </summary>\r
72                 /// <param name="name">The name to test.</param>\r
73                 /// <returns>True if the name matches, false otherwise.</returns>\r
74                 public virtual bool IsMatch(string name)\r
75                 {\r
76                         return nameFilter.IsMatch(Path.GetFullPath(name));\r
77                 }\r
78                 \r
79                 #region Instance Fields\r
80                 NameFilter nameFilter;\r
81                 #endregion\r
82         }\r
83 \r
84         /// <summary>\r
85         /// NameAnsSizeFilter filters based on name and file size.\r
86         /// </summary>\r
87         public class NameAndSizeFilter : PathFilter\r
88         {\r
89         \r
90                 /// <summary>\r
91                 /// Initialise a new instance of NameAndSizeFilter.\r
92                 /// </summary>\r
93                 /// <param name="filter">The filter to apply.</param>\r
94                 /// <param name="minSize">The minimum file size to include.</param>\r
95                 /// <param name="maxSize">The maximum file size to include.</param>\r
96                 public NameAndSizeFilter(string filter, long minSize, long maxSize) : base(filter)\r
97                 {\r
98                         this.minSize = minSize;\r
99                         this.maxSize = maxSize;\r
100                 }\r
101                 \r
102                 /// <summary>\r
103                 /// Test a filename to see if it matches the filter.\r
104                 /// </summary>\r
105                 /// <param name="fileName">The filename to test.</param>\r
106                 /// <returns>True if the filter matches, false otherwise.</returns>\r
107                 public override bool IsMatch(string fileName)\r
108                 {\r
109                         FileInfo fileInfo = new FileInfo(fileName);\r
110                         long length = fileInfo.Length;\r
111                         return base.IsMatch(fileName) &&\r
112                                 (MinSize <= length) && (MaxSize >= length);\r
113                 }\r
114                 \r
115                 long minSize = 0;\r
116                 \r
117                 /// <summary>\r
118                 /// The minimum size for a file that will match this filter.\r
119                 /// </summary>\r
120                 public long MinSize\r
121                 {\r
122                         get { return minSize; }\r
123                         set { minSize = value; }\r
124                 }\r
125                 \r
126                 long maxSize = long.MaxValue;\r
127                 \r
128                 /// <summary>\r
129                 /// The maximum size for a file that will match this filter.\r
130                 /// </summary>\r
131                 public long MaxSize\r
132                 {\r
133                         get { return maxSize; }\r
134                         set { maxSize = value; }\r
135                 }\r
136         }\r
137 }\r