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 temporaryItems;
49 string[] splittedInclude, splittedExclude;
52 throw new ArgumentNullException ("Includes");
54 throw new ArgumentNullException ("Excludes");
55 if (baseDirectory == null)
56 throw new ArgumentNullException ("BaseDirectory");
58 temporaryItems = new Hashtable ();
60 splittedInclude = includes.Split (';');
61 splittedExclude = excludes.Split (';');
63 foreach (string si in splittedInclude) {
64 ProcessInclude (si, temporaryItems);
66 if (excludes != String.Empty) {
67 foreach (string si in splittedExclude) {
68 ProcessExclude (si, temporaryItems);
72 matchedFilenames = new string [temporaryItems.Count];
74 foreach (DictionaryEntry de in temporaryItems)
75 matchedFilenames [i++] = (string) de.Value;
78 private void ProcessInclude (string name, Hashtable temporaryItems)
80 string[] separatedPath;
83 if (name.IndexOf ('?') == -1 && name.IndexOf ('*') == -1)
84 temporaryItems.Add (Path.GetFullPath (name), 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 temporaryItems.Add (fi.FullName, fi.FullName);
99 private void ProcessExclude (string name, Hashtable temporaryItems)
101 string[] separatedPath;
104 if (name.IndexOf ('?') == -1 && name.IndexOf ('*') == -1) {
105 if (temporaryItems.Contains (Path.GetFullPath (name)))
106 temporaryItems.Remove (Path.GetFullPath (name));
108 if (name.Split (Path.DirectorySeparatorChar).Length > name.Split (Path.AltDirectorySeparatorChar).Length) {
109 separatedPath = name.Split (Path.DirectorySeparatorChar);
111 separatedPath = name.Split (Path.AltDirectorySeparatorChar);
113 if (separatedPath.Length == 1 && separatedPath [0] == String.Empty)
115 fileInfo = ParseIncludeExclude (separatedPath, 0, baseDirectory);
116 foreach (FileInfo fi in fileInfo)
117 if (temporaryItems.Contains (fi.FullName))
118 temporaryItems.Remove (fi.FullName);
122 private FileInfo[] ParseIncludeExclude (string[] input, int ptr, DirectoryInfo directory)
124 if (input.Length > 1 && ptr == 0 && input [0] == String.Empty)
126 if (input.Length == ptr + 1) {
128 fi = directory.GetFiles (input [ptr]);
133 ArrayList fileInfos = new ArrayList ();
134 if (input [ptr] == ".") {
135 di = new DirectoryInfo [1];
137 } else if (input [ptr] == "..") {
138 di = new DirectoryInfo [1];
139 di [0] = directory.Parent;
141 di = directory.GetDirectories (input [ptr]);
142 foreach (DirectoryInfo info in di) {
143 fi = ParseIncludeExclude (input, ptr + 1, info);
144 foreach (FileInfo file in fi)
145 fileInfos.Add (file);
147 fi = new FileInfo [fileInfos.Count];
149 foreach (FileInfo file in fileInfos)
155 public DirectoryInfo BaseDirectory {
156 get { return baseDirectory; }
157 set { baseDirectory = value; }
160 public string Includes {
161 get { return includes; }
162 set { includes = value; }
165 public string Excludes {
166 get { return excludes; }
167 set { excludes = value; }
170 public string[] MatchedFilenames {
171 get { return matchedFilenames; }