Skip to content

Commit

Permalink
See #4 - v1.0.5.2 Added support for quoted values which can contain c…
Browse files Browse the repository at this point in the history
…omment's starting characters.
  • Loading branch information
MarioZ committed Feb 23, 2016
1 parent 26bbefa commit 2a974a2
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 12 deletions.
12 changes: 12 additions & 0 deletions MadMilkman.Ini.Tests/BugFixes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,17 @@ public void Bug2()
CollectionAssert.AreEqual(new string[] { "", "", "" }, deserializeObj.EmptyArray);
CollectionAssert.IsEmpty(deserializeObj.EmptyList);
}

[Test]
public void Bug3()
{
string iniFileContent = "[sektion]" + Environment.NewLine +
"key=\"Data Source=server;Initial Catalog=catalog;Integrated Security=SSPI\"";
IniFile file = IniUtilities.LoadIniFileContent(iniFileContent, new IniOptions());

IniKey key = file.Sections["sektion"].Keys["key"];
Assert.AreEqual("\"Data Source=server;Initial Catalog=catalog;Integrated Security=SSPI\"", key.Value);
Assert.IsNull(key.LeadingComment.Text);
}
}
}
21 changes: 21 additions & 0 deletions MadMilkman.Ini.Tests/IniFileReadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,5 +282,26 @@ public void ReadSectionEdgeCasesTest()
Assert.AreEqual("[;]", file.Sections[6].Name);
Assert.AreEqual(string.Empty, file.Sections[6].LeadingComment.Text);
}

[Test]
public void ReadQuotedValue()
{
string iniFileContent = "key1 = \"Test;Test\"" + Environment.NewLine +
"key2 = \"Test;Test\";" + Environment.NewLine +
"key3 = \"Test;Test" + Environment.NewLine +
"key4 = \"Test;Test;Test\"Test;Test;Test";

IniFile file = IniUtilities.LoadIniFileContent(iniFileContent, new IniOptions());
IniSection section = file.Sections[0];

Assert.AreEqual("\"Test;Test\"", file.Sections[0].Keys[0].Value);
Assert.IsNull(file.Sections[0].Keys[0].LeadingComment.Text);
Assert.AreEqual("\"Test;Test\"", file.Sections[0].Keys[1].Value);
Assert.AreEqual(string.Empty, file.Sections[0].Keys[1].LeadingComment.Text);
Assert.AreEqual("\"Test", file.Sections[0].Keys[2].Value);
Assert.AreEqual("Test", file.Sections[0].Keys[2].LeadingComment.Text);
Assert.AreEqual("\"Test;Test;Test\"Test", file.Sections[0].Keys[3].Value);
Assert.AreEqual("Test;Test", file.Sections[0].Keys[3].LeadingComment.Text);
}
}
}
Binary file modified MadMilkman.Ini.zip
Binary file not shown.
28 changes: 18 additions & 10 deletions MadMilkman.Ini/IniReaderWriter/IniReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,37 +152,45 @@ private void ReadKey(int leftIndention, string line, IniFile file)

this.currentSection.Keys.Add(currentKey);

this.ReadKeyValueAndLeadingComment(line.Substring(++keyDelimiterIndex).TrimStart(), currentKey);
this.ReadValue(line.Substring(++keyDelimiterIndex).TrimStart(), currentKey);
}

this.currentTrailingComment = null;
}

private void ReadKeyValueAndLeadingComment(string lineLeftover, IniKey key)
private void ReadValue(string lineLeftover, IniKey key)
{
/* REMARKS: First occurrence of comment's starting character (e.g. ';') defines key's value.
*
* CONSIDER: Implement a support for quoted values, thus enabling them to contain comment's starting characters. */

int valueEndIndex = lineLeftover.IndexOf((char)this.options.CommentStarter);

if (valueEndIndex == -1)
key.Value = lineLeftover.TrimEnd();

else if (valueEndIndex == 0)
key.Value = key.LeadingComment.Text = string.Empty;
else
this.ReadValueLeadingComment(lineLeftover, valueEndIndex, key);
}

/* MZ(2016-02-23): Added support for quoted values which can contain comment's starting characters. */
private void ReadValueLeadingComment(string lineLeftover, int potentialCommentIndex, IniKey key)
{
int quoteEndIndex = lineLeftover.IndexOf('"', 1);
if (lineLeftover[0] == '"' && quoteEndIndex != -1)
while (quoteEndIndex > potentialCommentIndex && potentialCommentIndex != -1)
potentialCommentIndex = lineLeftover.IndexOf((char)this.options.CommentStarter, ++potentialCommentIndex);

if (potentialCommentIndex == -1)
key.Value = lineLeftover.TrimEnd();
else
{
key.LeadingComment.Text = lineLeftover.Substring(valueEndIndex + 1);
key.LeadingComment.Text = lineLeftover.Substring(potentialCommentIndex + 1);

// The amount of 'whitespace' characters between key's value and comment's starting character.
int leftIndention = 0;
while (lineLeftover[--valueEndIndex] == ' ' || lineLeftover[valueEndIndex] == '\t')
while (lineLeftover[--potentialCommentIndex] == ' ' || lineLeftover[potentialCommentIndex] == '\t')
leftIndention++;

key.LeadingComment.LeftIndentation = leftIndention;
key.Value = lineLeftover.Substring(0, ++valueEndIndex);
key.Value = lineLeftover.Substring(0, ++potentialCommentIndex);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions MadMilkman.Ini/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("MadMilkman")]
[assembly: AssemblyProduct("MadMilkman.Ini")]
[assembly: AssemblyCopyright("Copyright © MadMilkman 2015")]
[assembly: AssemblyCopyright("Copyright © MadMilkman")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand All @@ -18,5 +18,5 @@
[assembly: AllowPartiallyTrustedCallers]
[assembly: CLSCompliant(true)]

[assembly: AssemblyVersion("1.0.5.1")]
[assembly: AssemblyVersion("1.0.5.2")]
[assembly: NeutralResourcesLanguageAttribute("en-US")]
1 change: 1 addition & 0 deletions RELEASENOTES
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[1.0.5.2] Added support for quoted values which can contain comment's starting characters.
[1.0.5.1] Fixed issue with NULL array and list when deserializing object.

[1.0.5.0] NEW RELEASE
Expand Down

0 comments on commit 2a974a2

Please sign in to comment.