private static void SearchForFileContentMatch(GrabArguments arguments, List <string> filePaths) { bool filePathProvided = !String.IsNullOrWhiteSpace(arguments.TargetFilePath) && filePaths.Count == 1; foreach (var filePath in filePaths) { using (var textStream = new StreamReader(File.OpenRead(filePath), arguments.Encoding)) { string line; int lineCount = 0; while ((line = textStream.ReadLine()) != null) { lineCount++; if (!LineContainsSearchPhrase(line, arguments.SearchPhrase)) { continue; } if (filePathProvided) { Console.WriteLine("{0}: {1}", lineCount, line); } else { string fileName = Path.GetFileName(filePath); Console.WriteLine("{0}: {1}: {2}", fileName, lineCount, line); } } } } }
private static GrabArguments ParseGrabArguments(string[] args) { if (args.Length == 0) { throw new ArgumentException("No arguments provided."); } Encoding encoding; int phraseIndex; if (EncodingProvided(args[0], out encoding)) { if (args.Length > 1) { phraseIndex = 1; } else { throw new ArgumentException("Search phrase missing."); } } else { phraseIndex = 0; } string searchedPhrase = args[phraseIndex]; string targetFilePath = null; int filePathIndex = phraseIndex + 1; if (args.Length > filePathIndex) { targetFilePath = args[filePathIndex]; } var arguments = new GrabArguments(searchedPhrase, targetFilePath, encoding); return(arguments); }
private static GrabArguments ParseGrabArguments(string[] args) { if (args.Length == 0) { throw new ArgumentException("No arguments provided."); } Encoding encoding; int phraseIndex; if (EncodingProvided(args[0], out encoding)) { if (args.Length > 1) { phraseIndex = 1; } else { throw new ArgumentException("Search phrase missing."); } } else { phraseIndex = 0; } string searchedPhrase = args[phraseIndex]; string targetFilePath = null; int filePathIndex = phraseIndex + 1; if (args.Length > filePathIndex) { targetFilePath = args[filePathIndex]; } var arguments = new GrabArguments(searchedPhrase, targetFilePath, encoding); return arguments; }
private static void SearchForFileContentMatch(GrabArguments arguments, List<string> filePaths) { bool filePathProvided = !String.IsNullOrWhiteSpace(arguments.TargetFilePath) && filePaths.Count == 1; foreach (var filePath in filePaths) { using (var textStream = new StreamReader(File.OpenRead(filePath), arguments.Encoding)) { string line; int lineCount = 0; while ((line = textStream.ReadLine()) != null) { lineCount++; if (!LineContainsSearchPhrase(line, arguments.SearchPhrase)) continue; if (filePathProvided) { Console.WriteLine("{0}: {1}", lineCount, line); } else { string fileName = Path.GetFileName(filePath); Console.WriteLine("{0}: {1}: {2}", fileName, lineCount, line); } } } } }