Fmt2public class Fmt2 extends Fmt Fmt - format text (like Berkeley UNIX fmt, with a few troff commands). |
Fields Summary |
---|
public static final int | COLWIDTHThe maximum column width | protected final int | MODE_FIThe constant for formatted mode | protected final int | MODE_NFThe constant for UNformatted mode | protected int | modeThe current formatting mode | protected int | colThe current output column. | Command[] | commandsThe Array of commands |
Constructors Summary |
---|
public Fmt2(String fname)Construct a Formatter given a filename, or "-" for stdin
super(fname);
|
Methods Summary |
---|
public void | format()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 void | main(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 void | put(java.lang.String s)Put a string to the output
System.out.print(s);
| protected void | putln(java.lang.String s)Put a string to the output, with a newline, i.e., output a newline
System.out.println(s);
| protected void | putln()Put the null string to the output as a line
System.out.println();
| void | spaceLine(int nLines)
if (col>0) {
putln(); // output blank line
col = 0;
}
for (int i=0; i<nLines; i++)
putln();
|
|