/**
* GraphFile.java class reads from and write to graph files.
* Part of the mtrL1.zip file.
*
* @author Michael Rivers
* @version January 26th, 2007
**/

import java.io.StreamTokenizer;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.Reader;
import java.io.File;
import java.io.IOException;

public class GraphFile
{
	// establish parameters
	private File file;
	private StreamTokenizer token;
	private PrintWriter ghost;
	private Reader reader;
	private FileWriter writer;
	
	/**
	* Default Constructor
	*/
	public GraphFile()
	{
		file = new File("");
	}

	/**
	* Second Constructor takes a string for the file name
	*/
	public GraphFile( String fileName )
	{
		file = new File( fileName );
	}
	
	/**
	* openForReading method instantiates token my StreamTokenizer
	*/
	public void openForReading() throws IOException
	{
		FileReader fr = new FileReader( file );
		reader = new BufferedReader( fr );
		token = new StreamTokenizer( reader );
		token.slashSlashComments( true );
	}
	
	/**
	* openForWriting method instantiates ghost my PrintWriter
	*/
	public void openForWriting() throws IOException
	{
		writer = new FileWriter( file );
		ghost = new PrintWriter( writer );
	}
	
	/**
	* closeFromReading I don't know what this should do . . . 
	*/
	public void closeFromReading() throws IOException
	{
		reader.close();
	}
	
	/**
	* closeFromWriting I don't know what this should do either . . .
	*/
	public void closeFromWriting() throws IOException
	{
		writer.close();
	}
	
	/**
	* nextInt finds the next int from the file
	*/
	public int nextInt() throws IOException
	{
		while( token.ttype != token.TT_NUMBER )
		{
			token.nextToken();
		}
		
		return (int) token.nval;
	}
	
	/**
	* nextDouble finds the next double from the file
	*/
	public double nextDouble() throws IOException
	{
		while( token.ttype != token.TT_NUMBER )
		{
			token.nextToken();
		}
		
		return (double) token.nval;
	}
	
	/**
	* print writes a string to the file
	*/
	public void print( String s )
	{
		ghost.print( s );
	}
	
	/**
	* println writes a string to the file on its own line
	*/ 
	public void println( String s )
	{
		ghost.println( s );
	}
}
