import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

/*********************************************************************
* DABUtils class of static constants and basic file checking routines.
* This is the header file of constants and methods that might be
* used in any and all contexts.
* <br><br>
* DUMMYVALUE is just to have a -1 dummy value defined as a constant<br>
* DUMMYSTRING is just to have a dummy string value defined as a constant<br>
*
* @author Duncan Buell
*/
public class DABUtils
{
  private static final String classLabel = "DABUtils: ";

  public static final int DUMMYVALUE = -1;
  public static final int MAXNUMFIELDS = 100;
  public static final String DUMMYSTRING = "DUMMYSTRING";

/*********************************************************************
* Method to close a PrintWriter file.
**/
  public static void CloseFile(PrintWriter theFile)
  {
    System.out.println(classLabel + "enter (PrintWriter) CloseFile");
    theFile.close();
    System.out.println(classLabel + "leave (PrintWriter) CloseFile");
  }

/*********************************************************************
* Method to close a Scanner file.
**/
  public static void CloseFile(Scanner theFile)
  {
    System.out.println(classLabel + "enter (Scanner) CloseFile");
    theFile.close();
    System.out.println(classLabel + "leave (Scanner) CloseFile");
  }

/*********************************************************************
* PrintWriterOpen method to open a file as a PrintWriter.
* 
* The main purpose of this method is to do the error checking in
* a subordinate method so as not to clutter up the code flow
* in methods that have to open files.
* 
* @param  outFileName the (String) name of the file to open
* @return The opened PrintWriter known to be not null
*/
  public static PrintWriter PrintWriterOpen(String outFileName)
  {
    PrintWriter localPrintWriter = null;

    if(outFileName.equals("System.out"))
    {
      localPrintWriter = new PrintWriter(System.out);
    }
    else
    {
      try
      {
        localPrintWriter = new PrintWriter(new File(outFileName));
      }
      catch (FileNotFoundException ex)
      {
        System.out.println(classLabel + "ERROR opening outFile " + outFileName);
        System.out.println(ex.getMessage());
        System.out.println("in" + System.getProperty("user.dir"));
        System.exit(1);
      }
    }

    return localPrintWriter;
  } // public static PrintWriter PrintWriterOpen(String outFileName)

/*********************************************************************
* ScannerOpen method to open a file as a Scanner.
* 
* The main purpose of this method is to do the error checking in
* a subordinate method so as not to clutter up the code flow
* in methods that have to open files.
* 
* @param  inFileName the (String) name of the file to open
* @return The opened Scanner known to be not null
*/
  public static Scanner ScannerOpen(String inFileName)
  {
    Scanner localScanner = null;

    if(inFileName.equals("System.in"))
    {
      localScanner = new Scanner(System.in);
    }
    else
    {
      try
      {
        localScanner = new Scanner(new File(inFileName));
      }
      catch (FileNotFoundException ex)
      {
        System.out.println("classLabel + ERROR opening inFile " + inFileName);
        System.out.println(ex.getMessage());
        System.out.println("in" + System.getProperty("user.dir"));
        System.exit(1);
      }
    }

    return localScanner;
  } // public static Scanner ScannerOpen(String inFileName)

} // public class DABUtils

