Path.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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.IO.Private;
  13. namespace System.IO
  14. {
  15. public class Path : Object
  16. {
  17. public static readonly char AltDirectorySeparatorChar = PlatformSpecific.AltDirectorySeparatorChar;
  18. public static readonly char DirectorySeparatorChar = PlatformSpecific.DirectorySeparatorChar;
  19. public static readonly char[] InvalidPathChars = PlatformSpecific.InvalidPathChars;
  20. public static readonly char PathSeparator = PlatformSpecific.PathSeparator;
  21. public static readonly char VolumeSeparatorChar = PlatformSpecific.VolumeSeparatorChar;
  22. private static readonly char[] PathSeparatorChars = { DirectorySeparatorChar,
  23. AltDirectorySeparatorChar,
  24. VolumeSeparatorChar };
  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. public static string Combine(string path1, string path2)
  49. {
  50. if(path1 == null || path2 == null)
  51. {
  52. return null;
  53. }
  54. CheckArgument.Empty(path2);
  55. // TODO: Check for invalid DirectoryInfo characters
  56. // although I don't think it is necesary for linux
  57. // TODO: Verify functionality further after NUnit tests written
  58. // since the documentation was rather sketchy
  59. if(IsPathRooted(path2))
  60. {
  61. if(path1.Equals(string.Empty))
  62. {
  63. return path2;
  64. }
  65. throw new ArgumentException("Rooted path");
  66. }
  67. string dirSep = new string(DirectorySeparatorChar, 1);
  68. string altSep = new string(AltDirectorySeparatorChar, 1);
  69. bool b1 = path1.EndsWith(dirSep) || path1.EndsWith(dirSep);
  70. bool b2 = path2.StartsWith(dirSep) || path2.StartsWith(altSep);
  71. if(b1 && b2)
  72. {
  73. throw new ArgumentException("Invalid combination");
  74. }
  75. if(!b1 && !b2)
  76. {
  77. return path1 + dirSep + path2;
  78. }
  79. return path1 + path2;
  80. }
  81. public static string GetDirectoryName(string path)
  82. {
  83. if(path != null)
  84. {
  85. CheckArgument.Empty(path);
  86. CheckArgument.WhitespaceOnly(path);
  87. CheckArgument.PathChars(path);
  88. if(path.Length > 2)
  89. {
  90. int nLast = path.LastIndexOfAny(PathSeparatorChars, path.Length - 2);
  91. if(nLast > 0)
  92. {
  93. return path.Substring(0, nLast);
  94. }
  95. }
  96. }
  97. return path;
  98. }
  99. public static string GetExtension(string path)
  100. {
  101. if(path == null)
  102. {
  103. return string.Empty;
  104. }
  105. CheckArgument.Empty(path);
  106. CheckArgument.WhitespaceOnly(path);
  107. int iExt = findExtension(path);
  108. int iLastSep = path.LastIndexOfAny( PathSeparatorChars );
  109. if(iExt > -1)
  110. { // okay it has an extension
  111. return path.Substring(iExt);
  112. }
  113. return string.Empty;
  114. }
  115. public static string GetFileName(string path)
  116. {
  117. if(path == null)
  118. {
  119. return string.Empty;
  120. }
  121. CheckArgument.Empty(path);
  122. CheckArgument.WhitespaceOnly(path);
  123. int nLast = path.LastIndexOfAny(PathSeparatorChars);
  124. if(nLast > 0)
  125. {
  126. return path.Substring(nLast + 1);
  127. }
  128. return nLast == 0 ? null : path;
  129. }
  130. public static string GetFileNameWithoutExtension(string path)
  131. {
  132. return ChangeExtension(GetFileName(path), null);
  133. }
  134. public static string GetFullPath(string path)
  135. {
  136. if(path != null)
  137. {
  138. //TODO: Implement this correctly
  139. return path;
  140. }
  141. return null;
  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. //TODO: Implement method
  156. return string.Empty;
  157. }
  158. /// <summary>
  159. /// Returns the path of the current systems temp directory
  160. /// </summary>
  161. public static string GetTempPath()
  162. { // TODO: This might vary with distribution and there
  163. // might be an api to provide it. Research is needed
  164. return "/tmp";
  165. }
  166. public static bool HasExtension(string path)
  167. {
  168. CheckArgument.Null(path);
  169. CheckArgument.Empty(path);
  170. CheckArgument.WhitespaceOnly(path);
  171. return findExtension(path) > -1;
  172. }
  173. public static bool IsPathRooted(string path)
  174. {
  175. return path.StartsWith(new string(VolumeSeparatorChar,1));
  176. }
  177. // private class methods
  178. private static int findExtension(string path)
  179. { // method should return the index of the path extension
  180. // start or -1 if no valid extension
  181. if(path != null)
  182. {
  183. int iLastDot = path.LastIndexOf(".");
  184. int iLastSep = path.LastIndexOfAny( PathSeparatorChars );
  185. if(iLastDot > iLastSep)
  186. {
  187. return iLastDot;
  188. }
  189. }
  190. return -1;
  191. }
  192. }
  193. }