Mes documents/Visual Studio 2005/Projects/TES4ModTranslator/TES4ModTranslator/Core/FileSystemScanner.cs

Go to the documentation of this file.
00001 // FileSystemScanner.cs
00002 //
00003 // Copyright 2005 John Reilly
00004 //
00005 // This program is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU General Public License
00007 // as published by the Free Software Foundation; either version 2
00008 // of the License, or (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 //
00019 // Linking this library statically or dynamically with other modules is
00020 // making a combined work based on this library.  Thus, the terms and
00021 // conditions of the GNU General Public License cover the whole
00022 // combination.
00023 // 
00024 // As a special exception, the copyright holders of this library give you
00025 // permission to link this library with independent modules to produce an
00026 // executable, regardless of the license terms of these independent
00027 // modules, and to copy and distribute the resulting executable under
00028 // terms of your choice, provided that you also meet, for each linked
00029 // independent module, the terms and conditions of the license of that
00030 // module.  An independent module is a module which is not derived from
00031 // or based on this library.  If you modify this library, you may extend
00032 // this exception to your version of the library, but you are not
00033 // obligated to do so.  If you do not wish to do so, delete this
00034 // exception statement from your version.
00035 
00036 
00037 using System;
00038 using System.IO;
00039 
00040 namespace ICSharpCode.SharpZipLib.Core
00041 {
00045         public class ScanEventArgs : EventArgs
00046         {
00051                 public ScanEventArgs(string name)
00052                 {
00053                         this.name = name;
00054                         ContinueRunning = true;
00055                 }
00056                 
00057                 string name;
00058                 
00062                 public string Name
00063                 {
00064                         get { return name; }
00065                 }
00066                 
00067                 bool continueRunning;
00068                 
00072                 public bool ContinueRunning
00073                 {
00074                         get { return continueRunning; }
00075                         set { continueRunning = value; }
00076                 }
00077         }
00078 
00082         public class DirectoryEventArgs : ScanEventArgs
00083         {
00089                 public DirectoryEventArgs(string name, bool hasMatchingFiles)
00090                         : base (name)
00091                 {
00092                         this.hasMatchingFiles = hasMatchingFiles;
00093                 }
00094                 
00098                 public bool HasMatchingFiles
00099                 {
00100                         get { return hasMatchingFiles; }
00101                 }
00102                 
00103                 bool hasMatchingFiles;
00104         }
00105         
00109         public class ScanFailureEventArgs
00110         {
00116                 public ScanFailureEventArgs(string name, Exception e)
00117                 {
00118                         this.name = name;
00119                         this.exception = e;
00120                         continueRunning = true;
00121                 }
00122                 
00123                 string name;
00124                 
00128                 public string Name
00129                 {
00130                         get { return name; }
00131                 }
00132                 
00133                 Exception exception;
00134                 
00138                 public Exception Exception
00139                 {
00140                         get { return exception; }
00141                 }
00142 
00143                 bool continueRunning;
00144                 
00148                 public bool ContinueRunning
00149                 {
00150                         get { return continueRunning; }
00151                         set { continueRunning = value; }
00152                 }
00153         }
00154         
00158         public delegate void ProcessDirectoryDelegate(object Sender, DirectoryEventArgs e);
00159         
00163         public delegate void ProcessFileDelegate(object sender, ScanEventArgs e);
00164         
00168         public delegate void DirectoryFailureDelegate(object sender, ScanFailureEventArgs e);
00169         
00173         public delegate void FileFailureDelegate(object sender, ScanFailureEventArgs e);
00174 
00178         public class FileSystemScanner
00179         {
00184                 public FileSystemScanner(string filter)
00185                 {
00186                         fileFilter = new PathFilter(filter);
00187                 }
00188                 
00194                 public FileSystemScanner(string fileFilter, string directoryFilter)
00195                 {
00196                         this.fileFilter = new PathFilter(fileFilter);
00197                         this.directoryFilter = new PathFilter(directoryFilter);
00198                 }
00199                 
00204                 public FileSystemScanner(IScanFilter fileFilter)
00205                 {
00206                         this.fileFilter = fileFilter;
00207                 }
00208                 
00214                 public FileSystemScanner(IScanFilter fileFilter, IScanFilter directoryFilter)
00215                 {
00216                         this.fileFilter = fileFilter;
00217                         this.directoryFilter = directoryFilter;
00218                 }
00219                 
00223                 public ProcessDirectoryDelegate ProcessDirectory;
00224                 
00228                 public ProcessFileDelegate ProcessFile;
00229 
00233                 public DirectoryFailureDelegate DirectoryFailure;
00234                 
00238                 public FileFailureDelegate FileFailure;
00239                 
00245                 public void OnDirectoryFailure(string directory, Exception e)
00246                 {
00247                         if ( DirectoryFailure == null ) {
00248                                 alive = false;
00249                         } else {
00250                                 ScanFailureEventArgs args = new ScanFailureEventArgs(directory, e);
00251                                 DirectoryFailure(this, args);
00252                                 alive = args.ContinueRunning;
00253                         }
00254                 }
00255                 
00261                 public void OnFileFailure(string file, Exception e)
00262                 {
00263                         if ( FileFailure == null ) {
00264                                 alive = false;
00265                         } else {
00266                                 ScanFailureEventArgs args = new ScanFailureEventArgs(file, e);
00267                                 FileFailure(this, args);
00268                                 alive = args.ContinueRunning;
00269                         }
00270                 }
00271 
00276                 public void OnProcessFile(string file)
00277                 {
00278                         if ( ProcessFile != null ) {
00279                                 ScanEventArgs args = new ScanEventArgs(file);
00280                                 ProcessFile(this, args);
00281                                 alive = args.ContinueRunning;
00282                         }
00283                 }
00284                 
00290                 public void OnProcessDirectory(string directory, bool hasMatchingFiles)
00291                 {
00292                         if ( ProcessDirectory != null ) {
00293                                 DirectoryEventArgs args = new DirectoryEventArgs(directory, hasMatchingFiles);
00294                                 ProcessDirectory(this, args);
00295                                 alive = args.ContinueRunning;
00296                         }
00297                 }
00298 
00304                 public void Scan(string directory, bool recurse)
00305                 {
00306                         alive = true;
00307                         ScanDir(directory, recurse);
00308                 }
00309                 
00310                 void ScanDir(string directory, bool recurse)
00311                 {
00312 
00313                         try {
00314                                 string[] names = System.IO.Directory.GetFiles(directory);
00315                                 bool hasMatch = false;
00316                                 for (int fileIndex = 0; fileIndex < names.Length; ++fileIndex) {
00317                                         if ( !fileFilter.IsMatch(names[fileIndex]) ) {
00318                                                 names[fileIndex] = null;
00319                                         } else {
00320                                                 hasMatch = true;
00321                                         }
00322                                 }
00323                                 
00324                                 OnProcessDirectory(directory, hasMatch);
00325                                 
00326                                 if ( alive && hasMatch ) {
00327                                         foreach (string fileName in names) {
00328                                                 try {
00329                                                         if ( fileName != null ) {
00330                                                                 OnProcessFile(fileName);
00331                                                                 if ( !alive ) {
00332                                                                         break;
00333                                                                 }
00334                                                         }
00335                                                 }
00336                                                 catch (Exception e)
00337                                                 {
00338                                                         OnFileFailure(fileName, e);
00339                                                 }
00340                                         }
00341                                 }
00342                         }
00343                         catch (Exception e) {
00344                                 OnDirectoryFailure(directory, e);
00345                         }
00346 
00347                         if ( alive && recurse ) {
00348                                 try {
00349                                         string[] names = System.IO.Directory.GetDirectories(directory);
00350                                         foreach (string fulldir in names) {
00351                                                 if ((directoryFilter == null) || (directoryFilter.IsMatch(fulldir))) {
00352                                                         ScanDir(fulldir, true);
00353                                                         if ( !alive ) {
00354                                                                 break;
00355                                                         }
00356                                                 }
00357                                         }
00358                                 }
00359                                 catch (Exception e) {
00360                                         OnDirectoryFailure(directory, e);
00361                                 }
00362                         }
00363                 }
00364                 
00365                 #region Instance Fields
00369                 IScanFilter fileFilter;
00373                 IScanFilter directoryFilter;
00377                 bool alive;
00378                 #endregion
00379         }
00380 }

Generated on Fri Jun 23 21:50:04 2006 for OblivionModTranslator by  doxygen 1.4.6-NO