예제 #1
0
        static public void Load()
        {
            string path = Path.Combine(Persistence.GetApplicationDir(), "Rules.txt");

            if (File.Exists(path))
            {
                TextReader reader = new StreamReader(path);
                //try
                {
                    while (true)
                    {
                        string line = reader.ReadLine();
                        if (line == null)
                        {
                            break;
                        }
                        if (line == "")
                        {
                            continue;
                        }
                        if (line.StartsWith("//"))
                        {
                            continue;
                        }

                        string[] parts = line.Split(new string[] { "=>" }, StringSplitOptions.RemoveEmptyEntries);
                        if (parts.Length != 2)
                        {
                            throw new ApplicationException("Incorrect format of rules.");
                        }
                        string conditions = parts[0].Trim();
                        string category   = parts[1].Trim();

                        Rule rule = new Rule();
                        rule.Category = category;

                        if (Categories.Contains(category) == false)
                        {
                            Categories.Add(category);
                        }

                        MatchCollection matches = Regex.Matches(conditions, @"(?<field>[a-zA-Z_\-]+)\:((""(?<value>([^""]*))"")|(?<value>\S*))");
                        foreach (Match m in matches)
                        {
                            string field = m.Groups["field"].Value;
                            string value = m.Groups["value"].Value;
                            switch (field)
                            {
                            case "process":
                                rule.Process = value;
                                break;

                            case "title":
                                rule.Title = value;
                                break;

                            case "document":
                                rule.Document = value;
                                break;

                            case "keyboard-min":
                                rule.KeyboardMin = double.Parse(value);
                                break;

                            case "keyboard-max":
                                rule.KeyboardMax = double.Parse(value);
                                break;

                            case "mouse-min":
                                rule.MouseMin = double.Parse(value);
                                break;

                            case "mouse-max":
                                rule.MouseMax = double.Parse(value);
                                break;

                            case "status-min":
                                rule.StatusMin = ParseUserStatus(value);
                                break;

                            case "status-max":
                                rule.StatusMax = ParseUserStatus(value);
                                break;
                            }
                        }
                        Rules.Add(rule);
                    }
                }

                /*catch
                 * {
                 *  // TODO
                 * }*/
                reader.Close();
            }
        }