import java.io.*;
import java.lang.*;

/**
 * @version  21 sep 1998
 * @author   Philippe Chassignet, Ecole Polytechnique
 * @see      Output
 * @see      TD
 */
public class Input {

  public static final char EOF_MARK = 27;
  public static final char EOF_CHAR = 0xFF;
  
  private BufferedReader reader;
  private Output out;
  private boolean file;
  private String buffer = null;
  private int pos = 0;
  private int length = -1;
  private boolean atEOF = false;

  public Input(InputStream stream, Output out) {
    reader = new BufferedReader(new InputStreamReader(stream));
    this.out = out;
    file = false;
  }

  public Input(String fileName) throws FileNotFoundException {
    reader = new BufferedReader(new FileReader(fileName));
    this.out = null;
    file = true;
  }

  public void close() {
    if (file)
      try {
        reader.close();
      }
      catch (java.io.IOException e) {
        System.out.println("close " + e);
      }
  }

  public boolean eof() {
    if (file)
      try {
        return !reader.ready();
      }
      catch (IOException e) {
        System.out.println("eof " + e);
        return true;
      }
    else
      return atEOF;
  }

  public void resetEOF() {
    if( atEOF ) {
      atEOF = false;
      clearLine();
    }
  }
  
  public void clearLine() {
    buffer = null;
    length = -1;
  }
  
  private void newLine() {
    pos = 0;
    try {
      buffer = reader.readLine();
    }
    catch (IOException e) {}
    if (buffer == null)
      length = -1;
    else
      length = buffer.length();
  }

  public char getChar() {
    if ( out != null )
      out.flush();
    if ( atEOF )
      return EOF_CHAR;
    if ( pos > length )
      newLine();
    if ( buffer == null )
      return EOF_CHAR;
    if ( pos == length ) {
      clearLine();
      return '\n';
    }
    if ( !file && (buffer.charAt(pos) == EOF_MARK) ) {
      atEOF = true;
      return EOF_CHAR;
    }
    return buffer.charAt(pos++);
  }

  public String getToken() {
    if ( out != null )
      out.flush();
    if ( atEOF )
      return null;
    do {
      if ( pos >= length )
        newLine();
      if( buffer == null )
        return null;
      while ( (pos < length) && Character.isSpaceChar(buffer.charAt(pos)) )
        pos++;
    } while ( pos >= length );
    if ( !file && (buffer.charAt(pos) == EOF_MARK) ) {
      atEOF = true;
      return null;
    }
    int start = pos;
    while ( (pos < length) && !Character.isSpaceChar(buffer.charAt(pos)) ) {
      pos++;
    }
    if ( !file && (buffer.charAt(pos-1) == EOF_MARK) ) {
      atEOF = true;
      return buffer.substring(start, pos-1);
    }
    else
      return buffer.substring(start, pos);
  }

  public String getLine() {
    if ( out != null )
      out.flush();
    if ( atEOF )
      return null;
    if ( pos > length )
      newLine();
    if ( buffer == null )
      return null;
    if ( pos == length ) {
      clearLine();
      return "";
    }
    if ( !file && (buffer.charAt(length-1) == EOF_MARK) ) {
      atEOF = true;
      return buffer.substring(pos, length-1);
    }
    String s = buffer.substring(pos, length);
    clearLine();
    return s;
  }

}

