/// <summary> /// Do a synchronous text search/replace /// Use RepleaceAsync to run a background thread /// </summary> /// <param name="configuration">Replace operation parameters</param> /// <returns>Search results before replacement</returns> public FRResults ReplaceSync(FRConfiguration configuration) { try { FRResults results = new FRResults(); List <String> files = configuration.GetFiles(); FRSearch search = configuration.GetSearch(); string replacement = configuration.Replacement; if (replacement == null) { return(results); } string src; List <SearchMatch> matches; foreach (String file in files) { src = configuration.GetSource(file); search.SourceFile = file; results[file] = matches = search.Matches(src); foreach (SearchMatch match in matches) { src = search.ReplaceAll(src, replacement, matches); configuration.SetSource(file, src); } matches = null; } return(results); } catch (Exception ex) { MessageBox.Show("Exception " + ex.Message + "\n" + ex.StackTrace); return(null); } }
/// <summary> /// Do a synchronous search /// Use SearchAsync to run a background thread /// </summary> /// <param name="configuration">Search operation parameters</param> /// <returns>Search results</returns> public FRResults SearchSync(FRConfiguration configuration) { try { FRResults results = new FRResults(); List <String> files = configuration.GetFiles(); FRSearch search = configuration.GetSearch(); foreach (String file in files) { String src = configuration.GetSource(file); List <SearchMatch> matches = search.Matches(src); FRSearch.ExtractResultsLineText(matches, src); results[file] = matches; } return(results); } catch (Exception ex) { MessageBox.Show("Exception " + ex.Message + "\n" + ex.StackTrace); return(null); } }
/// <summary> /// Background work main loop /// </summary> private void BackgroundWork(Object sender, DoWorkEventArgs e) { try { FRConfiguration configuration = e.Argument as FRConfiguration; if (configuration == null) { e.Result = null; return; } // get files Int32 count = 0; List <string> files = configuration.GetFiles(); if (files == null || files.Count == 0) { e.Result = new FRResults(); // empty results return; } FRResults results = new FRResults(); FRSearch search = configuration.GetSearch(); string replacement = configuration.Replacement; if (this.backgroundWorker.CancellationPending) { e.Cancel = true; } else { // do search Int32 total = files.Count; Int32 lastPercent = 0; List <SearchMatch> matches; string src; foreach (String file in files) { if (this.backgroundWorker.CancellationPending) { e.Cancel = true; } else { // work src = configuration.GetSource(file); search.SourceFile = file; results[file] = matches = search.Matches(src); if (matches.Count > 0) { if (replacement != null) { // replace text src = search.ReplaceAll(src, replacement, matches); configuration.SetSource(file, src); } else { FRSearch.ExtractResultsLineText(matches, src); } } matches = null; // progress count++; Int32 percent = (100 * count) / total; if (lastPercent != percent) { this.backgroundWorker.ReportProgress(percent); lastPercent = percent; } } } } e.Result = results; } catch (Exception ex) { MessageBox.Show("Exception during background operation:\n" + ex.Message + "\n" + ex.StackTrace); e.Result = null; } }