summaryrefslogtreecommitdiff
path: root/java/src/IceUtil/OutputBase.java
blob: beb87409879cad552bc3f35e8586e0ce4e5e871e (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// **********************************************************************
//
// Copyright (c) 2002
// Mutable Realms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************

package IceUtil;

public class OutputBase
{
    public
    OutputBase()
    {
        _out = null;
        _pos = 0;
        _indent = 0;
        _indentSize = 4;
        _useTab = true;
        _separator = true;
    }

    public
    OutputBase(java.io.PrintWriter out)
    {
        _out = out;
        _pos = 0;
        _indent = 0;
        _indentSize = 4;
        _useTab = true;
        _separator = true;
    }

    public
    OutputBase(String s)
    {
        _out = null;
        _pos = 0;
        _indent = 0;
        _indentSize = 4;
        _useTab = true;
        _separator = true;

        open(s);
    }

    public void
    setIndent(int indentSize)
    {
        _indentSize = indentSize;
    }

    public void
    setUseTab(boolean useTab)
    {
        _useTab = useTab;
    }

    public void
    open(String s)
    {
        try
        {
            java.io.FileWriter fw = new java.io.FileWriter(s);
            java.io.BufferedWriter bw = new java.io.BufferedWriter(fw);
            _out = new java.io.PrintWriter(bw);
        }
        catch(java.io.IOException ex)
        {
        }
    }

    public void
    print(String s)
    {
        final char[] arr = s.toCharArray();
        for(int i = 0; i < arr.length; i++)
        {
            if(arr[i] == '\n')
            {
                _pos = 0;
            }
            else
            {
                ++_pos;
            }
        }

        _out.print(s);
    }

    public void
    inc()
    {
        _indent += _indentSize;
    }

    public void
    dec()
    {
        assert(_indent >= _indentSize);
        _indent -= _indentSize;
    }

    public void
    useCurrentPosAsIndent()
    {
        _indentSave.addFirst(new Integer(_indent));
        _indent = _pos;
    }

    public void
    zeroIndent()
    {
        _indentSave.addFirst(new Integer(_indent));
        _indent = 0;
    }

    public void
    restoreIndent()
    {
        assert(!_indentSave.isEmpty());
        _indent = ((Integer)_indentSave.removeFirst()).intValue();
    }

    public void
    nl()
    {
        _out.println();
        _pos = 0;
        _separator = true;

        int indent = _indent;

        if(_useTab)
        {
            while(indent >= 8)
            {
                indent -= 8;
                _out.print('\t');
                _pos += 8;
            }
        }
        else
        {
            while(indent >= _indentSize)
            {
                indent -= _indentSize;
                _out.print("    ");
                _pos += _indentSize;
            }
        }

        while(indent > 0)
        {
            --indent;
            _out.print(' ');
            ++_pos;
        }

        _out.flush();
    }

    public void
    sp()
    {
        if(_separator)
        {
            _out.println();
        }
    }

    public boolean
    valid()
    {
        return (_out != null);
    }

    protected java.io.PrintWriter _out;
    protected int _pos;
    protected int _indent;
    protected int _indentSize;
    protected java.util.LinkedList _indentSave = new java.util.LinkedList();
    protected boolean _useTab;
    protected boolean _separator;

}