2 // DirectoryScanner.cs: Class used by BuildItem.
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2005 Marek Sieradzki
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
34 namespace Microsoft.Build.BuildEngine {
35 internal class DirectoryScanner {
37 DirectoryInfo baseDirectory;
40 string[] matchedFilenames;
42 public DirectoryScanner ()
48 Hashtable excludedItems;
49 ArrayList includedItems;
50 string[] splittedInclude, splittedExclude;
53 throw new ArgumentNullException ("Includes");
55 throw new ArgumentNullException ("Excludes");
56 if (baseDirectory == null)
57 throw new ArgumentNullException ("BaseDirectory");
59 excludedItems = new Hashtable ();
60 includedItems = new ArrayList ();
62 splittedInclude = includes.Split (';');
63 splittedExclude = excludes.Split (';');
65 if (excludes != String.Empty) {
66 foreach (string si in splittedExclude) {
67 ProcessExclude (si, excludedItems);
70 foreach (string si in splittedInclude) {
71 ProcessInclude (si, excludedItems, includedItems);
74 matchedFilenames = (string[])includedItems.ToArray (typeof (string));
77 private void ProcessInclude (string name, Hashtable excludedItems, ArrayList includedItems)
79 string[] separatedPath;
82 if (name.IndexOf ('?') == -1 && name.IndexOf ('*') == -1) {
83 if (!excludedItems.Contains (Path.GetFullPath(name)))
84 includedItems.Add (name);
86 if (name.Split (Path.DirectorySeparatorChar).Length > name.Split (Path.AltDirectorySeparatorChar).Length) {
87 separatedPath = name.Split (Path.DirectorySeparatorChar);
89 separatedPath = name.Split (Path.AltDirectorySeparatorChar);
91 if (separatedPath.Length == 1 && separatedPath [0] == String.Empty)
93 fileInfo = ParseIncludeExclude (separatedPath, 0, baseDirectory);
94 foreach (FileInfo fi in fileInfo)
95 if (!excludedItems.Contains (fi.FullName))
96 includedItems.Add (fi.FullName);
100 private void ProcessExclude (string name, Hashtable excludedItems)
102 string[] separatedPath;
105 if (name.IndexOf ('?') == -1 && name.IndexOf ('*') == -1) {
106 if (!excludedItems.Contains (Path.GetFullPath (name)))
107 excludedItems.Add (Path.GetFullPath (name), null);
109 if (name.Split (Path.DirectorySeparatorChar).Length > name.Split (Path.AltDirectorySeparatorChar).Length) {
110 separatedPath = name.Split (Path.DirectorySeparatorChar);
112 separatedPath = name.Split (Path.AltDirectorySeparatorChar);
114 if (separatedPath.Length == 1 && separatedPath [0] == String.Empty)
116 fileInfo = ParseIncludeExclude (separatedPath, 0, baseDirectory);
117 foreach (FileInfo fi in fileInfo)
118 if (!excludedItems.Contains (fi.FullName))
119 excludedItems.Add (fi.FullName, null);
123 private FileInfo[] ParseIncludeExclude (string[] input, int ptr, DirectoryInfo directory)
125 if (input.Length > 1 && ptr == 0 && input [0] == String.Empty)
127 if (input.Length == ptr + 1) {
129 fi = directory.GetFiles (input [ptr]);
134 ArrayList fileInfos = new ArrayList ();
135 if (input [ptr] == ".") {
136 di = new DirectoryInfo [1];
138 } else if (input [ptr] == "..") {
139 di = new DirectoryInfo [1];
140 di [0] = directory.Parent;
142 di = directory.GetDirectories (input [ptr]);
143 foreach (DirectoryInfo info in di) {
144 fi = ParseIncludeExclude (input, ptr + 1, info);
145 foreach (FileInfo file in fi)
146 fileInfos.Add (file);
148 fi = new FileInfo [fileInfos.Count];
150 foreach (FileInfo file in fileInfos)
156 public DirectoryInfo BaseDirectory {
157 get { return baseDirectory; }
158 set { baseDirectory = value; }
161 public string Includes {
162 get { return includes; }
163 set { includes = value; }
166 public string Excludes {
167 get { return excludes; }
168 set { excludes = value; }
171 public string[] MatchedFilenames {
172 get { return matchedFilenames; }