Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for property-only entries #212

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/IniParser.Tests/Unit/Parser/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,34 @@ public void parse_ini_string_with_custom_configuration()
Assert.That(section1.Properties["key2"], Is.EqualTo("value5"));
}

[Test, Description("Tests for ability to parse properties with no values")]
public void test_parsing_properties_without_value()
{
var iniString = @"
[section1]
value1
value2";
var parser = new IniDataParser();
parser.Configuration.AllowPropertiesWithoutValue = true;
var parsedData = parser.Parse(iniString);

Assert.That(parsedData["section1"].Contains("value1"), Is.True);
Assert.That(parsedData["section1"]["value1"], Is.Null);
}

[Test, Description("Tests for ability to parse properties with empty values")]
public void test_parsing_properties_with_empty_value()
{
var iniString = @"
[section1]
value1=";
var parser = new IniDataParser();
var parsedData = parser.Parse(iniString);

Assert.That(parsedData["section1"].Contains("value1"), Is.True);
Assert.That(parsedData["section1"]["value1"], Is.Empty);
}

[Test, Description("Test for Issue 3: http://code.google.com/p/ini-parser/issues/detail?id=3")]
public void allow_keys_with_dots()
{
Expand Down
10 changes: 10 additions & 0 deletions src/IniParser/Configuration/IniParserConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public IniParserConfiguration()
AllowDuplicateSections = ori.AllowDuplicateSections;
ThrowExceptionsOnError = ori.ThrowExceptionsOnError;
SkipInvalidLines = ori.SkipInvalidLines;
AllowPropertiesWithoutValue = ori.AllowPropertiesWithoutValue;
TrimSections = ori.TrimSections;
TrimProperties = ori.TrimProperties;
}
Expand Down Expand Up @@ -142,6 +143,15 @@ public enum EDuplicatePropertiesBehaviour
/// </remarks>
public bool SkipInvalidLines { get; set; } = false;

/// <summary>
/// If true, it will consider lines that does not contains an assignment
/// character as properties with a null value
/// </summary>
/// <remarks>
/// Defaults to <c>false</c>.
/// </remarks>
public bool AllowPropertiesWithoutValue { get; set; }

/// <summary>
/// If set to true, it will trim the whitespace out of the property when parsing.
/// If set to false, it will consider all the whitespace in the line as part of the
Expand Down
35 changes: 24 additions & 11 deletions src/IniParser/IniDataParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -343,20 +343,33 @@ protected virtual bool ProcessProperty(StringBuffer currentLine, IniData iniData

var propertyAssigmentIdx = currentLine.FindSubstring(Scheme.PropertyAssigmentString);

if (propertyAssigmentIdx.IsEmpty) return false;

var keyRange = Range.WithIndexes(0, propertyAssigmentIdx.start - 1);
var valueStartIdx = propertyAssigmentIdx.end + 1;
var valueSize = currentLine.Count - propertyAssigmentIdx.end - 1;
var valueRange = Range.FromIndexWithSize(valueStartIdx, valueSize);
StringBuffer key;
StringBuffer value;
if (propertyAssigmentIdx.IsEmpty)
{
if (Configuration.AllowPropertiesWithoutValue)
{
key = currentLine;
value = null;
}
else
return false;
}
else
{
var keyRange = Range.WithIndexes(0, propertyAssigmentIdx.start - 1);
var valueStartIdx = propertyAssigmentIdx.end + 1;
var valueSize = currentLine.Count - propertyAssigmentIdx.end - 1;
var valueRange = Range.FromIndexWithSize(valueStartIdx, valueSize);

var key = currentLine.Substring(keyRange);
var value = currentLine.Substring(valueRange);
key = currentLine.Substring(keyRange);
value = currentLine.Substring(valueRange);
}

if (Configuration.TrimProperties)
{
key.Trim();
value.Trim();
value?.Trim();
}

if (key.IsEmpty)
Expand Down Expand Up @@ -389,7 +402,7 @@ protected virtual bool ProcessProperty(StringBuffer currentLine, IniData iniData
}

AddKeyToKeyValueCollection(key.ToString(),
value.ToString(),
value?.ToString(),
iniData.Global,
"global");
}
Expand All @@ -398,7 +411,7 @@ protected virtual bool ProcessProperty(StringBuffer currentLine, IniData iniData
var currentSection = iniData.Sections.FindByName(_currentSectionNameTemp);

AddKeyToKeyValueCollection(key.ToString(),
value.ToString(),
value?.ToString(),
currentSection.Properties,
_currentSectionNameTemp);
}
Expand Down