DirectoryInfo.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #region License information
  2. // ----------------------------------------------------------------------------
  3. //
  4. // libeq2 - A library for analyzing the Everquest II File Format
  5. // Blaz (blaz@blazlabs.com)
  6. //
  7. // This program is free software; you can redistribute it and/or
  8. // modify it under the terms of the GNU General Public License
  9. // as published by the Free Software Foundation; either version 2
  10. // of the License, or (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with this program; if not, write to the Free Software
  19. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  20. //
  21. // ( The full text of the license can be found in the License.txt file )
  22. //
  23. // ----------------------------------------------------------------------------
  24. #endregion
  25. #region Using directives
  26. using System;
  27. using System.Collections.Generic;
  28. #endregion
  29. namespace Everquest2.IO
  30. {
  31. public class DirectoryInfo : FileSystemInfo
  32. {
  33. internal DirectoryInfo(FileSystem fileSystem, string path)
  34. {
  35. #region Preconditions
  36. if (path == null) throw new ArgumentNullException("path");
  37. #endregion
  38. this.fileSystem = fileSystem;
  39. this.path = path.TrimEnd('/');
  40. // If path is empty then this is the root directory
  41. if (path.Length == 0)
  42. {
  43. parent = null;
  44. }
  45. else
  46. {
  47. int parentNameEnd = path.LastIndexOfAny(FileSystem.directorySeparators, path.Length - 1);
  48. string parentName = parentNameEnd == -1 ? string.Empty : path.Substring(0, parentNameEnd);
  49. parent = fileSystem.GetDirectoryInfo(parentName);
  50. }
  51. }
  52. public override bool Exists
  53. {
  54. get { return fileSystem.DirectoryExists(FullName); }
  55. }
  56. public override string FullName
  57. {
  58. get { return path; }
  59. }
  60. public override string Name
  61. {
  62. get
  63. {
  64. string name = FullName;
  65. int nameStart = name.LastIndexOfAny(FileSystem.directorySeparators, name.Length - 1);
  66. return nameStart == -1 ? name : name.Substring(nameStart + 1);
  67. }
  68. }
  69. public DirectoryInfo Parent
  70. {
  71. get { return parent; }
  72. }
  73. public int ChildrenCount
  74. {
  75. get { lock (contents) return contents.Count; }
  76. }
  77. public int DirectoryCount
  78. {
  79. get { lock (directories) return directories.Count; }
  80. }
  81. public int FileCount
  82. {
  83. get { lock (files) return files.Count; }
  84. }
  85. public DirectoryInfo[] GetDirectories()
  86. {
  87. DirectoryInfo[] subdirectories;
  88. lock (directories)
  89. {
  90. subdirectories = new DirectoryInfo[directories.Count];
  91. directories.Values.CopyTo(subdirectories, 0);
  92. }
  93. return subdirectories;
  94. }
  95. public DirectoryInfo[] GetDirectories(string pattern)
  96. {
  97. System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(pattern);
  98. DirectoryInfo[] subdirectories = GetDirectories();
  99. return Array.FindAll<DirectoryInfo>(subdirectories, delegate(DirectoryInfo entry) { return regex.IsMatch(entry.Name); });
  100. }
  101. public DirectoryInfo GetDirectory(string directory)
  102. {
  103. DirectoryInfo result = null;
  104. lock (directories) directories.TryGetValue(directory, out result);
  105. return result;
  106. }
  107. public FileInfo[] GetFiles()
  108. {
  109. FileInfo[] directoryFiles;
  110. lock (files)
  111. {
  112. directoryFiles = new FileInfo[files.Count];
  113. files.Values.CopyTo(directoryFiles, 0);
  114. }
  115. return directoryFiles;
  116. }
  117. public FileInfo[] GetFiles(string pattern)
  118. {
  119. System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(pattern);
  120. FileInfo[] directoryFiles = GetFiles();
  121. return Array.FindAll<FileInfo>(directoryFiles, delegate(FileInfo entry) { return regex.IsMatch(entry.Name); });
  122. }
  123. public FileInfo GetFile(string file)
  124. {
  125. FileInfo result = null;
  126. lock (files) files.TryGetValue(file, out result);
  127. return result;
  128. }
  129. public FileSystemInfo[] GetFileSystemInfos()
  130. {
  131. FileSystemInfo[] directoryContents;
  132. lock (contents)
  133. {
  134. directoryContents = new FileSystemInfo[contents.Count];
  135. contents.CopyTo(directoryContents, 0);
  136. }
  137. return directoryContents;
  138. }
  139. internal void AddChild(FileSystemInfo child)
  140. {
  141. lock (contents) contents.Add(child);
  142. OnChildAdded(child);
  143. if (child is DirectoryInfo)
  144. {
  145. lock (directories) directories.Add(child.Name, child as DirectoryInfo);
  146. OnDirectoryAdded(child as DirectoryInfo);
  147. }
  148. if (child is FileInfo)
  149. {
  150. lock (files) files.Add(child.Name, child as FileInfo);
  151. OnFileAdded(child as FileInfo);
  152. }
  153. }
  154. private void OnChildAdded(FileSystemInfo child)
  155. {
  156. if (ChildAdded != null) ChildAdded(this, new ChildAddedEventArgs(child));
  157. }
  158. private void OnDirectoryAdded(DirectoryInfo directory)
  159. {
  160. if (DirectoryAdded != null) DirectoryAdded(this, new FileSystem.DirectoryAddedEventArgs(directory));
  161. }
  162. private void OnFileAdded(FileInfo file)
  163. {
  164. if (FileAdded != null) FileAdded(this, new FileSystem.FileAddedEventArgs(file));
  165. }
  166. #region Events
  167. public class ChildAddedEventArgs : EventArgs
  168. {
  169. public ChildAddedEventArgs(FileSystemInfo child)
  170. {
  171. this.child = child;
  172. }
  173. public FileSystemInfo child;
  174. }
  175. public event EventHandler<ChildAddedEventArgs> ChildAdded;
  176. public event EventHandler<FileSystem.DirectoryAddedEventArgs> DirectoryAdded;
  177. public event EventHandler<FileSystem.FileAddedEventArgs> FileAdded;
  178. #endregion
  179. #region Fields
  180. FileSystem fileSystem;
  181. DirectoryInfo parent;
  182. string path;
  183. IList<FileSystemInfo> contents = new List<FileSystemInfo>();
  184. Dictionary<string, DirectoryInfo> directories = new Dictionary<string, DirectoryInfo>(StringComparer.CurrentCultureIgnoreCase);
  185. Dictionary<string, FileInfo> files = new Dictionary<string, FileInfo>(StringComparer.CurrentCultureIgnoreCase);
  186. #endregion
  187. }
  188. }
  189. /* EOF */