Exemple #1
0
        /// <summary>
        /// Determine whether the other given path (<paramref name="a_other"/>) is equal to this one.
        /// </summary>
        /// <param name="a_other">Other path.</param>
        /// <returns>True iff paths are equal.</returns>
        public bool Equals(PathBuilder a_other)
        {
            if (a_other == null)
            {
                return(false);
            }

            return(_path.Equals(a_other._path, StringComparison.OrdinalIgnoreCase));
        }
Exemple #2
0
        /// <summary>
        /// Created a new path builder with a root identity pattern (<paramref name="a_rootPattern"/>).
        /// </summary>
        /// <param name="a_rootPattern">Root identity pattern.</param>
        /// <param name="a_padRoot">Whether or not to pad the root with a delimiter.</param>
        /// <returns>Created path.</returns>
        public PathBuilder WithRoot(string a_rootPattern, bool a_padRoot = true)
        {
            var path = new PathBuilder(this);

            path._rootPattern = a_rootPattern;
            path._padRoot     = a_padRoot;

            return(path);
        }
Exemple #3
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="a_other">Other path builder.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="a_other"/> is null.</exception>
        protected PathBuilder(PathBuilder a_other)
        {
            #region Argument Validation

            if (a_other == null)
                throw new ArgumentNullException(nameof(a_other));

            #endregion

            _path = a_other._path;
            _delimiter = a_other._delimiter;
            _rootPattern = a_other._rootPattern;
            _padRoot = a_other._padRoot;
        }
Exemple #4
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="a_fileSystem">Test file system.</param>
        /// <param name="a_path">File path.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="a_path"/> is null.</exception>
        public TestFile(TestFileSystem a_fileSystem, string a_path)
        {
            #region Argument Validation

            if (a_path == null)
                throw new ArgumentNullException(nameof(a_path));

            #endregion

            FileSystem = a_fileSystem ?? new TestFileSystem();
            Path = new PathBuilder(a_path);

            Name = PathBuilder.Create(Path).Name();
        }
Exemple #5
0
        /// <summary>
        /// Get the relative path between given paths (<paramref name="a_first"/>, <paramref name="a_second"/>).
        /// </summary>
        /// <param name="a_first">First path.</param>
        /// <param name="a_second">Second path.</param>
        /// <returns>
        /// Relative path.
        /// Empty string if the paths are the same.
        /// Null if there is no relationship.
        /// </returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="a_first"/> is null.</exception>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="a_second"/> is null.</exception>
        public static string Relative(PathBuilder a_first, PathBuilder a_second)
        {
            #region Argument Validation

            if (a_first == null)
            {
                throw new ArgumentNullException(nameof(a_first));
            }

            if (a_second == null)
            {
                throw new ArgumentNullException(nameof(a_second));
            }

            #endregion

            var first  = a_first.ToString();
            var second = a_second.ToString();

            if (a_first._delimiter != a_second._delimiter)
            {
                throw new InvalidOperationException("Delimiter mismatch.");
            }

            if (first == second)
            {
                return("");
            }

            if (first.StartsWith(second))
            {
                return(Relative(a_second, a_first));
            }

            if (!second.StartsWith(first, StringComparison.OrdinalIgnoreCase))
            {
                return(null);
            }

            var baseLength = first.Length;
            var relative   = second.Substring(baseLength);

            while (relative.StartsWith(a_first._delimiter))
            {
                relative = relative.Substring(a_second._delimiter.Length);
            }

            return(relative);
        }
