22
22
using System . Collections . ObjectModel ;
23
23
using System . IO ;
24
24
25
+ #nullable enable
26
+
25
27
namespace OpenQA . Selenium . Firefox . Internal
26
28
{
27
29
/// <summary>
28
30
/// Parses and reads an INI file.
29
31
/// </summary>
30
- internal class IniFileReader
32
+ internal sealed class IniFileReader
31
33
{
32
- private Dictionary < string , Dictionary < string , string > > iniFileStore = new Dictionary < string , Dictionary < string , string > > ( ) ;
34
+ private readonly Dictionary < string , Dictionary < string , string > > iniFileStore = new Dictionary < string , Dictionary < string , string > > ( ) ;
33
35
34
36
/// <summary>
35
37
/// Initializes a new instance of the <see cref="IniFileReader"/> class.
36
38
/// </summary>
37
39
/// <param name="fileName">The full path to the .INI file to be read.</param>
40
+ /// <exception cref="ArgumentNullException">If <paramref name="fileName"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
41
+ /// <exception cref="FileNotFoundException">If no file exists at file path <paramref name="fileName"/>.</exception>
38
42
public IniFileReader ( string fileName )
39
43
{
40
44
if ( string . IsNullOrEmpty ( fileName ) )
@@ -53,7 +57,7 @@ public IniFileReader(string fileName)
53
57
string [ ] iniFileContent = File . ReadAllLines ( fileName ) ;
54
58
foreach ( string iniFileLine in iniFileContent )
55
59
{
56
- if ( ! string . IsNullOrEmpty ( iniFileLine . Trim ( ) ) && ! iniFileLine . StartsWith ( ";" , StringComparison . OrdinalIgnoreCase ) )
60
+ if ( ! string . IsNullOrWhiteSpace ( iniFileLine ) && ! iniFileLine . StartsWith ( ";" , StringComparison . OrdinalIgnoreCase ) )
57
61
{
58
62
if ( iniFileLine . StartsWith ( "[" , StringComparison . OrdinalIgnoreCase ) && iniFileLine . EndsWith ( "]" , StringComparison . OrdinalIgnoreCase ) )
59
63
{
@@ -86,21 +90,24 @@ public IniFileReader(string fileName)
86
90
/// <summary>
87
91
/// Gets a <see cref="ReadOnlyCollection{T}"/> containing the names of the sections in the .INI file.
88
92
/// </summary>
89
- public ReadOnlyCollection < string > SectionNames
90
- {
91
- get
92
- {
93
- List < string > keyList = new List < string > ( this . iniFileStore . Keys ) ;
94
- return new ReadOnlyCollection < string > ( keyList ) ;
95
- }
96
- }
93
+ public ReadOnlyCollection < string > SectionNames => new ReadOnlyCollection < string > ( new List < string > ( this . iniFileStore . Keys ) ) ;
97
94
98
95
/// <summary>
99
96
/// Gets a value from the .INI file.
100
97
/// </summary>
101
98
/// <param name="sectionName">The section in which to find the key-value pair.</param>
102
99
/// <param name="valueName">The key of the key-value pair.</param>
103
100
/// <returns>The value associated with the given section and key.</returns>
101
+ /// <exception cref="ArgumentNullException">
102
+ /// <para>If <paramref name="sectionName"/> is <see langword="null"/> or <see cref="string.Empty"/>.</para>
103
+ /// <para>-or-</para>
104
+ /// <para>If <paramref name="valueName"/> is <see langword="null"/> or <see cref="string.Empty"/>.</para>
105
+ /// </exception>
106
+ /// <exception cref="ArgumentException">
107
+ /// <para>If no section named <paramref name="sectionName"/> exists.</para>
108
+ /// <para>-or-</para>
109
+ ///<para>If the section does not contain a value named <paramref name="valueName"/>.</para>
110
+ /// </exception>
104
111
public string GetValue ( string sectionName , string valueName )
105
112
{
106
113
if ( string . IsNullOrEmpty ( sectionName ) )
@@ -117,19 +124,17 @@ public string GetValue(string sectionName, string valueName)
117
124
118
125
string lowerCaseValueName = valueName . ToUpperInvariant ( ) ;
119
126
120
- if ( ! this . iniFileStore . ContainsKey ( lowerCaseSectionName ) )
127
+ if ( ! this . iniFileStore . TryGetValue ( lowerCaseSectionName , out Dictionary < string , string > ? section ) )
121
128
{
122
129
throw new ArgumentException ( "Section does not exist: " + sectionName , nameof ( sectionName ) ) ;
123
130
}
124
131
125
- Dictionary < string , string > section = this . iniFileStore [ lowerCaseSectionName ] ;
126
-
127
- if ( ! section . ContainsKey ( lowerCaseValueName ) )
132
+ if ( ! section . TryGetValue ( lowerCaseValueName , out string ? value ) )
128
133
{
129
134
throw new ArgumentException ( "Value does not exist: " + valueName , nameof ( valueName ) ) ;
130
135
}
131
136
132
- return section [ lowerCaseValueName ] ;
137
+ return value ;
133
138
}
134
139
}
135
140
}
0 commit comments