~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Core/MonoDevelop.Core/FilePath.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// FilePath.cs
 
3
//  
 
4
// Author:
 
5
//       Lluis Sanchez Gual <lluis@novell.com>
 
6
// 
 
7
// Copyright (c) 2011 Novell, Inc (http://www.novell.com)
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
using System.Collections.Generic;
 
29
using System.Diagnostics;
 
30
using System.IO;
 
31
using System.Linq;
 
32
using System.Text;
 
33
using System.Threading;
 
34
using Mono.Addins;
 
35
using MonoDevelop.Core.FileSystem;
 
36
 
 
37
namespace MonoDevelop.Core
 
38
{
 
39
        [Serializable]
 
40
        public struct FilePath: IComparable<FilePath>, IComparable, IEquatable<FilePath>
 
41
        {
 
42
                string fileName;
 
43
 
 
44
                public static readonly FilePath Null = new FilePath (null);
 
45
                public static readonly FilePath Empty = new FilePath (string.Empty);
 
46
 
 
47
                public FilePath (string name)
 
48
                {
 
49
                        fileName = name;
 
50
                }
 
51
 
 
52
                public bool IsNull {
 
53
                        get { return fileName == null; }
 
54
                }
 
55
 
 
56
                public bool IsNullOrEmpty {
 
57
                        get { return string.IsNullOrEmpty (fileName); }
 
58
                }
 
59
                
 
60
                public bool IsNotNull {
 
61
                        get { return fileName != null; }
 
62
                }
 
63
 
 
64
                public bool IsEmpty {
 
65
                        get { return fileName != null && fileName.Length == 0; }
 
66
                }
 
67
 
 
68
                public FilePath FullPath {
 
69
                        get {
 
70
                                return new FilePath (!string.IsNullOrEmpty (fileName) ? Path.GetFullPath (fileName) : "");
 
71
                        }
 
72
                }
 
73
                
 
74
                /// <summary>
 
75
                /// Returns a path in standard form, which can be used to be compared
 
76
                /// for equality with other canonical paths. It is similar to FullPath,
 
77
                /// but unlike FullPath, the directory "/a/b" is considered equal to "/a/b/"
 
78
                /// </summary>
 
79
                public FilePath CanonicalPath {
 
80
                        get {
 
81
                                string fp = Path.GetFullPath (fileName);
 
82
                                if (fp.Length > 0 && fp[fp.Length - 1] == Path.DirectorySeparatorChar)
 
83
                                        return fp.TrimEnd (Path.DirectorySeparatorChar);
 
84
                                else
 
85
                                        return fp;
 
86
                        }
 
87
                }
 
88
 
 
89
                public string FileName {
 
90
                        get {
 
91
                                return Path.GetFileName (fileName);
 
92
                        }
 
93
                }
 
94
 
 
95
                public string Extension {
 
96
                        get {
 
97
                                return Path.GetExtension (fileName);
 
98
                        }
 
99
                }
 
100
                
 
101
                public bool HasExtension (string extension)
 
102
                {
 
103
                        return fileName.Length > extension.Length
 
104
                                && fileName.EndsWith (extension, StringComparison.OrdinalIgnoreCase)
 
105
                                && fileName[fileName.Length - extension.Length - 1] != Path.PathSeparator;
 
106
                }
 
107
 
 
108
                public string FileNameWithoutExtension {
 
109
                        get {
 
110
                                return Path.GetFileNameWithoutExtension (fileName);
 
111
                        }
 
112
                }
 
113
 
 
114
                public FilePath ParentDirectory {
 
115
                        get {
 
116
                                return new FilePath (Path.GetDirectoryName (fileName));
 
117
                        }
 
118
                }
 
119
 
 
120
                public bool IsAbsolute {
 
121
                        get { return Path.IsPathRooted (fileName); }
 
122
                }
 
123
 
 
124
                public bool IsChildPathOf (FilePath basePath)
 
125
                {
 
126
                        StringComparison sc = PropertyService.IsWindows ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
 
127
                        if (basePath.fileName [basePath.fileName.Length - 1] != Path.DirectorySeparatorChar)
 
128
                                return fileName.StartsWith (basePath.fileName + Path.DirectorySeparatorChar, sc);
 
129
                        else
 
130
                                return fileName.StartsWith (basePath.fileName, sc);
 
131
                }
 
132
 
 
133
                public FilePath ChangeExtension (string ext)
 
134
                {
 
135
                        return Path.ChangeExtension (fileName, ext);
 
136
                }
 
137
 
 
138
                public FilePath Combine (params FilePath[] paths)
 
139
                {
 
140
                        string path = fileName;
 
141
                        foreach (FilePath p in paths)
 
142
                                path = Path.Combine (path, p.fileName);
 
143
                        return new FilePath (path);
 
144
                }
 
145
 
 
146
                public FilePath Combine (params string[] paths)
 
147
                {
 
148
                        string path = fileName;
 
149
                        foreach (string p in paths)
 
150
                                path = Path.Combine (path, p);
 
151
                        return new FilePath (path);
 
152
                }
 
153
                
 
154
                /// <summary>
 
155
                /// Builds a path by combining all provided path sections
 
156
                /// </summary>
 
157
                public static FilePath Build (params string[] paths)
 
158
                {
 
159
                        return Empty.Combine (paths);
 
160
                }
 
161
                
 
162
                public static FilePath GetCommonRootPath (IEnumerable<FilePath> paths)
 
163
                {
 
164
                        FilePath root = FilePath.Null;
 
165
                        foreach (FilePath p in paths) {
 
166
                                if (root.IsNull)
 
167
                                        root = p;
 
168
                                else if (root == p)
 
169
                                        continue;
 
170
                                else if (root.IsChildPathOf (p))
 
171
                                        root = p;
 
172
                                else {
 
173
                                        while (!root.IsNullOrEmpty && !p.IsChildPathOf (root))
 
174
                                                root = root.ParentDirectory;
 
175
                                }
 
176
                        }
 
177
                        return root;
 
178
                }
 
179
 
 
180
                public FilePath ToAbsolute (FilePath basePath)
 
181
                {
 
182
                        if (IsAbsolute)
 
183
                                return FullPath;
 
184
                        else
 
185
                                return Combine (basePath, this).FullPath;
 
186
                }
 
187
 
 
188
                public FilePath ToRelative (FilePath basePath)
 
189
                {
 
190
                        return FileService.AbsoluteToRelativePath (basePath, fileName);
 
191
                }
 
192
 
 
193
                public static implicit operator FilePath (string name)
 
194
                {
 
195
                        return new FilePath (name);
 
196
                }
 
197
 
 
198
                public static implicit operator string (FilePath filePath)
 
199
                {
 
200
                        return filePath.fileName;
 
201
                }
 
202
 
 
203
                public static bool operator == (FilePath name1, FilePath name2)
 
204
                {
 
205
                        if (PropertyService.IsWindows)
 
206
                                return string.Equals (name1.fileName, name2.fileName, StringComparison.OrdinalIgnoreCase);
 
207
                        else
 
208
                                return string.Equals (name1.fileName, name2.fileName, StringComparison.Ordinal);
 
209
                }
 
210
 
 
211
                public static bool operator != (FilePath name1, FilePath name2)
 
212
                {
 
213
                        return !(name1 == name2);
 
214
                }
 
215
 
 
216
                public override bool Equals (object obj)
 
217
                {
 
218
                        if (!(obj is FilePath))
 
219
                                return false;
 
220
 
 
221
                        FilePath fn = (FilePath) obj;
 
222
                        return this == fn;
 
223
                }
 
224
 
 
225
                public override int GetHashCode ( )
 
226
                {
 
227
                        if (fileName == null)
 
228
                                return 0;
 
229
                        if (PropertyService.IsWindows)
 
230
                                return fileName.ToLower ().GetHashCode ();
 
231
                        else
 
232
                                return fileName.GetHashCode ();
 
233
                }
 
234
 
 
235
                public override string ToString ( )
 
236
                {
 
237
                        return fileName;
 
238
                }
 
239
 
 
240
                public int CompareTo (FilePath filePath)
 
241
                {
 
242
                        return string.Compare (fileName, filePath.fileName, PropertyService.IsWindows);
 
243
                }
 
244
 
 
245
                int IComparable.CompareTo (object obj)
 
246
                {
 
247
                        if (!(obj is FilePath))
 
248
                                return -1;
 
249
                        return CompareTo ((FilePath) obj);
 
250
                }
 
251
 
 
252
                #region IEquatable<FilePath> Members
 
253
 
 
254
                bool IEquatable<FilePath>.Equals (FilePath other)
 
255
                {
 
256
                        return this == other;
 
257
                }
 
258
 
 
259
                #endregion
 
260
        }
 
261
 
 
262
        public static class FilePathUtil
 
263
        {
 
264
                public static string[] ToStringArray (this FilePath[] paths)
 
265
                {
 
266
                        string[] array = new string[paths.Length];
 
267
                        for (int n = 0; n < paths.Length; n++)
 
268
                                array[n] = paths[n].ToString ();
 
269
                        return array;
 
270
                }
 
271
                
 
272
                public static FilePath[] ToFilePathArray (this string[] paths)
 
273
                {
 
274
                        var array = new FilePath[paths.Length];
 
275
                        for (int n = 0; n < paths.Length; n++)
 
276
                                array[n] = paths[n];
 
277
                        return array;
 
278
                }
 
279
                
 
280
                public static IEnumerable<string> ToPathStrings (this IEnumerable<FilePath> paths)
 
281
                {
 
282
                        foreach (FilePath p in paths)
 
283
                                yield return p.ToString ();
 
284
                }
 
285
        }
 
286
}