PosixPath.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. namespace Renci.SshNet.Common
  3. {
  4. internal class PosixPath
  5. {
  6. public string Directory { get; private set; }
  7. public string File { get; private set; }
  8. public static PosixPath CreateAbsoluteOrRelativeFilePath(string path)
  9. {
  10. if (path == null)
  11. {
  12. throw new ArgumentNullException("path");
  13. }
  14. var posixPath = new PosixPath();
  15. var pathEnd = path.LastIndexOf('/');
  16. if (pathEnd == -1)
  17. {
  18. if (path.Length == 0)
  19. {
  20. throw new ArgumentException("The path is a zero-length string.", "path");
  21. }
  22. posixPath.Directory = ".";
  23. posixPath.File = path;
  24. }
  25. else if (pathEnd == 0)
  26. {
  27. posixPath.Directory = "/";
  28. if (path.Length > 1)
  29. {
  30. posixPath.File = path.Substring(pathEnd + 1);
  31. }
  32. }
  33. else
  34. {
  35. posixPath.Directory = path.Substring(0, pathEnd);
  36. if (pathEnd < path.Length - 1)
  37. {
  38. posixPath.File = path.Substring(pathEnd + 1);
  39. }
  40. }
  41. return posixPath;
  42. }
  43. /// <summary>
  44. /// Gets the file name part of a given POSIX path.
  45. /// </summary>
  46. /// <param name="path">The POSIX path to get the file name for.</param>
  47. /// <returns>
  48. /// The file name part of <paramref name="path"/>.
  49. /// </returns>
  50. /// <exception cref="NullReferenceException"><paramref name="path"/> is <c>null</c>.</exception>
  51. /// <remarks>
  52. /// <para>
  53. /// If <paramref name="path"/> contains no forward slash, then <paramref name="path"/>
  54. /// is returned.
  55. /// </para>
  56. /// <para>
  57. /// If path has a trailing slash, <see cref="GetFileName(string)"/> return a zero-length string.
  58. /// </para>
  59. /// </remarks>
  60. public static string GetFileName(string path)
  61. {
  62. if (path == null)
  63. throw new ArgumentNullException("path");
  64. var pathEnd = path.LastIndexOf('/');
  65. if (pathEnd == -1)
  66. return path;
  67. if (pathEnd == path.Length - 1)
  68. return string.Empty;
  69. return path.Substring(pathEnd + 1);
  70. }
  71. /// <summary>
  72. /// Gets the directory name part of a given POSIX path.
  73. /// </summary>
  74. /// <param name="path">The POSIX path to get the directory name for.</param>
  75. /// <returns>
  76. /// The directory part of the specified <paramref name="path"/>, or <c>.</c> if <paramref name="path"/>
  77. /// does not contain any directory information.
  78. /// </returns>
  79. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <c>null</c>.</exception>
  80. public static string GetDirectoryName(string path)
  81. {
  82. if (path == null)
  83. throw new ArgumentNullException("path");
  84. var pathEnd = path.LastIndexOf('/');
  85. if (pathEnd == -1)
  86. return ".";
  87. if (pathEnd == 0)
  88. return "/";
  89. if (pathEnd == path.Length - 1)
  90. return path.Substring(0, pathEnd);
  91. return path.Substring(0, pathEnd);
  92. }
  93. }
  94. }