< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 58
Coverable lines: 58
Total lines: 273
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 28
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

C:\projects\jeremyansel-io-locator\JeremyAnsel.IO.Locator\JeremyAnsel.IO.Locator\SystemFileLocator.cs

#LineLine coverage
 1// <copyright file="SystemFileLocator.cs" company="Jérémy Ansel">
 2// Copyright (c) 2015, 2019 Jérémy Ansel
 3// </copyright>
 4// <license>
 5// Licensed under the MIT license. See LICENSE.txt
 6// </license>
 7
 8namespace JeremyAnsel.IO.Locator
 9{
 10    using System;
 11    using System.Collections.Generic;
 12    using System.IO;
 13
 14    /// <summary>
 15    /// A file system file locator.
 16    /// </summary>
 17    internal sealed class SystemFileLocator : IFileLocator
 18    {
 19        /// <summary>
 20        /// A file locator.
 21        /// </summary>
 22        private IFileLocator locator;
 23
 24        /// <summary>
 25        /// The root path.
 26        /// </summary>
 27        private string rootPath;
 28
 29        /// <summary>
 30        /// Initializes a new instance of the <see cref="SystemFileLocator"/> class.
 31        /// </summary>
 32        /// <param name="root">The root path.</param>
 033        public SystemFileLocator(string root)
 34        {
 035            if (!Directory.Exists(root))
 36            {
 037                throw new DirectoryNotFoundException();
 38            }
 39
 040            this.rootPath = root;
 041        }
 42
 43        /// <summary>
 44        /// Initializes a new instance of the <see cref="SystemFileLocator"/> class.
 45        /// </summary>
 46        /// <param name="locator">A file locator.</param>
 47        /// <param name="root">The root path.</param>
 048        public SystemFileLocator(IFileLocator locator, string root)
 49        {
 050            if (locator == null && !Directory.Exists(root))
 51            {
 052                throw new DirectoryNotFoundException();
 53            }
 54
 055            this.locator = locator;
 056            this.rootPath = root;
 057        }
 58
 59        /// <summary>
 60        /// Immediately releases the unmanaged resources used by the <see cref="ArchiveFileLocator"/> object.
 61        /// </summary>
 62        public void Dispose()
 63        {
 064            GC.SuppressFinalize(this);
 065        }
 66
 67        /// <summary>
 68        /// Indicates whether the specified path exists.
 69        /// </summary>
 70        /// <param name="path">A path.</param>
 71        /// <returns>A boolean.</returns>
 72        public bool Exists(string path)
 73        {
 074            string fullPath = Path.Combine(this.rootPath, path);
 75
 076            if (this.locator != null)
 77            {
 078                return this.locator.Exists(fullPath);
 79            }
 80
 081            return File.Exists(fullPath);
 82        }
 83
 84        /// <summary>
 85        /// Open a file.
 86        /// </summary>
 87        /// <param name="path">A path.</param>
 88        /// <returns>A stream.</returns>
 89        public Stream Open(string path)
 90        {
 091            string fullPath = Path.Combine(this.rootPath, path);
 92
 093            if (this.locator != null)
 94            {
 095                return this.locator.Open(fullPath);
 96            }
 97
 098            return File.OpenRead(fullPath);
 99        }
 100
 101        /// <summary>
 102        /// Enumerate the files.
 103        /// </summary>
 104        /// <returns>An enumeration.</returns>
 105        public IEnumerable<string> EnumerateFiles()
 106        {
 0107            return this.EnumerateFiles(string.Empty);
 108        }
 109
 110        /// <summary>
 111        /// Enumerate the files.
 112        /// </summary>
 113        /// <param name="root">The root path.</param>
 114        /// <returns>An enumeration.</returns>
 115        public IEnumerable<string> EnumerateFiles(string root)
 116        {
 0117            string fullPath = Utilities.PathNormalize(Path.Combine(this.rootPath, root));
 118
 119            IEnumerable<string> results;
 120
 0121            if (this.locator != null)
 122            {
 0123                results = this.locator.EnumerateFiles(fullPath);
 124            }
 125            else
 126            {
 0127                results = Directory.EnumerateFiles(fullPath, "*.*", SearchOption.AllDirectories);
 128            }
 129
 0130            foreach (var file in results)
 131            {
 0132                yield return Utilities.PathNormalize(Utilities.PathNormalize(file).Substring(fullPath.Length));
 133            }
 0134        }
 135    }
 136}

