/// <summary>
        /// See <c>IScanner</c> documentation.
        /// </summary>
        public IMultiFileScanResult Scan(IEnumerable <string> filePaths, string searchingWord, FileScanCompleted callback, FileContentGetter contentGetter, ScanStopper stopper)
        {
            if (filePaths == null)
            {
                throw new ArgumentNullException("filePaths");
            }
            if (string.IsNullOrEmpty(searchingWord))
            {
                throw new ArgumentNullException("termTables");
            }

            MatchFinder finder = new MatchFinder(searchingWord);

            MultiFileScanResult allResults = new MultiFileScanResult();

            foreach (string filePath in filePaths)
            {
                if (stopper != null && stopper())
                {
                    break;
                }

                if (FileShouldBeScanned(filePath))
                {
                    IScanResult fileResult = ScanFile(filePath, finder, contentGetter, stopper);
                    allResults.Append(fileResult);
                    if (callback != null)
                    {
                        callback(fileResult);
                    }
                }
            }

            return(allResults);
        }
Exemple #2
0
 public ExclusionMatch(MatchFinder finder, MatchableExclusion exclusion, int line, int column)
 {
     _finder    = finder;
     _exclusion = exclusion;
     _line      = line;
     _column    = column;
 }
Exemple #3
0
 public TermMatch(MatchFinder finder, MatchableTerm term, int line, int column)
 {
     _finder = finder;
     _term   = term;
     _line   = line;
     _column = column;
 }
        private static IScanResult ScanFile(string filePath, MatchFinder finder, FileContentGetter contentGetter, ScanStopper stopper)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException("filePath");
            }

            // See if the content getter can give us the file contents.  If so, we'll scan that
            // string rather than loading the file from disk.
            if (contentGetter != null)
            {
                string content = contentGetter(filePath);
                if (content != null)
                {
                    return(ScanResult.ScanOccurred(filePath, GetScanHits(filePath, content, finder, stopper)));
                }
            }

            StreamReader reader = null;

            try
            {
                try
                {
                    reader = File.OpenText(filePath);
                }
                catch (Exception ex)
                {
                    if (ex is UnauthorizedAccessException ||
                        ex is ArgumentException ||
                        ex is ArgumentNullException ||
                        ex is PathTooLongException ||
                        ex is DirectoryNotFoundException ||
                        ex is FileNotFoundException ||
                        ex is NotSupportedException ||
                        ex is IOException)
                    {
                        return(ScanResult.ScanNotPossible(filePath));
                    }
                    else
                    {
                        throw;
                    }
                }

                return(ScanResult.ScanOccurred(filePath, GetScanHits(filePath, reader, finder, stopper)));
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }
        private static IEnumerable <IScanHit> GetScanHits(string filePath, string content, MatchFinder finder, ScanStopper stopper)
        {
            List <IScanHit> hits = new List <IScanHit>();

            MatchFoundCallback callback =
                (term, line, column, lineText, warning) => hits.Add(new ScanHit(filePath, line, column, lineText, term, warning));

            finder.Reset();

            foreach (char c in content)
            {
                if (stopper != null && stopper())
                {
                    break;
                }
                finder.AnalyzeNextCharacter(c, callback);
            }

            finder.Finish(callback);

            return(hits);
        }
        private static IEnumerable <IScanHit> GetScanHits(string filePath, StreamReader reader, MatchFinder finder, ScanStopper stopper)
        {
            List <IScanHit> hits = new List <IScanHit>();

            MatchFoundCallback callback =
                (term, line, column, lineText, warning) => hits.Add(new ScanHit(filePath, line, column, lineText, term, warning));

            finder.Reset();

            while (!reader.EndOfStream)
            {
                if (stopper != null && stopper())
                {
                    break;
                }
                finder.AnalyzeNextCharacter((char)reader.Read(), callback);
            }

            finder.Finish(callback);

            return(hits);
        }