aspclassic-core/AspClassic.Scripting/SourceCodeReader.cs
Jelle Luteijn 484dbfc9d9 progress
2022-05-15 11:19:49 +02:00

95 lines
1.6 KiB
C#

using System;
using System.IO;
using System.Text;
using AspClassic.Scripting.Utils;
namespace AspClassic.Scripting;
public class SourceCodeReader : TextReader
{
public new static readonly SourceCodeReader Null = new SourceCodeReader(TextReader.Null, null);
private readonly TextReader _textReader;
private readonly Encoding _encoding;
public Encoding Encoding => _encoding;
public TextReader BaseReader => _textReader;
public SourceCodeReader(TextReader textReader, Encoding encoding)
{
ContractUtils.RequiresNotNull(textReader, "textReader");
_encoding = encoding;
_textReader = textReader;
}
public override string ReadLine()
{
return _textReader.ReadLine();
}
public virtual bool SeekLine(int line)
{
if (line < 1)
{
throw new ArgumentOutOfRangeException("line");
}
if (line == 1)
{
return true;
}
int num = 1;
while (true)
{
switch (_textReader.Read())
{
case 13:
if (_textReader.Peek() == 10)
{
_textReader.Read();
}
num++;
if (num == line)
{
return true;
}
break;
case 10:
num++;
if (num == line)
{
return true;
}
break;
case -1:
return false;
}
}
}
public override string ReadToEnd()
{
return _textReader.ReadToEnd();
}
public override int Read(char[] buffer, int index, int count)
{
return _textReader.Read(buffer, index, count);
}
public override int Peek()
{
return _textReader.Peek();
}
public override int Read()
{
return _textReader.Read();
}
protected override void Dispose(bool disposing)
{
_textReader.Dispose();
}
}