Exemple #1
0
        /// <summary>
        /// Parses the specified text for configuration entries.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <returns></returns>
        public IList <ConfigLine> Parse(string text)
        {
            var results = new List <ConfigLine>();

            var lines   = text.Split(Environment.NewLine);
            var section = string.Empty;

            foreach (var line in lines)
            {
                // only process lines if content
                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }

                // section header?
                if (line.StartsWith("["))
                {
                    section = line.SubstringAfterChar("[").SubstringBeforeLastChar("]");

                    continue;
                }

                ConfigLine result;

                if (ConfigLine.TryParse(line, out result, section: section))
                {
                    results.Add(result);
                }
            }

            return(results);
        }
Exemple #2
0
        public void TestParseConfigLineWhenAComnentWithInvalidIndicator()
        {
            const string input = @"//comment";

            ConfigLine line;

            Assert.Throws <ArgumentException>(() => ConfigLine.TryParse(input, out line, ""));
        }
Exemple #3
0
        public void TestParseConfigLineWithEqualsInValue()
        {
            const string input = "test=value=something";

            ConfigLine line;

            ConfigLine.TryParse(input, out line);

            Assert.AreEqual("value=something", line.Value);
        }
Exemple #4
0
        public void TestParseConfigLineSetSectionName()
        {
            const string input = "test=value";

            ConfigLine line;

            ConfigLine.TryParse(input, out line, section: "Main");

            Assert.AreEqual("Main", line.Section);
        }
Exemple #5
0
        /// <summary>
        /// Tries to parse the given value into a <see cref="ConfigLine"/>.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="line">The line.</param>
        /// <param name="commentIndicator">The comment indicator.</param>
        /// <param name="section">The section.</param>
        /// <returns></returns>
        public static bool TryParse(string value, out ConfigLine line, string commentIndicator = "#", string section = "")
        {
            var result = false;

            line = null;

            // indicator valid?
            if (string.IsNullOrWhiteSpace(commentIndicator))
            {
                throw new ArgumentException("Comment indicator can't be null: " + commentIndicator);
            }

            // only process valid lines
            if (!string.IsNullOrWhiteSpace(value))
            {
                // comment?
                if (value.StartsWith(commentIndicator))
                {
                    line = new ConfigLine
                               {
                                   IsComment = true,
                                   Key = string.Empty,
                                   Value = value.Substring(commentIndicator.Length),
                                   Section = section
                               };

                    result = true;
                }
                else if (value.Contains("="))
                {
                    line = new ConfigLine
                               {
                                   Key = value.SubstringBeforeChar("="),
                                   Value = value.SubstringAfterChar("="),
                                   Section = section
                               };

                    result = true;
                }
                else
                {
                    line = new ConfigLine
                    {
                        Key = value,
                        Value = string.Empty,
                        Section = section
                    };

                    result = true;
                }

            }

            return result;
        }
Exemple #6
0
        /// <summary>
        /// Tries to parse the given value into a <see cref="ConfigLine"/>.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="line">The line.</param>
        /// <param name="commentIndicator">The comment indicator.</param>
        /// <param name="section">The section.</param>
        /// <returns></returns>
        public static bool TryParse(string value, out ConfigLine line, string commentIndicator = "#", string section = "")
        {
            var result = false;

            line = null;

            // indicator valid?
            if (string.IsNullOrWhiteSpace(commentIndicator))
            {
                throw new ArgumentException("Comment indicator can't be null: " + commentIndicator);
            }

            // only process valid lines
            if (!string.IsNullOrWhiteSpace(value))
            {
                // comment?
                if (value.StartsWith(commentIndicator))
                {
                    line = new ConfigLine
                    {
                        IsComment = true,
                        Key       = string.Empty,
                        Value     = value.Substring(commentIndicator.Length),
                        Section   = section
                    };

                    result = true;
                }
                else if (value.Contains("="))
                {
                    line = new ConfigLine
                    {
                        Key     = value.SubstringBeforeChar("="),
                        Value   = value.SubstringAfterChar("="),
                        Section = section
                    };

                    result = true;
                }
                else
                {
                    line = new ConfigLine
                    {
                        Key     = value,
                        Value   = string.Empty,
                        Section = section
                    };

                    result = true;
                }
            }

            return(result);
        }
Exemple #7
0
        public void TestParseConfigLineWithNoEqualsInValue()
        {
            const string input = "test value is here";

            ConfigLine line;

            ConfigLine.TryParse(input, out line);

            Assert.AreEqual("test value is here", line.Key);
            Assert.AreEqual("", line.Value);
        }
Exemple #8
0
        /// <summary>
        /// Sets the value.
        /// </summary>
        /// <param name="section">The section.</param>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        public void SetValue(string section, string key, string value)
        {
            var line = new ConfigLine
            {
                Section = section,
                Key     = key,
                Value   = value
            };

            Remove(line);

            Lines.Add(line);
        }
Exemple #9
0
        /// <summary>
        /// Removes the specified line.
        /// </summary>
        /// <param name="line">The line.</param>
        private void Remove(ConfigLine line)
        {
            for (var i = Lines.Count - 1; i >= 0; i--)
            {
                var candidate = Lines[i];

                if (string.Compare(line.Section, candidate.Section, true) == 0 &&
                    string.Compare(line.Key, candidate.Key, true) == 0)
                {
                    Lines.RemoveAt(i);
                }
            }
        }
Exemple #10
0
        public void TestParseConfigLine()
        {
            const string input = "test=value";

            ConfigLine line;

            var result = ConfigLine.TryParse(input, out line);

            Assert.IsTrue(result);
            Assert.AreEqual("test", line.Key);
            Assert.AreEqual("value", line.Value);
            Assert.IsFalse(line.IsComment);
        }
Exemple #11
0
        public void TestParseConfigLineWhenAComnentWithDifferentIndicator()
        {
            const string input = @"//comment";

            ConfigLine line;

            var result = ConfigLine.TryParse(input, out line, @"//");

            Assert.IsTrue(result);
            Assert.AreEqual(string.Empty, line.Key);
            Assert.AreEqual("comment", line.Value);
            Assert.IsTrue(line.IsComment);
        }
Exemple #12
0
        public void TestParseConfigLineWhenAComnent()
        {
            const string input = "#comment";

            ConfigLine line;

            var result = ConfigLine.TryParse(input, out line);

            Assert.IsTrue(result);
            Assert.AreEqual(string.Empty, line.Key);
            Assert.AreEqual("comment", line.Value);
            Assert.IsTrue(line.IsComment);
        }
Exemple #13
0
        /// <summary>
        /// Removes the specified line.
        /// </summary>
        /// <param name="line">The line.</param>
        private void Remove(ConfigLine line)
        {
            for (var i = Lines.Count - 1; i >= 0; i--)
            {
                var candidate = Lines[i];

                if (string.Compare(line.Section, candidate.Section, true) == 0 &&
                    string.Compare(line.Key, candidate.Key, true) == 0)
                {
                    Lines.RemoveAt(i);
                }
            }
        }
Exemple #14
0
        /// <summary>
        /// Sets the value.
        /// </summary>
        /// <param name="section">The section.</param>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        public void SetValue(string section, string key, string value)
        {
            var line = new ConfigLine
            {
                Section = section,
                Key = key,
                Value = value
            };

            Remove(line);

            Lines.Add(line);
        }