Path.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. int l = ret.Length;
  93. if (l >= 2 && ret [l - 1] != DirectorySeparatorChar &&
  94. ret [l - 2] == VolumeSeparatorChar)
  95. return ret + DirectorySeparatorChar;
  96. else
  97. return ret;
  98. }
  99. return String.Empty;
  100. }
  101. public static string GetExtension (string path)
  102. {
  103. if (path == null)
  104. return null;
  105. if (path.IndexOfAny (InvalidPathChars) != -1)
  106. throw new ArgumentException ("Illegal characters in path", "path");
  107. int iExt = findExtension (path);
  108. if (iExt > -1)
  109. {
  110. if (iExt < path.Length - 1)
  111. return path.Substring (iExt);
  112. }
  113. return string.Empty;
  114. }
  115. public static string GetFileName (string path)
  116. {
  117. if (path == null || path == String.Empty)
  118. return path;
  119. if (path.IndexOfAny (InvalidPathChars) != -1)
  120. throw new ArgumentException ("Illegal characters in path", "path");
  121. int nLast = path.LastIndexOfAny (PathSeparatorChars);
  122. if (nLast >= 0)
  123. return path.Substring (nLast + 1);
  124. return path;
  125. }
  126. public static string GetFileNameWithoutExtension (string path)
  127. {
  128. return ChangeExtension (GetFileName (path), null);
  129. }
  130. public static string GetFullPath (string path)
  131. {
  132. if (path == null)
  133. throw (new ArgumentNullException (
  134. "path",
  135. "You must specify a path when calling System.IO.Path.GetFullPath"));
  136. if (path.Trim () == String.Empty)
  137. throw new ArgumentException ("The path is not of a legal form", "path");
  138. if (path.Length >= 2 &&
  139. IsDsc (path [0]) &&
  140. IsDsc (path [1])) {
  141. if (path.Length == 2 || path.IndexOf (path [0], 2) < 0)
  142. throw new ArgumentException ("UNC pass should be of the form \\\\server\\share.");
  143. else
  144. if (path [0] == DirectorySeparatorChar)
  145. return path; // UNC
  146. else
  147. return path.Replace (AltDirectorySeparatorChar, DirectorySeparatorChar);
  148. }
  149. if (!IsPathRooted (path))
  150. path = Directory.GetCurrentDirectory () + DirectorySeparatorStr + path;
  151. else if (DirectorySeparatorChar == '\\' &&
  152. path.Length >= 2 &&
  153. IsDsc (path [0]) &&
  154. !IsDsc (path [1])) { // like `\abc\def'
  155. string current = Directory.GetCurrentDirectory ();
  156. if (current [1] == VolumeSeparatorChar)
  157. path = current.Substring (0, 2) + path;
  158. else
  159. path = current.Substring (0, current.IndexOf ('\\', current.IndexOf ("\\\\") + 1));
  160. }
  161. return CanonicalizePath (path);
  162. }
  163. static bool IsDsc (char c) {
  164. return c == DirectorySeparatorChar || c == AltDirectorySeparatorChar;
  165. }
  166. public static string GetPathRoot (string path)
  167. {
  168. if (path == null) return null;
  169. if (!IsPathRooted (path)) return String.Empty;
  170. if (DirectorySeparatorChar == '/') {
  171. // UNIX
  172. return IsDsc (path [0]) ? DirectorySeparatorStr : String.Empty;
  173. } else {
  174. // Windows
  175. int len = 2;
  176. if (path.Length == 1 && IsDsc (path [0]))
  177. return DirectorySeparatorStr;
  178. else if (path.Length < 2)
  179. return String.Empty;
  180. if (IsDsc (path [0]) && IsDsc (path[1])) {
  181. // UNC: \\server or \\server\share
  182. // Get server
  183. while (len < path.Length && !IsDsc (path [len])) len++;
  184. // Get share
  185. while (len < path.Length && !IsDsc (path [len])) len++;
  186. return DirectorySeparatorStr +
  187. DirectorySeparatorStr +
  188. path.Substring (2).Replace (AltDirectorySeparatorChar, DirectorySeparatorChar);
  189. } else if (IsDsc (path [0])) {
  190. // path starts with '\' or '/'
  191. return DirectorySeparatorStr;
  192. } else if (path[1] == VolumeSeparatorChar) {
  193. // C:\folder
  194. if (path.Length >= 3 && (IsDsc (path [2]))) len++;
  195. } else
  196. return Directory.GetCurrentDirectory ().Substring (0, 2);// + path.Substring (0, len);
  197. return path.Substring (0, len);
  198. }
  199. }
  200. public static string GetTempFileName ()
  201. {
  202. FileStream f = null;
  203. string path;
  204. Random rnd;
  205. int num = 0;
  206. rnd = new Random ();
  207. do {
  208. num = rnd.Next ();
  209. num++;
  210. path = Path.Combine (GetTempPath(), "tmp" + num.ToString("x"));
  211. try {
  212. f = new FileStream (path, FileMode.CreateNew);
  213. } catch {
  214. }
  215. } while (f == null);
  216. f.Close();
  217. return path;
  218. }
  219. /// <summary>
  220. /// Returns the path of the current systems temp directory
  221. /// </summary>
  222. public static string GetTempPath ()
  223. {
  224. string p = get_temp_path ();
  225. if (p.Length > 0 && p [p.Length - 1] != DirectorySeparatorChar)
  226. return p + DirectorySeparatorChar;
  227. return p;
  228. }
  229. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  230. private static extern string get_temp_path ();
  231. public static bool HasExtension (string path)
  232. {
  233. if (path == null || path.Trim () == String.Empty)
  234. return false;
  235. int pos = findExtension (path);
  236. return 0 <= pos && pos < path.Length - 1;
  237. }
  238. public static bool IsPathRooted (string path)
  239. {
  240. if (path == null || path.Length == 0)
  241. return false;
  242. if (path.IndexOfAny (InvalidPathChars) != -1)
  243. throw new ArgumentException ("Illegal characters in path", "path");
  244. char c = path [0];
  245. return (c == DirectorySeparatorChar ||
  246. c == AltDirectorySeparatorChar ||
  247. (!dirEqualsVolume && path.Length > 1 && path [1] == VolumeSeparatorChar));
  248. }
  249. // private class methods
  250. private static int findExtension (string path)
  251. {
  252. // method should return the index of the path extension
  253. // start or -1 if no valid extension
  254. if (path != null){
  255. int iLastDot = path.LastIndexOf ('.');
  256. int iLastSep = path.LastIndexOfAny ( PathSeparatorChars );
  257. if (iLastDot > iLastSep)
  258. return iLastDot;
  259. }
  260. return -1;
  261. }
  262. static Path () {
  263. VolumeSeparatorChar = MonoIO.VolumeSeparatorChar;
  264. DirectorySeparatorChar = MonoIO.DirectorySeparatorChar;
  265. AltDirectorySeparatorChar = MonoIO.AltDirectorySeparatorChar;
  266. PathSeparator = MonoIO.PathSeparator;
  267. InvalidPathChars = MonoIO.InvalidPathChars;
  268. // internal fields
  269. DirectorySeparatorStr = DirectorySeparatorChar.ToString ();
  270. PathSeparatorChars = new char [] {
  271. DirectorySeparatorChar,
  272. AltDirectorySeparatorChar,
  273. VolumeSeparatorChar
  274. };
  275. dirEqualsVolume = (DirectorySeparatorChar == VolumeSeparatorChar);
  276. }
  277. static string CanonicalizePath (string path) {
  278. // STEP 1: Check for empty string
  279. if (path == null) return path;
  280. path = path.Trim ();
  281. if (path == String.Empty) return path;
  282. // STEP 2: Check to see if this is only a root
  283. string root = GetPathRoot (path);
  284. // it will return '\' for path '\', while it should return 'c:\' or so.
  285. // Note: commenting this out makes the ened for the (target == 1...) check in step 5
  286. //if (root == path) return path;
  287. // STEP 3: split the directories, this gets rid of consecutative "/"'s
  288. string [] dirs = path.Split (DirectorySeparatorChar, AltDirectorySeparatorChar);
  289. // STEP 4: Get rid of directories containing . and ..
  290. int target = 0;
  291. for (int i = 0; i < dirs.Length; i++) {
  292. if (dirs [i] == "." || (i != 0 && dirs [i] == String.Empty)) continue;
  293. else if (dirs [i] == "..") {
  294. if (target != 0) target--;
  295. }
  296. else
  297. dirs [target++] = dirs [i];
  298. }
  299. // STEP 5: Combine everything.
  300. if (target == 0 || (target == 1 && dirs [0] == ""))
  301. return root;
  302. else {
  303. string ret = String.Join (DirectorySeparatorStr, dirs, 0, target);
  304. switch (DirectorySeparatorChar) {
  305. case '\\': // Windows
  306. // In GetFullPath(), it is assured that here never comes UNC. So this must only applied to such path that starts with '\', without drive specification.
  307. if (path [0] != DirectorySeparatorChar && path.StartsWith (root)) {
  308. if (ret.Length <= 2 && !ret.EndsWith (DirectorySeparatorStr)) // '\' after "c:"
  309. ret += DirectorySeparatorChar;
  310. return ret;
  311. } else {
  312. string current = Directory.GetCurrentDirectory ();
  313. if (current.Length > 1 && current [1] == VolumeSeparatorChar) {
  314. // DOS local file path
  315. if (ret.Length == 0 || IsDsc (ret [0]))
  316. ret += '\\';
  317. return current.Substring (0, 2) + ret;
  318. }
  319. else if (IsDsc (current [current.Length - 1]) && IsDsc (ret [0]))
  320. return current + ret.Substring (1);
  321. else
  322. return current + ret;
  323. }
  324. default: // Unix/Mac
  325. return ret;
  326. }
  327. }
  328. }
  329. }
  330. }