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; }
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; }
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(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; }
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; }