FileDocCategorySizeDatePackage
EnTab.javaAPI DocExample2653Thu Mar 29 14:10:12 BST 2001None

EnTab

public class EnTab extends Object
entab- replace blanks by tabs and blanks. Transmuted from K&R Software Tools book into C. Transmuted again, years later, into Java.
author
Ian F. Darwin, ian@darwinsys.com
version
$Id: EnTab.java,v 1.6 2001/03/29 18:10:12 ian Exp $

Fields Summary
protected Tabs
tabHandler
The Tabs (tab logic handler)
public static int
EOF
A symbolic constant for end-of-file
Constructors Summary
public EnTab(int n)
Constructor: just save the tab values.

arguments
n The number of spaces each tab is to replace.


	                 	 
	   
		tabHandler = new Tabs(n);
	
Methods Summary
public voidentab(java.io.BufferedReader is)
entab: process one entire file, replacing blanks with tabs.

argument
is A BufferedReader opened to the file to be read.

		String line;
		int c, col = 0, newcol;

		// main loop: process entire file one char at a time.
		do {
			newcol = col;
			// If we get a space, increment column count; if this
			// takes us to a tab stop, output a tab character.
			while ((c = is.read()) == ' ") {
				Debug.println("space", "Got space at " + col);
				newcol++;
				if (tabHandler.tabpos(newcol)) {
					Debug.println("tab", "Got a Tab Stop " + newcol);
					putchar('\t");
					col = newcol;
				}
			}
			// If we're just past a tab stop, we need to put the
			// "leftover" spaces back out, since we just consumed 
			// them in the "while c ... == ' ')" loop above.
			while (col < newcol) {
				Debug.println("pad", "Padding space at " + col);
				putchar(' ");
				col++;
			}
			Debug.println("out", "End of loop, c is " + c);

			// Now either we're at the end of the input file,
			// or we have a plain character to output.
			// If the "plain" char happens to be \r or \n, then
			// output it, but also set col back to 1.
			// This code for \r and \n should satisfy UNIX, Mac and MS.
			if (c != EOF) {
				putchar(c);
				col = (c == '\n" || c == '\r" ? 1 : col + 1);
			}
		} while (c != EOF);
		System.out.flush();	// output everything for this file.
	
public static voidmain(java.lang.String[] argv)
Main program: just create an EnTab program, and pass the standard input or the named file(s) through it.

		EnTab et = new EnTab(8);
		if (argv.length == 0)	// do standard input
			et.entab(new BufferedReader(
				new InputStreamReader(System.in)));
		else for (int i=0; i<argv.length; i++) {	// do each file
			et.entab(new BufferedReader(new FileReader(argv[i])));
		}
	
protected voidputchar(int ch)
putchar - convenience routine for printing one character

		System.out.print((char)ch);