コード例 #1
0
ファイル: UPathExtensions.cs プロジェクト: marinusmaurice/zio
        /// <summary>
        /// Converts the specified path to a relative path (by removing the leading `/`). If the path is already relative, returns the input.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>A relative path.</returns>
        /// <exception cref="ArgumentNullException">if path is <see cref="UPath.IsNull"/></exception>
        public static UPath ToRelative(this UPath path)
        {
            path.AssertNotNull();

            if (path.IsRelative)
            {
                return(path);
            }

            return(path.FullName is "/" ? UPath.Empty : new UPath(path.FullName.Substring(1), true));
        }
コード例 #2
0
ファイル: UPathExtensions.cs プロジェクト: vnisor/zio
        /// <summary>
        /// Converts the specified path to an absolute path (by adding a leading `/`). If the path is already absolute, returns the input.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>An absolute path.</returns>
        /// <exception cref="ArgumentNullException">if path is <see cref="UPath.IsNull"/></exception>
        public static UPath ToAbsolute(this UPath path)
        {
            path.AssertNotNull();

            if (path.IsAbsolute)
            {
                return(path);
            }

            return(path.IsEmpty ? UPath.Root : UPath.Root / path);
        }
コード例 #3
0
ファイル: FileChangedEventArgs.cs プロジェクト: vnisor/zio
        public FileChangedEventArgs(IFileSystem fileSystem, WatcherChangeTypes changeType, UPath fullPath)
        {
            if (fileSystem == null)
            {
                throw new ArgumentNullException(nameof(fileSystem));
            }
            fullPath.AssertNotNull(nameof(fullPath));
            fullPath.AssertAbsolute(nameof(fullPath));

            FileSystem = fileSystem;
            ChangeType = changeType;
            FullPath   = fullPath;
            Name       = fullPath.GetName();
        }
コード例 #4
0
ファイル: UPathExtensions.cs プロジェクト: tstavrianos/zio
        /// <summary>
        /// Checks if the path is in the given directory. Does not check if the paths exist.
        /// </summary>
        /// <param name="path">The path to check.</param>
        /// <param name="directory">The directory to check the path against.</param>
        /// <param name="recursive">True to check if it is anywhere in the directory, false to check if it is directly in the directory.</param>
        /// <returns>True when the path is in the given directory.</returns>
        public static bool IsInDirectory(this UPath path, UPath directory, bool recursive)
        {
            path.AssertNotNull();
            directory.AssertNotNull(nameof(directory));

            if (path.IsAbsolute != directory.IsAbsolute)
            {
                throw new ArgumentException("Cannot mix absolute and relative paths", nameof(directory));
            }

            var target = path.FullName;
            var dir    = directory.FullName;

            if (target.Length < dir.Length || !target.StartsWith(dir))
            {
                return(false);
            }

            if (target.Length == dir.Length)
            {
                // exact match due to the StartsWith above
                // the directory parameter is interpreted as a directory so trailing separator isn't important
                return(true);
            }

            var dirHasTrailingSeparator = dir[dir.Length - 1] == UPath.DirectorySeparator;

            if (!recursive)
            {
                // need to check if the directory part terminates
                var lastSeparatorInTarget = target.LastIndexOf(UPath.DirectorySeparator);
                var expectedLastSeparator = dir.Length - (dirHasTrailingSeparator ? 1 : 0);

                if (lastSeparatorInTarget != expectedLastSeparator)
                {
                    return(false);
                }
            }

            if (!dirHasTrailingSeparator)
            {
                // directory is missing ending slash, check that target has it
                return(target.Length > dir.Length && target[dir.Length] == UPath.DirectorySeparator);
            }

            return(true);
        }
コード例 #5
0
ファイル: UPathExtensions.cs プロジェクト: tstavrianos/zio
        /// <summary>
        /// Gets the directory of the specified path.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>The directory of the path.</returns>
        /// <exception cref="ArgumentNullException">if path is <see cref="UPath.IsNull"/></exception>
        public static UPath GetDirectory(this UPath path)
        {
            path.AssertNotNull();

            var fullname = path.FullName;

            if (fullname == "/")
            {
                return(new UPath());
            }

            var lastIndex = fullname.LastIndexOf(UPath.DirectorySeparator);

            if (lastIndex > 0)
            {
                return(fullname.Substring(0, lastIndex));
            }
            return(lastIndex == 0 ? UPath.Root : UPath.Empty);
        }
コード例 #6
0
ファイル: UPathExtensions.cs プロジェクト: tstavrianos/zio
        /// <summary>
        /// Gets the first directory of the specified path and return the remaining path (/a/b/c, first directory: /a, remaining: b/c)
        /// </summary>
        /// <param name="path">The path to extract the first directory and remaining.</param>
        /// <param name="remainingPath">The remaining relative path after the first directory</param>
        /// <returns>The first directory of the path.</returns>
        /// <exception cref="ArgumentNullException">if path is <see cref="UPath.IsNull"/></exception>
        public static string GetFirstDirectory(this UPath path, out UPath remainingPath)
        {
            path.AssertNotNull();
            remainingPath = UPath.Empty;

            string firstDirectory;
            var    fullname = path.FullName;
            var    index    = fullname.IndexOf(UPath.DirectorySeparator, 1);

            if (index < 0)
            {
                firstDirectory = fullname.Substring(1, fullname.Length - 1);
            }
            else
            {
                firstDirectory = fullname.Substring(1, index - 1);
                if (index + 1 < fullname.Length)
                {
                    remainingPath = fullname.Substring(index + 1);
                }
            }
            return(firstDirectory);
        }