01: package it.unimi.dsi.mg4j.test;
02:
03: import it.unimi.dsi.fastutil.io.FastBufferedOutputStream;
04: import it.unimi.dsi.mg4j.index.DiskBasedIndex;
05: import it.unimi.dsi.io.FastBufferedReader;
06: import it.unimi.dsi.lang.MutableString;
07:
08: import java.io.FileOutputStream;
09: import java.io.IOException;
10: import java.io.InputStreamReader;
11: import java.io.PrintStream;
12: import java.util.regex.Matcher;
13: import java.util.regex.Pattern;
14:
15: import com.martiansoftware.jsap.JSAP;
16: import com.martiansoftware.jsap.JSAPException;
17: import com.martiansoftware.jsap.JSAPResult;
18: import com.martiansoftware.jsap.Parameter;
19: import com.martiansoftware.jsap.SimpleJSAP;
20: import com.martiansoftware.jsap.UnflaggedOption;
21:
22: /** Separates a stat file into various heights. */
23:
24: final public class SelectHeights {
25:
26: private SelectHeights() {
27: }
28:
29: public static void main(final String[] arg) throws IOException,
30: JSAPException {
31:
32: SimpleJSAP jsap = new SimpleJSAP(
33: SelectHeights.class.getName(),
34: "Prints or selects parts of a stat file using global counts.",
35: new Parameter[] {
36: new UnflaggedOption("basename",
37: JSAP.STRING_PARSER, JSAP.REQUIRED,
38: "The basename of the height files."),
39: new UnflaggedOption("h", JSAP.INTEGER_PARSER,
40: JSAP.REQUIRED,
41: "The maximum height to scan.") });
42:
43: JSAPResult jsapResult = jsap.parse(arg);
44: if (jsap.messagePrinted())
45: return;
46: Pattern pattern = Pattern.compile("\t");
47: Matcher matcher = pattern.matcher("");
48:
49: final int h = jsapResult.getInt("h");
50: final String basename = jsapResult.getString("basename");
51: final PrintStream printStream[] = new PrintStream[h + 1];
52: for (int i = 0; i <= h; i++)
53: printStream[i] = new PrintStream(
54: new FastBufferedOutputStream(new FileOutputStream(
55: basename + "-" + i
56: + DiskBasedIndex.STATS_EXTENSION)));
57:
58: final MutableString line = new MutableString();
59: final FastBufferedReader reader = new FastBufferedReader(
60: new InputStreamReader(System.in));
61:
62: int l, start;
63: while (reader.readLine(line) != null) {
64: if (line.charAt(0) == '#')
65: continue;
66: matcher.reset(line);
67: l = 0;
68: start = 0;
69: while (matcher.find() && l <= h) {
70: printStream[l].println(line.subSequence(start, matcher
71: .start()));
72: start = matcher.end();
73: l++;
74: }
75: }
76:
77: for (int i = 0; i <= h; i++)
78: printStream[i].close();
79: }
80: }
81:
82: // Local Variables:
83: // mode: jde
84: // tab-width: 4
85: // End:
|