/* * SorOlvaso.java * sorokat olvas egy InputStream-ből, kommenteket sorszéli szóközöket kiszűrve * by pts@fazekas.hu at Fri Mar 30 20:07:13 CEST 2001 * további doksi: a README-ben és e file legkülső class-ának fejkommentjében * * Kincskereső Kisgömböc (C) Early May 2001 by eNTitánok (Rév Szilvia, * Szabó Péter <pts@inf.bme.hu>, Szurdi Miklós, Weizengruber Attila). * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package eNTitanok.util; import java.io.InputStream; import java.io.IOException; /** * A :SorOlvasó a nemüres sorokat string-ként adja vissza, #-kommentek és * szélső whitespace nélkül stb. Azért InputStream-et (byte) használunk és * nem Reader-t (char), hogy minden platformon ugyanúgy olvashassuk be a * file-t. */ public class SorOlvaso { /** * Akkor váltódik ki, ha a bemeneti szövegfile nem az előírt formátumú. */ public static class HibasBemenetException extends IOException {} /** * A bemeneti file-nak csak szóközt (32), soremelést (13 és/vagy 10) és * ASCII karaktereket (33 és 126 között) szabad tartalmaznia. Ha ez nem * teljesül, kiváltódik ez a kivétel. */ public static class BemenetenRosszKarakterException extends HibasBemenetException {} protected static final int S_SORELEJE=0, S_VEGE=1; protected InputStream is; protected StringBuffer b; protected int status; public SorOlvaso(InputStream is) { this.is=is; status=S_SORELEJE; b=new StringBuffer(); } /** * A bemenet karaktereit sorokká fűzi össze. * @return a következő nemüres sort adja vissza, soremelés, kommentek, nyitó * és záró whitespace nélkül */ public synchronized String readLine() throws IOException { while (status!=S_VEGE) { int c; while ((c=is.read())=='\n' || c=='\r' || c==' '); if (c==-1) { status=S_VEGE; break; } if (c<33 || c>126) throw new BemenetenRosszKarakterException(); if (c=='#') { while ((c=is.read())!=-1 && c!='\n' && c!='\r'); if (c==-1) { status=S_VEGE; break; } } else { b.setLength(0); b.append((char)c); while ((c=is.read())!=-1 && c!='\n' && c!='\r') { if (c<32 || c>126) throw new BemenetenRosszKarakterException(); b.append((char)c); } if (c==-1) status=S_VEGE; int i=0; while (i<b.length() && b.charAt(i)!='#') i++; b.setLength(i); i=b.length(); while (i>0 && b.charAt(i-1)==' ') i--; b.setLength(i); if (i>0) return b.toString(); } /// IF } /// WHILE return null; } /// readLine() public synchronized void close() throws IOException { status=S_VEGE; is.close(); is=null; } } /// class SorOlvaso