コード例 #1
0
ファイル: Form1.cs プロジェクト: mzkabbani/cSharpProjects
        private void ParseFile(string file, List<Rules> allRules)
        {
            int fileReplacementsCount = 0;
            List<string> regexMatchesForTokens = new List<string>();
            StreamReader reader = new StreamReader(file);
            string fileRead = string.Empty;
            try {
                fileRead = reader.ReadToEnd();
            } catch (Exception ex) {
                ShowCustomError(ex.Message, false);
            } finally {
                reader.Close();
            }
            for (int i = 0; i < allRules.Count; i++) {
                if (!allRules[i].regexMode) {
                    if (fileRead.Contains(allRules[i].oldEvent)) {
                        fileRead = fileRead.Replace(allRules[i].oldEvent, allRules[i].newEvent);
                        fileReplacementsCount++;
                    }
                } else if (allRules[i].regexMode) {//regex mode
                    GroupCollection groupCollection = Regex.Match(fileRead, allRules[i].oldEvent, RegexOptions.Compiled).Groups;
                    MatchCollection eventMatches = Regex.Matches(fileRead, allRules[i].oldEvent, RegexOptions.Compiled);
                    int counter = 0;
                    if (groupCollection.Count > 1) {
                        foreach (Match eventMatch in eventMatches) {
                            //1-extract the value
                            //2-replace with new event
                            MatchCollection matches = Regex.Matches(allRules[i].newEvent, @"{[\S]*}", RegexOptions.Compiled);
                            int groupNumber = 0;
                            string replacementEvent = string.Empty;
                            for (int p = 0; p < matches.Count; p++) {
                                groupNumber = Convert.ToInt32(matches[p].ToString().Replace("{", "").Replace("}", ""));
                                if (eventMatch.Groups[groupNumber].Value.Contains("\"")) {
                                    string extractedGroupActualValue = eventMatch.Groups[groupNumber].Value.Split('"')[1];
                                    // this is for the filter special cases
                                    if (allRules[i].filterEvent) {
                                        string lastCharacter = extractedGroupActualValue.Substring(extractedGroupActualValue.Length - 1);//get last character
                                        int filterIndex;
                                        string lastCharacterReplacement = string.Empty;
                                        if (Int32.TryParse(lastCharacter, out filterIndex)) {
                                            lastCharacterReplacement = filterMappings[filterIndex];
                                        }
                                        extractedGroupActualValue = lastCharacterReplacement + "::1";
                                    } else if (allRules[i].ActionToolBar) {//action TB specific
                                        string lastCharacter = extractedGroupActualValue.Substring(extractedGroupActualValue.Length - 1);//get  last character
                                        int actionTBIndex;
                                        string lastCharacterReplacement = string.Empty;
                                        if (Int32.TryParse(lastCharacter, out actionTBIndex)) {
                                            lastCharacterReplacement = actionToolbarMappings[actionTBIndex];
                                        }
                                        extractedGroupActualValue = lastCharacterReplacement + "::1";
                                    }
                                    replacementEvent = allRules[i].newEvent.Replace(matches[p].Value.ToString(), extractedGroupActualValue);
                                }
                            }
                            //3-replace value at selected point
                            if (counter == 0) {
                                fileReplacementsCount = fileReplacementsCount + Regex.Matches(fileRead, allRules[i].oldEvent, RegexOptions.Compiled).Count;
                                counter++;
                            }
                            fileRead = fileRead.Replace(eventMatch.Value, replacementEvent);
                            //fileRead = Regex.Replace(fileRead, allRules[i].oldEvent, replacementEvent);
                        }
                    } else {
                        fileReplacementsCount = fileReplacementsCount + Regex.Matches(fileRead, allRules[i].oldEvent).Count;
                        fileRead = ReplaceWithRegex(fileRead, allRules[i].oldEvent, allRules[i].newEvent);
                    }
                }
            }
            //replace Help About text with Comment
            Rules helpAboutRule = new Rules();
            helpAboutRule.ruleName = "HelpAbout";
            if (helpAboutEventIndex != -1) {

                fileRead = Regex.Replace(fileRead, allRules[helpAboutEventIndex].oldEvent, allRules[helpAboutEventIndex].newEvent, RegexOptions.Compiled);

            }
            //fileRead = fileRead.Replace(helpAbouttext, "<!-- ATTENTION: EVENT FILE BLOCK REACHED -->");
            //Write the finished file to output directory
            WriteNewFile(txtToDirectory.Text, file, fileRead);
            InsertLog("File Stats: " + fileReplacementsCount + " replacement(s) in file " + Path.GetFileName(file) + "\r\n");
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: mzkabbani/cSharpProjects
 private void btnAddRule_Click(object sender, EventArgs e)
 {
     try {
         if (IsValidToAddRule(txtOldEvent.Text, txtNewEvent.Text, txtRuleName.Text)) {
             Rules rules = new Rules();
             rules.ruleName = txtRuleName.Text;
             rules.oldEvent = txtOldEvent.Text;
             rules.newEvent = txtNewEvent.Text;
             rules.regexMode = chkRegexMode.Checked;
             rules.filterEvent = chkFilter.Checked;
             rules.ActionToolBar = chkActionTB.Checked;
             lbSavedRules.Items.Add(rules);
             AddRuleToAppDirectory(rules);
             InsertLog("Added rule :" + rules.ruleName + "\r\n");
             txtRuleName.ReadOnly = true;
         }
     } catch (Exception ex) {
         ShowCustomError(ex.Message, false);
     }
 }
コード例 #3
0
ファイル: Form1.cs プロジェクト: mzkabbani/cSharpProjects
 private void FetchSavedRule(string savedRule)
 {
     StreamReader reader = new StreamReader(savedRule);
     try {
         string ruleFileSaved = reader.ReadToEnd();
         string[] delimiter = new string[] { "\n#End#\n" };
         string[] ruleDetailes = ruleFileSaved.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
         Rules rules = new Rules();
         bool isRegexMode = false;
         bool isFilterEvent = false;
         bool isActionToolbar = false;
         if (ruleDetailes.Length > 2) {
             if (string.Equals(ruleDetailes[2], "True", StringComparison.InvariantCulture)) {
                 isRegexMode = true;
             }
             if (ruleDetailes.Length > 3) {
                 if (string.Equals(ruleDetailes[3], "True", StringComparison.InvariantCulture)) {
                     isFilterEvent = true;
                 }
                 if (ruleDetailes.Length > 4) {
                     if (string.Equals(ruleDetailes[4], "True", StringComparison.InvariantCulture)) {
                         isActionToolbar = true;
                     }
                 }
             }
         }
         rules.ruleName = Path.GetFileNameWithoutExtension(savedRule);
         rules.oldEvent = ruleDetailes[0];
         rules.newEvent = ruleDetailes[1];
         rules.regexMode = isRegexMode;
         rules.filterEvent = isFilterEvent;
         rules.ActionToolBar = isActionToolbar;
         lbSavedRules.Items.Add(rules);
     } finally {
         if (reader != null)
             reader.Close();
     }
 }
コード例 #4
0
ファイル: Form1.cs プロジェクト: mzkabbani/cSharpProjects
 private void AddRuleToAppDirectory(Rules rules)
 {
     string workingDirectory = Directory.GetCurrentDirectory();
     if (!Directory.Exists(rulesDirectory)) {
         Directory.CreateDirectory(rulesDirectory);
     }
     StreamWriter writer = new StreamWriter(rulesDirectory + @"\" + rules.ruleName + ".txt");
     try {
         writer.Write(rules.oldEvent + "\n#End#\n" + rules.newEvent + "\n#End#\n" + rules.regexMode.ToString() + "\n#End#\n" + rules.filterEvent.ToString() + "\n#End#\n" + rules.ActionToolBar.ToString());
     } finally {
         if (writer != null) {
             writer.Close();
         }
     }
 }