Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ProjectPath.cs « util « nunit20 « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 29e42e738c683ef26e917047ef3e8690278e7f70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#region Copyright (c) 2002-2003, James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole, Philip A. Craig
/************************************************************************************
'
' Copyright  2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
' Copyright  2000-2002 Philip A. Craig
'
' This software is provided 'as-is', without any express or implied warranty. In no 
' event will the authors be held liable for any damages arising from the use of this 
' software.
' 
' Permission is granted to anyone to use this software for any purpose, including 
' commercial applications, and to alter it and redistribute it freely, subject to the 
' following restrictions:
'
' 1. The origin of this software must not be misrepresented; you must not claim that 
' you wrote the original software. If you use this software in a product, an 
' acknowledgment (see the following) in the product documentation is required.
'
' Portions Copyright  2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
' or Copyright  2000-2002 Philip A. Craig
'
' 2. Altered source versions must be plainly marked as such, and must not be 
' misrepresented as being the original software.
'
' 3. This notice may not be removed or altered from any source distribution.
'
'***********************************************************************************/
#endregion

using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;

namespace NUnit.Util
{
	/// <summary>
	/// Static methods for manipulating project paths, including both directories
	/// and files. Some synonyms for System.Path methods are included as well.
	/// </summary>
	public class ProjectPath
	{
		public const uint FILE_ATTRIBUTE_DIRECTORY  = 0x00000010;  
		public const uint FILE_ATTRIBUTE_NORMAL     = 0x00000080;  
		public const int MAX_PATH = 256;

		#region Public methods

		public static bool IsAssemblyFileType( string path )
		{
			string extension = Path.GetExtension( path );
			return extension == ".dll" || extension == ".exe";
		}

		/// <summary>
		/// Returns the relative path from a base directory to another
		/// directory or file.
		/// </summary>
		public static string RelativePath( string from, string to )
		{
			from = Canonicalize( from );
			to = Canonicalize( to );

			// Second argument to PathRelativeTo must be absolute
			if ( !Path.IsPathRooted( to ) )
				return to;
			
			StringBuilder sb = new StringBuilder( MAX_PATH );

			// Return null if call fails
			if ( !PathRelativePathTo( sb, from, FILE_ATTRIBUTE_DIRECTORY, to, FILE_ATTRIBUTE_DIRECTORY ) )
				return null;

			// Remove initial .\ from path if present
			if ( sb.Length >=2 && sb[0] == '.' && sb[1] == '\\' )
				sb.Remove( 0, 2 );

			if ( sb.Length == 0 )
				return null;

			return sb.ToString();
		}

		/// <summary>
		/// Return the canonical form of a path.
		public static string Canonicalize( string path )
		{
			StringBuilder sb = new StringBuilder( MAX_PATH );
			if ( !PathCanonicalize( sb, path ) )
				throw new ArgumentException( string.Format( "Invalid path passed to PathCanonicalize: {0}", path ) );

			return sb.ToString();
		}

		/// <summary>
		/// True if the two paths are the same. However, two paths
		/// to the same file or directory using different network
		/// shares or drive letters are not treated as equal.
		/// </summary>
		public static bool SamePath( string path1, string path2 )
		{
			return Canonicalize(path1).ToLower() == Canonicalize(path2).ToLower();
		}

		/// <summary>
		/// True if the two paths are the same or if the second is
		/// directly or indirectly under the first. Note that paths 
		/// using different network shares or drive letters are 
		/// considered unrelated, even if they end up referencing
		/// the same subtrees in the file system.
		/// </summary>
		public static bool SamePathOrUnder( string path1, string path2 )
		{
			path1 = Canonicalize( path1 );
			path2 = Canonicalize( path2 );

			int length1 = path1.Length;
			int length2 = path2.Length;

			// if path1 is longer, then path2 can't be under it
			if ( length1 > length2 )
				return false;

			// if lengths are the same, check for equality
			if ( length1 == length2 )
				return path1.ToLower() == path2.ToLower();

			// path 2 is longer than path 1: see if initial parts match
			if ( path1.ToLower() != path2.Substring( 0, length1 ).ToLower() )
				return false;
			
			// must match through or up to a directory separator boundary
			return	path2[length1-1] == Path.DirectorySeparatorChar ||
					path2[length1] == Path.DirectorySeparatorChar;
		}

		#endregion

		#region Shlwapi functions used internally

		[DllImport("shlwapi.dll")]
		private static extern bool PathRelativePathTo(
			StringBuilder result,
			string from,
			uint attrFrom,
			string to,
			uint attrTo );

		[DllImport("shlwapi.dll")]
		private static extern bool PathCanonicalize(
			StringBuilder result,
			string path );

		[DllImport("shlwapi.dll")]
		private static extern int PathCommonPrefix(
			string file1,
			string file2,
			StringBuilder result );
			
		#endregion
	}
}