Sierra Toolkit  Version of the Day
PrintTable.cpp
1 /*------------------------------------------------------------------------*/
2 /* Copyright 2010 Sandia Corporation. */
3 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */
4 /* license for use of this work by or on behalf of the U.S. Government. */
5 /* Export of this program may require a license from the */
6 /* United States Government. */
7 /*------------------------------------------------------------------------*/
8 
9 #include <string>
10 #include <vector>
11 #include <sstream>
12 #include <iomanip>
13 
14 #include <stk_util/diag/PrintTable.hpp>
15 #include <stk_util/diag/Writer.hpp>
16 #include <iostream>
17 
18 namespace stk_classic {
19 
20 void
21 PrintTable::transpose_table() const
22 {}
23 
24 
25 void
26 PrintTable::calculate_column_widths() const
27 {
28  ColumnWidthVector column_width_set;
29 
30  // loop over the headers and find the longest field for each column by size and from m_columnWidth
31  for (Table::const_iterator row_it = m_header.begin(); row_it != m_header.end(); ++row_it) {
32  if ((*row_it).size() > m_columnWidth.size())
33  m_columnWidth.resize((*row_it).size(), 0);
34  if ((*row_it).size() > column_width_set.size())
35  column_width_set.resize((*row_it).size(), 0);
36 
37  int i = 0;
38  for (Row::const_iterator cell_it = (*row_it).begin(); cell_it != (*row_it).end(); ++cell_it, ++i) {
39  m_columnWidth[i] = std::max(m_columnWidth[i], (*cell_it).m_string.size());
40  column_width_set[i] = std::max(column_width_set[i], (*cell_it).m_width);
41  }
42  }
43 
44  // loop over the table and find the longest field for each column by size and from m_columnWidth
45  for (Table::const_iterator row_it = m_table.begin(); row_it != m_table.end(); ++row_it) {
46  if ((*row_it).size() > m_columnWidth.size())
47  m_columnWidth.resize((*row_it).size(), 0);
48  if ((*row_it).size() > column_width_set.size())
49  column_width_set.resize((*row_it).size(), 0);
50 
51  int i = 0;
52  for (Row::const_iterator cell_it = (*row_it).begin(); cell_it != (*row_it).end(); ++cell_it, ++i) {
53  m_columnWidth[i] = std::max(m_columnWidth[i], (*cell_it).m_string.size());
54  column_width_set[i] = std::max(column_width_set[i], (*cell_it).m_width);
55  }
56  }
57 
58  // choose m_width width over size() width for each column
59  m_tableWidth = 0;
60  for (ColumnWidthVector::size_type i = 0; i < m_columnWidth.size(); ++i) {
61  if (column_width_set[i] != 0)
62  m_columnWidth[i] = column_width_set[i];
63  m_tableWidth += m_columnWidth[i] + 1;
64  }
65 }
66 
67 
68 PrintTable &
69 PrintTable::at(
70  size_t row,
71  size_t col)
72 {
73  for (Table::size_type i = m_table.size(); i <= row; ++i)
74  m_table.push_back(Row());
75  for (Row::size_type i = m_table[row].size(); i <= col; ++i)
76  m_table[row].push_back(Cell());
77 
78  m_currentCell.m_string = std::string(m_currentCell.m_indent*2, ' ') + m_currentString.str();
79  m_table[row][col] = m_currentCell;
80 
81  m_currentCell = Cell();
82  m_currentString.str("");
83 
84  return *this;
85 }
86 
87 
88 PrintTable &
89 PrintTable::end_col()
90 {
91  m_currentCell.m_string = std::string(m_currentCell.m_indent*2, ' ') + m_currentString.str();
92  m_table.back().push_back(m_currentCell);
93  if (m_table.size() > 1 && m_table[0].size() <= m_table.back().size()) {
94  m_currentCell.m_string = "";
95  m_currentCell.m_flags = 0;
96  m_currentCell.m_justification = m_table[0][m_table[0].size() - 1].m_justification;
97  m_currentCell.m_width = m_table[0][m_table[0].size() - 1].m_width;
98  m_currentCell.m_indent = m_table[0][m_table[0].size() - 1].m_indent;
99  }
100  else {
101  m_currentCell = Cell();
102  }
103  m_currentString.str("");
104 
105  return *this;
106 }
107 
108 
109 PrintTable &
110 PrintTable::end_row()
111 {
112  if (!m_currentString.str().empty())
113  end_col();
114  m_table.push_back(Row());
115  return *this;
116 }
117 
118 
119 std::ostream &
121  std::ostream & os) const
122 {
123  if (m_flags & COMMA_SEPARATED_VALUES)
124  csvPrint(os);
125 
126  else {
127  if (m_flags & PRINT_TRANSPOSED)
128  transpose_table();
129 
130  calculate_column_widths();
131 
132  if (!m_title.empty()) {
133  int prespaces = 0;
134 
135  if(m_title.length() < m_tableWidth)
136  prespaces = (m_tableWidth - m_title.length())/2;
137 
138  os << m_commentPrefix;
139  os << std::left << std::setw(prespaces) << "" << m_title << '\n';
140  }
141 
142  for (Table::const_iterator row_it = m_header.begin(); row_it != m_header.end(); ++row_it) {
143  os << m_commentPrefix;
144  printRow(os, *row_it);
145  os << '\n';
146  }
147 
148  if (m_header.size() > 0) {
149  os << m_commentPrefix;
150  printHeaderBar(os);
151  os << '\n';
152  }
153 
154  for (Table::const_iterator row_it = m_table.begin(); row_it != m_table.end(); ++row_it) {
155  os << std::left << std::setw(m_commentPrefix.size()) << "";
156  printRow(os, *row_it);
157  os << '\n';
158  }
159  }
160 
161  return os;
162 }
163 
164 
165 std::ostream &
166 PrintTable::printRow(
167  std::ostream & os,
168  const Row & row) const
169 {
170  int i = 0;
171  int postspaces = 0;
172  for (Row::const_iterator cell_it = row.begin(); cell_it != row.end(); ++cell_it, ++i) {
173  os // << postspaces << ", "
174  << std::left << std::setw(postspaces) << "";
175  postspaces = 0;
176 
177  if (cell_it != row.begin())
178  os << " ";
179 
180  if ((*cell_it).m_flags & Cell::SPAN)
181  os << (*cell_it).m_string;
182  else if ((*cell_it).m_string.length() > m_columnWidth[i]) {
183  if ((*cell_it).m_justification & Cell::ENDS) {
184  int front_end = m_columnWidth[i]/4;
185  int back_begin = (*cell_it).m_string.size() - (m_columnWidth[i] - front_end);
186  os << (*cell_it).m_string.substr(0, front_end - 3) + "..." + (*cell_it).m_string.substr(back_begin, (*cell_it).m_string.size());
187  }
188  else { // if ((*cell_it).m_justification & Cell::TRUNC) {
189  os << (*cell_it).m_string.substr(0, m_columnWidth[i]);
190  }
191  }
192  else {
193  if ((*cell_it).m_string.length() == 0)
194  postspaces = m_columnWidth[i];
195  else if (((*cell_it).m_justification & Cell::JUSTIFY_MASK) == Cell::LEFT) {
196  postspaces = m_columnWidth[i] - (*cell_it).m_string.length();
197  os // << m_columnWidth[i] << ", " << postspaces << ", "
198  << std::left << (*cell_it).m_string;
199  }
200  else if (((*cell_it).m_justification & Cell::JUSTIFY_MASK) == Cell::CENTER) {
201  int prespaces = (m_columnWidth[i] - (*cell_it).m_string.length())/2;
202  postspaces = m_columnWidth[i] - (*cell_it).m_string.length() - prespaces;
203  os // << prespaces << " " << postspaces << ", "
204  << std::left << std::setw(prespaces) << "" << (*cell_it).m_string;
205  }
206  else // if (((*cell_it).m_justification & Cell::JUSTIFY_MASK) == Cell::RIGHT)
207  os // << m_columnWidth[i] << ", "
208  << std::right << std::setw(m_columnWidth[i]) << (*cell_it).m_string;
209  }
210  }
211 
212  return os;
213 }
214 
215 
216 std::ostream &
217 PrintTable::printHeaderBar(
218  std::ostream & os) const
219 {
220  os << std::setfill('-');
221 
222  for (ColumnWidthVector::size_type i = 0; i < m_columnWidth.size(); ++i) {
223  if (i != 0)
224  os << " ";
225  os << std::setw(m_columnWidth[i]) << "";
226  }
227  os << std::setfill(' ');
228 
229  return os;
230 }
231 
232 
233 std::ostream &
234 PrintTable::csvPrint(
235  std::ostream & os) const
236 {
237  if (!m_title.empty())
238  os << m_title << '\n';
239 
240  for (Table::const_iterator row_it = m_header.begin(); row_it != m_header.end(); ++row_it) {
241  const Row &row = (*row_it);
242  for (Row::const_iterator cell_it = row.begin(); cell_it != row.end(); ++cell_it) {
243  if (cell_it != row.begin())
244  os << ",";
245  os << (*cell_it).m_string;
246  }
247  os << '\n';
248  }
249 
250  for (Table::const_iterator row_it = m_table.begin(); row_it != m_table.end(); ++row_it) {
251  const Row &row = (*row_it);
252  for (Row::const_iterator cell_it = row.begin(); cell_it != row.end(); ++cell_it) {
253  if (cell_it != row.begin())
254  os << ",";
255  os << (*cell_it).m_string;
256  }
257  os << '\n';
258  }
259 
260  return os;
261 }
262 
263 
264 diag::Writer &
265 PrintTable::verbose_print(
266  diag::Writer & dout) const
267 {
268 // const ColumnWidthVector &column_width = calculate_column_widths();
269 
270 // for (Table::const_iterator row_it = m_header.begin(); row_it != m_header.end(); ++row_it) {
271 // printRow(os, *row_it);
272 // os << '\n';
273 // }
274 
275 // if (m_header.size() > 0)
276 // printHeaderBar(os);
277 
278 // for (Table::const_iterator row_it = m_table.begin(); row_it != m_table.end(); ++row_it) {
279 // int i = 0;
280 // for (Row::const_iterator cell_it = (*row_it).begin(); cell_it != (*row_it).end(); ++cell_it, ++i)
281 // if ((*cell_it).m_flags & Cell::SPAN)
282 // dout << (*cell_it).m_string;
283 // else
284 // dout << std::setw(column_width[i]) << (*cell_it).m_string;
285 // dout << dendl;
286 // }
287 
288  calculate_column_widths();
289 
290  dout << m_title << std::endl;
291 
292  for (Table::const_iterator row_it = m_header.begin(); row_it != m_header.end(); ++row_it) {
293  dout << "";
294  printRow(dout.getStream(), *row_it);
295  dout << diag::dendl;
296  }
297 
298  if (m_header.size() > 0) {
299  dout << "";
300  printHeaderBar(dout.getStream());
301  dout << diag::dendl;
302  }
303 
304  for (Table::const_iterator row_it = m_table.begin(); row_it != m_table.end(); ++row_it) {
305  dout << "";
306  printRow(dout.getStream(), *row_it);
307  dout << diag::dendl;
308  }
309 
310  return dout;
311 }
312 
313 } // namespace stk_classic
std::ostream & print(std::ostream &os, const std::string &indent, const Bucket &bucket)
Print the parts and entities of this bucket.
Definition: Bucket.cpp:259
std::ostream & dout()
Diagnostic output stream.
Definition: OutputLog.cpp:674
_setfill setfill(char fill)
Function setfill sets the fill character as a manipulator.
Definition: WriterManip.hpp:96
Writer & dendl(Writer &dout)
Writer function dendl calls the Writer::dendl manipulator.
Definition: Writer.hpp:520
_setw setw(int width)
Function setw sets the width for the next field as a manipulator.
Definition: WriterManip.hpp:44
Sierra Toolkit.