forked from mit-ll/LL-DLEP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable.cpp
More file actions
108 lines (92 loc) · 2.19 KB
/
Table.cpp
File metadata and controls
108 lines (92 loc) · 2.19 KB
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
/*
* Dynamic Link Exchange Protocol (DLEP)
*
* Copyright (C) 2015, 2016 Massachusetts Institute of Technology
*/
#include <iomanip>
#include "Table.h"
Table::Table(const std::vector<std::string> & headings)
{
table.push_back(headings);
current_row = 1;
current_column = 0;
}
void
Table::add_field(const std::string & value)
{
while (current_row >= table.size())
{
Row new_row;
new_row.resize(table[0].size());
table.push_back(new_row);
}
table[current_row][current_column++] = value;
}
void
Table::add_field(const std::string & field_name, const std::string & value)
{
const Row & headings = table[0];
// XXX vector doesn't have find() ???
for (current_column = 0; current_column < headings.size(); current_column++)
{
if (headings[current_column] == field_name)
{
break;
}
}
add_field(value);
}
void
Table::finish_row(bool force_empty_row)
{
if ( (current_column > 0) || force_empty_row)
{
current_row++;
current_column = 0;
}
}
unsigned int
Table::get_row_index()
{
return current_row;
}
void
Table::set_row_index(unsigned int ri)
{
current_row = ri;
}
void
Table::set_row_index_end()
{
current_row = table.size();
}
void Table::print(std::ostream & os)
{
// The first row has the column headings. There must be one
// for each possible column.
std::vector<std::size_t> column_widths(table[0].size(), 0);
// Figure out how wide columns need to be to print nicely.
for (const auto & row : table)
{
for (unsigned int ci = 0; ci < row.size(); ci++)
{
column_widths[ci] = std::max(column_widths[ci],
row[ci].length());
}
}
// Increase each column width by 1 to leave a space between columns.
for (unsigned int ci = 0; ci < column_widths.size(); ci++)
{
column_widths[ci]++;
}
// Print out all of the rows.
os << std::left;
for (const auto & row : table)
{
for (unsigned int ci = 0; ci < row.size(); ci++)
{
os << std::setw(column_widths[ci]) << row[ci];
}
os << std::endl;
}
}