summaryrefslogtreecommitdiffstats
path: root/tools/aapt/IndentPrinter.h
blob: 6fc94bc927b8ea60e45ef5429806dd8371191d23 (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
#ifndef __INDENT_PRINTER_H
#define __INDENT_PRINTER_H

class IndentPrinter {
public:
    IndentPrinter(FILE* stream, int indentSize=2)
        : mStream(stream)
        , mIndentSize(indentSize)
        , mIndent(0)
        , mNeedsIndent(true) {
    }

    void indent(int amount = 1) {
        mIndent += amount;
        if (mIndent < 0) {
            mIndent = 0;
        }
    }

    void print(const char* fmt, ...) {
        doIndent();
        va_list args;
        va_start(args, fmt);
        vfprintf(mStream, fmt, args);
        va_end(args);
    }

    void println(const char* fmt, ...) {
        doIndent();
        va_list args;
        va_start(args, fmt);
        vfprintf(mStream, fmt, args);
        va_end(args);
        fputs("\n", mStream);
        mNeedsIndent = true;
    }

    void println() {
        doIndent();
        fputs("\n", mStream);
        mNeedsIndent = true;
    }

private:
    void doIndent() {
        if (mNeedsIndent) {
            int numSpaces = mIndent * mIndentSize;
            while (numSpaces > 0) {
                fputs(" ", mStream);
                numSpaces--;
            }
            mNeedsIndent = false;
        }
    }

    FILE* mStream;
    const int mIndentSize;
    int mIndent;
    bool mNeedsIndent;
};

#endif // __INDENT_PRINTER_H