private void doSearchReplace(object sender, DoWorkEventArgs e) { try { if (!workerSearchReplace.CancellationPending) { timer = DateTime.Now; Dictionary<string, object> workerParams = (Dictionary<string, object>)e.Argument; //TODO: Check if this is needed MainViewModel param = (MainViewModel)workerParams["State"]; if (param.CurrentGrepOperation == GrepOperation.Search || param.CurrentGrepOperation == GrepOperation.SearchInResults) { int sizeFrom = 0; int sizeTo = 0; if (param.UseFileSizeFilter == FileSizeFilter.Yes) { sizeFrom = param.SizeFrom; sizeTo = param.SizeTo; } string filePatternInclude = "*.*"; if (param.TypeOfFileSearch == FileSearchType.Regex) filePatternInclude = ".*"; if (!string.IsNullOrEmpty(param.FilePattern)) filePatternInclude = param.FilePattern; string filePatternExclude = ""; if (!string.IsNullOrEmpty(param.FilePatternIgnore)) filePatternExclude = param.FilePatternIgnore; IEnumerable<string> files; Utils.CancelSearch = false; if (param.CurrentGrepOperation == GrepOperation.SearchInResults) { files = (List<string>)workerParams["Files"]; } else { files = Utils.GetFileListEx(FileOrFolderPath, filePatternInclude, filePatternExclude, param.TypeOfFileSearch == FileSearchType.Regex, param.IncludeSubfolder, param.IncludeHidden, param.IncludeBinary, sizeFrom, sizeTo); } if (Utils.CancelSearch) { e.Result = null; return; } if (param.TypeOfSearch == SearchType.Regex) { try { Regex pattern = new Regex(param.SearchFor); } catch (ArgumentException regException) { MessageBox.Show("Incorrect pattern: " + regException.Message, "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); e.Result = null; return; } } GrepCore grep = new GrepCore(); grep.SearchParams.FuzzyMatchThreshold = settings.Get<double>(GrepSettings.Key.FuzzyMatchThreshold); grep.SearchParams.LinesBefore = settings.Get<int>(GrepSettings.Key.ContextLinesBefore); grep.SearchParams.LinesAfter = settings.Get<int>(GrepSettings.Key.ContextLinesAfter); grep.SearchParams.ShowLinesInContext = settings.Get<bool>(GrepSettings.Key.ShowLinesInContext); GrepSearchOption searchOptions = GrepSearchOption.None; if (Multiline) searchOptions |= GrepSearchOption.Multiline; if (CaseSensitive) searchOptions |= GrepSearchOption.CaseSensitive; if (Singleline) searchOptions |= GrepSearchOption.SingleLine; if (WholeWord) searchOptions |= GrepSearchOption.WholeWord; if (StopAfterFirstMatch) searchOptions |= GrepSearchOption.StopAfterFirstMatch; grep.ProcessedFile += new GrepCore.SearchProgressHandler(grep_ProcessedFile); e.Result = grep.Search(files, param.TypeOfSearch, param.SearchFor, searchOptions, param.CodePage); grep.ProcessedFile -= new GrepCore.SearchProgressHandler(grep_ProcessedFile); } else { GrepCore grep = new GrepCore(); grep.SearchParams.FuzzyMatchThreshold = settings.Get<double>(GrepSettings.Key.FuzzyMatchThreshold); grep.SearchParams.LinesBefore = settings.Get<int>(GrepSettings.Key.ContextLinesBefore); grep.SearchParams.LinesAfter = settings.Get<int>(GrepSettings.Key.ContextLinesAfter); grep.SearchParams.ShowLinesInContext = settings.Get<bool>(GrepSettings.Key.ShowLinesInContext); GrepSearchOption searchOptions = GrepSearchOption.None; if (Multiline) searchOptions |= GrepSearchOption.Multiline; if (CaseSensitive) searchOptions |= GrepSearchOption.CaseSensitive; if (Singleline) searchOptions |= GrepSearchOption.SingleLine; if (WholeWord) searchOptions |= GrepSearchOption.WholeWord; if (StopAfterFirstMatch) searchOptions |= GrepSearchOption.WholeWord; grep.ProcessedFile += new GrepCore.SearchProgressHandler(grep_ProcessedFile); string[] files = ((List<string>)workerParams["Files"]).ToArray(); e.Result = grep.Replace(files, param.TypeOfSearch, Utils.GetBaseFolder(param.FileOrFolderPath), param.SearchFor, param.ReplaceWith, searchOptions, param.CodePage); grep.ProcessedFile -= new GrepCore.SearchProgressHandler(grep_ProcessedFile); } } } catch (Exception ex) { logger.LogException(LogLevel.Error, ex.Message, ex); bool isSearch = true; if (e.Argument is MainViewModel) { MainViewModel param = (MainViewModel)e.Argument; if (param.CurrentGrepOperation == GrepOperation.Search || param.CurrentGrepOperation == GrepOperation.SearchInResults) isSearch = true; else isSearch = false; } if (isSearch) MessageBox.Show("Search failed! See error log.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); else MessageBox.Show("Replace failed! See error log.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
public void TestGuidxReplaceWithPatternRegex() { Utils.CopyFiles(sourceFolder + "\\TestCase9", destinationFolder + "\\TestCase9", null, null); GrepCore core = new GrepCore(); List<GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase9", "guidx.txt"), SearchType.Regex, "h\\wre", GrepSearchOption.None, -1); Assert.Equal(results.Count, 1); Assert.Equal(results[0].SearchResults.Count, 6); core.Replace(Directory.GetFiles(destinationFolder + "\\TestCase9", "guidx.txt"), SearchType.Regex, destinationFolder + "\\TestCase9", "h\\wre", "$(guidx)", GrepSearchOption.None, -1); string fileContent = File.ReadAllText(destinationFolder + "\\TestCase9\\guidx.txt", Encoding.ASCII).Trim(); Assert.Equal(6, guidPattern.Matches(fileContent).Count); Dictionary<string, int> uniqueGuids = new Dictionary<string, int>(); foreach (Match match in guidPattern.Matches(fileContent)) { if (!uniqueGuids.ContainsKey(match.Value)) uniqueGuids[match.Value] = 1; else uniqueGuids[match.Value]++; } Assert.Equal(2, uniqueGuids.Keys.Count); }
public void TestReplaceWithNewLineWorks() { Utils.CopyFiles(sourceFolder + "\\TestCase8", destinationFolder + "\\TestCase8", null, null); GrepCore core = new GrepCore(); List<GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase8", "test.txt"), SearchType.Regex, "here", GrepSearchOption.None, -1); Assert.Equal(results.Count, 1); Assert.Equal(results[0].SearchResults.Count, 1); core.Replace(Directory.GetFiles(destinationFolder + "\\TestCase8", "test.txt"), SearchType.Regex, destinationFolder + "\\TestCase8", "here", "\\n", GrepSearchOption.None, -1); Assert.Equal(File.ReadAllText(destinationFolder + "\\TestCase8\\test.txt", Encoding.ASCII).Trim().Split('\n').Length, 2); }
public void TestReplaceWithPattern(SearchType type) { Utils.CopyFiles(sourceFolder + "\\TestCase9", destinationFolder + "\\TestCase9", null, null); GrepCore core = new GrepCore(); List<GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase9", "test.txt"), type, "here", GrepSearchOption.None, -1); Assert.Equal(results.Count, 1); Assert.Equal(results[0].SearchResults.Count, 6); core.Replace(Directory.GetFiles(destinationFolder + "\\TestCase9", "test.txt"), type, destinationFolder + "\\TestCase9", "here", "$(guid)", GrepSearchOption.None, -1); string fileContent = File.ReadAllText(destinationFolder + "\\TestCase9\\test.txt", Encoding.ASCII).Trim(); Assert.Equal(6, guidPattern.Matches(fileContent).Count); HashSet<string> uniqueGuids = new HashSet<string>(); foreach (Match match in guidPattern.Matches(fileContent)) { if (!uniqueGuids.Contains(match.Value)) uniqueGuids.Add(match.Value); else Assert.True(false, "All guides should be unique."); } }
private void doSearchReplace(object sender, DoWorkEventArgs e) { try { if (!workerSearchReplace.CancellationPending) { timer = DateTime.Now; if (e.Argument == SEARCH_KEY) { int sizeFrom = 0; int sizeTo = 0; if (!IsAllSizes) { sizeFrom = Utils.ParseInt(tbFileSizeFrom.Text, 0); sizeTo = Utils.ParseInt(tbFileSizeTo.Text, 0); } string filePattern = "*.*"; if (rbFileRegex.Checked) filePattern = ".*"; if (!string.IsNullOrEmpty(tbFilePattern.Text)) filePattern = tbFilePattern.Text; if (rbFileAsterisk.Checked) filePattern = filePattern.Replace("\\", ""); string[] files; if (DoSearchInResults) { List<string> filesFromSearch = new List<string>(); foreach (GrepSearchResult result in searchResults) { if (!filesFromSearch.Contains(result.FileNameReal)) { filesFromSearch.Add(result.FileNameReal); } } files = filesFromSearch.ToArray(); } else { files = Utils.GetFileList(tbFolderName.Text, filePattern, rbFileRegex.Checked, cbIncludeSubfolders.Checked, cbIncludeHiddenFolders.Checked, sizeFrom, sizeTo); } GrepCore grep = new GrepCore(); grep.ShowLinesInContext = Properties.Settings.Default.ShowLinesInContext; grep.LinesBefore = Properties.Settings.Default.ContextLinesBefore; grep.LinesAfter = Properties.Settings.Default.ContextLinesAfter; grep.PreviewFilesDuringSearch = Properties.Settings.Default.PreviewResults; grep.ProcessedFile += new GrepCore.SearchProgressHandler(grep_ProcessedFile); List<GrepSearchResult> results = null; GrepSearchOption searchOptions = GrepSearchOption.None; if (cbMultiline.Checked) searchOptions |= GrepSearchOption.Multiline; if (cbCaseSensitive.Checked) searchOptions |= GrepSearchOption.CaseSensitive; if (rbRegexSearch.Checked) results = grep.Search(files, SearchType.Regex, tbSearchFor.Text, searchOptions, CodePage); else if (rbXPathSearch.Checked) results = grep.Search(files, SearchType.XPath, tbSearchFor.Text, searchOptions, CodePage); else results = grep.Search(files, SearchType.PlainText, tbSearchFor.Text, searchOptions, CodePage); grep.ProcessedFile -= new GrepCore.SearchProgressHandler(grep_ProcessedFile); if (results != null) { searchResults = new List<GrepSearchResult>(results); e.Result = results.Count; } else { searchResults = new List<GrepSearchResult>(); e.Result = 0; } } else { GrepCore grep = new GrepCore(); grep.ShowLinesInContext = Properties.Settings.Default.ShowLinesInContext; grep.LinesBefore = Properties.Settings.Default.ContextLinesBefore; grep.LinesAfter = Properties.Settings.Default.ContextLinesAfter; grep.PreviewFilesDuringSearch = Properties.Settings.Default.PreviewResults; grep.ProcessedFile += new GrepCore.SearchProgressHandler(grep_ProcessedFile); List<string> files = new List<string>(); foreach (GrepSearchResult result in searchResults) { if (!result.ReadOnly) files.Add(result.FileNameReal); } GrepSearchOption searchOptions = GrepSearchOption.None; if (cbMultiline.Checked) searchOptions |= GrepSearchOption.Multiline; if (cbCaseSensitive.Checked) searchOptions |= GrepSearchOption.CaseSensitive; if (rbRegexSearch.Checked) e.Result = grep.Replace(files.ToArray(), SearchType.Regex, Utils.GetBaseFolder(tbFolderName.Text), tbSearchFor.Text, tbReplaceWith.Text, searchOptions, CodePage); else if (rbXPathSearch.Checked) e.Result = grep.Replace(files.ToArray(), SearchType.XPath, Utils.GetBaseFolder(tbFolderName.Text), tbSearchFor.Text, tbReplaceWith.Text, searchOptions, CodePage); else e.Result = grep.Replace(files.ToArray(), SearchType.PlainText, Utils.GetBaseFolder(tbFolderName.Text), tbSearchFor.Text, tbReplaceWith.Text, searchOptions, CodePage); grep.ProcessedFile -= new GrepCore.SearchProgressHandler(grep_ProcessedFile); } } } catch (Exception ex) { logger.LogException(LogLevel.Error, ex.Message, ex); if (e.Argument == SEARCH_KEY) MessageBox.Show("Search failed! See error log.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); else MessageBox.Show("Replace failed! See error log.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }