Hey guys, just came accross a bug, when using Nini, if you give it a file that has no request section but it does have keys. Then you get a null reference exception. Ive created a fix and a test for you to integrate if you see fit.
Test (To go in IniDocumentTest.cs)
[Test]
public void NoSectionExistsButKeysExist ()
{
StringWriter writer = new StringWriter ();
writer.WriteLine ("Test=100");
IniDocument doc = new IniDocument (new StringReader (writer.ToString ()));
Assert.AreEqual (0, doc.Sections.Count);
}
And here is my suggested patch for LoadReader() in IniDocument.cs (Just added "if (sectionFound)" on line 223)
private void LoadReader (IniReader reader)
{
reader.IgnoreComments = false;
bool sectionFound = false;
IniSection section = null;
try {
while (reader.Read ())
{
switch (reader.Type)
{
case IniType.Empty:
if (!sectionFound) {
initialComment.Add (reader.Comment);
} else {
section.Set (reader.Comment);
}
break;
case IniType.Section:
sectionFound = true;
// If section already exists then overwrite it
if (sections[reader.Name] != null) {
sections.Remove (reader.Name);
}
section = new IniSection (reader.Name, reader.Comment);
sections.Add (section);
break;
case IniType.Key:
if (sectionFound)
if (section.GetValue (reader.Name) == null) {
section.Set (reader.Name, reader.Value, reader.Comment);
}
break;
}
}
} catch (Exception ex) {
throw ex;
} finally {
// Always close the file
reader.Close ();
}
}