Path.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. namespace System.IO
  13. {
  14. public class Path : Object
  15. {
  16. public static readonly char AltDirectorySeparatorChar = PlatformSpecific.InvalidPathChars;
  17. public static readonly char DirectorySeparatorChar = PlatformSpecific.InvalidPathChars;
  18. public static readonly char[] InvalidPathChars = PlatformSpecific.InvalidPathChars;
  19. public static readonly char PathSeparator = PlatformSpecific.InvalidPathChars;
  20. public static readonly char VolumeSeparatorChar = PlatformSpecific.InvalidPathChars;
  21. private static readonly char[] PathSeparatorChars = { DirectorySeparatorChar,
  22. AltDirectorySeparatorChar,
  23. VolumeSeparatorChar };
  24. // class methods
  25. public static string ChangeExtension(string path, string extension)
  26. {
  27. if(path == null)
  28. {
  29. return null;
  30. }
  31. int iExt = findExtension(path);
  32. if(iExt < 0)
  33. {
  34. return extension == null ? path : path + extension;
  35. }
  36. else if(iExt > 0)
  37. {
  38. string temp = path.Substring(0, iExt);
  39. if(extension != null)
  40. {
  41. return temp + extension;
  42. }
  43. return temp;
  44. }
  45. return extension;
  46. }
  47. public static string Combine(string path1, string path2)
  48. {
  49. if(path1 == null || path2 == null)
  50. {
  51. return null;
  52. }
  53. throwEmptyIf(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. return null;
  85. }
  86. throwEmptyIf(path);
  87. throwWhiteSpaceOnlyIf(path);
  88. throwInvalidPathCharsIf(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. return path;
  98. }
  99. public static string GetExtension(string path)
  100. {
  101. if(path == null)
  102. {
  103. return string.Empty;
  104. }
  105. throwEmptyIf(path);
  106. throwWhiteSpaceOnlyIf(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. throwEmptyIf(path);
  122. throwWhiteSpaceOnlyIf(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. throwNullIf(path);
  169. throwEmptyIf(path);
  170. throwWhiteSpaceOnlyIf(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. private static void throwNullIf(string path)
  193. {
  194. if(path == null)
  195. {
  196. throw new ArgumentNullException();
  197. }
  198. }
  199. private static void throwEmptyIf(string path)
  200. {
  201. if(path != null && path.Length == 0)
  202. {
  203. throw new ArgumentException("Empty string");
  204. }
  205. }
  206. private static void throwWhiteSpaceOnlyIf(string path)
  207. {
  208. if(path != null)
  209. {
  210. string temp = path;
  211. temp.Trim();
  212. if(temp.Length == 0)
  213. {
  214. throw new ArgumentException("Whitespace only string");
  215. }
  216. }
  217. }
  218. private static void throwInvalidPathCharsIf(string path)
  219. {
  220. if(path != null && path.IndexOfAny(InvalidPathChars) > -1)
  221. {
  222. throw new ArgumentException("Invalid path characters");
  223. }
  224. }
  225. }
  226. }