Skip to content

Commit ccf92fa

Browse files
committed
Return null for sections that do not exist.
To support null conditional operator. Issue: #10 Related issues: #9
1 parent 0afde75 commit ccf92fa

File tree

3 files changed

+68
-29
lines changed

3 files changed

+68
-29
lines changed

src/IniFile/Ini.cs

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ namespace IniFile
4242
[DebuggerDisplay("INI file - {Count} sections")]
4343
public sealed partial class Ini
4444
{
45+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
46+
private readonly IniLoadSettings _settings;
47+
4548
/// <summary>
4649
/// Initializes a new empty instance of the <see cref="Ini"/> class with the default
4750
/// settings.
@@ -74,12 +77,11 @@ public Ini(FileInfo iniFile, IniLoadSettings settings = null) : base(GetEquality
7477
if (!iniFile.Exists)
7578
throw new FileNotFoundException(string.Format(ErrorMessages.IniFileDoesNotExist, iniFile.FullName), iniFile.FullName);
7679

77-
if (settings == null)
78-
settings = IniLoadSettings.Default;
80+
_settings = settings ?? IniLoadSettings.Default;
7981

8082
using (var stream = new FileStream(iniFile.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
81-
using (var reader = new StreamReader(stream, settings.Encoding ?? Encoding.UTF8, settings.DetectEncoding))
82-
ParseIniFile(reader, settings);
83+
using (var reader = new StreamReader(stream, _settings.Encoding ?? Encoding.UTF8, _settings.DetectEncoding))
84+
ParseIniFile(reader);
8385
}
8486

8587
/// <summary>
@@ -97,12 +99,11 @@ public Ini(string iniFilePath, IniLoadSettings settings = null) : base(GetEquali
9799
if (!File.Exists(iniFilePath))
98100
throw new FileNotFoundException(string.Format(ErrorMessages.IniFileDoesNotExist, iniFilePath), iniFilePath);
99101

100-
if (settings == null)
101-
settings = IniLoadSettings.Default;
102+
_settings = settings ?? IniLoadSettings.Default;
102103

103104
using (var stream = new FileStream(iniFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
104-
using (var reader = new StreamReader(stream, settings.Encoding ?? Encoding.UTF8, settings.DetectEncoding))
105-
ParseIniFile(reader, settings);
105+
using (var reader = new StreamReader(stream, _settings.Encoding ?? Encoding.UTF8, _settings.DetectEncoding))
106+
ParseIniFile(reader);
106107
}
107108

108109
/// <summary>
@@ -120,11 +121,10 @@ public Ini(Stream stream, IniLoadSettings settings = null) : base(GetEqualityCom
120121
if (!stream.CanRead)
121122
throw new ArgumentException(ErrorMessages.StreamNotReadable, nameof(stream));
122123

123-
if (settings == null)
124-
settings = IniLoadSettings.Default;
124+
_settings = settings ?? IniLoadSettings.Default;
125125

126-
using (var reader = new StreamReader(stream, settings.Encoding ?? Encoding.UTF8, settings.DetectEncoding))
127-
ParseIniFile(reader, settings);
126+
using (var reader = new StreamReader(stream, _settings.Encoding ?? Encoding.UTF8, _settings.DetectEncoding))
127+
ParseIniFile(reader);
128128
}
129129

130130
/// <summary>
@@ -139,10 +139,9 @@ public Ini(TextReader reader, IniLoadSettings settings = null) : base(GetEqualit
139139
if (reader == null)
140140
throw new ArgumentNullException(nameof(reader));
141141

142-
if (settings == null)
143-
settings = IniLoadSettings.Default;
142+
_settings = settings ?? IniLoadSettings.Default;
144143

145-
ParseIniFile(reader, settings);
144+
ParseIniFile(reader);
146145
}
147146

148147
/// <summary>
@@ -162,16 +161,13 @@ public static Ini Load(string content, IniLoadSettings settings = null)
162161
/// Transforms the INI content into an in-memory object model.
163162
/// </summary>
164163
/// <param name="reader">The INI content.</param>
165-
/// <param name="settings">Optional Ini file settings.</param>
166-
private void ParseIniFile(TextReader reader, IniLoadSettings settings)
164+
private void ParseIniFile(TextReader reader)
167165
{
168-
settings = settings ?? IniLoadSettings.Default;
169-
170166
// Go through all the lines and build a flat collection of INI objects.
171167
IList<IniItem> lineItems = ParseLines(reader).ToList();
172168

173169
// Go through the line objects and construct an object model.
174-
CreateObjectModel(settings, lineItems);
170+
CreateObjectModel(_settings, lineItems);
175171
}
176172

177173
/// <summary>
@@ -526,6 +522,23 @@ private static void FormatMinorItems(IList<MinorIniItem> minorItems, bool remove
526522

527523
public sealed partial class Ini : KeyedCollection<string, Section>
528524
{
525+
public new Section this[string key]
526+
{
527+
get
528+
{
529+
if (key == null)
530+
throw new ArgumentNullException(nameof(key));
531+
532+
IEqualityComparer<string> comparer = GetEqualityComparer(_settings);
533+
foreach (Section section in this)
534+
{
535+
if (comparer.Equals(key, section.Name))
536+
return section;
537+
}
538+
return null;
539+
}
540+
}
541+
529542
protected override string GetKeyForItem(Section item) =>
530543
item.Name;
531544

src/IniFile/Properties/IniFile.xml

Lines changed: 26 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/IniFile.Tests/IniFileTests.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,13 +281,15 @@ public void Can_read_multiline_property_value(string iniContent)
281281
ini["Section"]["Multiline"].ToString().ShouldBe(expectedValue);
282282
}
283283

284-
//[Fact]
285-
//public void Can_load_from_file()
286-
//{
287-
// var ini = new Ini(@"D:\Temp\Data.ini");
288-
// string ip = ini["tcp"]["ip"];
284+
[Fact]
285+
public void Can_load_from_file()
286+
{
287+
var ini = new Ini(@"D:\Temp\Data.ini");
288+
string ip = ini["tcp"]?["ip"];
289+
string missing = ini["tcp2"]?["missing"];
289290

290-
// ip.ShouldNotBeNull();
291-
//}
291+
ip.ShouldNotBeNull();
292+
missing.ShouldBeNull();
293+
}
292294
}
293295
}

0 commit comments

Comments
 (0)