public Executor(string inputFileName, string outputFileName, PatternForReplace patternForReplace, Regex CounterSearcher) { this.inputFileName = inputFileName; this.outputFileName = outputFileName; this.patternForReplace = patternForReplace; this.CounterSearcher = CounterSearcher; }
public PatternForReplace readPatternFromXML(string fileName) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.PreserveWhitespace = true; xmlDoc.Load(fileName); XmlNodeList list = xmlDoc.SelectNodes("patterns/pattern"); PatternForReplace result = new PatternForReplace(); result.regex = new Regex[list.Count]; result.replaceTo = new string[list.Count]; result.options = new RegexOptions[list.Count]; result.isFilter = new PatternForReplace.FilterTypes[list.Count]; result.numOfThreads = new int[list.Count]; int i = 0; for (int j = 0; j < list.Count; ++j) { result.options[j] = RegexOptions.IgnoreCase; } foreach (XmlNode e in list) { switch (e.SelectSingleNode("singleLine").InnerText.ToLower()) { case "false": result.isFilter[i] = PatternForReplace.FilterTypes.none; result.replaceTo[i] = e.SelectSingleNode("replaceTo").InnerText; break; case "true": result.options[i] |= RegexOptions.Multiline; result.isFilter[i] = PatternForReplace.FilterTypes.none; result.replaceTo[i] = e.SelectSingleNode("replaceTo").InnerText; break; case "filterline": result.isFilter[i] = PatternForReplace.FilterTypes.filterLine; break; case "filter": result.isFilter[i] = PatternForReplace.FilterTypes.filter; break; default: break; } result.regex[i] = new Regex(e.SelectSingleNode("search").InnerText, result.options[i]); if (e.SelectSingleNode("threads") != null && !String.IsNullOrEmpty(e.SelectSingleNode("threads").InnerText)) { result.numOfThreads[i] = int.Parse(e.SelectSingleNode("threads").InnerText); } ++i; } return(result); }
public void doReplace(string[] args) { Regex CounterSearcher = new Regex(@"\${counter(?<counter_name>[^}]*)}", RegexOptions.Compiled | RegexOptions.Multiline); #region Initializing file names string inputFileName = "input.txt"; if (args.Length > 0) { inputFileName = args[0]; } string outputFileName = "result.txt"; if (args.Length > 1) { outputFileName = args[1]; } string patternFileName = "pattern.xml"; if (args.Length > 2) { patternFileName = args[2]; } string listOption = "single"; if (args.Length > 3) { listOption = args[3]; } #endregion if (outputFileName.ToLower() != "console") { Console.WriteLine("Input file name: {0}", inputFileName); Console.WriteLine("Result file name: {0}", outputFileName); Console.WriteLine("Pattern configuration file name: {0}", patternFileName); } PatternForReplace patternForReplace = readPatternFromXML(patternFileName); switch (listOption.ToLower()) { case "single": Executor executor = new Executor(inputFileName, outputFileName, patternForReplace, CounterSearcher); executor.execute(); break; case "mask": ArrayList threads = new ArrayList(); foreach (string fileName in Directory.EnumerateFiles(Directory.GetCurrentDirectory(), inputFileName)) { threads.Add(new Thread(Executor.start)); ((Thread)(threads[threads.Count - 1])).Start(new Executor(fileName, fileName, patternForReplace, CounterSearcher)); } foreach (Thread thread in threads) { thread.Join(); } break; default: break; } }