Пример #1
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));
            }
        }
Пример #2
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);
            }
        }