using System;
namespace Renci.SshNet.Common
{
internal class PosixPath
{
public string Directory { get; private set; }
public string File { get; private set; }
public static PosixPath CreateAbsoluteOrRelativeFilePath(string path)
{
if (path == null)
{
throw new ArgumentNullException("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.", "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 == null)
throw new ArgumentNullException("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 == null)
throw new ArgumentNullException("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);
}
}
}