Esempio n. 1
0
 public static IniData ReadINI(string content)
 {
     #pragma warning disable CS0618
     var parser = new StringIniParser();
     #pragma warning restore CS0618
     return(parser.ParseString(content));
 }
Esempio n. 2
0
        public IniFileParser(string filepath)
        {
            if (filepath == null)
            {
                throw new NullReferenceException();
            }

            if (!new Regex(@".*\.ini").IsMatch(filepath))
            {
                throw new WrongFileFormatException();
            }

            string fileText;

            try
            {
                // reading file and deleting comments
                fileText = Regex.Replace(File.ReadAllText(filepath), @";.*$", "", RegexOptions.Multiline);
            }
            catch (Exception)
            {
                throw new FileReadingException();
            }

            try
            {
                _data = new StringIniParser().ParseString(fileText);
            }
            catch (Exception)
            {
                throw new FileReadingException();
            }
        }
Esempio n. 3
0
        public void check_that_comment_char_is_not_stored_as_the_key()
        {
            string data = "[data]" + Environment.NewLine + "key = value;";

            IniData inidata = new StringIniParser().ParseString(data);

            Assert.That(inidata["data"]["key"], Is.EqualTo("value;"));
        }
Esempio n. 4
0
        private static (string, string) ParsePropertyFile(string filepath)
        {
            IniData data             = new StringIniParser().ParseString(File.ReadAllText(filepath));
            string  driver           = data["CATALOG_PROPERTIES"]["driver"];
            string  connectionString = data["CATALOG_PROPERTIES"]["connection-string"];

            return(driver, connectionString);
        }
Esempio n. 5
0
        public void allow_keys_with_dots()
        {
            string strTest = "[section_issue.3]\nkey.with_dots = value\n";

            IniData data = new StringIniParser().ParseString(strTest);

            Assert.That(data.Sections.Count, Is.EqualTo(1));
            Assert.That(data.Sections["section_issue.3"]["key.with_dots"], Is.Not.Null);
        }
Esempio n. 6
0
        public void test_no_exception_is_raised_when_reading_url_like_section_names()
        {
            string data =
                @"[http://example.com/page] 
key1 = value1";

            IniData newData = new StringIniParser().ParseString(data);

            Assert.That(newData.Sections[@"http://example.com/page"]["key1"], Is.EqualTo("value1"));
        }
Esempio n. 7
0
        public void not_reproduced_error_tests()
        {
            string test = "[ExampleSection]\nkey = value;value\n";

            StringIniParser strParser = new StringIniParser();

            IniData data = strParser.ParseString(test);

            Assert.That(data.Sections.Count, Is.EqualTo(1));
            Assert.That(data.Sections["ExampleSection"], Is.Not.Null);
            Assert.That(data.Sections["ExampleSection"].Count, Is.EqualTo(1));
            Assert.That(data.Sections["ExampleSection"]["key"], Is.EqualTo("value"));
        }
Esempio n. 8
0
        public void WritingTotring_Test()
        {
            StringIniParser parser = new StringIniParser();
            IniData         data   = new IniData();

            data.Sections.AddSection("newSection1");
            data.Sections["newSection1"].AddKey("newKey1", "newValue1");
            data.Sections["newSection1"].AddKey("newKey2", "newValue5");

            string result = parser.WriteString(data);

            Assert.That(result, Is.Not.Empty);
            Assert.That(result.Length, Is.Not.EqualTo(0));
        }
Esempio n. 9
0
        public void check_using_another_leading_character_for_comments()
        {
            string data =
                @"[test]
# a comment
connectionString = Server=sqlserver.domain.com;Database=main;User ID=user;Password=password";


            StringIniParser parser = new StringIniParser();

            parser.Parser.Configuration.CommentString = "#";
            IniData iniData = parser.ParseString(data);

            Assert.That(
                iniData["test"]["connectionString"],
                Is.EqualTo("Server=sqlserver.domain.com;Database=main;User ID=user;Password=password"));

            Assert.That(
                iniData["test"].GetKeyData("connectionString").Comments[0], Is.EqualTo(" a comment"));
        }
Esempio n. 10
0
    public void WriteToFile(string filePath, string iniSection, IDictionary <string, string> data)
    {
        try {
            KeyDataCollection keyDataCollection = DictionaryToKeyDataCollection(data);
            IniData           iniData           = new IniData();

            SectionData sectionData = new SectionData(iniSection);
            sectionData.Keys = keyDataCollection;

            iniData.Sections.SetSectionData(iniSection, sectionData);

            StringIniParser iniParser = new StringIniParser();

            FileUtils.RemoveReadOnly(filePath);
            string fileContents = iniParser.WriteString(iniData);
            File.WriteAllText(filePath, fileContents);
        } catch (Exception e) {
            this.LogError("WriteToFile() error: " + e.ToString());
        }
    }
Esempio n. 11
0
    private IDictionary <string, string> Parse(string data, string iniSection)
    {
        if (String.IsNullOrEmpty(data))
        {
            this.LogError("Parse() error: empty or null content");
            return(null);
        }

        IniData iniData = null;

        try {
            StringIniParser iniParser = new StringIniParser();
            iniData = iniParser.ParseString(data);
        } catch (Exception e) {
            this.LogError("Parse() error: " + e.ToString());
        }

        if (iniData == null)
        {
            this.LogError("Parse() error: ini parse failed due to invalid data");
            return(null);
        }

        KeyDataCollection keyDataCollection = null;

        try {
            keyDataCollection = iniData[iniSection];
        } catch (Exception e) {
            this.LogError("Parse() exception: " + e.ToString());
            return(null);
        }

        if (keyDataCollection == null)
        {
            this.LogError("Parse() error: could not find section '" + iniSection + "' in ini");
            return(null);
        }

        return(KeyDataCollectionToDictionary(keyDataCollection));
    }