using System.Collections.Generic; using System.IO; using System.Linq; namespace Chernobyl { /// /// The default implementation of the ISearchPaths interface. Used to hold /// several file/folder paths for so that other systems can search these /// paths for files, folders, etc. This class will automatically convert all /// '\' to '/' for cross platform purposes. /// public class SearchPaths : ISearchPaths { /// /// Constructor. /// public SearchPaths() : this(false) { } /// /// Constructor. /// /// True if the current working /// directory should be added to the search path list. public SearchPaths(bool addWorkingDirectory) { Paths = new List(); if(addWorkingDirectory == true) Add(Directory.GetCurrentDirectory()); } /// /// Constructor. /// /// The paths to add to the internal /// list of search paths. public SearchPaths(params string[] paths) { Paths = new List(); foreach (string str in paths) Add(str); } /// /// Adds a search path to the search path list. /// /// The search path to add to the /// list of search paths. public void Add(string searchPath) { searchPath.Replace('\\', '/'); Paths.Add(searchPath); } /// /// Clears out the collection of search paths. /// public void Clear() { Paths.Clear(); } /// /// Checks if a search path is contained within /// this collection /// /// The search path to check for. /// True if the search path is contained within /// this collection. public bool Contains(string item) { return Paths.Contains(item); } /// /// Copies the array of search paths to this collection /// starting at . /// /// The array to copy over. /// The index to start at public void CopyTo(string[] array, int arrayIndex) { Paths.CopyTo(array, arrayIndex); } /// /// The number of search paths contained within this collection. /// public int Count { get { return Paths.Count; } } /// /// True if this collection is read only, false if otherwise. /// public bool IsReadOnly { get { return false; } } /// /// Removes a search path from this collection. /// /// The search path to remove. /// True if the search path was removed, false /// if otherwise. public bool Remove(string item) { return Paths.Remove(item); } /// /// Returns an enumerator to the paths. /// /// The enumerator to the paths. public IEnumerator GetEnumerator() { return Paths.GetEnumerator(); } /// /// Returns an enumerator to the paths. /// /// The enumerator to the paths. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return Paths.GetEnumerator(); } /// /// Searches the search paths for a file that matches the search pattern /// and returns a FileStream (for reading only) to that file or null if /// no file could be found. Only the first file found is returned. This /// method does not search subdirectories. /// /// The pattern to look for when searching /// for the file. See the search pattern parameter on /// for more information on /// this parameter /// Specifies how the file should be opened. /// The FileStream to the found file or null if no file could /// be found. public FileStream Find(string searchPattern, FileMode fileMode) { return Find(searchPattern, SearchOption.TopDirectoryOnly, fileMode); } /// /// Searches the search paths for a file that matches the search pattern /// and returns a FileStream (for reading only) to that file or null if /// no file could be found. /// /// The pattern to look for when searching /// for the file. See the search pattern parameter on /// for more information on /// this parameter /// Specifies how each of the search paths /// should be searched. See the searchOptions parameter on /// for more information. /// Specifies how the file should be opened. /// The FileStream to the found file or null if no file could /// be found. public FileStream Find(string searchPattern, SearchOption searchOptions, FileMode fileMode) { return Find(searchPattern, searchOptions, fileMode, FileAccess.Read); } /// /// Searches the search paths for a file that matches the search pattern /// and returns a FileStream to that file or null if no file could be /// found. /// /// The pattern to look for when searching /// for the file. See the search pattern parameter on /// for more information on /// this parameter /// Specifies how each of the search paths /// should be searched. See the searchOptions parameter on /// for more information. /// Specifies how the file should be opened. /// Specifies what operations you want to do on /// the file. /// The FileStream to the found file or null if no file could /// be found. public FileStream Find(string searchPattern, SearchOption searchOptions, FileMode fileMode, FileAccess fileAccess) { FileStream foundFile = null; List.Enumerator enumer = Paths.GetEnumerator(); while (enumer.MoveNext() && foundFile == null) { string[] files = Directory.GetFiles(enumer.Current, searchPattern, searchOptions); if (files.Length != 0) foundFile = new FileStream(files[0], fileMode, fileAccess); } return foundFile; } /// /// Returns the first path to the file specified. /// /// The name of the file to look for. /// The path to the file specified. public string FindFilePath(string file) { return FilePaths(file).First(); } /// /// Returns an enumerator of file paths that are contained /// in this SearchPath. Only paths to files that actually /// exist are returned. /// /// A string that should be /// appended to the end of the contained file paths before they /// are searched for existence. /// An enumerator of paths to files that exist. public IEnumerable FilePaths(string filepathAppendage) { string fullpath = ""; foreach (string path in Paths) { fullpath = Path.Combine(path, filepathAppendage); if (File.Exists(fullpath) == true) yield return fullpath; } } /// /// Returns an enumerator of file paths that are contained /// in this SearchPath. Only paths to files that actually /// exist are returned. /// /// An enumerator of paths to files that exist. public IEnumerable FilePaths() { return FilePaths(""); } /// /// Returns an enumerator of folder paths that are contained /// in this SearchPath. Only paths to folders that actually /// exist are returned. /// /// A string that should be /// appended to the end of the contained folder paths before they /// are searched for existence. /// An enumerator of paths to folders that exist. public IEnumerable DirectoryPaths(string directorypathAppendage) { string fullpath = ""; foreach (string path in Paths) { fullpath = Path.Combine(path, directorypathAppendage); if (Directory.Exists(fullpath) == true) yield return fullpath; } } /// /// Returns an enumerator of folder paths that are contained /// in this SearchPath. Only paths to folders that actually /// exist are returned. /// /// An enumerator of paths to folders that exist. public IEnumerable DirectoryPaths() { return DirectoryPaths(""); } /// /// Returns an enumeration of FileStreams created /// from search paths in this class. Any FileStreams /// to files which do not exist will not be returned. /// /// A string that should be appended /// to the front of each of the SearchPaths before they are checked for /// existence or created. /// The FileMode to open the files with. /// An enumeration of FileStreams which point to the files /// pointed to by the search paths. public IEnumerable Files(FileMode fileMode, string filepathAppendage) { string fullPath = ""; foreach (string path in Paths) { fullPath = Path.Combine(path, filepathAppendage); if (File.Exists(fullPath)) yield return new FileStream(fullPath, fileMode); } } /// /// Returns an enumeration of FileStreams created /// from search paths in this class. Any FileStreams /// to files which do not exist will not be returned. /// /// The FileMode to open the files with. /// An enumeration of FileStreams which point to the files /// pointed to by the search paths. public IEnumerable Files(FileMode fileMode) { return Files(fileMode, ""); } /// /// The collection of search paths. /// protected List Paths { get; set; } } }