2 // ReservedNameUtils.cs
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2006 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.
29 using System.Collections;
30 using System.Collections.Specialized;
34 namespace Mono.XBuild.Utilities {
36 internal static class ReservedNameUtils {
38 static string[] reservedMetadataNames;
39 static Hashtable reservedMetadataHash;
41 static ReservedNameUtils ()
43 reservedMetadataNames = new string [] {
44 "FullPath", "RootDir", "Filename", "Extension", "RelativeDir", "Directory",
45 "RecursiveDir", "Identity", "ModifiedTime", "CreatedTime", "AccessedTime"};
46 reservedMetadataHash = CollectionsUtil.CreateCaseInsensitiveHashtable (ReservedMetadataNameCount);
47 foreach (string s in reservedMetadataNames) {
48 reservedMetadataHash.Add (s, null);
52 public static ICollection ReservedMetadataNames {
54 return (ICollection) reservedMetadataNames.Clone ();
58 public static int ReservedMetadataNameCount {
60 return reservedMetadataNames.Length;
64 public static bool IsReservedMetadataName (string metadataName)
66 return reservedMetadataHash.Contains (metadataName);
69 public static string GetReservedMetadata (string itemSpec,
70 string metadataName, IDictionary metadata)
72 if (metadataName == null)
73 throw new ArgumentNullException ();
75 if (String.IsNullOrEmpty (itemSpec))
78 switch (metadataName.ToLowerInvariant ()) {
80 return Path.GetFullPath (itemSpec);
82 if (Path.IsPathRooted (itemSpec))
83 return Path.GetPathRoot (itemSpec);
85 return Path.GetPathRoot (Environment.CurrentDirectory);
87 return Path.GetFileNameWithoutExtension (itemSpec);
89 return Path.GetExtension (itemSpec);
91 return WithTrailingSlash (AbsoluteToRelativePath (Environment.CurrentDirectory, Path.GetDirectoryName (itemSpec)));
93 string fullpath = Path.GetFullPath (itemSpec);
94 return WithTrailingSlash (
95 Path.GetDirectoryName (fullpath).Substring (Path.GetPathRoot (fullpath).Length));
97 if (metadata != null && metadata.Contains ("RecursiveDir"))
98 return (string)metadata ["RecursiveDir"];
102 return Path.Combine (Path.GetDirectoryName (itemSpec), Path.GetFileName (itemSpec));
104 if (File.Exists (itemSpec))
105 return File.GetLastWriteTime (itemSpec).ToString ();
106 else if (Directory.Exists (itemSpec))
107 return Directory.GetLastWriteTime (itemSpec).ToString ();
111 if (File.Exists (itemSpec))
112 return File.GetCreationTime (itemSpec).ToString ();
113 else if (Directory.Exists (itemSpec))
114 return Directory.GetCreationTime (itemSpec).ToString ();
118 if (File.Exists (itemSpec))
119 return File.GetLastAccessTime (itemSpec).ToString ();
120 else if (Directory.Exists (itemSpec))
121 return Directory.GetLastAccessTime (itemSpec).ToString ();
125 throw new ArgumentException ("Invalid reserved metadata name");
129 static string WithTrailingSlash (string path)
131 if (String.IsNullOrEmpty (path))
135 return path + Path.DirectorySeparatorChar;
140 readonly static char[] separators = { Path.DirectorySeparatorChar, Path.VolumeSeparatorChar, Path.AltDirectorySeparatorChar };
141 static string AbsoluteToRelativePath (string baseDirectoryPath, string absPath)
143 if (!Path.IsPathRooted (absPath))
146 absPath = Path.GetFullPath (absPath);
147 baseDirectoryPath = Path.GetFullPath (baseDirectoryPath.TrimEnd (Path.DirectorySeparatorChar));
149 string[] bPath = baseDirectoryPath.Split (separators);
150 string[] aPath = absPath.Split (separators);
153 for (; indx < System.Math.Min (bPath.Length, aPath.Length); indx++) {
154 if (!bPath[indx].Equals(aPath[indx]))
161 StringBuilder result = new StringBuilder ();
163 for (int i = indx; i < bPath.Length; i++) {
164 result.Append ("..");
165 if (i + 1 < bPath.Length || aPath.Length - indx > 0)
166 result.Append (Path.DirectorySeparatorChar);
170 result.Append (String.Join(Path.DirectorySeparatorChar.ToString(), aPath, indx, aPath.Length - indx));
171 if (result.Length == 0)
173 return result.ToString ();
176 static string RelativeToAbsolutePath (string baseDirectoryPath, string relPath)
178 return Path.GetFullPath (Path.Combine (baseDirectoryPath, relPath));