-
Notifications
You must be signed in to change notification settings - Fork 6
/
PaddedPrinter.java
103 lines (79 loc) · 2.29 KB
/
PaddedPrinter.java
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
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS */
/* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,*/
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL */
/* SECURITY EXPLORATIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF */
/* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
package agsecres.tool;
import agsecres.tool.*;
import java.lang.*;
import java.util.*;
public class PaddedPrinter {
public static class PrintLevel {
int pad;
String header;
String prefix;
public PrintLevel(int pad, String header, String prefix) {
this.pad = pad;
this.header = header;
this.prefix = prefix;
}
public int pad() {
return pad;
}
public String header() {
return header;
}
public String prefix() {
return prefix;
}
}
static Stack<PrintLevel> levels = new Stack<PrintLevel>();
int lvl;
int pad;
public PaddedPrinter() {
lvl = 0;
pad = 0;
}
public void pad(int cnt, String header, String prefix) {
pad += cnt;
levels.push(new PrintLevel(pad, header, prefix));
lvl++;
if (header != null) {
String line = Utils.pad(pad) + header;
Utils.outputln(line);
}
}
public void pad(int cnt) {
pad(cnt, "");
}
public void pad(int cnt, String prefix) {
pad(cnt, null, prefix);
}
public PrintLevel leave() {
levels.pop();
PrintLevel pl = peek();
pad = pl.pad();
lvl--;
return pl;
}
public PrintLevel peek() {
PrintLevel pl = levels.peek();
return pl;
}
public static PaddedPrinter getInstance() {
PaddedPrinter pp = new PaddedPrinter();
pp.pad(0, "", "");
return pp;
}
public void println(String s) {
PrintLevel pl = peek();
String line = Utils.pad(pl.pad()) + pl.prefix() + s;
Utils.outputln(line);
}
public void printhex(String s, byte data[]) {
PrintLevel pl = peek();
Utils.print_buf(pl.pad(), s, data);
}
}