public string SearchFileForPattern(String FilePath, string Pattern, int TerminateStatus, string LogFilePath) { Logger NewLogObj = new Logger(); if (File.Exists(FilePath)) { StreamReader reader = new StreamReader(FilePath); string line = string.Empty; //Replace special chars Regex SpecialCharLookup = new Regex(@"(\(|\)|\*|\+|\?)"); MatchCollection SpecailCharMatchColl = SpecialCharLookup.Matches(Pattern); if (SpecailCharMatchColl.Count > 0) { foreach (Match SpecialCharMatch in SpecailCharMatchColl) { string MatchedString = SpecialCharMatch.ToString(); // string ReplaceWithString = @"\" + MatchedString; string ReplaceWithString = "\\" + MatchedString; Pattern = Pattern.Replace(MatchedString, ReplaceWithString); } } // Pattern = "^" + Pattern; Pattern = "^" + Pattern + "\\s*="; while ((line = reader.ReadLine()) != null) { //Match m = Regex.Match(line, Pattern, RegexOptions.CultureInvariant); if (Regex.IsMatch(line, Pattern, RegexOptions.CultureInvariant)) { reader.Close(); return(line); } } NewLogObj.WriteLogFile(LogFilePath, "Unable to find " + Pattern + " in " + FilePath, "fail"); if (TerminateStatus == 1) { NewLogObj.WriteLogFile(LogFilePath, "***Exiting Application from SearchFileForPattern***", "fail"); //Application.Exit(); } reader.Close(); return(null); } else { NewLogObj.WriteLogFile(LogFilePath, FilePath + " Does not exist", "fail"); if (TerminateStatus == 1) { NewLogObj.WriteLogFile(LogFilePath, "***Exiting Application from SearchFileForPattern***", "fail"); //Application.Exit(); return(null); } return(null); } }
//if the pattern appear multiple times, will return the pattern based on index // If index is 2, will return the second appearing similar pattern in the file public string SearchFileForSamePatternAppearinMultipleTimes(String FilePath, string Pattern, int MultipleOccurancePatternIndex, int TerminateStatus, string LogFilePath) { Logger NewLogObj = new Logger(); List <string> MatchedLines = new List <string>(); if (File.Exists(FilePath)) { StreamReader reader = new StreamReader(FilePath); string line = string.Empty; //Replace special chars Regex SpecialCharLookup = new Regex(@"(\(|\)|\*|\+|\?)"); MatchCollection SpecailCharMatchColl = SpecialCharLookup.Matches(Pattern); if (SpecailCharMatchColl.Count > 0) { foreach (Match SpecialCharMatch in SpecailCharMatchColl) { string MatchedString = SpecialCharMatch.ToString(); // string ReplaceWithString = @"\" + MatchedString; string ReplaceWithString = "\\" + MatchedString; Pattern = Pattern.Replace(MatchedString, ReplaceWithString); } } // Pattern = "^" + Pattern; Pattern = "^" + Pattern + "\\s*="; int MatchFoundFlag = 0; int IndexCounter = 0; while ((line = reader.ReadLine()) != null) { //Match m = Regex.Match(line, Pattern, RegexOptions.CultureInvariant); if (Regex.IsMatch(line, Pattern, RegexOptions.CultureInvariant)) { IndexCounter++; MatchedLines.Add(line); MatchFoundFlag = 1; if (IndexCounter == MultipleOccurancePatternIndex) { reader.Close(); return(line); } //reader.Close(); //return line; } } if (MatchFoundFlag == 0) { if (TerminateStatus == 1) { NewLogObj.WriteLogFile(LogFilePath, "Unable to find " + Pattern + " with index " + MultipleOccurancePatternIndex + " in " + FilePath, "fail"); //Application.Exit(); } else { NewLogObj.WriteLogFile(LogFilePath, "Unable to find " + Pattern + " with index " + MultipleOccurancePatternIndex + " in " + FilePath, "warn"); } reader.Close(); return(null); } return(null); } else { NewLogObj.WriteLogFile(LogFilePath, FilePath + " Does not exist", "fail"); if (TerminateStatus == 1) { NewLogObj.WriteLogFile(LogFilePath, "***Exiting Application from SearchFileForSamePatternAppearinMultipleTimes***", "fail"); //Application.Exit(); return(null); } return(null); } }
//string TTKFileName = null; //public void GenerateMapping() //{ // Logger NewLogObj = new Logger(); // string LogFilePath = NewLogObj.GetLogFilePath(); // string TTKFileName=CheckIfProductLocalizedForCurrentLocale(); // if (TTKFileName == null) // { // NewLogObj.WriteLogFile(LogFilePath, TTKFileName + " TTKFileName is null, Cannot genertae mapping", "fail"); // } // string[] Temp = TTKFileName.Split('-'); // TTKFileName = Temp[0]; // string CurrentDir = Directory.GetCurrentDirectory(); // string MappedFile = CurrentDir + "//Inputs//Mapped.txt"; // //if (File.Exists(MappedFile)) // //{ // // File.Delete(MappedFile); // //} // // File.Create(MappedFile); // //Introducing timeout of 1 min for attempting multiple times for perl script to start // int timer = 0; int PerlStartTimeout=60000; // 1 min // while (timer < PerlStartTimeout) // { // try // { // string PerlFilePath = CurrentDir + "//Inputs//" + TTKFileName; // Process myProcess = new Process(); // ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("perl.exe"); // //myProcessStartInfo.Arguments = "GenerateMapping.pl " + TTKFileName; // NewLogObj.WriteLogFile(LogFilePath, "GenerateMapping.pl \"" + PerlFilePath + "\"", "info"); // myProcessStartInfo.Arguments = "GenerateMapping.pl \"" + PerlFilePath + "\""; // myProcessStartInfo.UseShellExecute = false; // myProcessStartInfo.RedirectStandardOutput = true; // myProcess.StartInfo = myProcessStartInfo; // myProcess.Start(); // myProcess.WaitForExit(); // break; // } // catch (Exception Ex) // { // NewLogObj.WriteLogFile(LogFilePath, "Exception while calling GenerateMapping.pl " + Ex.ToString(), "fail"); // timer = timer + 2000; // Thread.Sleep(2000); // } // } // if (timer >= PerlStartTimeout) // { // NewLogObj.WriteLogFile(LogFilePath, "Unable to start GenerateMapping.pl even after retry attempts " , "fail"); // FileOperations FileObj = new FileOperations(); // FileObj.ExitTestEnvironment(); // } //} public void GenerateMapping(string[] TTKFiles) { Logger NewLogObj = new Logger(); FileOperations FileObj = new FileOperations(); Generic NewGenericObj = new Generic(); string LogFilePath = NewLogObj.GetLogFilePath(); NewLogObj.WriteLogFile(LogFilePath, "GenerateMapping ", "info"); NewLogObj.WriteLogFile(LogFilePath, "============= ", "info"); string[] WindowTitlesInEnglish = null; string FileToConvert = Directory.GetCurrentDirectory() + "\\Inputs\\In.txt"; if (File.Exists(FileToConvert)) { WindowTitlesInEnglish = System.IO.File.ReadAllLines(FileToConvert); } string[] TempWindowTitlesInEnglish = WindowTitlesInEnglish; string SystemLocale = NewGenericObj.GetSystemLocale(); string InputFileName = Directory.GetCurrentDirectory() + "\\Inputs\\Inputs_" + SystemLocale + ".txt"; string TTKHotKeyPatternSpecifier = FileObj.GetInputPattern(InputFileName, "TTKHotKeyPatternSpecifier"); //Replacing special chars for (int i = 0; i < TempWindowTitlesInEnglish.Length; i++) { //if (Regex.IsMatch(TempWindowTitlesInEnglish[i], TTKHotKeyPatternSpecifier, RegexOptions.CultureInvariant)) //{ // TempWindowTitlesInEnglish[i] = Regex.Replace(TempWindowTitlesInEnglish[i], TTKHotKeyPatternSpecifier, ""); //} Regex SpecialCharLookup = new Regex(@"(\(|\)|\*|\+|\?|\.)"); MatchCollection SpecailCharMatchColl = SpecialCharLookup.Matches(TempWindowTitlesInEnglish[i]); if (SpecailCharMatchColl.Count > 0) { string PreviousSpecialCharMatch = null; foreach (Match SpecialCharMatch in SpecailCharMatchColl) { string MatchedString = SpecialCharMatch.ToString(); // string ReplaceWithString = @"\" + MatchedString; if (PreviousSpecialCharMatch != MatchedString) { string ReplaceWithString = "\\" + MatchedString; TempWindowTitlesInEnglish[i] = TempWindowTitlesInEnglish[i].Replace(MatchedString, ReplaceWithString); } PreviousSpecialCharMatch = MatchedString; } } } string OutPutFile = Directory.GetCurrentDirectory() + "\\Inputs\\Mapped.txt"; if (File.Exists(OutPutFile)) { File.Delete(OutPutFile); } StreamWriter outfile = new StreamWriter(@OutPutFile); //string[] TTKFiles=Environment.GetCommandLineArgs(); for (int y = 0; y < TTKFiles.Length; y++) { string TTKFileFullPath = Directory.GetCurrentDirectory() + "\\Inputs\\" + TTKFiles[y]; if (!File.Exists(TTKFileFullPath)) { Console.WriteLine("TTKfile " + TTKFileFullPath + " does not exist"); NewLogObj.WriteLogFile(LogFilePath, TTKFiles[y] + " does not exist ", "fail"); FileObj.ExitTestEnvironment(); } } for (int y = 0; y < TTKFiles.Length; y++) { string TTKFile = TTKFiles[y]; string TTKFileFullPath = Directory.GetCurrentDirectory() + "\\Inputs\\" + TTKFile; string[] InputLines = System.IO.File.ReadAllLines(TTKFileFullPath); foreach (string WindowTitle in TempWindowTitlesInEnglish) { int FirstIterationFlag = 0; foreach (string line in InputLines) { string pattern = "(?<ENStringMatched>^" + WindowTitle + ")(?<UnicodeStringMatched>\t+.*)"; Regex r = new Regex(pattern); Match match = r.Match(line); if (match.Success) { //string EnString = match.Groups[1].Value; //string UnicodeString = match.Groups[2].Value; string EnString = match.Groups["ENStringMatched"].Value; string UnicodeString = match.Groups["UnicodeStringMatched"].Value; string[] TempArray = Regex.Split(UnicodeString, "\t+"); outfile.Write(EnString + "=" + TempArray[1] + "\n"); if (FirstIterationFlag == 0) { //Splice the element List <String> Temp = TempWindowTitlesInEnglish.ToList(); Temp.Remove(WindowTitle); TempWindowTitlesInEnglish = Temp.ToArray(); } FirstIterationFlag = 1; } } } } if (TempWindowTitlesInEnglish.Length > 0) { foreach (string Wnd in TempWindowTitlesInEnglish) { if (!Wnd.StartsWith("#")) { //outfile.Write(Wnd+"\n"); outfile.Write(Wnd + "=" + "" + "\n"); } } } outfile.Close(); }