summaryrefslogtreecommitdiff
path: root/clformat/src/main/java/bjc/utils/ioutils/format/directives/TabulateDirective.java
blob: a9814baba66cbcb42be80ccfeaf4e88401542d0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package bjc.utils.ioutils.format.directives;

import java.io.*;

import bjc.esodata.*;
import bjc.utils.ioutils.ReportWriter;
import bjc.utils.ioutils.format.*;

/**
 * Implementation of the T directive, which is used for some formatting based
 * controls.
 *
 * @author bjculkin
 *
 */
public class TabulateDirective implements Directive {
	@Override
	public Edict compile(CompileContext compCTX) {
		CLValue colinc = CLValue.nil();
		CLValue colid = CLValue.nil();

		CLParameters params = compCTX.decr.parameters;
		CLModifiers mods = compCTX.decr.modifiers;

		if (params.length() > 2) {
			params.mapIndex("colinc", 1);

			colinc = params.resolveKey("colinc");
		}

		boolean isRelative = mods.atMod;
		boolean fromIndent = mods.colonMod;

		if (isRelative) {
			if (params.length() > 1) {
				params.mapIndices("colrel");

				colid = params.resolveKey("colrel");
			}
		} else {
			if (params.length() > 1) {
				params.mapIndices("colnum");

				colid = params.resolveKey("colnum");
			}
		}

		return new TabulateEdict(isRelative, colinc, fromIndent, colid);
	}
}

class TabulateEdict implements Edict {
	private boolean isRelative;
	private boolean fromIndent;

	private CLValue colincVal;
	private CLValue colidVal;

	public TabulateEdict(boolean isRelative, CLValue colinc, boolean fromIndent,
			CLValue colid) {
		this.isRelative = isRelative;
		this.fromIndent = fromIndent;

		this.colincVal = colinc;

		this.colidVal = colid;
	}

	@Override
	public void format(FormatContext formCTX) throws IOException {
		ReportWriter writer = formCTX.writer;

		Tape<Object> itms = formCTX.items;

		char padchar = ' ';

		int currCol;

		if (fromIndent) currCol = writer.getIndentPos();
		else            currCol = writer.getLinePos();

		if (isRelative) {
			int colinc = colincVal.asInt(itms, "column increment", "T", 1);
			int colrel = colidVal.asInt(itms, "relative column number", "T", 1);

			for (int i = 0; i < colrel; i++) writer.write(padchar);

			int nSpaces = 0;

			while ((currCol + nSpaces) % colinc != 0) {
				writer.write(padchar);

				nSpaces++;
			}
		} else {
			int colinc = colincVal.asInt(itms, "column increment", "T", 1);
			int colnum = colidVal.asInt(itms, "column number", "T", 1);

			if (currCol < colnum) {
				for (int i = currCol; i < colnum; i++) writer.write(padchar);
			} else {
				if (colinc == 0) return;

				int k = 0;

				while (colnum > (currCol + (k * colinc))) {
					writer.write(padchar);

					k++;
				}
			}
		}
	}
}