/// <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); }
public void TestParseConfigLineWhenAComnentWithInvalidIndicator() { const string input = @"//comment"; ConfigLine line; Assert.Throws <ArgumentException>(() => ConfigLine.TryParse(input, out line, "")); }
public void TestParseConfigLineWithEqualsInValue() { const string input = "test=value=something"; ConfigLine line; ConfigLine.TryParse(input, out line); Assert.AreEqual("value=something", line.Value); }
public void TestParseConfigLineSetSectionName() { const string input = "test=value"; ConfigLine line; ConfigLine.TryParse(input, out line, section: "Main"); Assert.AreEqual("Main", line.Section); }
/// <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; }
/// <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); }
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); }
/// <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); }
/// <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); } } }
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); }
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); }
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); }