Path.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //------------------------------------------------------------------------------
  2. //
  3. // System.IO.Path.cs
  4. //
  5. // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
  6. //
  7. // Author: Jim Richardson, [email protected]
  8. // Dan Lewis ([email protected])
  9. // Created: Saturday, August 11, 2001
  10. //
  11. //------------------------------------------------------------------------------
  12. using System;
  13. namespace System.IO
  14. {
  15. public sealed class Path
  16. {
  17. public static readonly char AltDirectorySeparatorChar;
  18. public static readonly char DirectorySeparatorChar;
  19. public static readonly char[] InvalidPathChars;
  20. public static readonly char PathSeparator;
  21. internal static readonly string DirectorySeparatorStr;
  22. public static readonly char VolumeSeparatorChar;
  23. private static readonly char[] PathSeparatorChars;
  24. private Path () {}
  25. // class methods
  26. public static string ChangeExtension (string path, string extension)
  27. {
  28. if (path == null)
  29. {
  30. return null;
  31. }
  32. int iExt = findExtension (path);
  33. if (extension != null) {
  34. if (extension [0] != '.')
  35. extension = "." + extension;
  36. } else
  37. extension = "";
  38. if (iExt < 0) {
  39. return path + extension;
  40. } else if (iExt > 0) {
  41. string temp = path.Substring (0, iExt);
  42. return temp + extension;
  43. }
  44. return extension;
  45. }
  46. [MonoTODO]
  47. public static string Combine (string path1, string path2)
  48. {
  49. if (path1 == null || path2 == null)
  50. {
  51. return null;
  52. }
  53. CheckArgument.Empty (path2);
  54. // TODO: Check for invalid DirectoryInfo characters
  55. // although I don't think it is necesary for linux
  56. // TODO: Verify functionality further after NUnit tests written
  57. // since the documentation was rather sketchy
  58. if (IsPathRooted (path2))
  59. {
  60. if (path1.Equals (string.Empty))
  61. {
  62. return path2;
  63. }
  64. throw new ArgumentException ("Rooted path");
  65. }
  66. string dirSep = new string (DirectorySeparatorChar, 1);
  67. string altSep = new string (AltDirectorySeparatorChar, 1);
  68. bool b1 = path1.EndsWith (dirSep) || path1.EndsWith (dirSep);
  69. bool b2 = path2.StartsWith (dirSep) || path2.StartsWith (altSep);
  70. if (b1 && b2)
  71. {
  72. throw new ArgumentException ("Invalid combination");
  73. }
  74. if (!b1 && !b2)
  75. {
  76. return path1 + dirSep + path2;
  77. }
  78. return path1 + path2;
  79. }
  80. public static string GetDirectoryName (string path)
  81. {
  82. if (path != null)
  83. {
  84. CheckArgument.Empty (path);
  85. CheckArgument.WhitespaceOnly (path);
  86. CheckArgument.PathChars (path);
  87. if (path.Length > 0)
  88. {
  89. int nLast = path.LastIndexOfAny (PathSeparatorChars);
  90. if (nLast > 0)
  91. return path.Substring (0, nLast);
  92. else
  93. return String.Empty;
  94. }
  95. }
  96. return path;
  97. }
  98. public static string GetExtension (string path)
  99. {
  100. if (path == null)
  101. {
  102. return string.Empty;
  103. }
  104. CheckArgument.Empty (path);
  105. CheckArgument.WhitespaceOnly (path);
  106. int iExt = findExtension (path);
  107. if (iExt > -1)
  108. { // okay it has an extension
  109. return path.Substring (iExt);
  110. }
  111. return string.Empty;
  112. }
  113. public static string GetFileName (string path)
  114. {
  115. if (path == null)
  116. {
  117. return string.Empty;
  118. }
  119. CheckArgument.Empty (path);
  120. CheckArgument.WhitespaceOnly (path);
  121. int nLast = path.LastIndexOfAny (PathSeparatorChars);
  122. if (nLast > 0)
  123. {
  124. return path.Substring (nLast + 1);
  125. }
  126. return nLast == 0 ? null : path;
  127. }
  128. public static string GetFileNameWithoutExtension (string path)
  129. {
  130. return ChangeExtension (GetFileName (path), null);
  131. }
  132. public static string GetFullPath (string path)
  133. {
  134. if (path == null)
  135. throw (new ArgumentNullException (
  136. "path",
  137. "You must specify a path when calling System.IO.Path.GetFullPath"));
  138. if (path.StartsWith (new string (DirectorySeparatorChar, 1)) ||
  139. path.StartsWith (new string (AltDirectorySeparatorChar, 1)))
  140. return path;
  141. return Directory.GetCurrentDirectory () + new string (DirectorySeparatorChar, 1) + path;
  142. }
  143. public static string GetPathRoot (string path)
  144. {
  145. if (path != null ||
  146. (path.StartsWith (new string (DirectorySeparatorChar, 1)) ||
  147. path.StartsWith (new string (AltDirectorySeparatorChar, 1))))
  148. {
  149. return path.Substring (0, 1);
  150. }
  151. return null;
  152. }
  153. public static string GetTempFileName ()
  154. {
  155. string path;
  156. Random rnd;
  157. int num;
  158. rnd = new Random ();
  159. num = rnd.Next ();
  160. path = GetTempPath() + DirectorySeparatorChar + "tmp" + num.ToString("x");
  161. while (File.Exists(path) || Directory.Exists(path)) {
  162. num = rnd.Next ();
  163. path = GetTempPath() + DirectorySeparatorChar + "tmp" + num.ToString("x");
  164. }
  165. FileStream f = File.Create(path);
  166. f.Close();
  167. return path;
  168. }
  169. /// <summary>
  170. /// Returns the path of the current systems temp directory
  171. /// </summary>
  172. [MonoTODO]
  173. public static string GetTempPath ()
  174. { // TODO: This might vary with distribution and there
  175. // might be an api to provide it. Research is needed
  176. return "/tmp";
  177. }
  178. public static bool HasExtension (string path)
  179. {
  180. CheckArgument.Null (path);
  181. CheckArgument.Empty (path);
  182. CheckArgument.WhitespaceOnly (path);
  183. return findExtension (path) > -1;
  184. }
  185. public static bool IsPathRooted (string path)
  186. {
  187. return path.StartsWith (new string (VolumeSeparatorChar,1));
  188. }
  189. // private class methods
  190. private static int findExtension (string path)
  191. {
  192. // method should return the index of the path extension
  193. // start or -1 if no valid extension
  194. if (path != null){
  195. int iLastDot = path.LastIndexOf (".");
  196. int iLastSep = path.LastIndexOfAny ( PathSeparatorChars );
  197. if (iLastDot > iLastSep)
  198. return iLastDot;
  199. }
  200. return -1;
  201. }
  202. static Path () {
  203. VolumeSeparatorChar = MonoIO.VolumeSeparatorChar;
  204. DirectorySeparatorChar = MonoIO.DirectorySeparatorChar;
  205. AltDirectorySeparatorChar = MonoIO.AltDirectorySeparatorChar;
  206. PathSeparator = MonoIO.PathSeparator;
  207. InvalidPathChars = MonoIO.InvalidPathChars;
  208. // internal fields
  209. DirectorySeparatorStr = DirectorySeparatorChar.ToString ();
  210. PathSeparatorChars = new char [] {
  211. DirectorySeparatorChar,
  212. AltDirectorySeparatorChar,
  213. VolumeSeparatorChar
  214. };
  215. }
  216. }
  217. }