private void SearchExcel(Stream stream, string file, string searchPattern, GrepSearchOption searchOptions, SearchDelegates.DoSearch searchMethod, List <GrepSearchResult> searchResults) { try { var sheets = ExcelReader.ExtractExcelText(stream); foreach (var kvPair in sheets) { var lines = searchMethod(-1, 0, kvPair.Value, searchPattern, searchOptions, true); if (lines.Count > 0) { GrepSearchResult result = new GrepSearchResult(file, searchPattern, lines, Encoding.Default) { AdditionalInformation = " " + TranslationSource.Format(Resources.Main_ExcelSheetName, kvPair.Key) }; using (StringReader reader = new StringReader(kvPair.Value)) { result.SearchResults = Utils.GetLinesEx(reader, result.Matches, initParams.LinesBefore, initParams.LinesAfter); } result.ReadOnly = true; searchResults.Add(result); } } } catch (Exception ex) { logger.Error(ex, string.Format("Failed to search inside Excel file '{0}'", file)); } }
private IEnumerable <GrepMatch> RegexSearchIterator(int lineNumber, int filePosition, string text, string searchPattern, GrepSearchOption searchOptions) { RegexOptions regexOptions = RegexOptions.None; if (!searchOptions.HasFlag(GrepSearchOption.CaseSensitive)) { regexOptions |= RegexOptions.IgnoreCase; } if (searchOptions.HasFlag(GrepSearchOption.Multiline)) { regexOptions |= RegexOptions.Multiline; } if (searchOptions.HasFlag(GrepSearchOption.SingleLine)) { regexOptions |= RegexOptions.Singleline; } bool isWholeWord = searchOptions.HasFlag(GrepSearchOption.WholeWord); if (searchOptions.HasFlag(GrepSearchOption.BooleanOperators)) { BooleanExpression exp = new BooleanExpression(); if (exp.TryParse(searchPattern)) { return(RegexSearchIteratorBoolean(lineNumber, filePosition, text, exp, isWholeWord, regexOptions)); } } return(RegexSearchIterator(lineNumber, filePosition, text, searchPattern, isWholeWord, regexOptions)); }
protected string DoTextReplace(int lineNumber, int filePosition, string text, string searchText, string replaceText, GrepSearchOption searchOptions, IEnumerable <GrepMatch> replaceItems) { if (lineNumber > -1 && !replaceItems.Any(r => r.LineNumber == lineNumber && r.ReplaceMatch)) { return(text); } StringBuilder sb = new StringBuilder(); int counter = 0; foreach (var match in TextSearchIterator(lineNumber, filePosition, text, searchText, searchOptions)) { if (replaceItems.Any(r => match.Equals(r) && r.ReplaceMatch)) { sb.Append(text.Substring(counter, match.StartLocation - filePosition - counter)); sb.Append(DoPatternReplacement(searchText, replaceText)); counter = match.StartLocation - filePosition + match.Length; } if (Utils.CancelSearch) { break; } } sb.Append(text.Substring(counter)); return(sb.ToString()); }
public bool Replace(Stream readStream, Stream writeStream, string searchPattern, string replacePattern, SearchType searchType, GrepSearchOption searchOptions, Encoding encoding) { SearchDelegates.DoReplace replaceMethod = doTextReplaceCaseSensitive; switch (searchType) { case SearchType.PlainText: if ((searchOptions & GrepSearchOption.CaseSensitive) == GrepSearchOption.CaseSensitive) { replaceMethod = doTextReplaceCaseSensitive; } else { replaceMethod = doTextReplaceCaseInsensitive; } break; case SearchType.Regex: replaceMethod = doRegexReplace; break; case SearchType.XPath: replaceMethod = doXPathReplace; break; case SearchType.Soundex: replaceMethod = doFuzzyReplace; break; } if ((searchOptions & GrepSearchOption.Multiline) == GrepSearchOption.Multiline) return replaceMultiline(readStream, writeStream, searchPattern, replacePattern, searchOptions, replaceMethod, encoding); else return replace(readStream, writeStream, searchPattern, replacePattern, searchOptions, replaceMethod, encoding); }
public bool Replace(Stream readStream, Stream writeStream, string searchPattern, string replacePattern, SearchType searchType, GrepSearchOption searchOptions, Encoding encoding, IEnumerable <GrepMatch> replaceItems) { SearchDelegates.DoReplace replaceMethod = DoTextReplace; switch (searchType) { case SearchType.PlainText: replaceMethod = DoTextReplace; break; case SearchType.Regex: replaceMethod = DoRegexReplace; break; case SearchType.XPath: replaceMethod = DoXPathReplace; break; case SearchType.Soundex: replaceMethod = DoFuzzyReplace; break; } if (searchOptions.HasFlag(GrepSearchOption.Multiline) || searchType == SearchType.XPath) { return(ReplaceMultiline(readStream, writeStream, searchPattern, replacePattern, searchOptions, replaceMethod, encoding, replaceItems)); } else { return(Replace(readStream, writeStream, searchPattern, replacePattern, searchOptions, replaceMethod, encoding, replaceItems)); } }
public void TestReplaceOnFileWithout_UTF8_BOM(SearchType type, GrepSearchOption option, string searchFor, string replaceWith) { // Test for Issue #227 Utils.CopyFiles(sourceFolder + "\\TestCase15", destinationFolder + "\\TestCase15", null, null); GrepCore core = new GrepCore(); List <GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase15", "books.xml"), type, searchFor, option, -1); Assert.Equal(1, results.Count); string testFile = Path.Combine(destinationFolder, @"TestCase15\books.xml"); Dictionary <string, string> files = new Dictionary <string, string> { { testFile, Guid.NewGuid().ToString() + ".xml" } }; core.Replace(files, type, searchFor, replaceWith, option, -1); using (StreamReader reader = new StreamReader(testFile, true)) { Assert.Equal(Encoding.UTF8, reader.CurrentEncoding); // check there is no BOM int bb = reader.BaseStream.ReadByte(); Assert.NotEqual(0xEF, bb); Assert.Equal('<', bb); bb = reader.BaseStream.ReadByte(); Assert.NotEqual(0xBB, bb); bb = reader.BaseStream.ReadByte(); Assert.NotEqual(0xBF, bb); } var fileContent = File.ReadAllLines(destinationFolder + "\\TestCase15\\books.xml", Encoding.UTF8); Assert.Equal(38, fileContent.Length); Assert.Equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", fileContent[0]); Assert.Equal(" <book category=\"general\">", fileContent[8]); }
public List<GrepSearchResult> Search(Stream input, string fileName, string searchPattern, SearchType searchType, GrepSearchOption searchOptions, Encoding encoding) { SearchDelegates.DoSearch searchMethod = doTextSearchCaseSensitive; switch (searchType) { case SearchType.PlainText: if ((searchOptions & GrepSearchOption.CaseSensitive) == GrepSearchOption.CaseSensitive) { searchMethod = doTextSearchCaseSensitive; } else { searchMethod = doTextSearchCaseInsensitive; } break; case SearchType.Regex: searchMethod = doRegexSearch; break; case SearchType.XPath: searchMethod = doXPathSearch; break; case SearchType.Soundex: searchMethod = doFuzzySearchMultiline; break; } if ((searchOptions & GrepSearchOption.Multiline) == GrepSearchOption.Multiline || searchType == SearchType.XPath) return searchMultiline(input, fileName, searchPattern, searchOptions, searchMethod, encoding); else return search(input, fileName, searchPattern, searchOptions, searchMethod, encoding); }
protected List<GrepSearchResult.GrepMatch> doFuzzySearchMultiline(string text, string searchPattern, GrepSearchOption searchOptions, bool includeContext) { int counter = 0; fuzzyMatchEngine.Match_Threshold = (float)fuzzyMatchThreshold; bool isWholeWord = (searchOptions & GrepSearchOption.WholeWord) == GrepSearchOption.WholeWord; List<GrepSearchResult.GrepMatch> globalMatches = new List<GrepSearchResult.GrepMatch>(); while (counter < text.Length) { int matchLocation = fuzzyMatchEngine.match_main(text.Substring(counter), searchPattern, counter); if (matchLocation == -1) break; if (isWholeWord && !Utils.IsValidBeginText(text.Substring(counter).Substring(0, matchLocation))) { counter = counter + matchLocation + searchPattern.Length; continue; } int matchLength = fuzzyMatchEngine.match_length(text.Substring(counter), searchPattern, matchLocation, isWholeWord, fuzzyMatchThreshold); if (matchLength == -1) { counter = counter + matchLocation + searchPattern.Length; continue; } globalMatches.Add(new GrepSearchResult.GrepMatch(0, matchLocation + counter, matchLength)); counter = counter + matchLocation + matchLength; } return globalMatches; }
public List<GrepSearchResult> Search(string file, string searchPattern, SearchType searchType, GrepSearchOption searchOptions, Encoding encoding) { using (FileStream fileStream = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { return Search(fileStream, file, searchPattern, searchType, searchOptions, encoding); } }
public void TestReplaceOnFileWith_UTF8_BOM(SearchType type, GrepSearchOption option, string searchFor, string replaceWith) { // Test for Issue #227 dnGrep inserting extra BOM, and scrambling file Utils.CopyFiles(sourceFolder + "\\TestCase15", destinationFolder + "\\TestCase15", null, null); GrepCore core = new GrepCore(); List <GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase15", "books_bom.xml"), type, searchFor, option, -1); Assert.Equal(1, results.Count); core.Replace(Directory.GetFiles(destinationFolder + "\\TestCase15", "books_bom.xml"), type, searchFor, replaceWith, option, -1); using (StreamReader reader = new StreamReader(destinationFolder + "\\TestCase15\\books_bom.xml", true)) { Assert.Equal(Encoding.UTF8, reader.CurrentEncoding); // check there is a BOM int bb = reader.BaseStream.ReadByte(); Assert.Equal(0xEF, bb); bb = reader.BaseStream.ReadByte(); Assert.Equal(0xBB, bb); bb = reader.BaseStream.ReadByte(); Assert.Equal(0xBF, bb); // check that there are not two BOMs bb = reader.BaseStream.ReadByte(); Assert.NotEqual(0xEF, bb); Assert.Equal('<', bb); } var fileContent = File.ReadAllLines(destinationFolder + "\\TestCase15\\books_bom.xml", Encoding.UTF8); Assert.Equal(38, fileContent.Length); string line1 = fileContent[0].Replace("\ufeff", ""); // remove BOM Assert.Equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", line1); Assert.Equal(" <book category=\"general\">", fileContent[8]); }
public void TestSearchAndFilteredReplace(SearchType type, GrepSearchOption option, string searchFor, string replaceWith) { Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase15"), Path.Combine(destinationFolder, "TestCase15"), null, null); GrepCore core = new GrepCore(); List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destinationFolder, "TestCase15"), "books.xml"), type, searchFor, option, -1); Assert.Single(results); Assert.Equal(2, results[0].Matches.Count); // mark only the second match for replace results[0].Matches[1].ReplaceMatch = true; string testFile = Path.Combine(destinationFolder, "TestCase15", "books.xml"); List <ReplaceDef> files = new List <ReplaceDef> { new ReplaceDef(testFile, results[0].Matches) }; core.Replace(files, type, searchFor, replaceWith, option, -1); var fileContent = File.ReadAllText(testFile, Encoding.UTF8); Assert.Contains("<year>2003</year>", fileContent); Assert.Single(Regex.Matches(fileContent, "2002")); Assert.Single(Regex.Matches(fileContent, "2003")); }
public bool Replace(string sourceFile, string destinationFile, string searchPattern, string replacePattern, SearchType searchType, GrepSearchOption searchOptions, Encoding encoding) { using (FileStream readStream = File.Open(sourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (FileStream writeStream = File.OpenWrite(destinationFile)) { return Replace(readStream, writeStream, searchPattern, replacePattern, searchType, searchOptions, encoding); } }
public List <GrepSearchResult> ListFiles(IEnumerable <FileData> files, GrepSearchOption searchOptions, int codePage) { searchResults.Clear(); if (files == null) { return(searchResults); } int successful = 0; foreach (FileData fileInfo in files) { ProcessedFile(this, new ProgressStatus(true, searchResults.Count, successful, null, fileInfo.FullName)); Encoding encoding = Encoding.Default; if (codePage > -1) { encoding = Encoding.GetEncoding(codePage); } try { if (GrepSettings.Instance.Get <bool>(GrepSettings.Key.DetectEncodingForFileNamePattern)) { if (codePage == -1 && !fileInfo.FullName.Contains(ArchiveDirectory.ArchiveSeparator) && !Utils.IsArchive(fileInfo.FullName) && !Utils.IsBinary(fileInfo.FullName) && !Utils.IsPdfFile(fileInfo.FullName)) { encoding = Utils.GetFileEncoding(fileInfo.FullName); } } searchResults.Add(new GrepSearchResult(fileInfo, encoding)); successful++; } catch (Exception ex) { // will catch file not found errors (Everything search): logger.Error(ex, "Failed in ListFiles"); fileInfo.ErrorMsg = ex.Message; AddSearchResult(new GrepSearchResult(fileInfo, encoding)); } if (searchOptions.HasFlag(GrepSearchOption.StopAfterFirstMatch)) { break; } if (Utils.CancelSearch) { break; } } ProcessedFile(this, new ProgressStatus(false, searchResults.Count, successful, searchResults, null)); return(new List <GrepSearchResult>(searchResults)); }
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 bool Replace(string sourceFile, string destinationFile, string searchPattern, string replacePattern, SearchType searchType, GrepSearchOption searchOptions, Encoding encoding, IEnumerable <GrepMatch> replaceItems) { using (FileStream readStream = File.Open(sourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (FileStream writeStream = File.OpenWrite(destinationFile)) { return(Replace(readStream, writeStream, searchPattern, replacePattern, searchType, searchOptions, encoding, replaceItems)); } }
private void Replace() { GrepEnginePlainText engine = new GrepEnginePlainText(); engine.Initialize(new GrepEngineInitParams( GrepSettings.Instance.Get <bool>(GrepSettings.Key.ShowLinesInContext), GrepSettings.Instance.Get <int>(GrepSettings.Key.ContextLinesBefore), GrepSettings.Instance.Get <int>(GrepSettings.Key.ContextLinesAfter), GrepSettings.Instance.Get <double>(GrepSettings.Key.FuzzyMatchThreshold), GrepSettings.Instance.Get <bool>(GrepSettings.Key.ShowVerboseMatchCount), false), new FileFilter()); List <GrepSearchResult> results = new List <GrepSearchResult>(); GrepSearchOption searchOptions = GrepSearchOption.None; if (Multiline) { searchOptions |= GrepSearchOption.Multiline; } if (CaseSensitive) { searchOptions |= GrepSearchOption.CaseSensitive; } if (Singleline) { searchOptions |= GrepSearchOption.SingleLine; } if (WholeWord) { searchOptions |= GrepSearchOption.WholeWord; } string replacedString = ""; using (Stream inputStream = new MemoryStream(Encoding.Default.GetBytes(SampleText))) using (Stream writeStream = new MemoryStream()) { engine.Replace(inputStream, writeStream, SearchFor, ReplaceWith, TypeOfSearch, searchOptions, Encoding.Default); writeStream.Position = 0; StreamReader reader = new StreamReader(writeStream); replacedString = reader.ReadToEnd(); } SearchResults.Clear(); SearchResults.AddRange(results); Paragraph paragraph = new Paragraph(); paragraph.Inlines.Add(new Run(replacedString)); TestOutput = paragraph.Inlines; TestOutputText = replacedString; }
public void TestReplaceOnFileWith_UTF8_BOM(SearchType type, GrepSearchOption option, string searchFor, string replaceWith) { // Test for Issue #227 dnGrep inserting extra BOM, and scrambling file Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase15"), Path.Combine(destinationFolder, "TestCase15"), null, null); GrepCore core = new GrepCore(); List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destinationFolder, "TestCase15"), "books_bom.xml"), type, searchFor, option, -1); Assert.Single(results); // mark all matches for replace foreach (var match in results[0].Matches) { match.ReplaceMatch = true; } string testFile = Path.Combine(destinationFolder, "TestCase15", "books_bom.xml"); List <ReplaceDef> files = new List <ReplaceDef> { new ReplaceDef(testFile, results[0].Matches) }; core.Replace(files, type, searchFor, replaceWith, option, -1); using (FileStream stream = File.Open(testFile, FileMode.Open, FileAccess.Read, FileShare.Read)) using (StreamReader reader = new StreamReader(stream, true)) { Assert.Equal(Encoding.UTF8, reader.CurrentEncoding); // check there is a BOM int bb = reader.BaseStream.ReadByte(); Assert.Equal(0xEF, bb); bb = reader.BaseStream.ReadByte(); Assert.Equal(0xBB, bb); bb = reader.BaseStream.ReadByte(); Assert.Equal(0xBF, bb); // check that there are not two BOMs bb = reader.BaseStream.ReadByte(); Assert.NotEqual(0xEF, bb); Assert.Equal('<', bb); } var fileContent = File.ReadAllLines(Path.Combine(destinationFolder, "TestCase15", "books_bom.xml"), Encoding.UTF8); Assert.Equal(38, fileContent.Length); string line1 = fileContent[0].Replace("\ufeff", ""); // remove BOM Assert.Equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", line1); Assert.Equal(" <book category=\"general\">", fileContent[8]); }
protected List <GrepMatch> DoRegexSearch(int lineNumber, int filePosition, string text, string searchPattern, GrepSearchOption searchOptions, bool includeContext) { List <GrepMatch> globalMatches = new List <GrepMatch>(); foreach (var match in RegexSearchIterator(lineNumber, filePosition, text, searchPattern, searchOptions)) { globalMatches.Add(match); if (Utils.CancelSearch) { break; } } return(globalMatches); }
public void TestSearchLongLineWithManyMatches(SearchType type, GrepSearchOption option, bool verbose) { Utils.CopyFiles(sourceFolder + "\\TestCase14", destinationFolder + "\\TestCase14", null, null); GrepCore core = new GrepCore(); core.SearchParams = new GrepEngineInitParams(false, 0, 0, 0.5, verbose); Stopwatch sw = new Stopwatch(); sw.Start(); List <GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase14", "*.txt"), type, "1234", option, -1); sw.Stop(); Assert.Equal(2, results.Count); Assert.Equal(102456, results[0].Matches.Count); Assert.Equal(102456, results[1].Matches.Count); Assert.True(sw.Elapsed < TimeSpan.FromSeconds(1.25)); }
protected List<GrepSearchResult.GrepMatch> doXPathSearch(string text, string searchXPath, GrepSearchOption searchOptions, bool includeContext) { List<GrepSearchResult.GrepMatch> results = new List<GrepSearchResult.GrepMatch>(); // Check if file is an XML file if (text.Length > 5 && text.Substring(0, 5).ToLower() == "<?xml") { List<XPathPosition> positions = new List<XPathPosition>(); using (StringReader reader = new StringReader(text)) { // Code from http://stackoverflow.com/questions/10606534/how-to-search-xml-files-with-xpath-returning-line-and-column-numbers-of-found XPathDocument xmlDoc = new XPathDocument(reader); var xpn = xmlDoc.CreateNavigator(); xpn.MoveToFollowing(XPathNodeType.Element); var xns = xpn.GetNamespacesInScope(XmlNamespaceScope.All); var xmngr = new XmlNamespaceManager(xpn.NameTable); foreach (var key in xns.Keys) xmngr.AddNamespace(key, xns[key]); var xpni = xpn.Select(searchXPath, xmngr); int foundCounter = 0; while (xpni.MoveNext()) { foundCounter++; var xn = xpni.Current; var xpathPositions = new XPathPosition(); if (xn.NodeType == System.Xml.XPath.XPathNodeType.Attribute) { xpathPositions.EndsOnAttribute = true; xpathPositions.AttributeName = xn.Name; } List<int> currentPositions = new List<int>(); getXpathPositions(xn, ref currentPositions); if (xpathPositions.EndsOnAttribute) currentPositions.RemoveAt(currentPositions.Count - 1); xpathPositions.Path = currentPositions; positions.Add(xpathPositions); } } results.AddRange(getFilePositions(text, positions)); } return results; }
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; }
private IEnumerable <GrepMatch> RegexSearchIterator(int lineNumber, int filePosition, string text, string searchPattern, GrepSearchOption searchOptions) { RegexOptions regexOptions = RegexOptions.None; if (!searchOptions.HasFlag(GrepSearchOption.CaseSensitive)) { regexOptions |= RegexOptions.IgnoreCase; } if (searchOptions.HasFlag(GrepSearchOption.Multiline)) { regexOptions |= RegexOptions.Multiline; } if (searchOptions.HasFlag(GrepSearchOption.SingleLine)) { regexOptions |= RegexOptions.Singleline; } bool isWholeWord = searchOptions.HasFlag(GrepSearchOption.WholeWord); if (searchOptions.HasFlag(GrepSearchOption.BooleanOperators)) { Utils.ParseBooleanOperators(searchPattern, out List <string> andClauses, out List <string> orClauses); if (andClauses != null && andClauses.Count > 1) { return(RegexSearchIteratorAnd(lineNumber, filePosition, text, andClauses, isWholeWord, regexOptions)); } if (orClauses != null && orClauses.Count > 1) { return(RegexSearchIteratorOr(lineNumber, filePosition, text, orClauses, isWholeWord, regexOptions)); } } return(RegexSearchIterator(lineNumber, filePosition, text, searchPattern, isWholeWord, regexOptions)); }
private void SearchWord(Stream stream, string file, string searchPattern, GrepSearchOption searchOptions, SearchDelegates.DoSearch searchMethod, List <GrepSearchResult> searchResults) { try { var text = WordReader.ExtractWordText(stream); var lines = searchMethod(-1, 0, text, searchPattern, searchOptions, true); if (lines.Count > 0) { GrepSearchResult result = new GrepSearchResult(file, searchPattern, lines, Encoding.Default); using (StringReader reader = new StringReader(text)) { result.SearchResults = Utils.GetLinesEx(reader, result.Matches, initParams.LinesBefore, initParams.LinesAfter); } result.ReadOnly = true; searchResults.Add(result); } } catch (Exception ex) { logger.Error(ex, string.Format("Failed to search inside Word file '{0}'", file)); } }
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 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); }
private void SearchPowerPoint(Stream stream, string file, string searchPattern, GrepSearchOption searchOptions, SearchDelegates.DoSearch searchMethod, List <GrepSearchResult> searchResults) { try { var slides = PowerPointReader.ExtractPowerPointText(stream); foreach (var slide in slides) { var lines = searchMethod(-1, 0, slide.Item2, searchPattern, searchOptions, true); if (lines.Count > 0) { GrepSearchResult result = new GrepSearchResult(file, searchPattern, lines, Encoding.Default) { AdditionalInformation = " " + TranslationSource.Format(Resources.Main_PowerPointSlideNumber, slide.Item1) }; using (StringReader reader = new StringReader(slide.Item2)) { result.SearchResults = Utils.GetLinesEx(reader, result.Matches, initParams.LinesBefore, initParams.LinesAfter); } result.ReadOnly = true; searchResults.Add(result); } } } catch (Exception ex) { logger.Error(ex, string.Format("Failed to search inside PowerPoint file '{0}'", file)); } }
public List <GrepSearchResult> Search(string file, string searchPattern, SearchType searchType, GrepSearchOption searchOptions, Encoding encoding) { load(); SearchDelegates.DoSearch searchMethodMultiline = doTextSearchCaseSensitive; switch (searchType) { case SearchType.PlainText: case SearchType.XPath: if ((searchOptions & GrepSearchOption.CaseSensitive) == GrepSearchOption.CaseSensitive) { searchMethodMultiline = doTextSearchCaseSensitive; } else { searchMethodMultiline = doTextSearchCaseInsensitive; } break; case SearchType.Regex: searchMethodMultiline = doRegexSearch; break; case SearchType.Soundex: searchMethodMultiline = doFuzzySearchMultiline; break; } List <GrepSearchResult> result = searchMultiline(file, searchPattern, searchOptions, searchMethodMultiline); return(result); }
public bool Replace(string sourceFile, string destinationFile, string searchPattern, string replacePattern, SearchType searchType, GrepSearchOption searchOptions, Encoding encoding, IEnumerable <GrepMatch> replaceItems) { throw new Exception("The method or operation is not implemented."); }
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(); } } } }
/// <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 string doFuzzyReplace(string text, string searchPattern, string replacePattern, GrepSearchOption searchOptions) { int counter = 0; StringBuilder result = new StringBuilder(); fuzzyMatchEngine.Match_Threshold = (float)fuzzyMatchThreshold; bool isWholeWord = (searchOptions & GrepSearchOption.WholeWord) == GrepSearchOption.WholeWord; while (counter < text.Length) { int matchLocation = fuzzyMatchEngine.match_main(text.Substring(counter), searchPattern, counter); if (matchLocation == -1) { result.Append(text.Substring(counter)); break; } if (isWholeWord && !Utils.IsValidBeginText(text.Substring(counter).Substring(0, matchLocation + counter))) { result.Append(text.Substring(counter)); counter = counter + matchLocation + searchPattern.Length; continue; } int matchLength = fuzzyMatchEngine.match_length(text.Substring(counter), searchPattern, matchLocation, isWholeWord, fuzzyMatchThreshold); if (matchLength == -1) { result.Append(text.Substring(counter)); counter = counter + matchLocation + searchPattern.Length; continue; } // Text before match result.Append(text.Substring(counter, matchLocation)); // New text result.Append(doPatternReplacement(replacePattern)); counter = counter + matchLocation + matchLength; } return result.ToString(); }
private List <GrepSearchResult> SearchMultiline(Stream input, string file, string searchPattern, GrepSearchOption searchOptions, SearchDelegates.DoSearch searchMethod) { List <GrepSearchResult> searchResults = new List <GrepSearchResult>(); string ext = Path.GetExtension(file); if (ext.StartsWith(".doc", StringComparison.OrdinalIgnoreCase)) { SearchWord(input, file, searchPattern, searchOptions, searchMethod, searchResults); } else if (ext.StartsWith(".xls", StringComparison.OrdinalIgnoreCase)) { SearchExcel(input, file, searchPattern, searchOptions, searchMethod, searchResults); } else if (ext.StartsWith(".ppt", StringComparison.OrdinalIgnoreCase)) { SearchPowerPoint(input, file, searchPattern, searchOptions, searchMethod, searchResults); } return(searchResults); }
protected string doTextReplaceCaseInsensitive(string text, string searchText, string replaceText, GrepSearchOption searchOptions) { StringBuilder sb = new StringBuilder(); int index = 0; int counter = 0; bool isWholeWord = (searchOptions & GrepSearchOption.WholeWord) == GrepSearchOption.WholeWord; while (index >= 0) { index = text.ToLowerInvariant().IndexOf(searchText.ToLowerInvariant(), index, StringComparison.InvariantCulture); if (index >= 0) { if (isWholeWord && (!Utils.IsValidBeginText(text.Substring(0, index)) || !Utils.IsValidEndText(text.Substring(index + searchText.Length)))) { index++; continue; } sb.Append(text.Substring(counter, index - counter)); sb.Append(doPatternReplacement(replaceText)); counter = index + searchText.Length; index++; } } sb.Append(text.Substring(counter)); return sb.ToString(); }
protected string doRegexReplace(string text, string searchPattern, string replacePattern, GrepSearchOption searchOptions) { RegexOptions regexOptions = RegexOptions.None; if ((searchOptions & GrepSearchOption.CaseSensitive) != GrepSearchOption.CaseSensitive) regexOptions |= RegexOptions.IgnoreCase; if ((searchOptions & GrepSearchOption.Multiline) == GrepSearchOption.Multiline) regexOptions |= RegexOptions.Multiline; if ((searchOptions & GrepSearchOption.SingleLine) == GrepSearchOption.SingleLine) regexOptions |= RegexOptions.Singleline; bool isWholeWord = (searchOptions & GrepSearchOption.WholeWord) == GrepSearchOption.WholeWord; if (isWholeWord) { if (!searchPattern.Trim().StartsWith("\\b")) searchPattern = "\\b" + searchPattern.Trim(); if (!searchPattern.Trim().EndsWith("\\b")) searchPattern = searchPattern.Trim() + "\\b"; } return Regex.Replace(text, searchPattern, doPatternReplacement(replacePattern), regexOptions); }
protected List<GrepSearchResult.GrepMatch> doTextSearchCaseSensitive(string text, string searchText, GrepSearchOption searchOptions, bool includeContext) { List<GrepSearchResult.GrepLine> results = new List<GrepSearchResult.GrepLine>(); int index = 0; bool isWholeWord = (searchOptions & GrepSearchOption.WholeWord) == GrepSearchOption.WholeWord; List<GrepSearchResult.GrepMatch> globalMatches = new List<GrepSearchResult.GrepMatch>(); while (index >= 0) { index = text.IndexOf(searchText, index, StringComparison.InvariantCulture); if (index >= 0) { if (isWholeWord && (!Utils.IsValidBeginText(text.Substring(0, index)) || !Utils.IsValidEndText(text.Substring(index + searchText.Length)))) { index++; continue; } globalMatches.Add(new GrepSearchResult.GrepMatch(0, index, searchText.Length)); index++; } } return globalMatches; }
protected List<GrepSearchResult.GrepMatch> doRegexSearch(string text, string searchPattern, GrepSearchOption searchOptions, bool includeContext) { RegexOptions regexOptions = RegexOptions.None; if ((searchOptions & GrepSearchOption.CaseSensitive) != GrepSearchOption.CaseSensitive) regexOptions |= RegexOptions.IgnoreCase; if ((searchOptions & GrepSearchOption.Multiline) == GrepSearchOption.Multiline) regexOptions |= RegexOptions.Multiline; if ((searchOptions & GrepSearchOption.SingleLine) == GrepSearchOption.SingleLine) regexOptions |= RegexOptions.Singleline; bool isWholeWord = (searchOptions & GrepSearchOption.WholeWord) == GrepSearchOption.WholeWord; if (isWholeWord) { if (!searchPattern.Trim().StartsWith("\\b")) searchPattern = "\\b" + searchPattern.Trim(); if (!searchPattern.Trim().EndsWith("\\b")) searchPattern = searchPattern.Trim() + "\\b"; } List<GrepSearchResult.GrepLine> results = new List<GrepSearchResult.GrepLine>(); List<GrepSearchResult.GrepMatch> globalMatches = new List<GrepSearchResult.GrepMatch>(); foreach (Match match in Regex.Matches(text, searchPattern, regexOptions)) { globalMatches.Add(new GrepSearchResult.GrepMatch(0, match.Index, match.Length)); } return globalMatches; }
public List<GrepSearchResult> Search(string file, string searchPattern, SearchType searchType, GrepSearchOption searchOptions, Encoding encoding) { load(); SearchDelegates.DoSearch searchMethodMultiline = doTextSearchCaseSensitive; switch (searchType) { case SearchType.PlainText: case SearchType.XPath: if ((searchOptions & GrepSearchOption.CaseSensitive) == GrepSearchOption.CaseSensitive) { searchMethodMultiline = doTextSearchCaseSensitive; } else { searchMethodMultiline = doTextSearchCaseInsensitive; } break; case SearchType.Regex: searchMethodMultiline = doRegexSearch; break; case SearchType.Soundex: searchMethodMultiline = doFuzzySearchMultiline; break; } List<GrepSearchResult> result = searchMultiline(file, searchPattern, searchOptions, searchMethodMultiline); return result; }
public List <GrepSearchResult> Search(string fileName, string searchPattern, SearchType searchType, GrepSearchOption searchOptions, Encoding encoding) { using (var input = File.Open(fileName, FileMode.Open, FileAccess.Read)) { return(Search(input, fileName, searchPattern, searchType, searchOptions, encoding)); } }
private List<GrepSearchResult> searchMultiline(Stream input, string fileName, string searchPattern, GrepSearchOption searchOptions, SearchDelegates.DoSearch searchMethod, Encoding encoding) { List<GrepSearchResult> searchResults = new List<GrepSearchResult>(); using (StreamReader readStream = new StreamReader(input, encoding)) { string fileBody = readStream.ReadToEnd(); var lines = searchMethod(fileBody, searchPattern, searchOptions, true); //Utils.CleanResults(ref lines); if (lines.Count > 0) { searchResults.Add(new GrepSearchResult(fileName, searchPattern, lines)); } } return searchResults; }
// the stream version will get called if the file is in an archive public List <GrepSearchResult> Search(Stream input, string fileName, string searchPattern, SearchType searchType, GrepSearchOption searchOptions, Encoding encoding) { SearchDelegates.DoSearch searchMethodMultiline = DoTextSearch; switch (searchType) { case SearchType.PlainText: case SearchType.XPath: searchMethodMultiline = DoTextSearch; break; case SearchType.Regex: searchMethodMultiline = DoRegexSearch; break; case SearchType.Soundex: searchMethodMultiline = DoFuzzySearch; break; } List <GrepSearchResult> result = SearchMultiline(input, fileName, searchPattern, searchOptions, searchMethodMultiline); return(result); }
protected string doXPathReplace(string text, string searchXPath, string replaceText, GrepSearchOption searchOptions) { if (text.Length > 5 && text.Substring(0, 5).ToLower() == "<?xml") { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(text); XmlNodeList xmlNodes = xmlDoc.SelectNodes(searchXPath); foreach (XmlNode xmlNode in xmlNodes) { xmlNode.InnerXml = doPatternReplacement(replaceText); } StringBuilder sb = new StringBuilder(); StringWriter stringWriter = new StringWriter(sb); using (XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter)) { xmlWriter.Formatting = Formatting.Indented; xmlDoc.WriteContentTo(xmlWriter); xmlWriter.Flush(); } return sb.ToString(); } return text; }
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; }
public bool Replace(string sourceFile, string destinationFile, string searchPattern, string replacePattern, SearchType searchType, GrepSearchOption searchOptions, Encoding encoding) { throw new Exception("The method or operation is not implemented."); }
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); }
private List<GrepSearchResult> search(Stream input, string fileName, string searchPattern, GrepSearchOption searchOptions, SearchDelegates.DoSearch searchMethod, Encoding encoding) { List<GrepSearchResult> searchResults = new List<GrepSearchResult>(); using (StreamReader readStream = new StreamReader(input, encoding)) { string line = null; int counter = 1; int charCounter = 0; List<GrepSearchResult.GrepMatch> matches = new List<GrepSearchResult.GrepMatch>(); while (readStream.Peek() >= 0) { line = readStream.ReadLine(true); if (Utils.CancelSearch) { return searchResults; } List<GrepSearchResult.GrepMatch> results = searchMethod(line, searchPattern, searchOptions, false); if (results.Count > 0) { foreach (GrepSearchResult.GrepMatch m in results) { matches.Add(new GrepSearchResult.GrepMatch(0, m.StartLocation + charCounter, (int)m.Length)); } } charCounter += line.Length; counter++; } if (matches.Count > 0) { searchResults.Add(new GrepSearchResult(fileName, searchPattern, matches)); } } return searchResults; }
private List <GrepSearchResult> searchMultiline(string file, string searchPattern, GrepSearchOption searchOptions, SearchDelegates.DoSearch searchMethod) { List <GrepSearchResult> searchResults = new List <GrepSearchResult>(); try { // Open a given Word document as readonly object wordDocument = openDocument(file, true); if (wordDocument != null) { // Get Selection Property wordSelection = wordApplication.GetType().InvokeMember("Selection", BindingFlags.GetProperty, null, wordApplication, null); // create range and find objects object range = getProperty(wordDocument, "Content"); // create text object text = getProperty(range, "Text"); var lines = searchMethod(-1, Utils.CleanLineBreaks(text.ToString()), searchPattern, searchOptions, true); if (lines.Count > 0) { GrepSearchResult result = new GrepSearchResult(file, searchPattern, lines, Encoding.Default); using (StringReader reader = new StringReader(text.ToString())) { result.SearchResults = Utils.GetLinesEx(reader, result.Matches, linesBefore, linesAfter); } result.ReadOnly = true; searchResults.Add(result); } closeDocument(wordDocument); } } catch (Exception ex) { logger.Log <Exception>(LogLevel.Error, "Failed to search inside Word file", ex); } finally { releaseSelection(); } return(searchResults); }
private bool replaceMultiline(Stream inputStream, Stream outputStream, string searchPattern, string replacePattern, GrepSearchOption searchOptions, SearchDelegates.DoReplace replaceMethod, Encoding encoding) { using (StreamReader readStream = new StreamReader(inputStream, encoding)) { StreamWriter writeStream = new StreamWriter(outputStream, encoding); string fileBody = readStream.ReadToEnd(); fileBody = replaceMethod(fileBody, searchPattern, replacePattern, searchOptions); writeStream.Write(fileBody); writeStream.Flush(); } return true; }
// the stream version will get called if the file is in an archive public List <GrepSearchResult> Search(Stream input, string fileName, string searchPattern, SearchType searchType, GrepSearchOption searchOptions, Encoding encoding) { // write the stream to a temp folder, and run the file version of the search string tempFolder = Path.Combine(Utils.GetTempFolder(), "dnGREP-PDF"); // the fileName may contain the partial path of the directory structure in the archive string filePath = Path.Combine(tempFolder, fileName); // use the directory name to also include folders within the archive string directory = Path.GetDirectoryName(filePath); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } using (var fileStream = File.Create(filePath)) { input.Seek(0, SeekOrigin.Begin); input.CopyTo(fileStream); } return(Search(filePath, searchPattern, searchType, searchOptions, encoding)); }
/// <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; } }
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>()); } }
private bool replace(Stream inputStream, Stream outputStream, string searchPattern, string replacePattern, GrepSearchOption searchOptions, SearchDelegates.DoReplace replaceMethod, Encoding encoding) { using (StreamReader readStream = new StreamReader(inputStream, encoding)) { StreamWriter writeStream = new StreamWriter(outputStream, encoding); string line = null; int counter = 1; while ((line = readStream.ReadLine()) != null) { line = replaceMethod(line, searchPattern, replacePattern, searchOptions); writeStream.WriteLine(line); counter++; } writeStream.Flush(); } return true; }
/// <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 List<GrepSearchResult> searchMultiline(string file, string searchPattern, GrepSearchOption searchOptions, SearchDelegates.DoSearch searchMethod) { List<GrepSearchResult> searchResults = new List<GrepSearchResult>(); try { // Open a given Word document as readonly object wordDocument = openDocument(file, true); // Get Selection Property wordSelection = wordApplication.GetType().InvokeMember("Selection", BindingFlags.GetProperty, null, wordApplication, null); // create range and find objects object range = getProperty(wordDocument, "Content"); // create text object text = getProperty(range, "Text"); var lines = searchMethod(Utils.CleanLineBreaks(text.ToString()), searchPattern, searchOptions, true); if (lines.Count > 0) { GrepSearchResult result = new GrepSearchResult(file, searchPattern, lines); using (StringReader reader = new StringReader(text.ToString())) { result.SearchResults = Utils.GetLinesEx(reader, result.Matches, linesBefore, linesAfter); } result.ReadOnly = true; searchResults.Add(result); } closeDocument(wordDocument); } catch (Exception ex) { logger.LogException(LogLevel.Error, "Failed to search inside Word file", ex); } finally { releaseSelection(); } return searchResults; }