import java.io.*;

/**
 * A filter which expands tabs
 * <pre>
 *    This is what Java/C++ can look like
 *        we have several constructors. 
 *        1. A default constructor which uses a tabstop of 8
 *        2. A constructor which takes tabstop as a parameter.
 * </pre>
 *
 * @version 1.3 04/19/98
 * @author sfb
 * @see java.io.System
 *
 */
public class Tabs
{
	private int tabstop;
	private int position;
	
	public Tabs() { tabstop = 8; }
	public Tabs ( int tstop )
	{
		if ( tstop > 0 )
			tabstop = tstop;
		else
			tabstop = 8;	
	}

	private int findstop() {
		int increment = tabstop - (position % tabstop);
		position += increment;
		return increment;
	}

	public void filter ( InputStream in, OutputStream out ) throws IOException {
		int c;
		int increment;
		position = 0;

		while ( (c = in.read()) >= 0  )
			switch ( c )
			{
			// c is a tab
			case '\t':
				increment = findstop();
				//position += increment;
				for ( ; increment > 0; increment-- )
					out.write ( ' ' );
				break;
			// c is a newline
			case '\n':
				out.write ( c );
				position = 0;
				break;
			// c is anything else
			default:
				out.write ( c );
				position++;
				break;
			}
	}

	public static void main ( String av[] ) {
		int tstop = 8;;
		if ( av.length > 0 )
			try {
				tstop = Integer.parseInt ( av[0] );
			} catch ( NumberFormatException e ) {
				System.err.println ( "Usage: Tabs [tabstop]" );
				System.err.println ( "using tabstop = 8" );
			}
		try {
			Tabs t = new Tabs(tstop);
			t.filter ( System.in, System.out );
		}
		catch ( Exception e) {
			System.err.println ( "exception: " + e.getMessage() );
			e.printStackTrace();
		}
	}
}

