public static void GetListOfUniqueDirsAndUniqueFileNames(
            List <FilePathAbsolute> listOfFilePath,
            out List <DirectoryPathAbsolute> listOfUniqueDirs,
            out List <string> listOfUniqueFileNames)
        {
            listOfUniqueDirs      = new List <DirectoryPathAbsolute>();
            listOfUniqueFileNames = new List <string>();

            if (listOfFilePath == null)
            {
                return;
            }

            foreach (FilePathAbsolute filePath in listOfFilePath)
            {
                if (PathHelper.IsNullOrEmpty(filePath))
                {
                    continue;
                }

                DirectoryPathAbsolute dir = filePath.ParentDirectoryPath;
                if (!ListOfPathHelper.Contains(listOfUniqueDirs, dir))
                {
                    listOfUniqueDirs.Add(dir);
                }

                string fileName = filePath.FileName;
                Debug.Assert(fileName != null && fileName.Length > 0);

                if (!ListOfStringHelperContainsIgnoreCase(listOfUniqueFileNames, fileName))
                {
                    listOfUniqueFileNames.Add(fileName);
                }
            }
        }
Exemple #2
0
 public bool CanGetAbsolutePathFrom(DirectoryPathAbsolute path)
 {
     try
     {
         this.GetAbsolutePathFrom(path);
         return(true);
     }
     catch { }
     return(false);
 }
Exemple #3
0
        protected static string GetAbsolutePathFrom(DirectoryPathAbsolute pathFrom, BasePath pathTo)
        {
            Debug.Assert(pathTo.IsRelativePath);

            if (pathTo.IsFilePath)
            {
                pathTo = pathTo.ParentDirectoryPath;
            }

            return(InternalStringHelper.GetAbsolutePath(pathFrom.Path, pathTo.Path));
        }
Exemple #4
0
        //----------------------------------------------
        //
        //   Try Rebase path
        //
        //----------------------------------------------
        // originalPath "A:\X1\X2\X3"  validPath "B:\Y1\X1"  result "B:\Y1\X1\X2\X3"   deeperCommonDirName ="X1"
        // originalPath "A:\X1\X2\X3"  validPath "B:\Y1\Y2"  result null               deeperCommonDirName =null
        // originalPath "A:\X1\X2\X3"  validPath "B:\X1\X2"  result "B:\X1\X2\X3"      deeperCommonDirName ="X2
        // originalPath "A:\X1\X2\X3"  validPath "B:\X2\X3"  result "B:\X2\X3"         deeperCommonDirName ="X3"
        // originalPath "A:\X1\X2\X3"  validPath "B:\X2"     result "B:\X2\X3"         deeperCommonDirName ="X2"
        // originalPath "A:\X1\X2\X3"  validPath "B:\X3\X2"  result "B:\X3\X2\X3"      deeperCommonDirName ="X2"
        // originalPath "A:\X1\X2\X3"  validPath "B:\X3\Y1"  result "B:\X3"            deeperCommonDirName ="X3"
        // originalPath "A:\X1\X2\X3"  validPath "A:\"       result null               deeperCommonDirName =null
        // originalPath "A:\X1\X2\X3"  validPath "A:\Y1"     result null               deeperCommonDirName =null
        // Algo:
        // 1) Find all common dir name between originalPath and validPath
        // 2) If no common dir name then return null, can't guess the path
        // 3) Get the deeper common dir name (deeper in validPath)
        // 4) Return Path(deeperCommonDirName)+ Pathes in originalPath after deeperCommonDirName
        public static bool TryRebasePath(
            DirectoryPathAbsolute originalPath,
            DirectoryPathAbsolute validPath,
            out DirectoryPathAbsolute rebasedPath)
        {
            rebasedPath = DirectoryPathAbsolute.Empty;
            if (PathHelper.IsNullOrEmpty(originalPath))
            {
                return(false);
            }
            if (PathHelper.IsNullOrEmpty(validPath))
            {
                return(false);
            }
            List <string> originalPathDirs = new List <string>(originalPath.Path.Split(Path.DirectorySeparatorChar));
            List <string> validPathDirs    = new List <string>(validPath.Path.Split(Path.DirectorySeparatorChar));

            int indexInValidPathOfDeeperCommonDirName    = -1;
            int indexInOriginalPathOfDeeperCommonDirName = -1;

            // We begin at 1 both loop to avoid Driver in pathes
            for (int validPathIndex = 1; validPathIndex < validPathDirs.Count; validPathIndex++)
            {
                for (int originalPathIndex = 1; originalPathIndex < originalPathDirs.Count; originalPathIndex++)
                {
                    if (validPathDirs[validPathIndex].Length > 0 && // Avoid comparison of empty string that can happen if DirectorySeparatorChar at the end of pathes
                        string.Compare(validPathDirs[validPathIndex], originalPathDirs[originalPathIndex], true) == 0)
                    {
                        indexInValidPathOfDeeperCommonDirName    = validPathIndex;
                        indexInOriginalPathOfDeeperCommonDirName = originalPathIndex;
                        break;
                    }
                }
            }
            if (indexInValidPathOfDeeperCommonDirName == -1)
            {
                // No common dir name, return null
                return(false);
            }

            // Concate Path(deeperCommonDirName)+ Pathes in originalPath after deeperCommonDirName
            Debug.Assert(indexInOriginalPathOfDeeperCommonDirName >= 0);
            List <string> inferedDirNames = validPathDirs.GetRange(0, indexInValidPathOfDeeperCommonDirName);

            inferedDirNames.AddRange(
                originalPathDirs.GetRange(indexInOriginalPathOfDeeperCommonDirName, originalPathDirs.Count - indexInOriginalPathOfDeeperCommonDirName));

            string[] arrayInferedDirNames = new string[inferedDirNames.Count];
            inferedDirNames.CopyTo(arrayInferedDirNames);
            string inferedPath = string.Join(Path.DirectorySeparatorChar.ToString(), arrayInferedDirNames);

            rebasedPath = new DirectoryPathAbsolute(inferedPath);
            return(true);
        }
 //
 //  Absolute/Relative path conversion
 //
 public DirectoryPathRelative GetPathRelativeFrom(DirectoryPathAbsolute path)
 {
     if (path == null)
     {
         throw new ArgumentNullException();
     }
     if (PathHelper.IsEmpty(this) || PathHelper.IsEmpty(path))
     {
         throw new ArgumentException("Cannot compute a relative path from an empty path.");
     }
     return(new DirectoryPathRelative(BasePath.GetPathRelative(path, this)));
 }
