using System; namespace Renci.SshNet.Common { /// /// Represents a POSIX path. /// internal sealed class PosixPath { private PosixPath() { } /// /// Gets the directory of the path. /// /// /// The directory of the path. /// public string Directory { get; private set; } /// /// Gets the file part of the path. /// /// /// The file part of the path, or if the path represents a directory. /// public string File { get; private set; } /// /// Create a from the specified path. /// /// The path. /// /// A created from the specified path. /// /// is . /// is empty (""). public static PosixPath CreateAbsoluteOrRelativeFilePath(string path) { if (path is null) { throw new ArgumentNullException(nameof(path)); } var posixPath = new PosixPath(); var pathEnd = path.LastIndexOf('/'); if (pathEnd == -1) { if (path.Length == 0) { throw new ArgumentException("The path is a zero-length string.", nameof(path)); } posixPath.Directory = "."; posixPath.File = path; } else if (pathEnd == 0) { posixPath.Directory = "/"; if (path.Length > 1) { posixPath.File = path.Substring(pathEnd + 1); } } else { posixPath.Directory = path.Substring(0, pathEnd); if (pathEnd < path.Length - 1) { posixPath.File = path.Substring(pathEnd + 1); } } return posixPath; } /// /// Gets the file name part of a given POSIX path. /// /// The POSIX path to get the file name for. /// /// The file name part of . /// /// is null. /// /// /// If contains no forward slash, then /// is returned. /// /// /// If path has a trailing slash, return a zero-length string. /// /// public static string GetFileName(string path) { if (path is null) { throw new ArgumentNullException(nameof(path)); } var pathEnd = path.LastIndexOf('/'); if (pathEnd == -1) { return path; } if (pathEnd == path.Length - 1) { return string.Empty; } return path.Substring(pathEnd + 1); } /// /// Gets the directory name part of a given POSIX path. /// /// The POSIX path to get the directory name for. /// /// The directory part of the specified , or . if /// does not contain any directory information. /// /// is null. public static string GetDirectoryName(string path) { if (path is null) { throw new ArgumentNullException(nameof(path)); } var pathEnd = path.LastIndexOf('/'); if (pathEnd == -1) { return "."; } if (pathEnd == 0) { return "/"; } if (pathEnd == path.Length - 1) { return path.Substring(0, pathEnd); } return path.Substring(0, pathEnd); } } }