FileDocCategorySizeDatePackage
Fmt2.javaAPI DocExample3038Sun Feb 08 21:34:04 GMT 2004None

Fmt2

public class Fmt2 extends Fmt
Fmt - format text (like Berkeley UNIX fmt, with a few troff commands).
author
Ian F. Darwin, http://www.darwinsys.com/
version
$Id: Fmt2.java,v 1.6 2004/02/09 03:34:03 ian Exp $

Fields Summary
public static final int
COLWIDTH
The maximum column width
protected final int
MODE_FI
The constant for formatted mode
protected final int
MODE_NF
The constant for UNformatted mode
protected int
mode
The current formatting mode
protected int
col
The current output column.
Command[]
commands
The Array of commands
Constructors Summary
public Fmt2(String fname)
Construct a Formatter given a filename, or "-" for stdin

		super(fname);
	
Methods Summary
public voidformat()
Format the File contained in a constructed Fmt object


	          
	     
		String w, f;
		col = 0;
outer:
		while ((w = in.readLine()) != null) {
			if (w.length() == 0) {	// null line
				spaceLine(1);
				continue;
			}
			if (w.startsWith(".")) {// troff command, handle it.
				for (int i=0; i<commands.length; i++) {
					Command v = commands[i];
					if (v.cmdName.equals(w.substring(1, 3))) {
						v.action();
						continue outer;
					} 
				}
				// else an unrecognized troff command
				if (col>0) putln();	// flush
				putln(w);
				col = 0;
				continue;
			}

			// otherwise it's text, so deal with it.
			if (mode == MODE_NF) {
				putln(w);
				continue;
			}

			// else, we have to format it.
			StringTokenizer st = new StringTokenizer(w);
			while (st.hasMoreTokens()) {
				f = st.nextToken();

				if (col + f.length() > COLWIDTH) {
					putln();
					col = 0;
				}
				put(f + " ");
				col += f.length() + 1;
			}
		}
		if (col>0) putln();
		in.close();
	
public static voidmain(java.lang.String[] av)


	       
		if (av.length == 0)
			new Fmt2("-").format();
		else for (int i=0; i<av.length; i++)
			new Fmt2(av[i]).format();
	
protected voidput(java.lang.String s)
Put a string to the output

		System.out.print(s);
	
protected voidputln(java.lang.String s)
Put a string to the output, with a newline, i.e., output a newline

		System.out.println(s);
	
protected voidputln()
Put the null string to the output as a line

		System.out.println();
	
voidspaceLine(int nLines)

		if (col>0) {
			putln();	// output blank line
			col = 0;
		}
		for (int i=0; i<nLines; i++)
			putln();