This repository has been archived by the owner on May 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathtprinter.cc
189 lines (168 loc) · 4.48 KB
/
tprinter.cc
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
// Copyright (c) 2015, Baidu.com, Inc. All Rights Reserved
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Author: Xu Peilin ([email protected])
#include "tprinter.h"
#include <stdarg.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#include "string_util.h"
#include <assert.h>
namespace baidu {
namespace common {
TPrinter::TPrinter() :
kMaxColWidth(50),
_cols(0) {
}
TPrinter::TPrinter(int cols) :
kMaxColWidth(50),
_cols(cols) {
if (cols > 0) {
_col_width.resize(cols, 0);
}
}
TPrinter::TPrinter(int cols, int width) :
kMaxColWidth(width),
_cols(cols) {
assert(width >= 0);
if (cols > 0) {
_col_width.resize(cols, 0);
}
}
TPrinter::~TPrinter() {}
bool TPrinter::AddRow(const std::vector<string>& cols) {
if (cols.size() != _cols) {
std::cerr << "arg num error: " << cols.size() << " vs " << _cols << std::endl;
return false;
}
Line line;
for (size_t i = 0; i < cols.size(); ++i) {
string item = cols[i];
if (item.size() > kMaxColWidth) {
item = item.substr(0, kMaxColWidth);
}
if (item.size() > static_cast<uint32_t>(_col_width[i])) {
_col_width[i] = item.size();
}
if (item.size() == 0) {
item = "-";
}
line.push_back(item);
}
_table.push_back(line);
return true;
}
bool TPrinter::AddRow(int argc, ...) {
if (static_cast<uint32_t>(argc) != _cols) {
std::cerr << "arg num error: " << argc << " vs " << _cols << std::endl;
return false;
}
std::vector<string> v;
va_list args;
va_start(args, argc);
for (int i = 0; i < argc; ++i) {
string item = va_arg(args, char*);
v.push_back(item);
}
va_end(args);
return AddRow(v);
}
bool TPrinter::AddRow(const std::vector<int64_t>& cols) {
if (cols.size() != _cols) {
std::cerr << "arg num error: " << cols.size() << " vs " << _cols << std::endl;
return false;
}
std::vector<string> v;
for (size_t i = 0; i < cols.size(); ++i) {
v.push_back(common::NumToString(cols[i]));
}
return AddRow(v);
}
void TPrinter::Print(bool has_head) {
if (_table.size() < 1) {
return;
}
int line_len = 0;
for (size_t i = 0; i < _cols; ++i) {
line_len += 2 + _col_width[i];
std::cout << " " << std::setfill(' ')
<< std::setw(_col_width[i])
<< std::setiosflags(std::ios::left)
<< _table[0][i];
}
std::cout << std::endl;
if (has_head) {
for (int i = 0; i < line_len + 2; ++i) {
std::cout << "-";
}
std::cout << std::endl;
}
for (size_t i = 1; i < _table.size(); ++i) {
for (size_t j = 0; j < _cols; ++j) {
std::cout << " " << std::setfill(' ')
<< std::setw(_col_width[j])
<< std::setiosflags(std::ios::left)
<< _table[i][j];
}
std::cout << std::endl;
}
}
string TPrinter::ToString(bool has_head) {
std::ostringstream ostr;
if (_table.size() < 1) {
return "";
}
int line_len = 0;
for (size_t i = 0; i < _cols; ++i) {
line_len += 2 + _col_width[i];
ostr << " " << std::setfill(' ')
<< std::setw(_col_width[i])
<< std::setiosflags(std::ios::left)
<< _table[0][i];
}
ostr << std::endl;
if (has_head) {
for (int i = 0; i < line_len + 2; ++i) {
ostr << "-";
}
ostr << std::endl;
}
for (size_t i = 1; i < _table.size(); ++i) {
for (size_t j = 0; j < _cols; ++j) {
ostr << " " << std::setfill(' ')
<< std::setw(_col_width[j])
<< std::setiosflags(std::ios::left)
<< _table[i][j];
}
ostr << std::endl;
}
return ostr.str();
}
void TPrinter::Reset() {
std::vector<int> tmp(_cols, 0);
_col_width.swap(tmp);
_table.clear();
}
void TPrinter::Reset(int cols) {
_cols = cols;
Reset();
}
string TPrinter::RemoveSubString(const string& input, const string& substr) {
string ret;
string::size_type p = 0;
string tmp = input;
while (1) {
tmp = tmp.substr(p);
p = tmp.find(substr);
ret.append(tmp.substr(0, p));
if (p == string::npos) {
break;
}
p += substr.size();
}
return ret;
}
} // namespace common
}