예제 #1
0
 public static bool IsNestedPath(string basePath, string fullPath)
 {
     return(basePath.Length > 0 &&
            fullPath.Length > basePath.Length &&
            fullPath.StartsWith(basePath, StringComparison.OrdinalIgnoreCase) &&
            (PathUtilities.IsDirectorySeparator(basePath[basePath.Length - 1]) || PathUtilities.IsDirectorySeparator(fullPath[basePath.Length])));
 }
예제 #2
0
        public static string GetNestedPath(string baseDirectory, string fullPath)
        {
            if (IsNestedPath(baseDirectory, fullPath))
            {
                var relativePath = fullPath.Substring(baseDirectory.Length);
                while (relativePath.Length > 0 && PathUtilities.IsDirectorySeparator(relativePath[0]))
                {
                    relativePath = relativePath.Substring(1);
                }

                return(relativePath);
            }

            return(fullPath);
        }
예제 #3
0
        public static string GetRelativePath(string basePath, string path)
        {
            if (IsRelativePath(basePath, path))
            {
                var relativePath = path.Substring(basePath.Length);
                while (relativePath.Length > 0 && PathUtilities.IsDirectorySeparator(relativePath[0]))
                {
                    relativePath = relativePath.Substring(1);
                }

                return(relativePath);
            }

            return(path);
        }
예제 #4
0
        private static string ResolveRelativePath(PathKind kind, string path, string basePath, string baseDirectory)
        {
            switch (kind)
            {
            case PathKind.Empty:
                return(null);

            case PathKind.Relative:
                baseDirectory = GetBaseDirectory(basePath, baseDirectory);
                if (baseDirectory == null)
                {
                    return(null);
                }

                // with no search paths relative paths are relative to the base directory:
                return(PathUtilities.CombinePathsUnchecked(baseDirectory, path));

            case PathKind.RelativeToCurrentDirectory:
                baseDirectory = GetBaseDirectory(basePath, baseDirectory);
                if (baseDirectory == null)
                {
                    return(null);
                }

                if (path.Length == 1)
                {
                    // "."
                    return(baseDirectory);
                }
                else
                {
                    // ".\path"
                    return(PathUtilities.CombinePathsUnchecked(baseDirectory, path));
                }

            case PathKind.RelativeToCurrentParent:
                baseDirectory = GetBaseDirectory(basePath, baseDirectory);
                if (baseDirectory == null)
                {
                    return(null);
                }

                // ".."
                return(PathUtilities.CombinePathsUnchecked(baseDirectory, path));

            case PathKind.RelativeToCurrentRoot:
                string baseRoot;
                if (basePath != null)
                {
                    baseRoot = PathUtilities.GetPathRoot(basePath);
                }
                else if (baseDirectory != null)
                {
                    baseRoot = PathUtilities.GetPathRoot(baseDirectory);
                }
                else
                {
                    return(null);
                }

                if (baseRoot == null)
                {
                    return(null);
                }

                Debug.Assert(PathUtilities.IsDirectorySeparator(path[0]));
                Debug.Assert(path.Length == 1 || !PathUtilities.IsDirectorySeparator(path[1]));
                Debug.Assert(baseRoot.Length >= 3);
                return(PathUtilities.CombinePathsUnchecked(baseRoot, path.Substring(1)));

            case PathKind.RelativeToDriveDirectory:
                // drive relative paths not supported, can't resolve:
                return(null);

            case PathKind.Absolute:
                return(path);

            default:
                // EDMAURER this is not using ExceptionUtilities.UnexpectedValue() because this file
                // is shared via linking with other code that doesn't have the ExceptionUtilities.
                throw new InvalidOperationException(string.Format("Unexpected PathKind {0}.", kind));
            }
        }
예제 #5
0
        private static string ResolveRelativePath(PathKind kind, string path, string basePath, string baseDirectory)
        {
            switch (kind)
            {
            case PathKind.Empty:
                return(null);

            case PathKind.Relative:
                baseDirectory = GetBaseDirectory(basePath, baseDirectory);
                if (baseDirectory == null)
                {
                    return(null);
                }

                // with no search paths relative paths are relative to the base directory:
                return(PathUtilities.CombinePathsUnchecked(baseDirectory, path));

            case PathKind.RelativeToCurrentDirectory:
                baseDirectory = GetBaseDirectory(basePath, baseDirectory);
                if (baseDirectory == null)
                {
                    return(null);
                }

                if (path.Length == 1)
                {
                    // "."
                    return(baseDirectory);
                }
                else
                {
                    // ".\path"
                    return(PathUtilities.CombinePathsUnchecked(baseDirectory, path));
                }

            case PathKind.RelativeToCurrentParent:
                baseDirectory = GetBaseDirectory(basePath, baseDirectory);
                if (baseDirectory == null)
                {
                    return(null);
                }

                // ".."
                return(PathUtilities.CombinePathsUnchecked(baseDirectory, path));

            case PathKind.RelativeToCurrentRoot:
                string baseRoot;
                if (basePath != null)
                {
                    baseRoot = PathUtilities.GetPathRoot(basePath);
                }
                else if (baseDirectory != null)
                {
                    baseRoot = PathUtilities.GetPathRoot(baseDirectory);
                }
                else
                {
                    return(null);
                }

                if (string.IsNullOrEmpty(baseRoot))
                {
                    return(null);
                }

                Debug.Assert(PathUtilities.IsDirectorySeparator(path[0]));
                Debug.Assert(path.Length == 1 || !PathUtilities.IsDirectorySeparator(path[1]));
                return(PathUtilities.CombinePathsUnchecked(baseRoot, path.Substring(1)));

            case PathKind.RelativeToDriveDirectory:
                // drive relative paths not supported, can't resolve:
                return(null);

            case PathKind.Absolute:
                return(path);

            default:
                throw ExceptionUtilities.UnexpectedValue(kind);
            }
        }