/*********************************************************************
 * Author: B. Alex Bridges                                           *
 * Login ID: brid0129                                                *
 * Class: CPSC-431, Winter 2000                                      *
 * Project: Laboratory Exercise 3                                    *
 * Description: This program is a lexical analyzer, the parser       *
 *              portion of a syntax analyzer, and a semantic analyzer*
 *              for the lanaguage Micro Modula-2.                    *
 * Contents: General methods for various file-related actions.       *
 *********************************************************************/

/* IMPORTS */
import java.io.*;


class File
{
  /* CONSTANTS */
  final static boolean b_debug = false;      // CONTROLS EXTRA DEBUG OUTPUT
  final static String str_end = "###END###"; // END OF FILE STRING

  /* GLOBAL VARIABLES */
  // NONE

  /*************************************************************************
   * Method: check                                                         *
   * Purpose: Checks for the existence of the specified file.              *
   * Input: --PARAMATERS--                                                 *
   *        => 'str_filename' = The name of the file.                      *
   * Output: --RETURNS--                                                   *
   *         => true  = The file exists.                                   *
   *         => false = The file does NOT exist.                           *
   *************************************************************************/
  public static boolean check(String str_filename)
  {
    /* LOCAL VARIABLES */
    FileReader fr_temp; // TEMPORARY FILE READER

    try
    {
      /* CHECK FOR FILE'S EXISTENCE */
      fr_temp = new FileReader(str_filename);

      return true;
    } // try
    /* EXCEPTION HANDLING */
    catch(FileNotFoundException exception)
    {
      return false;
    } // catch
  } // method check

  /*************************************************************************
   * Method: open                                                          *
   * Purpose: Opens the file for future reading.                           *
   * Input: --PARAMATERS--                                                 *
   *        => 'str_filename' = The name of the file.                      *
   *        => 'b_lines' = Boolean for inclusion of line numbers.          *
   * Output: --RETURNS--                                                   *
   *         => The buffered reader for the specified file.                *
   *************************************************************************/
  public static BufferedReader open(String str_filename, boolean b_lines)
  {
    /* LOCAL VARIABLES */
    BufferedReader br_file_in;  // FILE INPUT STREAM

    try
    {
      /* FILE INPUT STREAM SETUP FOR SPECIFIED FILE */
      if(!b_lines)
        br_file_in = new BufferedReader( new FileReader(str_filename) );
      else
        br_file_in = new LineNumberReader( new FileReader(str_filename) );

      return br_file_in;
    } // try
    /* EXCEPTION HANDLING */
    catch(FileNotFoundException exception)
    {
      System.out.println("\n FATAL EXCEPTION: File '"+str_filename+"' does not exist.\n");
      return(null);
    } // catch
  } // method open

  /*************************************************************************
   * Method: read_line                                                     *
   * Purpose: Reads the next line of the file's buffered reader.           *
   * Input: --PARAMATERS--                                                 *
   *        => 'br_file' = The buffered reader for the file.               *
   * Output: --RETURNS--                                                   *
   *         => If available, the next line of text, else "###END###".     *
   *************************************************************************/
  public static String read_line(BufferedReader br_file_in)
  {
    /* LOCAL VARIABLES */
    String str_line = new String(); // THE LINE OF TEXT

   try
    {
      if( br_file_in.ready() )
        str_line = br_file_in.readLine();
      else
        str_line = str_end;

      return str_line;
    } // try
    /* EXCEPTION HANDLING */
    catch(IOException exception)
    {
      System.out.println("\n FATAL EXCEPTION: File input problem.\n");
      return(null);
    } // catch
  } // method read_line
} // class File

