// $Id: TextFileReader.java,v 1.7 2004/03/05 17:05:44 m31 Exp $
//
// This file is part of the program suite "Principia Mathematica II"
// which is a working prototype for the main project:
// "Hilbert II" - http://www.qedeq.org
//
// Copyright 2001-2004  Michael Meyling <mime@qedeq.org>.
//
// "Principia Mathematica II" 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.

package com.meyling.principia.io;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


/**
 * Wrapper for the class <code>FileReader</code>.
 *
 * @version $Revision: 1.7 $
 * @author  Michael Meyling
 */
public class TextFileReader {

    /** file to read from */
    private BufferedReader in = null;

    /** file name */
    private String inFileName = null;

    /** line counter */
    private int lineNumber = 0;

    /** last read line */
    private String line = null;

    /**
     * is there a row in the read buffer? Is used to undo the reading of
     * a singular line.
     */
    private boolean alreadyRead = false;


    /**
     * Creates a <code>TextFileReader</code> object.
     *
     * @param   inputName   name of the input file
     * @throws  IOException if the buffered reader couldn't be created
     */
    public TextFileReader(String inputName) throws IOException {
        inFileName = inputName;
        in = new BufferedReader(new FileReader(inFileName));
        lineNumber = 0;
        alreadyRead = false;
    }


    /**
     * Closes input.
     */
    public void close() {
        if (in != null) {
            try {
                in.close();
            } catch (Exception e) {
            }
            in = null;
        }
    }


    /**
     * For simple reading a complete line of the input file. The line
     * is stored in the class attribut <code>line</code>.
     * Comments are ignored.
     *
     * @return  eingelesene Zeile
     * @throws  IOException wenn ein Lesefehler auftrat
     */
    public String readLine() throws IOException {

        if (in == null) {
            throw new IOException("Input stream already closed");
        }
        if (!alreadyRead) {
            do {
                line = in.readLine();
                lineNumber++;
            } while (line != null && (line.length() == 0
                || line.startsWith("#")));
            if (line == null) {
                in.close();
                in = null;
            }
        }
        alreadyRead = false;
        return line;
    }


    /**
     * Undos the {@link #readLine} method. Only one {@link #readLine}
     * call can be cancelled. Further calls lead to an IllegalStateException.
     * A following {@link #readLine} call gives the last result again.
     *
     * @throws IllegalStateException     if undo isn't allowed any more
     */
    public void unReadLine() throws IllegalStateException {
        if (line != null && !alreadyRead) {
            alreadyRead = true;
        } else {
            throw new IllegalStateException(
                "Reading back to many lines");
        }

    }


    /**
     * Get currently read line.
     *
     * @return  currently read line
     */
    public String getLine() {
        return line;
    }


    /**
     * Get currently read line number.
     *
     * @return  currently read line number
     */
    public int getLineNumber() {
        return lineNumber;
    }


    /**
     * Get name of input file.
     *
     * @return  name of input file
     */
    public String getInFileName() {
       return inFileName;
    }


}
