public KeyValuePairReader(string file)
 {
     string section = "Default";
     IDictionary keys = new KeyValuesDictionary();
     sections.Add(section, keys);
     using (StreamReader streamReader = new StreamReader(file))
     {
         string line = streamReader.ReadLine();
         while (line != null)
         {
             line = line.Trim();
             if (line.StartsWith("["))
             {
                 section = line.Substring(1, line.Length - 2);
                 keys = new KeyValuesDictionary();
                 sections.Add(section, keys);
             }
             else if (line != "" && !line.StartsWith("-") && !line.StartsWith("#"))
             {
                 int sepratorIndex = line.IndexOfAny(new char[] {'-', '=', ':'});
                 string key = line.Substring(0, sepratorIndex);
                 string value = line.Substring(sepratorIndex + 1);
                 keys.Add(key, value);
             }
             line = streamReader.ReadLine();
         }
     }
 }
示例#2
0
        public KeyValuePairReader(string file)
        {
            string      section = "Default";
            IDictionary keys    = new KeyValuesDictionary();

            sections.Add(section, keys);
            using (StreamReader streamReader = new StreamReader(file))
            {
                string line = streamReader.ReadLine();
                while (line != null)
                {
                    line = line.Trim();
                    if (line.StartsWith("["))
                    {
                        section = line.Substring(1, line.Length - 2);
                        keys    = new KeyValuesDictionary();
                        sections.Add(section, keys);
                    }
                    else if (line != "" && !line.StartsWith("-") && !line.StartsWith("#"))
                    {
                        int    sepratorIndex = line.IndexOfAny(new char[] { '-', '=', ':' });
                        string key           = line.Substring(0, sepratorIndex);
                        string value         = line.Substring(sepratorIndex + 1);
                        keys.Add(key, value);
                    }
                    line = streamReader.ReadLine();
                }
            }
        }
示例#3
0
        public void Default()
        {
            KeyValuePairReader  reader = new KeyValuePairReader(@"../../Commons/TestData/Default.options");
            KeyValuesDictionary keys   = reader.GetKeys();

            CheckKey(keys, "Key1", "Value1");
            CheckKey(keys, "Key2", "Value2");

            string value = keys["NotExists"];

            Assert.AreEqual("", value);

            Assert.AreEqual("Value1", reader.GetKey("Key1"));
            Assert.AreEqual("Value2", reader.GetKey("Key2"));

            string[] values = keys.GetValues("Key3");
            Assert.AreEqual(3, values.Length);
            Assert.AreEqual("Value3", values[0]);
            Assert.AreEqual("Value4", values[1]);
            Assert.AreEqual("Value5", values[2]);

            values = keys.GetValues("NotExists");
            Assert.AreEqual(0, values.Length);
        }