-
Notifications
You must be signed in to change notification settings - Fork 12
/
SourceInputStream.cs
52 lines (47 loc) · 1.12 KB
/
SourceInputStream.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace cnpl
{
class SourceInputStream : IDisposable
{
private TextReader mIStream = null;
private Queue<int> mBuffer = new Queue<int>();
public SourceInputStream(string path)
{
mIStream = new StreamReader(File.OpenRead(path));
Line = 0;
Column = 0;
}
public int Line { get; private set; }
public int Column { get; private set; }
public void Dispose()
{
if (mIStream != null)
mIStream.Close();
}
public int Input()
{
if (mBuffer.Count > 0)
return mBuffer.Dequeue();
var ch = mIStream.Read();
if (ch == '\n')
{
Line++;
Column = 0;
}
else
{
Column++;
}
return ch;
}
public void Return(int ch)
{
mBuffer.Enqueue(ch);
}
}
}