public static int Compare(SearchFile sf1, SearchFile sf2) { if (sf1 == null && sf2 == null) { return(0); } if (sf1 == null) { return(-1); } if (sf2 == null) { return(1); } if (sf1.File.Directory != null && sf2.File.Directory != null) { var pathCmp = sf1.File.Directory.ToString().ToUpperInvariant() .CompareTo(sf2.File.Directory.ToString().ToUpperInvariant()); if (pathCmp != 0) { return(pathCmp); } } return(sf1.File.Name.ToUpperInvariant() .CompareTo(sf2.File.Name.ToUpperInvariant())); }
public static int Compare(SearchResult?r1, SearchResult?r2) { if (r1 == null && r2 == null) { return(0); } if (r1 == null) { return(-1); } if (r2 == null) { return(1); } if (r1.File != null && r2.File != null) { var sfCmp = SearchFile.Compare(r1.File, r2.File); if (sfCmp != 0) { return(sfCmp); } } if (r1.LineNum == r2.LineNum) { return(r1.MatchStartIndex - r2.MatchStartIndex); } return(r1.LineNum - r2.LineNum); }
private void SearchBinaryFile(SearchFile sf) { if (Settings.Verbose) { Common.Log($"Searching binary file {FileUtil.ContractPath(sf.FullName)}"); } try { using var sr = new StreamReader(sf.FullName, _binaryEncoding); var contents = sr.ReadToEnd(); foreach (var p in Settings.SearchPatterns) { var matches = p.Matches(contents).Cast <Match>(); if (Settings.FirstMatch) { matches = matches.Take(1); } foreach (var m in matches) { AddSearchResult(new SearchResult( p, sf, 0, m.Index + 1, m.Index + m.Length + 1, null)); } } } catch (IOException e) { Common.Log(e.Message); } }
public void Search() { var expandedPath = FileUtil.ExpandPath(Settings.StartPath); if (Directory.Exists(expandedPath)) { var startDir = new DirectoryInfo(expandedPath); if (IsSearchDirectory(startDir)) { SearchPath(startDir); } else { throw new SearchException("Startpath does not match search settings"); } } else { var f = new FileInfo(expandedPath); var sf = new SearchFile(f, _fileTypes.GetFileType(f)); if (FilterFile(sf)) { SearchFile(sf); } else { throw new SearchException("Startpath does not match search settings"); } } }
public void SearchResultMultiLine_ToString_EqualsExpected() { var settings = new SearchSettings(); var pattern = new Regex("Search"); var searchFile = new SearchFile(CsSearchPath, "Searcher.cs", FileType.Text); var lineNum = 10; var matchStartIndex = 15; var matchEndIndex = 23; var line = "\tpublic class Searcher"; var linesBefore = new List<string> { "namespace CsSearch", "{" }; var linesAfter = new List<string> {"\t{", "\t\tprivate readonly FileTypes _fileTypes;"}; var searchResult = new SearchResult(pattern, searchFile, lineNum, matchStartIndex, matchEndIndex, line, linesBefore, linesAfter); var expectedPath = CsSearchPath + "/Searcher.cs"; var expectedOutput = string.Format(new string('=', 80) + "\n" + "{0}: {1}: [{2}:{3}]\n" + new string('-', 80) + "\n" + " 8 | namespace CsSearch\n" + " 9 | {{\n" + "> 10 | public class Searcher\n" + " 11 | {{\n" + " 12 | private readonly FileTypes _fileTypes;\n", expectedPath, lineNum, matchStartIndex, matchEndIndex); Assert.AreEqual(searchResult.ToString(settings), expectedOutput); }
public void SearchFile(SearchFile f) { switch (f.Type) { case FileType.Code: case FileType.Text: case FileType.Xml: SearchTextFile(f); break; case FileType.Binary: SearchBinaryFile(f); break; case FileType.Archive: Common.Log($"Skipping archive file {FileUtil.ContractPath(f.FullName)}"); break; default: { if (Settings.Verbose) { Common.Log($"Skipping file {FileUtil.ContractPath(f.FullName)}"); } break; } } }
public SearchResult(Regex searchPattern, SearchFile file, int lineNum, int matchStartIndex, int matchEndIndex, string line, IList <string> linesBefore, IList <string> linesAfter) { Initialize(searchPattern, file, lineNum, matchStartIndex, matchEndIndex, line, linesBefore, linesAfter); }
public bool IsSearchFile(SearchFile sf) { return((Settings.InExtensions.Count == 0 || Settings.InExtensions.Contains(sf.File.Extension)) && (Settings.OutExtensions.Count == 0 || !Settings.OutExtensions.Contains(sf.File.Extension)) && (Settings.InFilePatterns.Count == 0 || Settings.InFilePatterns.Any(p => p.Match(sf.File.Name).Success)) && (Settings.OutFilePatterns.Count == 0 || !Settings.OutFilePatterns.Any(p => p.Match(sf.File.Name).Success))); }
public bool FilterFile(SearchFile sf) { if (FileUtil.IsHidden(sf.File) && Settings.ExcludeHidden) { return(false); } if (sf.Type.Equals(FileType.Archive)) { return(Settings.SearchArchives && IsArchiveSearchFile(sf)); } return(!Settings.ArchivesOnly && IsSearchFile(sf)); }
private void Initialize(Regex searchPattern, SearchFile file, int lineNum, int matchStartIndex, int matchEndIndex, string line, IList <string> linesBefore, IList <string> linesAfter) { SearchPattern = searchPattern; File = file; LineNum = lineNum; MatchStartIndex = matchStartIndex; MatchEndIndex = matchEndIndex; Line = line; LinesBefore = linesBefore; LinesAfter = linesAfter; }
private void SearchTextFile(SearchFile f) { if (Settings.Verbose) { Common.Log($"Searching text file {FileUtil.ContractPath(f.FullName)}"); } if (Settings.MultiLineSearch) { SearchTextFileContents(f); } else { SearchTextFileLines(f); } }
private void SearchTextFile(SearchFile f) { if (Settings.Debug) { Common.Log($"Searching text file {FileUtil.ContractOrRelativePath(f.FullName, Settings.StartPath!)}"); } if (Settings.MultiLineSearch) { SearchTextFileContents(f); } else { SearchTextFileLines(f); } }
private void SearchTextFile(SearchFile f) { if (Settings.Verbose) { Log(string.Format("Searching text file {0}", FileUtil.GetRelativePath(f.FullName, Settings.StartPath))); } if (Settings.MultiLineSearch) { SearchTextFileContents(f); } else { SearchTextFileLines(f); } }
public void SearchResultBinaryFile_ToString_EqualsExpected() { var settings = new SearchSettings(); var pattern = new Regex("Search"); var searchFile = new SearchFile(CsSearchPath, "Searcher.exe", FileType.Binary); var lineNum = 0; var matchStartIndex = 0; var matchEndIndex = 0; string line = null; var searchResult = new SearchResult(pattern, searchFile, lineNum, matchStartIndex, matchEndIndex, line); var expectedPath = CsSearchPath + "/Searcher.exe"; var expectedOutput = string.Format("{0} matches", expectedPath); Assert.AreEqual(searchResult.ToString(settings), expectedOutput); }
public void SearchResultSingleLine_ToString_EqualsExpected() { var settings = new SearchSettings(); var pattern = new Regex("Search"); var searchFile = new SearchFile(CsSearchPath, "Searcher.cs", FileType.Text); var lineNum = 10; var matchStartIndex = 15; var matchEndIndex = 23; var line = "\tpublic class Searcher\n"; var searchResult = new SearchResult(pattern, searchFile, lineNum, matchStartIndex, matchEndIndex, line); var expectedPath = CsSearchPath + "/Searcher.cs"; var expectedOutput = string.Format("{0}: {1}: [{2}:{3}]: {4}", expectedPath, lineNum, matchStartIndex, matchEndIndex, line.Trim()); Assert.AreEqual(searchResult.ToString(settings), expectedOutput); }
private void SearchTextFileContents(SearchFile f) { try { var contents = FileUtil.GetFileContents(f); var results = SearchContents(contents); foreach (var r in results) { r.File = f; AddSearchResult(r); } } catch (IOException e) { Log(e.Message); } }
private void SearchTextFileLines(SearchFile f) { try { var enumerableLines = FileUtil.EnumerableStringFromFile(f); var results = SearchLines(enumerableLines); foreach (var r in results) { r.File = f; AddSearchResult(r); } } catch (IOException e) { Log(e.Message); } }
public void DoSearchFile(SearchFile f) { if (f.Type == FileType.Text) { SearchTextFile(f); } else if (f.Type == FileType.Binary) { SearchBinaryFile(f); } else if (f.Type == FileType.Archive) { Log(string.Format("Skipping archive file {0}", FileUtil.GetRelativePath(f.FullName, Settings.StartPath))); } else if (Settings.Verbose) { Log(string.Format("Skipping file {0}", FileUtil.GetRelativePath(f.FullName, Settings.StartPath))); } }
// TODO: switch to use SearchLines with buffering private void SearchBinaryFile(SearchFile f) { if (Settings.Verbose) { Log(string.Format("Searching binary file {0}", FileUtil.GetRelativePath(f.FullName, Settings.StartPath))); } try { using (var sr = new StreamReader(f.FullName)) { var contents = sr.ReadToEnd(); foreach (var p in Settings.SearchPatterns.Where(p => p.Match(contents).Success)) { AddSearchResult(new SearchResult(p, f, 0, 0, 0, null)); } } } catch (IOException e) { Log(e.Message); } }
public static IEnumerable <string> EnumerableStringFromFile(SearchFile f) { return(EnumerableStringFromFile(f.FullName)); }
public static string GetFileContents(SearchFile f) { return GetFileContents(f.FullName); }
public static IEnumerable<string> EnumerableStringFromFile(SearchFile f) { return EnumerableStringFromFile(f.FullName); }
public void SearchFileBackSlashes_ToString_EqualsExpected() { var searchFile = new SearchFile(WinCsSearchPath, "Searcher.cs", FileType.Text); Assert.AreEqual(searchFile.ToString(), WinCsSearchPath + @"\Searcher.cs"); }
public void SearchFile_ToString_EqualsExpected() { var searchFile = new SearchFile(CsSearchPath, "Searcher.cs", FileType.Text); Assert.AreEqual(searchFile.ToString(), CsSearchPath + "/Searcher.cs"); }
public SearchResult(Regex searchPattern, SearchFile file, int lineNum, int matchStartIndex, int matchEndIndex, string line) { Initialize(searchPattern, file, lineNum, matchStartIndex, matchEndIndex, line, new List <string>(), new List <string>()); }
public static string GetFileContents(SearchFile f, Encoding encoding) { return(GetFileContents(f.FullName, encoding)); }
public static IEnumerable <string> EnumerableStringFromFile(SearchFile f, Encoding enc) { return(EnumerableStringFromFile(f.FullName, enc)); }
public static string GetFileContents(SearchFile f) { return(GetFileContents(f.FullName)); }