Path.cs 5.6 KB

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