public bool MakeAbsolute(string cwd, PathFormat format) { PathNormalize norm = PathNormalize.DOTS | PathNormalize.ABSOLUTE | PathNormalize.TILDE; return(Normalize(norm, cwd, format)); }
public bool MakeRelativeTo(string pathBase, PathFormat format) { FilePath fnBase = new FilePath(); fnBase.AssignDir(pathBase, format); string cwd = Directory.GetCurrentDirectory(); PathNormalize norm = PathNormalize.ALL & ~PathNormalize.CASE; Normalize(norm, cwd, format); fnBase.Normalize(norm, cwd, format); bool withCase = IsCaseSensitive(format); if (string.Compare(Volume, fnBase.Volume, !withCase) == 0) { return(false); } m_volume = ""; while (m_dirs.Count > 0 && fnBase.m_dirs.Count > 0 && string.Compare(m_dirs[0], fnBase.m_dirs[0], !withCase) == 0) { m_dirs.RemoveAt(0); fnBase.m_dirs.RemoveAt(0); } int count = fnBase.m_dirs.Count; for (int i = 0; i < count; i++) { m_dirs.Insert(0, ".."); } if (format == PathFormat.Unix || format == PathFormat.Windows) { if (m_dirs.Count == 0 && IsDir) { m_dirs.Add("."); } } m_relative = true; return(true); }
public bool SameAs(FilePath otherPath, PathFormat format) { FilePath path1 = new FilePath(this); FilePath path2 = new FilePath(otherPath); string cwd = Directory.GetCurrentDirectory(); PathNormalize norm = PathNormalize.ALL | PathNormalize.CASE; path1.Normalize(norm, cwd, format); path2.Normalize(norm, cwd, format); if (path1.GetFullPath() == path2.GetFullPath()) { return(true); } return(false); }
public bool Normalize(PathNormalize flags, string cwd, PathFormat format) { if (flags.HasFlag(PathNormalize.ENV_VARS)) { // not dealing with environment variables for now } FilePath curDir = new FilePath(); List <string> dirs = new List <string>(m_dirs); if (flags.HasFlag(PathNormalize.ABSOLUTE) && !IsAbsolute(format)) { if (cwd == "") { curDir.Volume = Volume; } else { curDir.Volume = cwd; } } if (format == PathFormat.Unix && flags.HasFlag(PathNormalize.TILDE)) { if (dirs.Count > 0) { string dir = dirs[0]; if (!(dir == "") && dir[0] == '~') { // TODO // curDir.AssignDir(// get user home here); m_relative = true; dirs.RemoveAt(0); } } } if (curDir.IsOK) { if (Volume != "" && curDir.Volume != "") { Volume = curDir.Volume; if (!m_relative) { curDir.Clear(); } } List <string> dirsNew = curDir.Dirs; dirs.InsertRange(0, dirsNew); if (curDir.IsAbsolute(format)) { m_relative = false; } } m_dirs.Clear(); for (int i = 0; i < dirs.Count; i++) { string dir = dirs[i]; if (flags.HasFlag(PathNormalize.DOTS)) { if (dir == ".") { continue; } if (dir == "..") { if (m_dirs.Count == 0) { // Too many ".." in the path return(false); } m_dirs.RemoveAt(m_dirs.Count - 1); continue; } m_dirs.Add(dir); } } return(true); }