https://raw.githubusercontent.com/JeremyAnsel/JeremyAnsel.IO.Locator/16c8f4344390c6376cd2208cc1ae63a39862df37/JeremyAnsel.IO.Locator/JeremyAnsel.IO.Locator/SystemFileLocator.cs

#LineLine coverage
 1// <copyright file="SystemFileLocator.cs" company="Jérémy Ansel">
 2// Copyright (c) 2015, 2019 Jérémy Ansel
 3// </copyright>
 4// <license>
 5// Licensed under the MIT license. See LICENSE.txt
 6// </license>
 7
 8namespace JeremyAnsel.IO.Locator
 9{
 10    using System;
 11    using System.Collections.Generic;
 12    using System.IO;
 13
 14    /// <summary>
 15    /// A file system file locator.
 16    /// </summary>
 17    internal sealed class SystemFileLocator : IFileLocator
 18    {
 19        /// <summary>
 20        /// A file locator.
 21        /// </summary>
 22        private IFileLocator locator;
 23
 24        /// <summary>
 25        /// The root path.
 26        /// </summary>
 27        private string rootPath;
 28
 29        /// <summary>
 30        /// Initializes a new instance of the <see cref="SystemFileLocator"/> class.
 31        /// </summary>
 32        /// <param name="root">The root path.</param>
 033        public SystemFileLocator(string root)
 34        {
 035            if (!Directory.Exists(root))
 36            {
 037                throw new DirectoryNotFoundException();
 38            }
 39
 040            this.rootPath = root;
 041        }
 42
 43        /// <summary>
 44        /// Initializes a new instance of the <see cref="SystemFileLocator"/> class.
 45        /// </summary>
 46        /// <param name="locator">A file locator.</param>
 47        /// <param name="root">The root path.</param>
 048        public SystemFileLocator(IFileLocator locator, string root)
 49        {
 050            if (locator == null && !Directory.Exists(root))
 51            {
 052                throw new DirectoryNotFoundException();
 53            }
 54
 055            this.locator = locator;
 056            this.rootPath = root;
 057        }
 58
 59        /// <summary>
 60        /// Immediately releases the unmanaged resources used by the <see cref="ArchiveFileLocator"/> object.
 61        /// </summary>
 62        public void Dispose()
 63        {
 064            GC.SuppressFinalize(this);
 065        }
 66
 67        /// <summary>
 68        /// Indicates whether the specified path exists.
 69        /// </summary>
 70        /// <param name="path">A path.</param>
 71        /// <returns>A boolean.</returns>
 72        public bool Exists(string path)
 73        {
 074            string fullPath = Path.Combine(this.rootPath, path);
 75
 076            if (this.locator != null)
 77            {
 078                return this.locator.Exists(fullPath);
 79            }
 80
 081            return File.Exists(fullPath);
 82        }
 83
 84        /// <summary>
 85        /// Open a file.
 86        /// </summary>
 87        /// <param name="path">A path.</param>
 88        /// <returns>A stream.</returns>
 89        public Stream Open(string path)
 90        {
 091            string fullPath = Path.Combine(this.rootPath, path);
 92
 093            if (this.locator != null)
 94            {
 095                return this.locator.Open(fullPath);
 96            }
 97
 098            return File.OpenRead(fullPath);
 99        }
 100
 101        /// <summary>
 102        /// Enumerate the files.
 103        /// </summary>
 104        /// <returns>An enumeration.</returns>
 105        public IEnumerable<string> EnumerateFiles()
 106        {
 0107            return this.EnumerateFiles(string.Empty);
 108        }
 109
 110        /// <summary>
 111        /// Enumerate the files.
 112        /// </summary>
 113        /// <param name="root">The root path.</param>
 114        /// <returns>An enumeration.</returns>
 115        public IEnumerable<string> EnumerateFiles(string root)
 116        {
 0117            string fullPath = Utilities.PathNormalize(Path.Combine(this.rootPath, root));
 118
 119            IEnumerable<string> results;
 120
 0121            if (this.locator != null)
 122            {
 0123                results = this.locator.EnumerateFiles(fullPath);
 124            }
 125            else
 126            {
 0127                results = Directory.EnumerateFiles(fullPath, "*.*", SearchOption.AllDirectories);
 128            }
 129
 0130            foreach (var file in results)
 131            {
 0132                yield return Utilities.PathNormalize(Utilities.PathNormalize(file).Substring(fullPath.Length));
 133            }
 0134        }
 135    }
 136}
 137