Merge pull request #2417 from razzfazz/guard_substr
[mono.git] / mcs / class / Microsoft.Build / Microsoft.Build.Internal / WindowsCompatibilityExtensions.cs
index 38f9d6b29e016d15f62fc2457200ae2647a26484..22354c999adcab3bc638fa3973359c0b2a573367 100644 (file)
@@ -27,6 +27,7 @@
 //
 using System;
 using System.IO;
+using Mono.XBuild.Utilities;
 
 namespace Microsoft.Build.Internal
 {
@@ -34,8 +35,27 @@ namespace Microsoft.Build.Internal
        {
                public static string NormalizeFilePath (string path)
                {
+                       if (MSBuildUtils.RunningOnWindows || string.IsNullOrWhiteSpace (path) || File.Exists (path) || Directory.Exists (path))
+                               return path;
                        return path.Replace ('\\', Path.DirectorySeparatorChar);
                }
+               
+               public static string FindMatchingPath (string path)
+               {
+                       if (MSBuildUtils.RunningOnWindows || string.IsNullOrWhiteSpace (path) || File.Exists (path) || Directory.Exists (path))
+                               return path;
+                       path = path.Replace ('\\', Path.DirectorySeparatorChar);
+                       var file = Path.GetFileName (path);
+                       var dir = FindMatchingPath (Path.GetDirectoryName (path));
+                       if (Directory.Exists (dir)) {
+                               foreach (FileSystemInfo e in new DirectoryInfo (dir.Length > 0 ? dir : ".").EnumerateFileSystemInfos ()) {
+                                       if (e.Name.Equals (file, StringComparison.OrdinalIgnoreCase))
+                                               return dir.Length > 0 ? Path.Combine (dir, e.Name) : e.Name;
+                               }
+                       }
+                       // The directory part is still based on case insensitive match.
+                       return dir.Length > 0 ? Path.Combine (dir, file) : file;
+               }
        }
 }