Path.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. public static readonly char VolumeSeparatorChar = _os.VolumeSeparator;
  23. private static readonly char[] PathSeparatorChars = { DirectorySeparatorChar,
  24. AltDirectorySeparatorChar,
  25. VolumeSeparatorChar };
  26. // class methods
  27. public static string ChangeExtension(string path, string extension)
  28. {
  29. if(path == null)
  30. {
  31. return null;
  32. }
  33. int iExt = findExtension(path);
  34. if(iExt < 0)
  35. {
  36. return extension == null ? path : path + extension;
  37. }
  38. else if(iExt > 0)
  39. {
  40. string temp = path.Substring(0, iExt);
  41. if(extension != null)
  42. {
  43. return temp + extension;
  44. }
  45. return temp;
  46. }
  47. return extension;
  48. }
  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. {
  139. //TODO: Implement this correctly
  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. CheckArgument.Null(path);
  170. CheckArgument.Empty(path);
  171. CheckArgument.WhitespaceOnly(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. }
  194. }