/// <summary> /// Search for content in the specified file based on the search criteria. /// </summary> /// <param name="fileName">The file name for which the content is searched for.</param> /// <param name="searchTerms">The terms to search in the file.</param> /// <param name="matcher">Criteria for matching terms in file.</param> /// <returns>List of matches that are found in the file.</returns> public static List <MatchedLine> Search(string fileName, IEnumerable <string> searchTerms, Matcher matcher) { var handler = GetSearchHandler(fileName); List <MatchedLine> matchedLines = new List <MatchedLine>(); try { matchedLines = handler.Search(fileName, searchTerms, matcher); ClearInvalidResults(matchedLines, searchTerms, matcher); } catch (Exception ex) { throw new Exception(String.Format("{0} {1}. {2}", Strings.ErrorAccessingFile, fileName, ex.Message)); } return(matchedLines); }
/// <summary> /// Remove any results that are invalid based on the search type being ANY or ALL. /// </summary> /// <param name="matchedLines">The list of matched lines.</param> /// <param name="searchTerms">The list of terms that were searched.</param> /// <param name="matcher">The matcher object that decides what can be displayed.</param> private static void ClearInvalidResults(List <MatchedLine> matchedLines, IEnumerable <string> searchTerms, Matcher matcher) { bool canShowResult = true; if (matcher.AllMatchesInFile) { List <string> test = matchedLines.Select(ml => ml.SearchTerm.ToUpper()).Distinct().ToList(); canShowResult = matchedLines.Select(ml => ml.SearchTerm.ToUpper()).Distinct().Count() == searchTerms.Count(); } if (!canShowResult) { matchedLines.Clear(); } }