-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileTokenRead.java
executable file
·112 lines (83 loc) · 3.57 KB
/
FileTokenRead.java
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
// package cheatdetect;
import java.io.File;
import java.io.*;
import java.util.LinkedList;
import java.util.Iterator;
public class FileTokenRead{
//private BufferedInputStream in;
//private int leftoverChar; // holds character already read from file but not given to the user yet
//private boolean endOfFile; // true if reached the end of the file
//private int position; // holds current position in the file
//private int tokenLength; // holds length of the token last read from file
private LinkedList<Token> tokens;
// constructor which starts reading with the beginning of the file
public FileTokenRead(String name) throws java.io.IOException
{
tokens = new LinkedList<Token>();
BufferedInputStream in = new BufferedInputStream(new FileInputStream(name));
// now parse the input file and put all tokens into the linked list
int ch = in.read();
int position = 0;
//System.out.println();
//System.out.println();
//System.out.println();
boolean endOfFile = false;
while ( ch != -1 )
{
while ( isWhiteSpace((char) ch) ){
ch = in.read();
position++;
if ( ch == -1 ){
endOfFile = true;
break;
}
}
if ( ! endOfFile )
{
StringBuffer buf = new StringBuffer();
buf.append( (char) ch);
if ( !isLetter((char) ch ) ){ // token is a single non-letter character
ch = in.read();
position++;
}
else {
ch = in.read();
position++;
while ( isLetterNumberSpecialChar_((char) ch) )
{ // token starts with a letter and consits of consequitive letters and numbers
buf.append((char) ch);
ch = in.read();
position++;
}
}
tokens.addLast(new Token(buf.toString(),position-buf.length(),position-1));
//System.out.print(buf.toString());
//System.out.print("#");
}
}
in.close();
}
public Iterator<Token> getIterator(){
return(tokens.listIterator());
}
// this function checks if the input character is a letter
private boolean isLetter( char in ){
char nextChar = Character.toLowerCase((char) in);
if ( (nextChar >= 'a' && nextChar <= 'z') )
return (true);
else return(false);
}
private boolean isWhiteSpace( char in ){
char nextChar = Character.toLowerCase((char) in);
if ( (nextChar >= 33 && nextChar <= 126) )
return (false);
else return(true);
}
// this function checks if the input character is a letter or a number
private boolean isLetterNumberSpecialChar_( char in ){
char nextChar = Character.toLowerCase((char) in);
if ( (nextChar >= 'a' && nextChar <= 'z') || (nextChar >= '0' && (nextChar <= '9')) || nextChar == '_')
return (true);
else return(false);
}
}