Ejemplo n.º 1
0
 public Pair LoadPairFromFile(string filename)
 {
     Pair p = new Pair(Path.GetFileNameWithoutExtension(filename));
     List<string> lines = GetLinesFromFile(filename);
     // 总共有两种类型,只有两行的单字符对照,以及多行的字符串对照
     if (lines.Count == 2)
     {
         // 对于只有两行的单字符对照进行读取,返回一个Pair
         int length = lines[0].Length <= lines[1].Length ? lines[0].Length : lines[1].Length;
         for (int i = 0; i < length; i++)
         {
             p.From.Add(lines[0][i].ToString());
             p.To.Add(lines[1][i].ToString());
         }
         p.usingRex = false;
     }
     else if (lines.Count > 2)
     {
         Regex bracketCheck = new Regex(@"^\[.*?\]$");
         Regex keyCheck = new Regex(@"(?<=^\[).+(?==)");
         Regex valueCheck = new Regex(@"(?<==).+(?=\]$)");
         string separator = "->";
         foreach (string str in lines)
         {
             if (bracketCheck.IsMatch(str))
             {
                 string key = keyCheck.Match(str).ToString();
                 string value = valueCheck.Match(str).ToString();
                 try
                 {
                     if (key == "UseRex") p.usingRex = Convert.ToBoolean(value);
                     else if (key == "Separator") separator = value;
                     else if (key == "Space") p.Space = CheckRegExpStyle(value);
                     else if (key == "Empty") p.Empty = CheckRegExpStyle(value);
                     else if (key == "IgnoreCase" && Convert.ToBoolean(value)) p.RegOptions |= RegexOptions.IgnoreCase;
                     else if (key == "Multiline" && Convert.ToBoolean(value)) p.RegOptions |= RegexOptions.Multiline;
                     else if (key == "Singleline" && Convert.ToBoolean(value)) p.RegOptions |= RegexOptions.Singleline;
                     else if (key == "IgnorePatternWhitespace" && Convert.ToBoolean(value)) p.RegOptions |= RegexOptions.IgnorePatternWhitespace;
                     else if (key == "ExplicitCapture" && Convert.ToBoolean(value)) p.RegOptions |= RegexOptions.ExplicitCapture;
                 }
                 catch (Exception)
                 {
                     MessageBox.Show(key + " : " + value);
                 }
             }
             else
             {
                 Regex pairCheck = new Regex(separator);
                 string[] ss = pairCheck.Split(str);
                 if (ss.Length < 2) continue;
                 p.From.Add(ss[0]);
                 p.To.Add(ss[1]);
             }
         }
         p.CheckSpaceAndEmpty();
     }
     else
     {
         // 表明外部文件有问题,内容不足两行
         return null;
     }
     return p;
 }
Ejemplo n.º 2
0
        public Pair LoadPairFromFile(string filename)
        {
            Pair          p     = new Pair(Path.GetFileNameWithoutExtension(filename));
            List <string> lines = GetLinesFromFile(filename);

            // 总共有两种类型,只有两行的单字符对照,以及多行的字符串对照
            if (lines.Count == 2)
            {
                // 对于只有两行的单字符对照进行读取,返回一个Pair
                int length = lines[0].Length <= lines[1].Length ? lines[0].Length : lines[1].Length;
                for (int i = 0; i < length; i++)
                {
                    p.From.Add(lines[0][i].ToString());
                    p.To.Add(lines[1][i].ToString());
                }
                p.usingRex = false;
            }
            else if (lines.Count > 2)
            {
                Regex  bracketCheck = new Regex(@"^\[.*?\]$");
                Regex  keyCheck     = new Regex(@"(?<=^\[).+(?==)");
                Regex  valueCheck   = new Regex(@"(?<==).+(?=\]$)");
                string separator    = "->";
                foreach (string str in lines)
                {
                    if (bracketCheck.IsMatch(str))
                    {
                        string key   = keyCheck.Match(str).ToString();
                        string value = valueCheck.Match(str).ToString();
                        try
                        {
                            if (key == "UseRex")
                            {
                                p.usingRex = Convert.ToBoolean(value);
                            }
                            else if (key == "Separator")
                            {
                                separator = value;
                            }
                            else if (key == "Space")
                            {
                                p.Space = CheckRegExpStyle(value);
                            }
                            else if (key == "Empty")
                            {
                                p.Empty = CheckRegExpStyle(value);
                            }
                            else if (key == "IgnoreCase" && Convert.ToBoolean(value))
                            {
                                p.RegOptions |= RegexOptions.IgnoreCase;
                            }
                            else if (key == "Multiline" && Convert.ToBoolean(value))
                            {
                                p.RegOptions |= RegexOptions.Multiline;
                            }
                            else if (key == "Singleline" && Convert.ToBoolean(value))
                            {
                                p.RegOptions |= RegexOptions.Singleline;
                            }
                            else if (key == "IgnorePatternWhitespace" && Convert.ToBoolean(value))
                            {
                                p.RegOptions |= RegexOptions.IgnorePatternWhitespace;
                            }
                            else if (key == "ExplicitCapture" && Convert.ToBoolean(value))
                            {
                                p.RegOptions |= RegexOptions.ExplicitCapture;
                            }
                        }
                        catch (Exception)
                        {
                            MessageBox.Show(key + " : " + value);
                        }
                    }
                    else
                    {
                        Regex    pairCheck = new Regex(separator);
                        string[] ss        = pairCheck.Split(str);
                        if (ss.Length < 2)
                        {
                            continue;
                        }
                        p.From.Add(ss[0]);
                        p.To.Add(ss[1]);
                    }
                }
                p.CheckSpaceAndEmpty();
            }
            else
            {
                // 表明外部文件有问题,内容不足两行
                return(null);
            }
            return(p);
        }