blob: 1ab6bd87913ec137038affb3612205e5e22f9611 [file] [log] [blame]
Joe Onorato1754d742016-11-21 17:51:35 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "printer.h"
18
19#include <unistd.h>
20#include <stdlib.h>
21#include <string.h>
22
23#define INITIAL_BUF_SIZE (16*1024)
24
25char const* SPACES = " ";
26const int SPACE_COUNT = strlen(SPACES);
27
28Out::Out(int fd)
29 :mOut(fd == STDOUT_FILENO ? stdout : fdopen(fd, "w")),
30 mBufSize(INITIAL_BUF_SIZE),
31 mBuf((char*)malloc(INITIAL_BUF_SIZE)),
32 mIndent(0),
33 mPendingIndent(false)
34{
35}
36
37Out::~Out()
38{
39 fclose(mOut);
40}
41
42int
43Out::reallocate(int size)
44{
45 if (size > mBufSize) {
46 char* p = (char*)malloc(size);
47 if (p != NULL) {
48 free(mBuf);
49 mBufSize = size;
50 mBuf = p;
51 return size;
52 }
53 }
54 return mBufSize;
55}
56
57void
58Out::printf(const char* format, ...)
59{
60 if (mPendingIndent) {
61 print_indent();
62 mPendingIndent = false;
63 }
64
65 int len;
66
67 va_list args;
68 va_start(args, format);
69
70 len = vsnprintf(mBuf, mBufSize, format, args);
71 bool truncated = (len >= mBufSize) && (reallocate(len) < len);
72
73 len = vsnprintf(mBuf, mBufSize, format, args);
74 va_end(args);
75
76 if (len > 0) {
77 if (mIndent == 0) {
78 fwrite(mBuf, len, 1, mOut);
79 } else {
80 char* last = mBuf;
81 char* p;
82 do {
83 p = strchr(last, '\n');
84 int size = p != NULL ? p - last + 1 : strlen(last);
85 fwrite(last, size, 1, mOut);
86 if (p != NULL) {
87 if (p[1] == '\0') {
88 mPendingIndent = true;
89 } else {
90 print_indent();
91 }
92 }
93 last = p+1;
94 } while (p != NULL);
95 }
96 }
97}
98
99void
100Out::indent()
101{
102 mPendingIndent = true;
103 mIndent += 2;
104}
105
106void
107Out::dedent()
108{
109 if (mIndent > 0) {
110 mIndent -= 2;
111 }
112}
113
114void
115Out::print_indent()
116{
117#if 0
118 fprintf(mOut, "[%d]", mIndent);
119#else
120 int indent = mIndent;
121 while (indent > SPACE_COUNT) {
122 fwrite(SPACES, SPACE_COUNT, 1, mOut);
123 indent -= SPACE_COUNT;
124 }
125 fwrite(SPACES + SPACE_COUNT - indent, indent, 1, mOut);
126#endif
127}