Path.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. //------------------------------------------------------------------------------
  2. //
  3. // System.IO.Path.cs
  4. //
  5. // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
  6. // Copyright (C) 2002 Ximian, Inc. (http://www.ximian.com)
  7. // Copyright (C) 2003 Ben Maurer
  8. //
  9. // Author: Jim Richardson, [email protected]
  10. // Dan Lewis ([email protected])
  11. // Gonzalo Paniagua Javier ([email protected])
  12. // Ben Maurer ([email protected])
  13. // Created: Saturday, August 11, 2001
  14. //
  15. //------------------------------------------------------------------------------
  16. using System;
  17. using System.Runtime.CompilerServices;
  18. namespace System.IO
  19. {
  20. public sealed class Path
  21. {
  22. public static readonly char AltDirectorySeparatorChar;
  23. public static readonly char DirectorySeparatorChar;
  24. public static readonly char[] InvalidPathChars;
  25. public static readonly char PathSeparator;
  26. internal static readonly string DirectorySeparatorStr;
  27. public static readonly char VolumeSeparatorChar;
  28. private static readonly char[] PathSeparatorChars;
  29. private static bool dirEqualsVolume;
  30. private Path () {}
  31. // class methods
  32. public static string ChangeExtension (string path, string extension)
  33. {
  34. if (path == null)
  35. {
  36. return null;
  37. }
  38. if (path.IndexOfAny (InvalidPathChars) != -1)
  39. throw new ArgumentException ("Illegal characters in path", "path");
  40. int iExt = findExtension (path);
  41. if (extension != null && path.Length != 0) {
  42. if (extension [0] != '.')
  43. extension = "." + extension;
  44. } else
  45. extension = String.Empty;
  46. if (iExt < 0) {
  47. return path + extension;
  48. } else if (iExt > 0) {
  49. string temp = path.Substring (0, iExt);
  50. return temp + extension;
  51. }
  52. return extension;
  53. }
  54. public static string Combine (string path1, string path2)
  55. {
  56. if (path1 == null)
  57. throw new ArgumentNullException ("path1");
  58. if (path2 == null)
  59. throw new ArgumentNullException ("path2");
  60. if (path1 == String.Empty)
  61. return path2;
  62. if (path2 == String.Empty)
  63. return path1;
  64. if (path1.IndexOfAny (InvalidPathChars) != -1)
  65. throw new ArgumentException ("Illegal characters in path", "path1");
  66. if (path2.IndexOfAny (InvalidPathChars) != -1)
  67. throw new ArgumentException ("Illegal characters in path", "path2");
  68. //TODO???: UNC names
  69. // LAMESPEC: MS says that if path1 is not empty and path2 is a full path
  70. // it should throw ArgumentException
  71. if (IsPathRooted (path2))
  72. return path2;
  73. if (Array.IndexOf (PathSeparatorChars, path1 [path1.Length - 1]) == -1)
  74. return path1 + DirectorySeparatorChar + path2;
  75. return path1 + path2;
  76. }
  77. public static string GetDirectoryName (string path)
  78. {
  79. // LAMESPEC: For empty string MS docs say both
  80. // return null AND throw exception. Seems .NET throws.
  81. if (path == String.Empty)
  82. throw new ArgumentException();
  83. if (path == null || GetPathRoot (path) == path)
  84. return null;
  85. CheckArgument.WhitespaceOnly (path);
  86. CheckArgument.PathChars (path);
  87. int nLast = path.LastIndexOfAny (PathSeparatorChars);
  88. if (nLast == 0)
  89. nLast++;
  90. if (nLast > 0) {
  91. string ret = path.Substring (0, nLast);
  92. if (ret [ret.Length - 1] != DirectorySeparatorChar)
  93. return ret + DirectorySeparatorChar;
  94. else
  95. return ret;
  96. }
  97. return String.Empty;
  98. }
  99. public static string GetExtension (string path)
  100. {
  101. if (path == null)
  102. return null;
  103. if (path.IndexOfAny (InvalidPathChars) != -1)
  104. throw new ArgumentException ("Illegal characters in path", "path");
  105. int iExt = findExtension (path);
  106. if (iExt > -1)
  107. {
  108. if (iExt < path.Length - 1)
  109. return path.Substring (iExt);
  110. }
  111. return string.Empty;
  112. }
  113. public static string GetFileName (string path)
  114. {
  115. if (path == null || path == String.Empty)
  116. return path;
  117. if (path.IndexOfAny (InvalidPathChars) != -1)
  118. throw new ArgumentException ("Illegal characters in path", "path");
  119. int nLast = path.LastIndexOfAny (PathSeparatorChars);
  120. if (nLast >= 0)
  121. return path.Substring (nLast + 1);
  122. return path;
  123. }
  124. public static string GetFileNameWithoutExtension (string path)
  125. {
  126. return ChangeExtension (GetFileName (path), null);
  127. }
  128. public static string GetFullPath (string path)
  129. {
  130. if (path == null)
  131. throw (new ArgumentNullException (
  132. "path",
  133. "You must specify a path when calling System.IO.Path.GetFullPath"));
  134. if (path.Trim () == String.Empty)
  135. throw new ArgumentException ("The path is not of a legal form", "path");
  136. if (path.Length >= 2 &&
  137. IsDsc (path [0]) &&
  138. IsDsc (path [1])
  139. ) {
  140. if (path.Length == 2 || path.IndexOf (path [0], 2) < 0)
  141. throw new ArgumentException ("UNC pass should be of the form \\\\server\\share.");
  142. else
  143. if (path [0] == DirectorySeparatorChar)
  144. return path; // UNC
  145. else
  146. return path.Replace (AltDirectorySeparatorChar, DirectorySeparatorChar);
  147. }
  148. if (!IsPathRooted (path))
  149. path = Directory.GetCurrentDirectory () + DirectorySeparatorStr + path;
  150. else if (DirectorySeparatorChar == '\\' &&
  151. path.Length >= 2 &&
  152. IsDsc (path [0]) &&
  153. !IsDsc (path [1])) { // like `\abc\def'
  154. string current = Directory.GetCurrentDirectory ();
  155. if (current [1] == VolumeSeparatorChar)
  156. path = current.Substring (0, 2) + path;
  157. else
  158. path = current.Substring (0, current.IndexOf ('\\', current.IndexOf ("\\\\") + 1));
  159. }
  160. return CanonicalizePath (path);
  161. }
  162. static bool IsDsc (char c) {
  163. return c == DirectorySeparatorChar || c == AltDirectorySeparatorChar;
  164. }
  165. public static string GetPathRoot (string path)
  166. {
  167. if (path == null) return null;
  168. if (!IsPathRooted (path)) return String.Empty;
  169. if (DirectorySeparatorChar == '/') {
  170. // UNIX
  171. return IsDsc (path [0]) ? DirectorySeparatorStr : String.Empty;
  172. } else {
  173. // Windows
  174. int len = 2;
  175. if (path.Length == 1 && IsDsc (path [0]))
  176. return DirectorySeparatorStr;
  177. else if (path.Length < 2)
  178. return String.Empty;
  179. if (IsDsc (path [0]) && IsDsc (path[1])) {
  180. // UNC: \\server or \\server\share
  181. // Get server
  182. while (len < path.Length && !IsDsc (path [len])) len++;
  183. // Get share
  184. while (len < path.Length && !IsDsc (path [len])) len++;
  185. return DirectorySeparatorStr +
  186. DirectorySeparatorStr +
  187. path.Substring (2).Replace (AltDirectorySeparatorChar, DirectorySeparatorChar);
  188. } else if (IsDsc (path [0])) {
  189. // path starts with '\' or '/'
  190. return DirectorySeparatorStr;
  191. } else if (path[1] == VolumeSeparatorChar) {
  192. // C:\folder
  193. if (path.Length >= 3 && (IsDsc (path [2]))) len++;
  194. } else
  195. return Directory.GetCurrentDirectory ().Substring (0, 2);// + path.Substring (0, len);
  196. return path.Substring (0, len);
  197. }
  198. }
  199. public static string GetTempFileName ()
  200. {
  201. FileStream f = null;
  202. string path;
  203. Random rnd;
  204. int num = 0;
  205. rnd = new Random ();
  206. do {
  207. num = rnd.Next ();
  208. num++;
  209. path = Path.Combine (GetTempPath(), "tmp" + num.ToString("x"));
  210. try {
  211. f = new FileStream (path, FileMode.CreateNew);
  212. } catch {
  213. }
  214. } while (f == null);
  215. f.Close();
  216. return path;
  217. }
  218. /// <summary>
  219. /// Returns the path of the current systems temp directory
  220. /// </summary>
  221. public static string GetTempPath ()
  222. {
  223. string p = get_temp_path ();
  224. if (p.Length > 0 && p [p.Length - 1] != DirectorySeparatorChar)
  225. return p + DirectorySeparatorChar;
  226. return p;
  227. }
  228. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  229. private static extern string get_temp_path ();
  230. public static bool HasExtension (string path)
  231. {
  232. if (path == null || path.Trim () == String.Empty)
  233. return false;
  234. int pos = findExtension (path);
  235. return 0 <= pos && pos < path.Length - 1;
  236. }
  237. public static bool IsPathRooted (string path)
  238. {
  239. if (path == null || path.Length == 0)
  240. return false;
  241. if (path.IndexOfAny (InvalidPathChars) != -1)
  242. throw new ArgumentException ("Illegal characters in path", "path");
  243. char c = path [0];
  244. return (c == DirectorySeparatorChar ||
  245. c == AltDirectorySeparatorChar ||
  246. (!dirEqualsVolume && path.Length > 1 && path [1] == VolumeSeparatorChar));
  247. }
  248. // private class methods
  249. private static int findExtension (string path)
  250. {
  251. // method should return the index of the path extension
  252. // start or -1 if no valid extension
  253. if (path != null){
  254. int iLastDot = path.LastIndexOf ('.');
  255. int iLastSep = path.LastIndexOfAny ( PathSeparatorChars );
  256. if (iLastDot > iLastSep)
  257. return iLastDot;
  258. }
  259. return -1;
  260. }
  261. static Path () {
  262. VolumeSeparatorChar = MonoIO.VolumeSeparatorChar;
  263. DirectorySeparatorChar = MonoIO.DirectorySeparatorChar;
  264. AltDirectorySeparatorChar = MonoIO.AltDirectorySeparatorChar;
  265. PathSeparator = MonoIO.PathSeparator;
  266. InvalidPathChars = MonoIO.InvalidPathChars;
  267. // internal fields
  268. DirectorySeparatorStr = DirectorySeparatorChar.ToString ();
  269. PathSeparatorChars = new char [] {
  270. DirectorySeparatorChar,
  271. AltDirectorySeparatorChar,
  272. VolumeSeparatorChar
  273. };
  274. dirEqualsVolume = (DirectorySeparatorChar == VolumeSeparatorChar);
  275. }
  276. static string CanonicalizePath (string path) {
  277. // STEP 1: Check for empty string
  278. if (path == null) return path;
  279. path = path.Trim ();
  280. if (path == String.Empty) return path;
  281. // STEP 2: Check to see if this is only a root
  282. string root = GetPathRoot (path);
  283. // if (root == path) return path; // it will return '\' for path '\', while it should return 'c:\' or so.
  284. // STEP 3: split the directories, this gets rid of consecutative "/"'s
  285. string [] dirs = path.Split (DirectorySeparatorChar, AltDirectorySeparatorChar);
  286. // STEP 4: Get rid of directories containing . and ..
  287. int target = 0;
  288. for (int i = 0; i < dirs.Length; i++) {
  289. if (dirs [i] == "." || (i != 0 && dirs [i] == String.Empty)) continue;
  290. else if (dirs [i] == "..") {
  291. if (target != 0) target--;
  292. }
  293. else
  294. dirs [target++] = dirs [i];
  295. }
  296. // STEP 5: Combine everything.
  297. if (target == 0)
  298. return root;
  299. else {
  300. string ret = String.Join (DirectorySeparatorStr, dirs, 0, target);
  301. switch (DirectorySeparatorChar) {
  302. case '\\': // Windows
  303. // In GetFullPath(), it is assured that here never comes UNC. So this must only applied to such path that starts with '\', without drive specification.
  304. if (path [0] != DirectorySeparatorChar && path.StartsWith (root)) {
  305. if (ret.Length <= 2 && !ret.EndsWith (DirectorySeparatorStr)) // '\' after "c:"
  306. ret += DirectorySeparatorChar;
  307. return ret;
  308. } else {
  309. string current = Directory.GetCurrentDirectory ();
  310. if (current.Length > 1 && current [1] == VolumeSeparatorChar) {
  311. // DOS local file path
  312. if (ret.Length == 0 || IsDsc (ret [0]))
  313. ret += '\\';
  314. return current.Substring (0, 2) + ret;
  315. }
  316. else if (IsDsc (current [current.Length - 1]) && IsDsc (ret [0]))
  317. return current + ret.Substring (1);
  318. else
  319. return current + ret;
  320. }
  321. default: // Unix/Mac
  322. return ret;
  323. }
  324. }
  325. }
  326. }
  327. }