Menu

#32 embedded quotes in keyvalue

open
nobody
INI (3)
5
2006-12-08
2006-12-08
JohnLScott
No

I am using Nini.Ini to read INI formatted data files produced by a legacy application. I don't know if this is even valid for INI files, but I have to read values with embedded quotes, for example:

[SomeSection]
function=Foobar("String Parameter")

As it is now, IniReader will truncate after the first quote it encounters, even if the quote is not the first non-whitespace character of the value. It will return a value of:
Foobar(

I am now simply turning on ConsumeAllKeyText and this solves my problem completely. However, it seems to me might be good if IniReader did not get confused by the appearance of a quote after the first character of a value, so I am suggesting a change for your consideration.

The following NUnit test demonstrates the issue:

[Test]
public void KeyWithEmbeddedQuotes()
{
StringWriter writer = new StringWriter();
writer.WriteLine("[Nini]");
writer.WriteLine(" function = Foobar(\"Hello\") ");
IniReader reader = new IniReader(new StringReader(writer.ToString()));

Assert.IsTrue(reader.Read());
Assert.IsTrue(reader.Read());
Assert.AreEqual(IniType.Key, reader.Type);
Assert.AreEqual("function", reader.Name);
Assert.AreEqual("Foobar(\"Hello\")", reader.Value);
Assert.AreEqual(null, reader.Comment);

Assert.IsFalse(reader.Read());
}

Should this be something you want to support, the code in IniReader.ReadKeyValue() is easily changed:

if (!this.ConsumeAllKeyText && ch == '"') {
//ReadChar (); // <--- Move this...

if (!foundQuote && characters == 1) {
ReadChar(); // <--- ...to here
foundQuote = true;
continue;
//} else { <--- And change this...
} else if (foundQuote) { // <--- to this
break;
}
}

Discussion


Log in to post a comment.

MongoDB Logo MongoDB