Exemple #6
0
        //
        //  Absolute/Relative path conversion
        //
        public FilePathAbsolute GetAbsolutePathFrom(DirectoryPathAbsolute path)
        {
            if (path == null)
            {
                throw new ArgumentNullException();
            }
            if (PathHelper.IsEmpty(this) || PathHelper.IsEmpty(path))
            {
                throw new ArgumentException("Cannot compute an absolute path from an empty path.");
            }
            string pathAbsolute = BasePath.GetAbsolutePathFrom(path, this);

            return(new FilePathAbsolute(pathAbsolute + System.IO.Path.DirectorySeparatorChar + this.FileName));
        }
        public bool IsChildDirectoryOf(DirectoryPathAbsolute parentDir)
        {
            if (parentDir == null)
            {
                throw new ArgumentNullException("parentDir");
            }
            if (parentDir.IsEmpty)
            {
                throw new ArgumentException("Empty parentDir not accepted", "parentDir");
            }
            string parentPathUpperCase = parentDir.Path.ToUpper();
            string thisPathUpperCase   = this.Path.ToUpper();

            return(thisPathUpperCase.Contains(parentPathUpperCase));
        }
Exemple #8
0
        protected static string GetPathRelative(DirectoryPathAbsolute pathFrom, BasePath pathTo)
        {
            Debug.Assert(pathTo.IsAbsolutePath);

            if (string.Compare(pathFrom.DriveProtected, pathTo.DriveProtected, true) != 0)
            {
                throw new ArgumentException(@"Cannot compute relative path from 2 paths that are not on the same drive 
PathFrom = """ + pathFrom.Path + @"""
PathTo   = """ + pathTo.Path + @"""");
            }

            if (pathTo.IsFilePath)
            {
                pathTo = pathTo.ParentDirectoryPath;
            }

            return(InternalStringHelper.GetPathRelativeTo(pathFrom.Path, pathTo.Path));
        }
        public static bool TryGetCommonRootDirectory(List <DirectoryPathAbsolute> listOfPaths, out DirectoryPathAbsolute commonRootDirectory)
        {
            commonRootDirectory = DirectoryPathAbsolute.Empty;
            if (listOfPaths == null)
            {
                return(false);
            }
            if (listOfPaths.Count == 0)
            {
                return(false);
            }

            // If the list contains a path null or empty -> no commonRootDirectory
            foreach (DirectoryPathAbsolute path in listOfPaths)
            {
                if (PathHelper.IsNullOrEmpty(path))
                {
                    return(false);
                }
            }

            if (listOfPaths.Count == 1)
            {
                commonRootDirectory = listOfPaths[0];
                return(true);
            }


            //
            //  Case where all paths are identical
            //
            bool allPathsAreIdentical = true;

            for (int i = 1; i < listOfPaths.Count; i++)
            {
                if (!listOfPaths[0].Equals(listOfPaths[i]))
                {
                    allPathsAreIdentical = false;
                    break;
                }
            }
            if (allPathsAreIdentical)
            {
                commonRootDirectory = listOfPaths[0];
                return(true);
            }

            //
            //  Case where some paths are not on the same drive
            //
            for (int i = 1; i < listOfPaths.Count; i++)
            {
                if (string.Compare(listOfPaths[0].Drive, listOfPaths[i].Drive, true) != 0)
                {
                    return(false);
                }
            }

            //
            //  Build listOfSplittedPaths
            //
            List <string[]> listOfSplittedPaths = new List <string[]>();
            int             maxDeepForRootDir   = int.MaxValue;

            foreach (DirectoryPathAbsolute path in listOfPaths)
            {
                string[] pathSplitted = path.Path.Split(new char[] { Path.DirectorySeparatorChar });
                if (pathSplitted.Length < maxDeepForRootDir)
                {
                    maxDeepForRootDir = pathSplitted.Length;
                }
                listOfSplittedPaths.Add(pathSplitted);
            }


            //
            // Compute commonRootDirPath from listOfSplittedPaths
            //
            string commonRootDirPath = string.Empty;

            for (int i = 0; i < maxDeepForRootDir; i++)
            {
                string current = listOfSplittedPaths[0][i];
                foreach (string[] pathSplitted in listOfSplittedPaths)
                {
                    if (string.Compare(pathSplitted[i], current, true) != 0)
                    {
                        commonRootDirectory = new DirectoryPathAbsolute(commonRootDirPath);
                        return(true);
                    }
                }
                if (i == 0)
                {
                    commonRootDirPath += current;
                }
                else
                {
                    commonRootDirPath += Path.DirectorySeparatorChar + current;
                }
            }
            commonRootDirectory = new DirectoryPathAbsolute(commonRootDirPath);
            return(true);
        }