Exemple #6
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="a_other">Other path builder.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="a_other"/> is null.</exception>
        protected PathBuilder(PathBuilder a_other)
        {
            #region Argument Validation

            if (a_other == null)
            {
                throw new ArgumentNullException(nameof(a_other));
            }

            #endregion

            _path        = a_other._path;
            _delimiter   = a_other._delimiter;
            _rootPattern = a_other._rootPattern;
            _padRoot     = a_other._padRoot;
        }
        /// <summary>
        /// Get a child file under the given directory (<paramref name="a_directory"/>) with the given name (<paramref name="a_fileName"/>).
        /// </summary>
        /// <param name="a_directory">"This" directory.</param>
        /// <param name="a_fileName">File name.</param>
        /// <returns>File path to the child file.</returns>
        /// <exception cref="NullReferenceException">Thrown if <paramref name="a_directory"/> is null.</exception>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="a_fileName"/> is null.</exception>
        public static string FilePath(this DirectoryInfo a_directory, string a_fileName)
        {
            #region Argument Validation

            if (a_directory == null)
            {
                throw new NullReferenceException(nameof(a_directory));
            }

            if (a_fileName == null)
            {
                throw new ArgumentNullException(nameof(a_fileName));
            }

            #endregion

            return(PathBuilder.Create(a_directory.FullName).Child(a_fileName));
        }
Exemple #8
0
        /// <summary>
        /// Create a file that is this file but with the given extension (<paramref name="a_extension"/>).
        /// </summary>
        /// <param name="a_extension">New extension.</param>
        /// <returns>Created file with the new extension.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="a_extension"/> is null.</exception>
        public IFile ChangeExtension(string a_extension)
        {
            #region Argument Validation

            if (a_extension == null)
            {
                throw new ArgumentNullException(nameof(a_extension));
            }

            #endregion

            var extension = a_extension.TrimStart('.');

            var path    = TargetFile.FullName;
            var newName = PathBuilder.Create(path).NameWithoutExtension() + "." + extension;
            var newPath = PathBuilder.Create(path).Parent().Child(newName);

            return(new FsFile(newPath));
        }
Exemple #9
0
        /// <summary>
        /// Add a relative path to this path to create a child / descendent path.
        /// </summary>
        /// <param name="a_relativePath">Relative path.</param>
        /// <returns>Created child path.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="a_relativePath"/> is null.</exception>
        public PathBuilder Child(string a_relativePath)
        {
            #region Argument Validation

            if (a_relativePath == null)
            {
                throw new ArgumentNullException(nameof(a_relativePath));
            }

            #endregion

            if (a_relativePath == "")
            {
                return(this);
            }

            if (a_relativePath == _delimiter)
            {
                return(this);
            }

            var pathValue = "";

            if (_path == "")
            {
                pathValue = a_relativePath;
            }
            else if (_path.EndsWith(_delimiter))
            {
                pathValue = _path + a_relativePath;
            }
            else
            {
                pathValue = _path + _delimiter + a_relativePath;
            }

            pathValue = pathValue.Replace(_delimiter + _delimiter, _delimiter);

            var path = new PathBuilder(pathValue, _delimiter);

            return(ApplyOptions(path));
        }
Exemple #10
0
        /// <summary>
        /// Apply options in this path builder to the given one (<paramref name="a_path"/>).
        /// </summary>
        /// <param name="a_path">Path builder to which to apply options.</param>
        /// <returns>Given path builder.</returns>
        private PathBuilder ApplyOptions(PathBuilder a_path)
        {
            a_path._rootPattern = _rootPattern;
            a_path._padRoot = _padRoot;

            return a_path;
        }
Exemple #11
0
        /// <summary>
        /// Created a new path builder with a root identity pattern (<paramref name="a_rootPattern"/>).
        /// </summary>
        /// <param name="a_rootPattern">Root identity pattern.</param>
        /// <param name="a_padRoot">Whether or not to pad the root with a delimiter.</param>
        /// <returns>Created path.</returns>
        public PathBuilder WithRoot(string a_rootPattern, bool a_padRoot = true)
        {
            var path = new PathBuilder(this);
            path._rootPattern = a_rootPattern;
            path._padRoot = a_padRoot;

            return path;
        }
Exemple #12
0
        /// <summary>
        /// Get the parent element's path.
        /// </summary>
        /// <returns>Parent path.</returns>
        public PathBuilder Parent()
        {
            if (IsRoot(_path))
                return null;

            var lastIndex = _path.LastIndexOf(_delimiter, StringComparison.OrdinalIgnoreCase);

            if (lastIndex >= _path.Length - 1)
                lastIndex = _path.LastIndexOf(_delimiter, lastIndex - 1, StringComparison.OrdinalIgnoreCase);

            if (lastIndex < 0)
                return ApplyOptions(Create(""));

            var value = _path.Substring(0, lastIndex);

            if (IsRoot(value) && _padRoot)
                value += _delimiter;

            var path =  new PathBuilder(value, _delimiter);

            return ApplyOptions(path);
        }
