import java.util.*;
import java.io.*;

/**********************************************************************
 * A set of utility functions for use in cs239.
 *
 * @author	Arch Harris
 * @version	February 2009, version 1
 *********************************************************************/
public class Util1
{
	/** If an error is encountered that causes an exception, a string
	  * describing the exception. **/
	private static String errorMessage;

	/*************************************************************
	 * Creates a new Scanner object. If an exception occurs, catches
	 * the exception and sets the Util1 error string to a description
	 * of the exception.
	 *
	 * @param object	The I/O entity to be scanned.
	 * @return			Null if an exception occurs, the Scanner object otherwise
	 *************************************************************/
	private static Scanner newScanner(File inputFile, InputStream inputStream, 
	 Readable inputReadable, String inputString)
	{
		Scanner	ss;

		errorMessage = "";
		try
		{
			if (inputFile != null)
				ss = new Scanner(inputFile);
			else if (inputStream != null)
				ss = new Scanner(inputStream);
			else if (inputReadable != null)
				ss = new Scanner(inputReadable);
			else if (inputString != null)
				ss = new Scanner(inputString);
			else
			{
				ss = null;
				System.err.printf("Bug in class Util1, newScanner called"
				 + " with all null arguments.\n");
				System.exit(1);
			}
		}
		catch (Exception ee)
		{
			ss = null;
			errorMessage = ee.toString();
		}
		return ss;
	}
	public static Scanner newScanner(File object)
	{
		return newScanner(object, null, null, null);
	}
	public static Scanner newScanner(InputStream object)
	{
		return newScanner(null, object, null, null);
	}
	public static Scanner newScanner(Readable object)
	{
		return newScanner(null, null, object, null);
	}
	public static Scanner newScanner(String object)
	{
		return newScanner(null, null, null, object);
	}

	/*************************************************************
	 * Return an error message for the last Util1 method executed.
	 *
	 * @return	Null if no error occurred occurs, otherwise a description
	 *			of the error.
	 *************************************************************/
	public static String getErrorMessage()
	{
		return errorMessage;
	}

	/*************************************************************
	 * Creates a new PrintWriter object. If an exception occurs, catches
	 * the exception and sets the Util1 error string to a description
	 * of the exception.
	 *
	 * @param object	The I/O entity to be printed.
	 * @param autoFlush	If true, the println() methods will flush the output buffer.
	 * @return			Null if an exception occurs, the PrintWriter object otherwise
	 *************************************************************/
	private static PrintWriter newPrintWriter(OutputStream outputStream,
	 File outputFile,
	 Writer outputWriter, boolean autoFlush)
	{
		PrintWriter	pw;

		errorMessage = "";
		try
		{
			if (outputStream != null)
				pw = new PrintWriter(outputStream, autoFlush);
			else if (outputFile != null)
				pw = new PrintWriter(outputFile);
			else if (outputWriter != null)
				pw = new PrintWriter(outputWriter, autoFlush);
			else
			{
				pw = null;
				System.err.printf("Bug in clapw Util1, newPrintWriter called"
				 + " with all null arguments.\n");
				System.exit(1);
			}
		}
		catch (Exception ee)
		{
			pw = null;
			errorMessage = ee.toString();
		}
		return pw;
	}
	public static PrintWriter newPrintWriter(OutputStream object)
	{
		return newPrintWriter(object, null, null, false);
	}
	public static PrintWriter newPrintWriter(OutputStream object, boolean autoflush)
	{
		return newPrintWriter(object, null, null, autoflush);
	}
	public static PrintWriter newPrintWriter(File object)
	{
		return newPrintWriter(null, object, null, false);
	}
	public static PrintWriter newPrintWriter(Writer object)
	{
		return newPrintWriter(null, null, object, false);
	}
	public static PrintWriter newPrintWriter(Writer object, boolean autoflush)
	{
		return newPrintWriter(null, null, object, autoflush);
	}
}
