-
Notifications
You must be signed in to change notification settings - Fork 0
/
Input.cs
115 lines (99 loc) · 3.03 KB
/
Input.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
namespace Lex
{
/*
* Class: CInput
* Description:
*/
using System;
using System.IO;
public class Input
{
/*
* Member Variables
*/
private StreamReader instream; /* Lex specification file. */
public bool eof_reached; /* Whether EOF has been encountered. */
public bool pushback_line;
public char[] line; /* Line buffer. */
public int line_read; /* Number of bytes read into line buffer. */
public int line_index; /* Current index into line buffer. */
public int line_number; /* Current line number. */
/***************************************************************
Constants
**************************************************************/
private const int BUFFER_SIZE = 1024;
const bool EOF = true;
const bool NOT_EOF = false;
/*
* Function: Input
*/
public Input(StreamReader ihandle)
{
#if DEBUG
Utility.assert(ihandle != null);
#endif
/* Initialize input stream. */
instream = ihandle;
line = new char[BUFFER_SIZE];
line_read = 0;
line_index = 0;
/* Initialize state variables. */
eof_reached = false;
line_number = 0;
pushback_line = false;
}
/*
* Function: GetLine
* Description: Returns true on EOF, false otherwise.
* Guarantees not to return a blank line, or a line
* of zero length.
*/
public bool GetLine()
{
int elem;
/* Has EOF already been reached? */
if (eof_reached)
return true;
/* pushback current line? */
if (pushback_line)
{
pushback_line = false;
/* check for empty line. */
for (elem = 0; elem < line_read; ++elem)
if (!Utility.IsSpace(line[elem]))
break;
/* nonempty? */
if (elem < line_read)
{
line_index = 0;
return false;
}
}
while (true)
{
String lineStr = instream.ReadLine();
if (lineStr == null)
{
eof_reached = true;
line_index = 0;
return true;
}
line = (lineStr + "\n").ToCharArray();
line_read = line.Length;
line_number++;
/* check for empty lines and discard them */
elem = 0;
while (Utility.IsSpace(line[elem]))
{
elem++;
if (elem == line_read)
break;
}
if (elem < line_read)
break;
}
line_index = 0;
return false;
}
}
}