Exemple #13
0
        /// <summary>
        /// Determine whether the other given path (<paramref name="a_other"/>) is equal to this one.
        /// </summary>
        /// <param name="a_other">Other path.</param>
        /// <returns>True iff paths are equal.</returns>
        public bool Equals(PathBuilder a_other)
        {
            if (a_other == null)
                return false;

            return _path.Equals(a_other._path, StringComparison.OrdinalIgnoreCase);
        }
Exemple #14
0
        /// <summary>
        /// Add a relative path to this path to create a child / descendent path.
        /// </summary>
        /// <param name="a_relativePath">Relative path.</param>
        /// <returns>Created child path.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="a_relativePath"/> is null.</exception>
        public PathBuilder Child(string a_relativePath)
        {
            #region Argument Validation

            if (a_relativePath == null)
                throw new ArgumentNullException(nameof(a_relativePath));

            #endregion

            if (a_relativePath == "")
                return this;

            if (a_relativePath == _delimiter)
                return this;

            var pathValue = "";

            if (_path == "")
                pathValue = a_relativePath;
            else if (_path.EndsWith(_delimiter))
                pathValue = _path + a_relativePath;
            else
                pathValue = _path + _delimiter + a_relativePath;

            pathValue = pathValue.Replace(_delimiter + _delimiter, _delimiter);

            var path = new PathBuilder(pathValue, _delimiter);

            return ApplyOptions(path);
        }
Exemple #15
0
        /// <summary>
        /// Get the relative path between given paths (<paramref name="a_first"/>, <paramref name="a_second"/>).
        /// </summary>
        /// <param name="a_first">First path.</param>
        /// <param name="a_second">Second path.</param>
        /// <returns>
        /// Relative path.
        /// Empty string if the paths are the same.
        /// Null if there is no relationship.
        /// </returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="a_first"/> is null.</exception>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="a_second"/> is null.</exception>
        public static string Relative(PathBuilder a_first, PathBuilder a_second)
        {
            #region Argument Validation

            if (a_first == null)
                throw new ArgumentNullException(nameof(a_first));

            if (a_second == null)
                throw new ArgumentNullException(nameof(a_second));

            #endregion

            var first = a_first.ToString();
            var second = a_second.ToString();

            if (a_first._delimiter != a_second._delimiter)
                throw new InvalidOperationException("Delimiter mismatch.");

            if (first == second)
                return "";

            if (first.StartsWith(second))
                return Relative(a_second, a_first);

            if (!second.StartsWith(first, StringComparison.OrdinalIgnoreCase))
                return null;

            var baseLength = first.Length;
            var relative = second.Substring(baseLength);

            while (relative.StartsWith(a_first._delimiter))
                relative = relative.Substring(a_second._delimiter.Length);

            return relative;
        }
Exemple #16
0
 public void ConstructWithNullDelimiter()
 {
     // Execute
     var result = new PathBuilder(a_path: @"\some\odd\path", a_delimiter: null);
 }
Exemple #17
0
        public void ConstructWithEndingDelimiter()
        {
            // Execute
            var result = new PathBuilder(@":some:odd:path:", ":");

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(@":some:odd:path:", result.ToString());
        }
Exemple #18
0
 public void ConstructWithNullPath()
 {
     // Execute
     var result = new PathBuilder(a_path: null, a_delimiter:":");
 }
Exemple #19
0
 /// <summary>
 /// Get the file path for a file with the given name (<paramref name="a_name"/>) or relative path under this directory.
 /// </summary>
 /// <param name="a_name">File name or relative path.</param>
 /// <returns>File path.</returns>
 public PathBuilder FilePath(string a_name)
 {
     return(PathBuilder.Create(TargetDirectory.FilePath(a_name)));
 }
Exemple #20
0
        public void Construct()
        {
            // Execute
            var result = new PathBuilder(@"\some\odd\path");

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(@"\some\odd\path", result.ToString());
        }