Path.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 = '\\'; // TODO: verify this
  17. public static readonly char DirectorySeparatorChar = '/';
  18. public static readonly char[] InvalidPathChars = { '\0' }; // TODO: research invalid chars
  19. public static readonly char PathSeparator = ';'; // might be a space for unix/linux
  20. public static readonly char VolumeSeparatorChar = '/';
  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: figure out what the equivilent linux api to
  139. // windoze ::GetCurrentDirectory() is and PInvoke it
  140. return path;
  141. }
  142. return null;
  143. }
  144. public static string GetPathRoot(string path)
  145. {
  146. if(path != null ||
  147. (path.StartsWith(new string(DirectorySeparatorChar, 1)) ||
  148. path.StartsWith(new string(AltDirectorySeparatorChar, 1))))
  149. {
  150. return path.Substring(0, 1);
  151. }
  152. return null;
  153. }
  154. public static string GetTempFileName()
  155. {
  156. //TODO: Implement method
  157. return string.Empty;
  158. }
  159. /// <summary>
  160. /// Returns the path of the current systems temp directory
  161. /// </summary>
  162. public static string GetTempPath()
  163. { // TODO: This might vary with distribution and there
  164. // might be an api to provide it. Research is needed
  165. return "/tmp";
  166. }
  167. public static bool HasExtension(string path)
  168. {
  169. throwNullIf(path);
  170. throwEmptyIf(path);
  171. throwWhiteSpaceOnlyIf(path);
  172. return findExtension(path) > -1;
  173. }
  174. public static bool IsPathRooted(string path)
  175. {
  176. return path.StartsWith(new string(VolumeSeparatorChar,1));
  177. }
  178. // private class methods
  179. private static int findExtension(string path)
  180. { // method should return the index of the path extension
  181. // start or -1 if no valid extension
  182. if(path != null)
  183. {
  184. int iLastDot = path.LastIndexOf(".");
  185. int iLastSep = path.LastIndexOfAny( PathSeparatorChars );
  186. if(iLastDot > iLastSep)
  187. {
  188. return iLastDot;
  189. }
  190. }
  191. return -1;
  192. }
  193. private static void throwNullIf(string path)
  194. {
  195. if(path == null)
  196. {
  197. throw new ArgumentNullException();
  198. }
  199. }
  200. private static void throwEmptyIf(string path)
  201. {
  202. if(path != null && path.Length == 0)
  203. {
  204. throw new ArgumentException("Empty string");
  205. }
  206. }
  207. private static void throwWhiteSpaceOnlyIf(string path)
  208. {
  209. if(path != null)
  210. {
  211. string temp = path;
  212. temp.Trim();
  213. if(temp.Length == 0)
  214. {
  215. throw new ArgumentException("Whitespace only string");
  216. }
  217. }
  218. }
  219. private static void throwInvalidPathCharsIf(string path)
  220. {
  221. if(path != null && path.IndexOfAny(InvalidPathChars) > -1)
  222. {
  223. throw new ArgumentException("Invalid path characters");
  224. }
  225. }
  226. }
  227. }