Path.cs 5.8 KB

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