package tools;
import tools.WhiteCharException;
import tools.EofException;
import java.io.*;
public class ScanReader {
/**
* flags say has unreaded character
*/
private boolean hasNextChar;
/**
* unreaded character
*/
private char nextChar;
/**
* wraped Reader object
*/
private Reader reader;
public ScanReader (Reader r) {
reader=r;
}
/**
* read single character, except any white characters and end of file
* @throws WhiteCharException read white character
* @throws EofException end of file
* @return read character
*/
private char readChar() throws IOException, WhiteCharException,EofException {
char ch;
if (hasNextChar) {
ch=nextChar;
hasNextChar=false;
}
else {
int i=reader.read();
if (i!=-1)
ch=(char)i;
else
throw new EofException("Koniec danych");
}
if ((ch=='\t') || (ch=='\n') || (ch==' '))
throw new WhiteCharException();
else
return ch;
}
/**
* read single character, except new line and end of file
* @throws WhiteCharException read new line character
* @throws EofException end of file
* @return read character
*/
private char readCharExceptNewLine() throws IOException, WhiteCharException,EofException {
char ch;
if (hasNextChar) {
ch=nextChar;
hasNextChar=false;
}
else {
int i=reader.read();
if (i!=-1)
ch=(char)i;
else
throw new EofException("Koniec danych");
}
if (ch=='\n')
throw new WhiteCharException();
else
return ch;
}
/**
* read string without first whitecharacters
* @return read string
*/
private String readString () throws IOException {
char buf[]=new char[1024];
int i=0;
while (true) {
try {
buf[i]=readChar();
i++;
}
catch (WhiteCharException ex) {
if (i!=0)
return new String(buf).substring(0,i);
}
catch (EofException ex) {
if (i!=0)
return new String(buf).substring(0,i);
else
throw new IOException("Koniec danych");
}
catch (IOException ex) {
throw ex;
}
}
}
/**
* convert string into byte
* @param s input string
*/
private byte convertToByte(String s) throws NumberFormatException {
try {
int buf=Integer.parseInt(s);
if (buf>=Byte.MIN_VALUE && buf<=Byte.MAX_VALUE)
return (byte) buf;
else
throw new NumberFormatException("Liczba poza zakresem: "+s);
}
catch (Exception ex) {
throw new NumberFormatException("Liczba poza zakresem: "+s);
}
}
/**
* convert string into short
* @param s input string
*/
private short convertToShort(String s) throws NumberFormatException {
try {
int buf=Integer.parseInt(s);
if (buf>=Short.MIN_VALUE && buf<=Short.MAX_VALUE)
return (short) buf;
else
throw new NumberFormatException("Liczba poza zakresem: "+s);
}
catch (Exception ex) {
throw new NumberFormatException("Liczba poza zakresem: "+s);
}
}
/**
* convert string into long
* @param s input string
*/
private long convertToLong(String s) throws NumberFormatException {
try {
int buf=Integer.parseInt(s);
if (buf>=Long.MIN_VALUE && buf<=Long.MAX_VALUE)
return (long) buf;
else
throw new NumberFormatException("Liczba poza zakresem: "+s);
}
catch (Exception ex) {
throw new NumberFormatException("Liczba poza zakresem: "+s);
}
}
/**
* convert string into float
* @param s input string
*/
private float convertToFloat(String s) throws NumberFormatException {
try {
return Float.parseFloat(s);
}
catch (Exception ex) {
throw new NumberFormatException("Liczba poza zakresem: "+s);
}
}
/**
* convert string into double
* @param s input string
*/
private double convertToDouble(String s) throws NumberFormatException {
try {
return Double.parseDouble(s);
}
catch (Exception ex) {
throw new NumberFormatException("Liczba poza zakresem: "+s);
}
}
/**
* convert string into int
* @param s input string
*/
private int convertToInt(String s) throws NumberFormatException {
try {
return Integer.parseInt(s);
}
catch (NumberFormatException ex) {
throw new NumberFormatException("Liczba poza zakresem: "+s);
}
}
/**
* convert string into hex
* @param s input string
*/
private int convertHexToInt(String s) {
try {
s=s.substring(2,s.length());
return Integer.parseInt(s,16);
}
catch (Exception ex) {
throw new NumberFormatException("Liczba poza zakresem: "+s);
}
}
/**
* convert string into bin
* @param s input string
*/
private int convertBinToInt(String s) {
try {
s=s.substring(2,s.length());
return Integer.parseInt(s,2);
}
catch (Exception ex) {
throw new NumberFormatException("Liczba poza zakresem: "+s);
}
}
/**
* read decimal byte
* @return read value
*/
public byte scanByte () throws IOException, NumberFormatException {
String s=readString();
if (s.matches("^[-+]?[0-9]*$"))
return convertToByte(s);
else
throw new NumberFormatException("Niepoprawny format: "+s);
}
/**
* read decimal short
* @return read value
*/
public short scanShort () throws IOException, NumberFormatException {
String s=readString();
if (s.matches("^[-+]?[0-9]*$"))
return convertToShort(s);
else
throw new NumberFormatException("Niepoprawny format: "+s);
}
/**
* read decimal long
* @return read value
*/
public long scanLong () throws IOException, NumberFormatException {
String s=readString();
if (s.matches("^[-+]?[0-9]*$"))
return convertToLong(s);
else
throw new NumberFormatException("Niepoprawny format: "+s);
}
/**
* read float double
* @return read value
*/
public float scanFloat () throws IOException, NumberFormatException {
String s=readString();
if (s.matches("^[-+]?[0-9]+[.]?[0-9]*$"))
return convertToFloat(s);
else
throw new NumberFormatException("Niepoprawny format: "+s);
}
/**
* read decimal double
* @return read value
*/
public double scanDouble () throws IOException, NumberFormatException {
String s=readString();
if (s.matches("^[-+]?[0-9]+[.]?[0-9]*$"))
return convertToDouble(s);
else
throw new NumberFormatException("Niepoprawny format: "+s);
}
/**
* read decimal int
* @return read value
*/
public int scanInt () throws IOException, NumberFormatException {
String s=readString();
if (s.matches("^[-+]?[0-9]*$"))
return convertToInt(s);
else
throw new NumberFormatException("Niepoprawny format: "+s);
}
/**
* read hex number (0hxxxx)
* @return decimal version
*/
public int scanIntHex () throws IOException, NumberFormatException {
String s=readString();
if (s.matches("^0[hx][a-fA-F0-9]+$"))
return convertHexToInt(s);
else
throw new NumberFormatException("Niepoprawny format: "+s);
}
/**
* read binary number (0bxxxx)
* @return decimal version
*/
public int scanIntBin () throws IOException, NumberFormatException {
String s=readString();
if (s.matches("^0b[0-1]+$"))
return convertBinToInt(s);
else
throw new NumberFormatException("Niepoprawny format: "+s);
}
/**
* read Id ([a-zA-Z][a-zA-z_0-9])
* @return read Id
*/
public String scanId () throws IOException {
String s=readString();
if (s.matches("^[a-zA-Z][a-zA-z_0-9]*$"))
return s;
else
throw new NumberFormatException("Niepoprawny identyfikator: "+s);
}
/**
* read single char
* @return read char
*/
public char scanChar () throws IOException {
int i=reader.read();
if (i!=-1)
return (char)i;
else
throw new IOException("Koniec danych");
}
/**
* read string to first white characters
* @return read string
*/
public String scanString () throws IOException {
char buf[]=new char[1024];
int i=0;
while (true) {
try {
buf[i]=readChar();
i++;
}
catch (WhiteCharException ex) {
return new String(buf);//.substring(0,i);
}
catch (EofException ex) {
if (i!=0)
return new String(buf).substring(0,i);
else
throw new IOException("Koniec danych");
}
catch (IOException ex) {
throw ex;
}
}
}
/**
* read string to first new line characters
* @return read string
*/
public String scanEOLn () throws IOException {
char buf[]=new char[1024];
int i=0;
while (true) {
try {
buf[i]=readCharExceptNewLine();
i++;
}
catch (EofException ex) {
if (i!=0)
return new String(buf).substring(0,i);
else
throw new IOException("Koniec danych");
}
catch (WhiteCharException ex) {
return new String(buf).substring(0,i);
}
catch (IOException ex) {
throw ex;
}
}
}
/**
* skip white characters
* @return numbers of white characters
*/
public int skipWS () throws IOException {
int i=0;
while (true) {
try {
nextChar=readChar();
hasNextChar=true;
break;
}
catch (WhiteCharException ex) {
i++;
}
catch (EofException ex) {
return i;
}
catch (IOException ex) {
throw ex;
}
}
return i;
}
/**
* check is end of file
* @return true for end, false not end
*/
public boolean isEOF () throws IOException {
while (true) {
int buf=reader.read();
if (buf==-1)
return true;
else {
char buf2=(char)buf;
if (!(buf2=='\n' || buf2=='\t' || buf2==' ')) {
nextChar=buf2;
hasNextChar=true;
return false;
}
}
}
}
}