I am writing a basic Scanner
class similar to Java's. Here is what I have (well, the relevant parts):
using System.Text;
using System.Collections.Generic;
namespace System.IO
{
/// <summary>
/// <remarks>
/// Scanner is a wrapper for a <see cref="System.IO.TextReader" />
/// instance, making it easier to read values of certain types. It
/// also takes advantage of the
/// <see cref="System.IO.EndOfStreamException" /> class.
/// </remarks>
/// <seealso cref="System.IO.TextReader" />
/// </summary>
public class Scanner
{
private TextReader Reader;
private Queue<char> CharacterBuffer = new Queue<char>();
/// <summary>
/// <remarks>
/// Defaults to reading from <see cref="Console.In"/>
/// </remarks>
/// </summary>
public Scanner() : this(Console.In)
{
}
public Scanner(TextReader reader)
{
this.Reader = reader;
}
public char Peek()
{
if (this.CharacterBuffer.Count > 0)
return this.CharacterBuffer.Peek();
try
{
return Convert.ToChar(this.Reader.Peek());
}
catch (OverflowException)
{
throw new EndOfStreamException();
}
}
public char ReadChar()
{
if (this.CharacterBuffer.Count > 0)
return this.CharacterBuffer.Dequeue();
try
{
return Convert.ToChar(this.Reader.Read());
}
catch (OverflowException)
{
throw new EndOfStreamException();
}
}
}
}
From the few tests that I ran, this works great with real files, but it doesn't quite work as expected with stdin. If I use Scanner.Peek
or Scanner.ReadChar
, after a newline is sent, the TextReader
instance thinks it is at the end of the file (I think) and throws an EndOfStreamException
instance since this.Reader.Read
and this.Reader.Peek
returns -1
.
How can I force it to request a new character while still supporting true files and StringReader
instances?