public static Path CalculateFullPath(Path currentLocation, Path changeCommand) { changeCommand = (changeCommand ?? string.Empty).NormalizeSlashes(); currentLocation = currentLocation.NormalizeSlashes(); bool applyParts = false; Path resultPath; // use the input 'changeCommand' path if it's // 'rooted' otherwise we go from the currentLocation if (changeCommand.HasDrive()) { // windows case where changeCommand == "/" or "\" but the currentLocation has a "C:" drive string currentLocationDrive = currentLocation.GetDrive(); if (changeCommand.StartsWithSlash() && !changeCommand.GetDrive().Equals(currentLocationDrive, StringComparison.InvariantCultureIgnoreCase)) { resultPath = new Path(currentLocation.CorrectSlash, currentLocation.WrongSlash, string.Format("{0}:{1}", currentLocationDrive, changeCommand)); } else { resultPath = changeCommand; } } else { applyParts = true; resultPath = currentLocation; } var correctSeparator = Char.Parse(currentLocation.CorrectSlash); var changeParts = changeCommand.ToString().Split(correctSeparator).Where(s => !string.IsNullOrEmpty(s)); foreach (var part in changeParts) { // ignore single dot as it does nothing... if (part == ".") { continue; } // ignore trying to go up a dir from the root dir if (part == ".." && resultPath.IsRootPath()) { continue; } if (part == "..") { resultPath = resultPath.GetParentPath(currentLocation.GetDrive()); } else if (applyParts) { resultPath = resultPath.Combine(part); } } return resultPath.ApplyDriveSlash(); }
public static Path CalculateFullPath(Path currentLocation, Path changeCommand) { changeCommand = (changeCommand ?? string.Empty).NormalizeSlashes(); currentLocation = currentLocation.NormalizeSlashes(); bool applyParts = false; Path resultPath; // use the input 'changeCommand' path if it's // 'rooted' otherwise we go from the currentLocation if (changeCommand.HasDrive()) { // windows case where changeCommand == "/" or "\" but the currentLocation has a "C:" drive string currentLocationDrive = currentLocation.GetDrive(); if (changeCommand.StartsWithSlash() && !changeCommand.GetDrive().Equals(currentLocationDrive, StringComparison.InvariantCultureIgnoreCase)) { resultPath = new Path(currentLocation.CorrectSlash, currentLocation.WrongSlash, string.Format("{0}:{1}", currentLocationDrive, changeCommand)); } else { resultPath = changeCommand; } } else { applyParts = true; resultPath = currentLocation; } var correctSeparator = Char.Parse(currentLocation.CorrectSlash); var changeParts = changeCommand.ToString().Split(correctSeparator).Where(s => !string.IsNullOrEmpty(s)); foreach (var part in changeParts) { // ignore single dot as it does nothing... if (part == ".") { continue; } // ignore trying to go up a dir from the root dir if (part == ".." && resultPath.IsRootPath()) { continue; } if (part == "..") { resultPath = resultPath.GetParentPath(currentLocation.GetDrive()); } else if (applyParts) { resultPath = resultPath.Combine(part); } } return(resultPath.ApplyDriveSlash()); }
public Path Combine(Path child) { var parent = this; if (string.IsNullOrEmpty(parent) && string.IsNullOrEmpty(child)) { return(CorrectSlash); // root } if (string.IsNullOrEmpty(parent) && !string.IsNullOrEmpty(child)) { return(child.NormalizeSlashes()); } parent = parent.NormalizeSlashes(); if (!string.IsNullOrEmpty(parent) && string.IsNullOrEmpty(child)) { if (parent.EndsWithSlash()) { return(parent); } else { return(parent.AppendSlashAtEnd()); } } child = child.NormalizeSlashes(); var builder = new System.Text.StringBuilder(parent); if (!parent.EndsWithSlash()) { builder.Append(CorrectSlash); } // Make sure we do not add two \ if (child.StartsWithSlash()) { builder.Append(child, 1, child.Length - 1); } else { builder.Append(child); } return(new Path(CorrectSlash, WrongSlash, builder.ToString())); }
public Path Combine(Path child) { var parent = this; if (string.IsNullOrEmpty(parent) && string.IsNullOrEmpty(child)) { return child; } if (string.IsNullOrEmpty(parent) && !string.IsNullOrEmpty(child)) { return child.NormalizeSlashes(); } parent = parent.NormalizeSlashes(); if (!string.IsNullOrEmpty(parent) && string.IsNullOrEmpty(child)) { if (parent.EndsWithSlash()) { return parent; } else { return parent.AppendSlashAtEnd(); } } child = child.NormalizeSlashes(); var builder = new System.Text.StringBuilder(parent); if (!parent.EndsWithSlash()) builder.Append(CorrectSlash); // Make sure we do not add two \ if (child.StartsWithSlash()) { builder.Append(child, 1, child.Length - 1); } else { builder.Append(child); } return new Path(CorrectSlash, WrongSlash, builder.ToString()); }
private bool PathIsAbsolute(Path path) { string drive = null; if (path.TryGetDriveName(out drive)) { // TODO: workaround to be removed when we completeley remove the Path class and // use PathIntrinsics instead if (System.IO.Path.DirectorySeparatorChar == '\\' && path.StartsWithSlash()) { return false; } return true; } return false; }