/*
 * TextFileReader.java    1.0   Michael Zanussi
 *
 * Copyright (C) 2004 Michael Zanussi
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and without
 * fee is hereby granted provided that this copyright notice
 * appears in all copies.
 */

package com.michaelzanussi.bayesian;

import java.io.*;

/**
 * Wrapper class around Java's BufferedReader class.
 
 @author <a href="mailto:admin@michaelzanussi.com">Michael Zanussi</a>
 @version 1.0 (20 Feb 2004) 
 */
public class TextFileReader implements BasicIO {

  // The input buffer.
  private BufferedReader buffer;
  
  /**
   * No-arg constructor.
   */
  public TextFileReader() {
    
    buffer = null;
    
  }
  
  /**
   * Opens the specified file for reading. Supports direct file 
   * support or an input stream from the standard input.
   
   @param file the file to open or <code>null</code> for standard input.
   @return <code>true</code> if successful.
   */
  public boolean openFile file ) {
    
    try {
      iffile == null ) {
        // Standard input. 
        buffer = new BufferedReadernew InputStreamReaderSystem.in ) );
      }
      else {  
        // Direct file.
        buffer = new BufferedReadernew FileReaderfile ) );
      }
      return true;
    catchIOException e ) {
      System.err.println"ERROR: " + e.getMessage() );
      System.exit);
    }
    
    return false;

  }

  /**
   * Closes the file reader.
   
   @return <code>true</code> if successful.
   */
  public boolean close() {
    
    try {
      ifbuffer != null ) {
        buffer.close();
        return true;
      }
    catchIOException e ) {
      System.err.println"ERROR: " + e.getMessage() );
    }
    
    return false;
    
  }

  /**
   * Returns the current line read from the buffer.
   
   @return the current line read from the buffer.
   */
  public String readLine() {
    
    String string = null;
    
    try {
      string = buffer.readLine();
    catchIOException e ) {
      System.err.println"ERROR: " + e.getMessage() );
    }

    return string;
    
  }
  
}