public List <GrepSearchResult> Search(string file, string searchPattern, SearchType searchType, GrepSearchOption searchOptions, Encoding encoding) { string tempFolder = Path.Combine(Utils.GetTempFolder(), "dnGREP-PDF"); try { // Extract text string tempFile = ExtractText(file); if (!File.Exists(tempFile)) { throw new ApplicationException("pdftotext failed to create text file."); } // GrepCore cannot check encoding of the original pdf file. If the encoding parameter is not default // then it is the user-specified code page. If the encoding parameter *is* the default, // then it most likely not been set, so get the encoding of the extracted text file: if (encoding == Encoding.Default) { encoding = Utils.GetFileEncoding(tempFile); } IGrepEngine engine = GrepEngineFactory.GetSearchEngine(tempFile, initParams, FileFilter); List <GrepSearchResult> results = engine.Search(tempFile, searchPattern, searchType, searchOptions, encoding); if (results.Count > 0) { using (FileStream reader = File.Open(tempFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (StreamReader streamReader = new StreamReader(reader, encoding)) { foreach (var result in results) { result.SearchResults = Utils.GetLinesEx(streamReader, result.Matches, initParams.LinesBefore, initParams.LinesAfter); } } foreach (GrepSearchResult result in results) { result.ReadOnly = true; if (file.Contains(tempFolder)) { result.FileNameDisplayed = file.Substring(tempFolder.Length + 1); } else { result.FileNameDisplayed = file; } result.FileNameReal = file; } } GrepEngineFactory.ReturnToPool(tempFile, engine); return(results); } catch (Exception ex) { logger.Error(ex, $"Failed to search inside PDF file: {ex.Message}"); return(new List <GrepSearchResult>()); } }
public List <GrepSearchResult> CaptureGroupSearch(IEnumerable <string> files, string filePatternInclude, GrepSearchOption searchOptions, SearchType searchType, string searchPattern, int codePage) { searchResults.Clear(); if (files == null || !files.Any()) { return(searchResults); } Utils.CancelSearch = false; try { foreach (string filePath in files) { string fileName = Path.GetFileName(filePath); string modSearchPattern = Regex.Replace(fileName, filePatternInclude, searchPattern, RegexOptions.IgnoreCase, TimeSpan.FromSeconds(4.0)); if (string.IsNullOrEmpty(modSearchPattern)) { continue; } else if (searchType == SearchType.Regex && !Utils.ValidateRegex(modSearchPattern)) { logger.Error($"Capture group search pattern is not a valid regular expression: '{modSearchPattern}'"); continue; } Search(filePath, searchType, modSearchPattern, searchOptions, codePage); if (searchOptions.HasFlag(GrepSearchOption.StopAfterFirstMatch) && searchResults.Count > 0) { break; } if (Utils.CancelSearch) { break; } } } catch (Exception ex) { logger.Error(ex, "Failed in capture group search"); } finally { if (cancellationTokenSource != null) { cancellationTokenSource.Dispose(); cancellationTokenSource = null; } GrepEngineFactory.UnloadEngines(); } return(new List <GrepSearchResult>(searchResults)); }
public List <GrepSearchResult> Search(string file, string searchPattern, SearchType searchType, GrepSearchOption searchOptions, Encoding encoding) { string tempFolder = Path.Combine(Utils.GetTempFolder(), "dnGREP-PDF"); try { // Extract text string tempFile = extractText(file); if (!File.Exists(tempFile)) { throw new ApplicationException("pdftotext failed to create text file."); } IGrepEngine engine = GrepEngineFactory.GetSearchEngine(tempFile, initParams, FileFilter); List <GrepSearchResult> results = engine.Search(tempFile, searchPattern, searchType, searchOptions, encoding); if (results.Count > 0) { using (FileStream reader = File.Open(tempFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (StreamReader streamReader = new StreamReader(reader)) { foreach (var result in results) { result.SearchResults = Utils.GetLinesEx(streamReader, result.Matches, initParams.LinesBefore, initParams.LinesAfter); } } foreach (GrepSearchResult result in results) { result.ReadOnly = true; if (file.Contains(tempFolder)) { result.FileNameDisplayed = file.Substring(tempFolder.Length + 1); } else { result.FileNameDisplayed = file; } result.FileNameReal = file; } } GrepEngineFactory.ReturnToPool(tempFile, engine); return(results); } catch (Exception ex) { logger.Log <Exception>(LogLevel.Error, "Failed to search inside Pdf file", ex); return(new List <GrepSearchResult>()); } }
public BaseMainViewModel() { RequestClose += MainViewModel_RequestClose; PropertyChanged += MainViewModel_PropertyChanged; CurrentGrepOperation = GrepOperation.None; IsCaseSensitiveEnabled = true; IsMultilineEnabled = true; IsWholeWordEnabled = true; Utils.ArchiveExtensions = GrepEngineFactory.GetArchiveExtenstions(); CanSearchArchives = Utils.ArchiveExtensions.Count > 0; LoadSettings(); }
public BaseMainViewModel() { PropertyChanged += MainViewModel_PropertyChanged; CurrentGrepOperation = GrepOperation.None; IsCaseSensitiveEnabled = true; IsMultilineEnabled = true; IsWholeWordEnabled = true; Utils.ArchiveExtensions = GrepEngineFactory.GetArchiveExtenstions(); CanSearchArchives = Utils.ArchiveExtensions.Count > 0; LoadSettings(); IsEverythingAvailable = EverythingSearch.IsAvailable; }
public List <GrepSearchResult> Search(string file, string searchPattern, SearchType searchType, GrepSearchOption searchOptions, Encoding encoding) { List <GrepSearchResult> searchResults = new List <GrepSearchResult>(); SevenZipExtractor extractor = new SevenZipExtractor(file); GrepEnginePlainText plainTextEngine = new GrepEnginePlainText(); plainTextEngine.Initialize(new GrepEngineInitParams(showLinesInContext, linesBefore, linesAfter, fuzzyMatchThreshold)); string tempFolder = Utils.FixFolderName(Utils.GetTempFolder()) + "dnGREP-Archive\\" + Utils.GetHash(file) + "\\"; if (Directory.Exists(tempFolder)) { Utils.DeleteFolder(tempFolder); } Directory.CreateDirectory(tempFolder); try { extractor.ExtractArchive(tempFolder); foreach (string archiveFileName in Directory.GetFiles(tempFolder, "*.*", SearchOption.AllDirectories)) { IGrepEngine engine = GrepEngineFactory.GetSearchEngine(archiveFileName, new GrepEngineInitParams(showLinesInContext, linesBefore, linesAfter, fuzzyMatchThreshold)); var innerFileResults = engine.Search(archiveFileName, searchPattern, searchType, searchOptions, encoding); using (FileStream reader = File.Open(archiveFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (StreamReader streamReader = new StreamReader(reader)) { foreach (var result in innerFileResults) { if (!result.HasSearchResults) { result.SearchResults = Utils.GetLinesEx(streamReader, result.Matches, linesBefore, linesAfter); } } } searchResults.AddRange(innerFileResults); } foreach (GrepSearchResult result in searchResults) { result.FileNameDisplayed = file + "\\" + result.FileNameDisplayed.Substring(tempFolder.Length); result.FileNameReal = file; result.ReadOnly = true; } } catch (Exception ex) { logger.LogException(LogLevel.Error, "Failed to search inside archive.", ex); } return(searchResults); }
public List <GrepSearchResult> Search(string file, string searchPattern, SearchType searchType, GrepSearchOption searchOptions, Encoding encoding) { try { // Extract text string tempFile = extractText(file); if (!File.Exists(tempFile)) { throw new ApplicationException("pdftotext failed to create text file."); } IGrepEngine engine = GrepEngineFactory.GetSearchEngine(tempFile, new GrepEngineInitParams(showLinesInContext, linesBefore, linesAfter, fuzzyMatchThreshold)); List <GrepSearchResult> results = engine.Search(tempFile, searchPattern, searchType, searchOptions, encoding); using (FileStream reader = File.Open(tempFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (StreamReader streamReader = new StreamReader(reader)) { foreach (var result in results) { result.SearchResults = Utils.GetLinesEx(streamReader, result.Matches, linesBefore, linesAfter); } } foreach (GrepSearchResult result in results) { result.ReadOnly = true; result.FileNameDisplayed = file; result.FileNameReal = file; } return(results); } catch (Exception ex) { logger.LogException(LogLevel.Error, "Failed to search inside Pdf file", ex); return(new List <GrepSearchResult>()); } }
public List <GrepSearchResult> Search(Stream input, string file, string searchPattern, SearchType searchType, GrepSearchOption searchOptions, Encoding encoding) { List <GrepSearchResult> searchResults = new List <GrepSearchResult>(); var filter = FileFilter.ToStandardFilter(); var includeRegexPatterns = new List <Regex>(); var excludeRegexPatterns = new List <Regex>(); Utils.PrepareFilters(filter, includeRegexPatterns, excludeRegexPatterns); List <string> hiddenDirectories = new List <string>(); try { using (SevenZipExtractor extractor = new SevenZipExtractor(input)) { foreach (var fileInfo in extractor.ArchiveFileData) { var attr = (FileAttributes)fileInfo.Attributes; string innerFileName = fileInfo.FileName; if (fileInfo.IsDirectory) { if (!filter.IncludeHidden && attr.HasFlag(FileAttributes.Hidden) && !hiddenDirectories.Contains(innerFileName)) { hiddenDirectories.Add(innerFileName); } continue; } if (CheckHidden(filter, attr) && CheckHidden(filter, innerFileName, hiddenDirectories) && CheckSize(filter, fileInfo.Size) && CheckDate(filter, fileInfo) && IsPatternMatch(innerFileName, includeRegexPatterns) && !IsPatternMatch(innerFileName, excludeRegexPatterns)) { using (Stream stream = new MemoryStream()) { extractor.ExtractFile(innerFileName, stream); stream.Seek(0, SeekOrigin.Begin); if (CheckBinary(filter, stream)) { // Need to check the encoding of each file in the archive. If the encoding parameter is not default // then it is the user-specified code page. If the encoding parameter *is* the default, // then it most likely not been set, so get the encoding of the extracted text file: if (encoding == Encoding.Default && !Utils.IsBinary(stream)) { stream.Seek(0, SeekOrigin.Begin); encoding = Utils.GetFileEncoding(stream); } IGrepEngine engine = GrepEngineFactory.GetSearchEngine(innerFileName, initParams, filter); var innerFileResults = engine.Search(stream, innerFileName, searchPattern, searchType, searchOptions, encoding); if (innerFileResults.Count > 0) { using (Stream readStream = new MemoryStream()) { extractor.ExtractFile(innerFileName, readStream); readStream.Seek(0, SeekOrigin.Begin); using (StreamReader streamReader = new StreamReader(readStream, encoding)) { foreach (var result in innerFileResults) { if (Utils.CancelSearch) { break; } if (!result.HasSearchResults) { result.SearchResults = Utils.GetLinesEx(streamReader, result.Matches, initParams.LinesBefore, initParams.LinesAfter); } } } searchResults.AddRange(innerFileResults); } } GrepEngineFactory.ReturnToPool(innerFileName, engine); } if (Utils.CancelSearch) { break; } } } } } foreach (GrepSearchResult result in searchResults) { result.FileNameDisplayed = file + "\\" + result.FileNameDisplayed; result.FileNameReal = file; result.ReadOnly = true; } } catch (Exception ex) { logger.Log <Exception>(LogLevel.Error, string.Format("Failed to search inside archive '{0}'", file), ex); } return(searchResults); }
public int Replace(Dictionary <string, string> files, SearchType searchType, string searchPattern, string replacePattern, GrepSearchOption searchOptions, int codePage) { string tempFolder = Utils.GetTempFolder(); if (files == null || files.Count == 0 || !Directory.Exists(tempFolder)) { return(0); } replacePattern = Utils.ReplaceSpecialCharacters(replacePattern); int processedFiles = 0; Utils.CancelSearch = false; string tempFileName = null; try { foreach (string file in files.Keys) { ProcessedFile(this, new ProgressStatus(true, processedFiles, processedFiles, null, file)); // the value in the files dictionary is the temp file name assigned by // the caller for any possible Undo operation tempFileName = Path.Combine(tempFolder, files[file]); IGrepEngine engine = GrepEngineFactory.GetReplaceEngine(file, SearchParams, FileFilter); try { processedFiles++; // Copy file Utils.CopyFile(file, tempFileName, true); Utils.DeleteFile(file); Encoding encoding = Encoding.Default; if (codePage > -1) { encoding = Encoding.GetEncoding(codePage); } else if (!Utils.IsBinary(tempFileName)) { encoding = Utils.GetFileEncoding(tempFileName); } if (Utils.CancelSearch) { break; } if (!engine.Replace(tempFileName, file, searchPattern, replacePattern, searchType, searchOptions, encoding)) { throw new ApplicationException("Replace failed for file: " + file); } if (!Utils.CancelSearch) { ProcessedFile(this, new ProgressStatus(false, processedFiles, processedFiles, null, file)); } File.SetAttributes(file, File.GetAttributes(tempFileName)); GrepEngineFactory.ReturnToPool(file, engine); if (Utils.CancelSearch) { // Replace the file Utils.DeleteFile(file); Utils.CopyFile(tempFileName, file, true); break; } } catch (Exception ex) { logger.Log <Exception>(LogLevel.Error, ex.Message, ex); try { // Replace the file if (File.Exists(tempFileName) && File.Exists(file)) { Utils.DeleteFile(file); Utils.CopyFile(tempFileName, file, true); } } catch { // DO NOTHING } return(-1); } } } finally { GrepEngineFactory.UnloadEngines(); } return(processedFiles); }
/// <summary> /// Searches folder for files whose content matches regex /// </summary> /// <param name="files">Files to search in. If one of the files does not exist or is open, it is skipped.</param> /// <param name="searchRegex">Regex pattern</param> /// <returns>List of results. If nothing is found returns empty list</returns> public List <GrepSearchResult> Search(IEnumerable <string> files, SearchType searchType, string searchPattern, GrepSearchOption searchOptions, int codePage) { searchResults.Clear(); if (files == null) { return(searchResults); } Utils.CancelSearch = false; if (searchPattern == null || searchPattern.Trim() == "") { int count = 0; foreach (string file in files) { count++; ProcessedFile(this, new ProgressStatus(true, searchResults.Count, count, null, file)); searchResults.Add(new GrepSearchResult(file, searchPattern, null, Encoding.Default)); if ((searchOptions & GrepSearchOption.StopAfterFirstMatch) == GrepSearchOption.StopAfterFirstMatch) { break; } if (Utils.CancelSearch) { break; } } ProcessedFile(this, new ProgressStatus(false, searchResults.Count, count, searchResults, null)); return(new List <GrepSearchResult>(searchResults)); } else { processedFilesCount = 0; foundfilesCount = 0; try { if (SearchParams.SearchParallel) { cancellationTokenSource = new CancellationTokenSource(); ParallelOptions po = new ParallelOptions(); po.MaxDegreeOfParallelism = Environment.ProcessorCount * 4 / 5; po.CancellationToken = cancellationTokenSource.Token; Parallel.ForEach(files, po, f => Search(f, searchType, searchPattern, searchOptions, codePage)); } else { foreach (var file in files) { Search(file, searchType, searchPattern, searchOptions, codePage); if (searchOptions.HasFlag(GrepSearchOption.StopAfterFirstMatch) && searchResults.Count > 0) { break; } } } } catch (OperationCanceledException) { // expected for stop after first match or user cancel } catch (Exception ex) { logger.Error(ex, "Failed in search in files"); } finally { if (cancellationTokenSource != null) { cancellationTokenSource.Dispose(); cancellationTokenSource = null; } GrepEngineFactory.UnloadEngines(); } return(new List <GrepSearchResult>(searchResults)); } }
public int Replace(IEnumerable <string> files, SearchType searchType, string searchPattern, string replacePattern, GrepSearchOption searchOptions, int codePage) { string tempFolder = Utils.GetTempFolder(); if (files == null || !Directory.Exists(tempFolder)) { return(0); } replacePattern = Utils.ReplaceSpecialCharacters(replacePattern); int processedFiles = 0; Utils.CancelSearch = false; string tempFileName = null; try { foreach (string file in files) { ProcessedFile(this, new ProgressStatus(processedFiles, null, file)); tempFileName = Path.Combine(tempFolder, Path.GetFileName(file)); IGrepEngine engine = GrepEngineFactory.GetReplaceEngine(file, SearchParams, FileFilter); try { processedFiles++; // Copy file Utils.CopyFile(file, tempFileName, true); Utils.DeleteFile(file); Encoding encoding = Encoding.Default; if (codePage > -1) { encoding = Encoding.GetEncoding(codePage); } else if (!Utils.IsBinary(file)) { encoding = Utils.GetFileEncoding(tempFileName); } if (Utils.CancelSearch) { break; } if (!engine.Replace(tempFileName, file, searchPattern, replacePattern, searchType, searchOptions, encoding)) { throw new ApplicationException("Replace failed for file: " + file); } if (!Utils.CancelSearch) { ProcessedFile(this, new ProgressStatus(processedFiles, null, file)); } File.SetAttributes(file, File.GetAttributes(tempFileName)); if (Utils.CancelSearch) { // Replace the file Utils.DeleteFile(file); Utils.CopyFile(tempFileName, file, true); break; } } catch (Exception ex) { logger.Log <Exception>(LogLevel.Error, ex.Message, ex); try { // Replace the file if (File.Exists(tempFileName) && File.Exists(file)) { Utils.DeleteFile(file); Utils.CopyFile(tempFileName, file, true); } } catch { // DO NOTHING } return(-1); } finally { try { if (!string.IsNullOrWhiteSpace(tempFileName)) { Utils.DeleteFile(tempFileName); } } catch { // DO NOTHING } } } } finally { GrepEngineFactory.UnloadEngines(); } return(processedFiles); }
private void Search(string file, SearchType searchType, string searchPattern, GrepSearchOption searchOptions, int codePage) { try { ProcessedFile(this, new ProgressStatus(true, processedFilesCount, foundfilesCount, null, file)); IGrepEngine engine = GrepEngineFactory.GetSearchEngine(file, SearchParams, FileFilter); Interlocked.Increment(ref processedFilesCount); Encoding encoding = Encoding.Default; if (codePage > -1) { encoding = Encoding.GetEncoding(codePage); } else if (!Utils.IsBinary(file) && !Utils.IsPdfFile(file)) { encoding = Utils.GetFileEncoding(file); } if (Utils.CancelSearch) { if (cancellationTokenSource != null) { cancellationTokenSource.Cancel(); } return; } List <GrepSearchResult> fileSearchResults = engine.Search(file, searchPattern, searchType, searchOptions, encoding); if (fileSearchResults != null && fileSearchResults.Count > 0) { AddSearchResults(fileSearchResults); } int hits = fileSearchResults.Where(r => r.IsSuccess).Count(); Interlocked.Add(ref foundfilesCount, hits); ProcessedFile(this, new ProgressStatus(false, processedFilesCount, foundfilesCount, fileSearchResults, file)); GrepEngineFactory.ReturnToPool(file, engine); } catch (Exception ex) { logger.Log <Exception>(LogLevel.Error, ex.Message, ex); AddSearchResult(new GrepSearchResult(file, searchPattern, ex.Message, false)); if (ProcessedFile != null) { List <GrepSearchResult> _results = new List <GrepSearchResult>(); _results.Add(new GrepSearchResult(file, searchPattern, ex.Message, false)); ProcessedFile(this, new ProgressStatus(false, processedFilesCount, foundfilesCount, _results, file)); } } finally { if ((searchOptions & GrepSearchOption.StopAfterFirstMatch) == GrepSearchOption.StopAfterFirstMatch && searchResults.Count > 0) { if (cancellationTokenSource != null) { cancellationTokenSource.Cancel(); } } } }
public int Replace(IEnumerable <ReplaceDef> files, SearchType searchType, string searchPattern, string replacePattern, GrepSearchOption searchOptions, int codePage) { string tempFolder = Utils.GetTempFolder(); if (files == null || !files.Any() || !Directory.Exists(tempFolder)) { return(0); } GrepEngineBase.ResetGuidxCache(); replacePattern = Utils.ReplaceSpecialCharacters(replacePattern); int processedFiles = 0; Utils.CancelSearch = false; string tempFileName = null; try { foreach (var item in files) { ProcessedFile(this, new ProgressStatus(true, processedFiles, processedFiles, null, item.OrginalFile)); // the value in the files dictionary is the temp file name assigned by // the caller for any possible Undo operation tempFileName = Path.Combine(tempFolder, item.BackupName); IGrepEngine engine = GrepEngineFactory.GetReplaceEngine(item.OrginalFile, SearchParams, FileFilter); try { processedFiles++; // Copy file Utils.CopyFile(item.OrginalFile, tempFileName, true); Utils.DeleteFile(item.OrginalFile); Encoding encoding = Encoding.Default; if (codePage > -1) { encoding = Encoding.GetEncoding(codePage); } else if (!Utils.IsBinary(tempFileName)) { encoding = Utils.GetFileEncoding(tempFileName); } // The UTF-8 encoding returned from Encoding.GetEncoding("utf-8") includes the BOM - see Encoding.GetPreamble() // If this file does not have the BOM, then change to an encoder without the BOM so the BOM is not added in // the replace operation if (encoding is UTF8Encoding && !Utils.HasUtf8ByteOrderMark(tempFileName)) { encoding = new UTF8Encoding(false); } if (Utils.CancelSearch) { break; } if (!engine.Replace(tempFileName, item.OrginalFile, searchPattern, replacePattern, searchType, searchOptions, encoding, item.ReplaceItems)) { throw new ApplicationException("Replace failed for file: " + item.OrginalFile); } if (!Utils.CancelSearch) { ProcessedFile(this, new ProgressStatus(false, processedFiles, processedFiles, null, item.OrginalFile)); } File.SetAttributes(item.OrginalFile, File.GetAttributes(tempFileName)); GrepEngineFactory.ReturnToPool(item.OrginalFile, engine); if (Utils.CancelSearch) { // Replace the file Utils.DeleteFile(item.OrginalFile); Utils.CopyFile(tempFileName, item.OrginalFile, true); break; } } catch (Exception ex) { logger.Log <Exception>(LogLevel.Error, ex.Message, ex); try { // Replace the file if (File.Exists(tempFileName) && File.Exists(item.OrginalFile)) { Utils.DeleteFile(item.OrginalFile); Utils.CopyFile(tempFileName, item.OrginalFile, true); } } catch { // DO NOTHING } return(-1); } } } finally { GrepEngineFactory.UnloadEngines(); } return(processedFiles); }
/// <summary> /// Searches folder for files whose content matches regex /// </summary> /// <param name="files">Files to search in. If one of the files does not exist or is open, it is skipped.</param> /// <param name="searchRegex">Regex pattern</param> /// <returns>List of results. If nothing is found returns empty list</returns> public List <GrepSearchResult> Search(IEnumerable <string> files, SearchType searchType, string searchPattern, GrepSearchOption searchOptions, int codePage) { searchResults.Clear(); if (files == null) { return(searchResults); } if (string.IsNullOrEmpty(searchPattern)) { return(searchResults); } Utils.CancelSearch = false; processedFilesCount = 0; foundfilesCount = 0; try { if (SearchParams.SearchParallel) { cancellationTokenSource = new CancellationTokenSource(); ParallelOptions po = new ParallelOptions { MaxDegreeOfParallelism = Math.Max(1, Environment.ProcessorCount * 4 / 5), CancellationToken = cancellationTokenSource.Token }; Parallel.ForEach(files, po, f => Search(f, searchType, searchPattern, searchOptions, codePage)); } else { foreach (var file in files) { Search(file, searchType, searchPattern, searchOptions, codePage); if (searchOptions.HasFlag(GrepSearchOption.StopAfterFirstMatch) && searchResults.Count > 0) { break; } if (Utils.CancelSearch) { break; } } } } catch (OperationCanceledException) { // expected for stop after first match or user cancel } catch (Exception ex) { logger.Error(ex, "Failed in search in files"); } finally { if (cancellationTokenSource != null) { cancellationTokenSource.Dispose(); cancellationTokenSource = null; } GrepEngineFactory.UnloadEngines(); } return(new List <GrepSearchResult>(searchResults)); }
private void Search(string file, SearchType searchType, string searchPattern, GrepSearchOption searchOptions, int codePage) { try { ProcessedFile(this, new ProgressStatus(true, processedFilesCount, foundfilesCount, null, file)); bool isArchive = Utils.IsArchive(file); Encoding encoding = Encoding.Default; if (codePage > -1) { encoding = Encoding.GetEncoding(codePage); } else if (!isArchive && !Utils.IsBinary(file) && !Utils.IsPdfFile(file)) { encoding = Utils.GetFileEncoding(file); } if (Utils.CancelSearch) { if (cancellationTokenSource != null) { cancellationTokenSource.Cancel(); } return; } IGrepEngine engine = GrepEngineFactory.GetSearchEngine(file, SearchParams, FileFilter, searchType); if (isArchive && engine is ArchiveEngine archiveEngine) { archiveEngine.SetSearchOptions(FileFilter, SearchParams); archiveEngine.StartingFileSearch += ArchiveEngine_StartingFileSearch; foreach (var fileSearchResults in archiveEngine.Search(file, searchPattern, searchType, searchOptions, encoding)) { if (fileSearchResults != null && fileSearchResults.Count > 0) { AddSearchResults(fileSearchResults); } int hits = fileSearchResults.Where(r => r.IsSuccess).Count(); Interlocked.Add(ref foundfilesCount, hits); ProcessedFile(this, new ProgressStatus(false, processedFilesCount, foundfilesCount, fileSearchResults, file)); } archiveEngine.StartingFileSearch -= ArchiveEngine_StartingFileSearch; } else { Interlocked.Increment(ref processedFilesCount); var fileSearchResults = engine.Search(file, searchPattern, searchType, searchOptions, encoding).ToList(); if (fileSearchResults != null && fileSearchResults.Count > 0) { AddSearchResults(fileSearchResults); } int hits = fileSearchResults.Where(r => r.IsSuccess).Count(); Interlocked.Add(ref foundfilesCount, hits); ProcessedFile(this, new ProgressStatus(false, processedFilesCount, foundfilesCount, fileSearchResults, file)); } GrepEngineFactory.ReturnToPool(file, engine); } catch (Exception ex) { logger.Error(ex, "Failed in Search"); AddSearchResult(new GrepSearchResult(file, searchPattern, ex.Message, false)); if (ProcessedFile != null) { List <GrepSearchResult> _results = new List <GrepSearchResult> { new GrepSearchResult(file, searchPattern, ex.Message, false) }; ProcessedFile(this, new ProgressStatus(false, processedFilesCount, foundfilesCount, _results, file)); } } finally { if (searchOptions.HasFlag(GrepSearchOption.StopAfterFirstMatch) && searchResults.Count > 0) { if (cancellationTokenSource != null) { cancellationTokenSource.Cancel(); } } } }
public List <GrepSearchResult> Search(Stream input, string file, string searchPattern, SearchType searchType, GrepSearchOption searchOptions, Encoding encoding) { List <GrepSearchResult> searchResults = new List <GrepSearchResult>(); var includeRegexPatterns = new List <Regex>(); var excludeRegexPatterns = new List <Regex>(); Utils.PrepareFilters(FileFilter, includeRegexPatterns, excludeRegexPatterns); List <string> hiddenDirectories = new List <string>(); try { using (SevenZipExtractor extractor = new SevenZipExtractor(input)) { foreach (var fileInfo in extractor.ArchiveFileData) { var attr = (FileAttributes)fileInfo.Attributes; string innerFileName = fileInfo.FileName; if (fileInfo.IsDirectory) { if (!FileFilter.IncludeHidden && attr.HasFlag(FileAttributes.Hidden) && !hiddenDirectories.Contains(innerFileName)) { hiddenDirectories.Add(innerFileName); } continue; } if (CheckHidden(FileFilter, attr) && CheckHidden(FileFilter, innerFileName, hiddenDirectories) && CheckSize(FileFilter, fileInfo.Size) && CheckDate(FileFilter, fileInfo) && IsPatternMatch(innerFileName, includeRegexPatterns) && !IsPatternMatch(innerFileName, excludeRegexPatterns)) { using (Stream stream = new MemoryStream()) { extractor.ExtractFile(innerFileName, stream); stream.Seek(0, SeekOrigin.Begin); if (CheckBinary(FileFilter, stream)) { IGrepEngine engine = GrepEngineFactory.GetSearchEngine(innerFileName, initParams, FileFilter); var innerFileResults = engine.Search(stream, innerFileName, searchPattern, searchType, searchOptions, encoding); if (innerFileResults.Count > 0) { using (Stream readStream = new MemoryStream()) { extractor.ExtractFile(innerFileName, readStream); readStream.Seek(0, SeekOrigin.Begin); using (StreamReader streamReader = new StreamReader(readStream)) { foreach (var result in innerFileResults) { if (Utils.CancelSearch) { break; } if (!result.HasSearchResults) { result.SearchResults = Utils.GetLinesEx(streamReader, result.Matches, initParams.LinesBefore, initParams.LinesAfter); } } } searchResults.AddRange(innerFileResults); } } } if (Utils.CancelSearch) { break; } } } } } foreach (GrepSearchResult result in searchResults) { result.FileNameDisplayed = file + "\\" + result.FileNameDisplayed; result.FileNameReal = file; result.ReadOnly = true; } } catch (Exception ex) { logger.Log <Exception>(LogLevel.Error, string.Format("Failed to search inside archive '{0}'", file), ex); } return(searchResults); }
public int Replace(IEnumerable <string> files, SearchType searchType, string baseFolder, string searchPattern, string replacePattern, GrepSearchOption searchOptions, int codePage) { string tempFolder = Utils.GetTempFolder(); if (Directory.Exists(tempFolder)) { Utils.DeleteFolder(tempFolder); } Directory.CreateDirectory(tempFolder); if (files == null || !Directory.Exists(tempFolder) || !Directory.Exists(baseFolder)) { return(0); } baseFolder = Utils.FixFolderName(baseFolder); tempFolder = Utils.FixFolderName(tempFolder); replacePattern = Utils.ReplaceSpecialCharacters(replacePattern); int processedFiles = 0; Utils.CancelSearch = false; try { foreach (string file in files) { if (!file.Contains(baseFolder)) { continue; } string tempFileName = file.Replace(baseFolder, tempFolder); IGrepEngine engine = GrepEngineFactory.GetReplaceEngine(file, searchParams); try { processedFiles++; // Copy file Utils.CopyFile(file, tempFileName, true); Utils.DeleteFile(file); Encoding encoding = null; if (codePage == -1) { encoding = Utils.GetFileEncoding(tempFileName); } else { encoding = Encoding.GetEncoding(codePage); } if (Utils.CancelSearch) { break; } if (!engine.Replace(tempFileName, file, searchPattern, replacePattern, searchType, searchOptions, encoding)) { throw new ApplicationException("Replace failed for file: " + file); } if (!Utils.CancelSearch && ProcessedFile != null) { ProcessedFile(this, new ProgressStatus(processedFiles, null)); } File.SetAttributes(file, File.GetAttributes(tempFileName)); if (Utils.CancelSearch) { // Replace the file Utils.DeleteFile(file); Utils.CopyFile(tempFileName, file, true); break; } } catch (Exception ex) { logger.LogException(LogLevel.Error, ex.Message, ex); try { // Replace the file if (File.Exists(tempFileName) && File.Exists(file)) { Utils.DeleteFile(file); Utils.CopyFile(tempFileName, file, true); } } catch { // DO NOTHING } return(-1); } } } finally { GrepEngineFactory.UnloadEngines(); } return(processedFiles); }
/// <summary> /// Searches folder for files whose content matches regex /// </summary> /// <param name="files">Files to search in. If one of the files does not exist or is open, it is skipped.</param> /// <param name="searchRegex">Regex pattern</param> /// <returns>List of results. If nothing is found returns empty list</returns> public List <GrepSearchResult> Search(IEnumerable <string> files, SearchType searchType, string searchPattern, GrepSearchOption searchOptions, int codePage) { searchResults.Clear(); if (files == null) { return(searchResults); } Utils.CancelSearch = false; if (searchPattern == null || searchPattern.Trim() == "") { int count = 0; foreach (string file in files) { count++; ProcessedFile(this, new ProgressStatus(true, searchResults.Count, count, null, file)); Encoding encoding = Encoding.Default; if (codePage > -1) { encoding = Encoding.GetEncoding(codePage); } try { if (GrepSettings.Instance.Get <bool>(GrepSettings.Key.DetectEncodingForFileNamePattern)) { if (codePage == -1 && !Utils.IsArchive(file) && !Utils.IsBinary(file) && !Utils.IsPdfFile(file)) { encoding = Utils.GetFileEncoding(file); } } searchResults.Add(new GrepSearchResult(file, searchPattern, null, encoding)); } catch (Exception ex) { // will catch file not found errors (Everything search): logger.Error(ex, "Failed in File Search"); AddSearchResult(new GrepSearchResult(file, searchPattern, ex.Message, false)); } if (searchOptions.HasFlag(GrepSearchOption.StopAfterFirstMatch)) { break; } if (Utils.CancelSearch) { break; } } ProcessedFile(this, new ProgressStatus(false, searchResults.Count, count, searchResults, null)); return(new List <GrepSearchResult>(searchResults)); } else { processedFilesCount = 0; foundfilesCount = 0; try { if (SearchParams.SearchParallel) { cancellationTokenSource = new CancellationTokenSource(); ParallelOptions po = new ParallelOptions { MaxDegreeOfParallelism = Math.Max(1, Environment.ProcessorCount * 4 / 5), CancellationToken = cancellationTokenSource.Token }; Parallel.ForEach(files, po, f => Search(f, searchType, searchPattern, searchOptions, codePage)); } else { foreach (var file in files) { Search(file, searchType, searchPattern, searchOptions, codePage); if (searchOptions.HasFlag(GrepSearchOption.StopAfterFirstMatch) && searchResults.Count > 0) { break; } if (Utils.CancelSearch) { break; } } } } catch (OperationCanceledException) { // expected for stop after first match or user cancel } catch (Exception ex) { logger.Error(ex, "Failed in search in files"); } finally { if (cancellationTokenSource != null) { cancellationTokenSource.Dispose(); cancellationTokenSource = null; } GrepEngineFactory.UnloadEngines(); } return(new List <GrepSearchResult>(searchResults)); } }
public List <GrepSearchResult> Search(string file, string searchPattern, SearchType searchType, GrepSearchOption searchOptions, Encoding encoding) { List <GrepSearchResult> searchResults = new List <GrepSearchResult>(); SevenZipExtractor extractor = new SevenZipExtractor(file); string tempFolder = Utils.FixFolderName(Utils.GetTempFolder()) + "dnGREP-Archive\\" + Utils.GetHash(file) + "\\"; FileFilter filter = FileFilter.ChangePath(tempFolder); // if the search pattern(s) only match archive files, need to include an 'any' file type to search inside the archive. // otherwise, keep the original pattern set so the user can specify what types of files to search inside the archive. var patterns = Utils.SplitPath(FileFilter.NamePatternToInclude).ToList(); bool hasNonArchivePattern = patterns.Where(p => !Utils.IsArchiveExtension(Path.GetExtension(p))).Any(); if (!hasNonArchivePattern) { patterns.Add(FileFilter.IsRegex ? ".*" : "*.*"); filter = filter.ChangeIncludePattern(string.Join(";", patterns.ToArray())); } if (Directory.Exists(tempFolder)) { Utils.DeleteFolder(tempFolder); } Directory.CreateDirectory(tempFolder); try { extractor.ExtractArchive(tempFolder); foreach (var innerFileName in Utils.GetFileListEx(filter)) { IGrepEngine engine = GrepEngineFactory.GetSearchEngine(innerFileName, initParams, FileFilter); var innerFileResults = engine.Search(innerFileName, searchPattern, searchType, searchOptions, encoding); if (innerFileResults.Count > 0) { using (FileStream reader = File.Open(innerFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (StreamReader streamReader = new StreamReader(reader)) { foreach (var result in innerFileResults) { if (Utils.CancelSearch) { break; } if (!result.HasSearchResults) { result.SearchResults = Utils.GetLinesEx(streamReader, result.Matches, initParams.LinesBefore, initParams.LinesAfter); } } } searchResults.AddRange(innerFileResults); } if (Utils.CancelSearch) { break; } } foreach (GrepSearchResult result in searchResults) { result.FileNameDisplayed = file + "\\" + result.FileNameDisplayed.Substring(tempFolder.Length); result.FileNameReal = file; result.ReadOnly = true; } } catch (Exception ex) { logger.Log <Exception>(LogLevel.Error, string.Format("Failed to search inside archive '{0}'", file), ex); } return(searchResults); }
/// <summary> /// Searches folder for files whose content matches regex /// </summary> /// <param name="files">Files to search in. If one of the files does not exist or is open, it is skipped.</param> /// <param name="searchRegex">Regex pattern</param> /// <returns>List of results. If nothing is found returns empty list</returns> public List <GrepSearchResult> Search(IEnumerable <string> files, SearchType searchType, string searchPattern, GrepSearchOption searchOptions, int codePage) { List <GrepSearchResult> searchResults = new List <GrepSearchResult>(); if (files == null) { return(searchResults); } Utils.CancelSearch = false; if (searchPattern == null || searchPattern.Trim() == "") { foreach (string file in files) { searchResults.Add(new GrepSearchResult(file, searchPattern, null)); if ((searchOptions & GrepSearchOption.StopAfterFirstMatch) == GrepSearchOption.StopAfterFirstMatch) { break; } } if (ProcessedFile != null) { ProcessedFile(this, new ProgressStatus(searchResults.Count, searchResults)); } return(searchResults); } else { int processedFiles = 0; try { foreach (string file in files) { try { IGrepEngine engine = GrepEngineFactory.GetSearchEngine(file, SearchParams); processedFiles++; Encoding encoding = null; if (codePage == -1) { encoding = Utils.GetFileEncoding(file); } else { encoding = Encoding.GetEncoding(codePage); } if (Utils.CancelSearch) { return(searchResults); } List <GrepSearchResult> fileSearchResults = engine.Search(file, searchPattern, searchType, searchOptions, encoding); if (fileSearchResults != null && fileSearchResults.Count > 0) { searchResults.AddRange(fileSearchResults); } if (ProcessedFile != null) { ProcessedFile(this, new ProgressStatus(processedFiles, fileSearchResults)); } } catch (Exception ex) { logger.LogException(LogLevel.Error, ex.Message, ex); searchResults.Add(new GrepSearchResult(file, searchPattern, ex.Message, false)); if (ProcessedFile != null) { List <GrepSearchResult> _results = new List <GrepSearchResult>(); _results.Add(new GrepSearchResult(file, searchPattern, ex.Message, false)); ProcessedFile(this, new ProgressStatus(processedFiles, _results)); } } if ((searchOptions & GrepSearchOption.StopAfterFirstMatch) == GrepSearchOption.StopAfterFirstMatch && searchResults.Count > 0) { break; } } } finally { GrepEngineFactory.UnloadEngines(); } return(searchResults); } }
private void SaveSettings() { if (EnableWindowsIntegration) { ShellRegister("Directory"); ShellRegister("Drive"); ShellRegister("*"); ShellRegister("here"); } else { ShellUnregister("Directory"); ShellUnregister("Drive"); ShellUnregister("*"); ShellUnregister("here"); } if (EnableStartupAcceleration) { StartupRegister(); } else { StartupUnregister(); } ApplicationFontFamily = EditApplicationFontFamily; MainFormFontSize = EditMainFormFontSize; ReplaceFormFontSize = EditReplaceFormFontSize; DialogFontSize = EditDialogFontSize; Settings.Set(GrepSettings.Key.EnableUpdateChecking, EnableCheckForUpdates); Settings.Set(GrepSettings.Key.UpdateCheckInterval, CheckForUpdatesInterval); Settings.Set(GrepSettings.Key.CustomEditor, CustomEditorPath); Settings.Set(GrepSettings.Key.CustomEditorArgs, CustomEditorArgs); Settings.Set(GrepSettings.Key.CompareApplication, CompareApplicationPath); Settings.Set(GrepSettings.Key.CompareApplicationArgs, CompareApplicationArgs); Settings.Set(GrepSettings.Key.ShowFilePathInResults, ShowFilePathInResults); Settings.Set(GrepSettings.Key.AllowSearchingForFileNamePattern, AllowSearchWithEmptyPattern); Settings.Set(GrepSettings.Key.DetectEncodingForFileNamePattern, DetectEncodingForFileNamePattern); Settings.Set(GrepSettings.Key.ExpandResults, AutoExpandSearchTree); Settings.Set(GrepSettings.Key.ShowVerboseMatchCount, ShowVerboseMatchCount); Settings.Set(GrepSettings.Key.ShowFileInfoTooltips, ShowFileInfoTooltips); Settings.Set(GrepSettings.Key.MatchTimeout, MatchTimeout); Settings.Set(GrepSettings.Key.FuzzyMatchThreshold, MatchThreshold); Settings.Set(GrepSettings.Key.ShowLinesInContext, ShowLinesInContext); Settings.Set(GrepSettings.Key.ContextLinesBefore, ContextLinesBefore); Settings.Set(GrepSettings.Key.ContextLinesAfter, ContextLinesAfter); Settings.Set(GrepSettings.Key.MaxSearchBookmarks, MaxSearchBookmarks); Settings.Set(GrepSettings.Key.MaxPathBookmarks, MaxPathBookmarks); Settings.Set(GrepSettings.Key.MaxExtensionBookmarks, MaxExtensionBookmarks); Settings.Set(GrepSettings.Key.OptionsOnMainPanel, OptionsLocation == PanelSelection.MainPanel); Settings.Set(GrepSettings.Key.FollowWindowsTheme, FollowWindowsTheme); Settings.Set(GrepSettings.Key.CurrentTheme, CurrentTheme); Settings.Set(GrepSettings.Key.UseDefaultFont, UseDefaultFont); Settings.Set(GrepSettings.Key.ApplicationFontFamily, ApplicationFontFamily); Settings.Set(GrepSettings.Key.MainFormFontSize, MainFormFontSize); Settings.Set(GrepSettings.Key.ReplaceFormFontSize, ReplaceFormFontSize); Settings.Set(GrepSettings.Key.DialogFontSize, DialogFontSize); Settings.Set(GrepSettings.Key.PdfToTextOptions, PdfToTextOptions); if (ArchiveOptions.IsChanged) { string nameKey = "Archive"; string addKey = "Add" + nameKey + "Extensions"; string remKey = "Rem" + nameKey + "Extensions"; Settings.Set(addKey, CleanExtensions(ArchiveOptions.AddExtensions)); Settings.Set(remKey, CleanExtensions(ArchiveOptions.RemExtensions)); ArchiveOptions.SetUnchanged(); ArchiveDirectory.Reinitialize(); } bool pluginsChanged = Plugins.Any(p => p.IsChanged); foreach (var plugin in Plugins) { string nameKey = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(plugin.Name); string enabledkey = nameKey + "Enabled"; string addKey = "Add" + nameKey + "Extensions"; string remKey = "Rem" + nameKey + "Extensions"; Settings.Set(enabledkey, plugin.IsEnabled); Settings.Set(addKey, CleanExtensions(plugin.AddExtensions)); Settings.Set(remKey, CleanExtensions(plugin.RemExtensions)); plugin.SetUnchanged(); } Settings.Save(); if (pluginsChanged) { GrepEngineFactory.ReloadPlugins(); } }