Implemented various methods that were previously just do nothing stubs.
[mono.git] / mcs / class / corlib / System.IO / Path.cs
1 //------------------------------------------------------------------------------
2 // 
3 // System.IO.Path.cs 
4 //
5 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
6 // 
7 // Author:         Jim Richardson, develop@wtfo-guru.com
8 // Created:        Saturday, August 11, 2001 
9 //
10 //------------------------------------------------------------------------------
11
12 using System;
13
14 namespace System.IO
15 {
16         public class Path : Object
17         {
18                 public static readonly char AltDirectorySeparatorChar = PlatformSpecific.InvalidPathChars;
19                 public static readonly char DirectorySeparatorChar = PlatformSpecific.InvalidPathChars;
20                 public static readonly char[] InvalidPathChars = PlatformSpecific.InvalidPathChars;
21                 public static readonly char PathSeparator = PlatformSpecific.InvalidPathChars;
22                 public static readonly char VolumeSeparatorChar = PlatformSpecific.InvalidPathChars;
23
24                 private static readonly char[] PathSeparatorChars = {   DirectorySeparatorChar, 
25                                                                                                                                 AltDirectorySeparatorChar,
26                                                                                                                                 VolumeSeparatorChar };
27
28                 // class methods
29                 public static string ChangeExtension(string path, string extension)
30                 {
31                         if(path == null)
32                         {
33                                 return null;
34                         }
35
36                         int iExt = findExtension(path);
37                         
38                         if(iExt < 0)
39                         {
40                                 return extension == null ? path : path + extension;
41                         }
42                         else if(iExt > 0)
43                         {
44                                 string temp = path.Substring(0, iExt);
45                                 if(extension != null)
46                                 {
47                                         return temp + extension;
48                                 }
49                                 return  temp;
50                         }
51
52                         return extension;
53                 }
54
55                 public static string Combine(string path1, string path2)
56                 {
57                         if(path1 == null || path2 == null)
58                         {
59                                 return null;
60                         }
61
62                         throwEmptyIf(path2);
63
64                         // TODO: Check for invalid DirectoryInfo characters
65                         //       although I don't think it is necesary for linux
66
67                         // TODO: Verify functionality further after NUnit tests written
68                         //               since the documentation was rather sketchy
69
70                         if(IsPathRooted(path2))
71                         {
72                                 if(path1.Equals(string.Empty))
73                                 {
74                                         return path2;
75                                 }
76                                 throw new ArgumentException("Rooted path");
77                         }
78                         
79                         string dirSep = new string(DirectorySeparatorChar, 1);
80                         string altSep = new string(AltDirectorySeparatorChar, 1);
81                         
82                         bool b1 = path1.EndsWith(dirSep) || path1.EndsWith(dirSep);
83                         bool b2 = path2.StartsWith(dirSep) || path2.StartsWith(altSep);
84                         if(b1 && b2)
85                         {
86                                 throw new ArgumentException("Invalid combination");
87                         }
88                         
89                         if(!b1 && !b2)
90                         {
91                                 return path1 + dirSep + path2;
92                         }
93
94                         return path1 + path2;
95                 }
96
97                 public static string GetDirectoryName(string path)
98                 {
99                         if(path == null)
100                         {
101                                 return null;
102                         }
103                         throwEmptyIf(path);
104                         throwWhiteSpaceOnlyIf(path);
105                         throwInvalidPathCharsIf(path);
106
107                         if(path.Length > 2)
108                         {
109                                 int nLast = path.LastIndexOfAny(PathSeparatorChars, path.Length - 2);
110
111                                 if(nLast > 0)
112                                 {
113                                         return path.Substring(0, nLast);
114                                 }
115                         }
116                         return path;
117                 }
118
119                 public static string GetExtension(string path)
120                 {
121                         if(path == null)
122                         {
123                                 return string.Empty;
124                         }
125
126                         throwEmptyIf(path);
127                         throwWhiteSpaceOnlyIf(path);
128                         
129                         int iExt = findExtension(path);
130                         int iLastSep = path.LastIndexOfAny( PathSeparatorChars );
131
132                         if(iExt > -1)
133                         {       // okay it has an extension
134                                 return path.Substring(iExt);
135                         }
136                         return string.Empty;
137                 }
138
139                 public static string GetFileName(string path)
140                 {
141                         if(path == null)
142                         {
143                                 return string.Empty;
144                         }
145
146                         throwEmptyIf(path);
147                         throwWhiteSpaceOnlyIf(path);
148
149                         int nLast = path.LastIndexOfAny(PathSeparatorChars);
150
151                         if(nLast > 0)
152                         {
153                                 return path.Substring(nLast + 1);
154                         }
155
156                         return nLast == 0 ? null : path;
157                 }
158
159                 public static string GetFileNameWithoutExtension(string path)
160                 {
161                         return ChangeExtension(GetFileName(path), null);
162                 }
163
164                 public static string GetFullPath(string path)
165                 {
166                         if(path != null)
167                         {
168                                 //TODO: Implement this correctly
169                                 return path;
170                         }
171                         return null;
172                 }
173
174                 public static string GetPathRoot(string path)
175                 {
176                         if(path != null || 
177                                 (path.StartsWith(new string(DirectorySeparatorChar, 1)) ||
178                                         path.StartsWith(new string(AltDirectorySeparatorChar, 1))))
179                         {
180                                 return path.Substring(0, 1);
181                         }
182                         return null;
183                 }
184
185                 public static string GetTempFileName()
186                 {
187                         //TODO: Implement method
188                         return string.Empty;
189                 }
190
191                 /// <summary>
192                 /// Returns the path of the current systems temp directory
193                 /// </summary>
194                 public static string GetTempPath()
195                 {       // TODO: This might vary with distribution and there
196                         //       might be an api to provide it. Research is needed
197                         return "/tmp";
198                 }
199
200                 public static bool HasExtension(string path)
201                 {  
202                         throwNullIf(path);
203                         throwEmptyIf(path);
204                         throwWhiteSpaceOnlyIf(path);
205                         
206                         return findExtension(path) > -1;
207                 }
208
209                 public static bool IsPathRooted(string path)
210                 {
211                         return path.StartsWith(new string(VolumeSeparatorChar,1));
212                 }
213
214                 // private class methods
215
216                 private static int findExtension(string path)
217                 {       // method should return the index of the path extension
218                         // start or -1 if no valid extension
219                         if(path != null)
220                         {               
221                                 int iLastDot = path.LastIndexOf(".");
222                                 int iLastSep = path.LastIndexOfAny( PathSeparatorChars );
223
224                                 if(iLastDot > iLastSep)
225                                 {
226                                         return iLastDot;
227                                 }
228                         }
229                         return -1;
230                 }
231
232                 private static void throwNullIf(string path)
233                 {
234                         if(path == null)
235                         {
236                                 throw new ArgumentNullException();
237                         }
238                 }
239
240                 private static void throwEmptyIf(string path)
241                 {
242                         if(path != null && path.Length == 0)
243                         {
244                                 throw new ArgumentException("Empty string");
245                         }
246                 }
247
248                 private static void throwWhiteSpaceOnlyIf(string path)
249                 {
250                         if(path != null)
251                         {
252                                 string temp = path;
253                                 temp.Trim();
254                                 if(temp.Length == 0)
255                                 {
256                                         throw new ArgumentException("Whitespace only string");
257                                 }
258                         }
259                 }
260                 
261                 private static void throwInvalidPathCharsIf(string path)
262                 {
263                         if(path != null && path.IndexOfAny(InvalidPathChars) > -1)
264                         {
265                                 throw new ArgumentException("Invalid path characters");
266                         }
267                 }
268         }
269 }