Source code: com/pepperview/romzinger/DataFileTokenizer.java
1 /*
2 * DataFileTokenizer.java -
3 * Copyright (C) 2000 Fabrice Armisen
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 package com.pepperview.romzinger;
21
22 import java.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.FileReader;
25
26 /**
27 * Description of the Class
28 *
29 * @author Fabrice Armisen
30 * @created May 12, 2001
31 */
32 public class DataFileTokenizer
33 {
34
35 private BufferedReader in = null;
36 private String nextToken = null;
37 private String curLine = null;
38 private int cursor = 0;
39 private int lineNb = 0;
40 private String line = null;
41
42
43 /**
44 * Constructor for the DataFileTokenizer object
45 *
46 * @param fileName Description of Parameter
47 * @exception IOException Description of Exception
48 * @exception MalformedDataFileException Description of Exception
49 * @since
50 */
51 public DataFileTokenizer( String fileName ) throws IOException, MalformedDataFileException
52 {
53 in = new BufferedReader( new FileReader( fileName ) );
54 readNextLine();
55 preFetchNextToken();
56 }
57
58
59 /**
60 * Gets the LineNumber attribute of the DataFileTokenizer object
61 *
62 * @return The LineNumber value
63 * @since
64 */
65 public int getLineNumber()
66 {
67 return lineNb;
68 }
69
70
71
72 /**
73 * Description of the Method
74 *
75 * @return Description of the Returned Value
76 * @since
77 */
78 public boolean hasMoreTokens()
79 {
80 return null != nextToken;
81 }
82
83
84 /**
85 * Description of the Method
86 *
87 * @return Description of the Returned Value
88 * @exception IOException Description of Exception
89 * @exception MalformedDataFileException Description of Exception
90 * @since
91 */
92 public int nextIntToken() throws IOException, MalformedDataFileException
93 {
94 String token = nextToken();
95 try
96 {
97 return Integer.parseInt( token );
98 }
99 catch ( NumberFormatException nfe )
100 {
101 throw new MalformedDataFileException( getLineNumber() );
102 }
103
104 }
105
106
107 /**
108 * Description of the Method
109 *
110 * @return Description of the Returned Value
111 * @exception IOException Description of Exception
112 * @exception MalformedDataFileException Description of Exception
113 * @since
114 */
115 public long nextLongToken() throws IOException, MalformedDataFileException
116 {
117 String token = nextToken();
118 try
119 {
120 return Long.parseLong( token );
121 }
122 catch ( NumberFormatException nfe )
123 {
124 throw new MalformedDataFileException( getLineNumber() );
125 }
126
127 }
128
129
130 /**
131 * Description of the Method
132 *
133 * @return Description of the Returned Value
134 * @exception IOException Description of Exception
135 * @exception MalformedDataFileException Description of Exception
136 * @since
137 */
138 public long nextLongHexToken() throws IOException, MalformedDataFileException
139 {
140 String token = nextToken();
141 try
142 {
143 return Long.parseLong( token, 16 );
144 }
145 catch ( NumberFormatException nfe )
146 {
147 throw new MalformedDataFileException( getLineNumber() );
148 }
149
150 }
151
152
153 /**
154 * Description of the Method
155 *
156 * @return Description of the Returned Value
157 * @exception IOException Description of Exception
158 * @exception MalformedDataFileException Description of Exception
159 * @since
160 */
161 public String nextToken() throws IOException, MalformedDataFileException
162 {
163 String token = nextToken;
164 preFetchNextToken();
165 // System.out.println( "("+token+")");
166 return token;
167 }
168
169
170 /**
171 * Description of the Method
172 *
173 * @exception IOException Description of Exception
174 * @exception MalformedDataFileException Description of Exception
175 * @since
176 */
177 private void preFetchNextToken() throws IOException, MalformedDataFileException
178 {
179 int start = -1;
180 nextToken = null;
181 while ( true )
182 {
183 if ( null == line )
184 {
185 return;
186 }
187
188 start = cursor;
189 while ( start < line.length() && Character.isWhitespace( line.charAt( start ) ) )
190 {
191 start++;
192 }
193
194 if ( start >= line.length() )
195 {
196 readNextLine();
197 preFetchNextToken();
198 return;
199 }
200
201 if ( line.charAt( start ) == ';' )
202 {
203 readNextLine();
204 }
205 else
206 {
207 break;
208 }
209 }
210
211 boolean quoteDelimited = false;
212 if ( line.charAt( start ) == '"' )
213 {
214 start++;
215 quoteDelimited = true;
216 }
217
218 int end = start + 1;
219
220 if ( !quoteDelimited )
221 {
222 while ( end < line.length() && !Character.isWhitespace( line.charAt( end ) ) )
223 {
224 end++;
225 }
226 }
227 else
228 {
229 while ( end < line.length() && '"' != line.charAt( end ) )
230 {
231 end++;
232 }
233 }
234
235 if ( end <= line.length() )
236 {
237
238 if ( line.length() == end )
239 {
240 nextToken = line.substring( start );
241 readNextLine();
242 }
243 else
244 {
245 nextToken = line.substring( start, end );
246 cursor = quoteDelimited ? end + 1 : end;
247 }
248 }
249
250 if ( null == nextToken )
251 {
252 // end of line
253
254 readNextLine();
255 preFetchNextToken();
256 }
257
258 }
259
260
261 /**
262 * Description of the Method
263 *
264 * @exception IOException Description of Exception
265 * @since
266 */
267 private void readNextLine() throws IOException
268 {
269 line = in.readLine();
270 lineNb++;
271 cursor = 0;
272 if ( null != line && line.length() == 0 )
273 {
274 readNextLine();
275 return;
276 }
277 // System.out.println( ">" + line );
278 }
279
280 }
281
282