Sierra Toolkit Version of the Day
PrintTable.cpp
00001 /*------------------------------------------------------------------------*/
00002 /*                 Copyright 2010 Sandia Corporation.                     */
00003 /*  Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive   */
00004 /*  license for use of this work by or on behalf of the U.S. Government.  */
00005 /*  Export of this program may require a license from the                 */
00006 /*  United States Government.                                             */
00007 /*------------------------------------------------------------------------*/
00008 
00009 #include <string>
00010 #include <vector>
00011 #include <sstream>
00012 #include <iomanip>
00013 
00014 #include <stk_util/diag/PrintTable.hpp>
00015 #include <stk_util/diag/Writer.hpp>
00016 #include <iostream>
00017 
00018 namespace stk {
00019 
00020 void
00021 PrintTable::transpose_table() const
00022 {}
00023 
00024 
00025 void
00026 PrintTable::calculate_column_widths() const
00027 {
00028   ColumnWidthVector column_width_set;
00029 
00030   // loop over the headers and find the longest field for each column by size and from m_columnWidth
00031   for (Table::const_iterator row_it = m_header.begin(); row_it != m_header.end(); ++row_it) {
00032     if ((*row_it).size() > m_columnWidth.size())
00033       m_columnWidth.resize((*row_it).size(), 0);
00034     if ((*row_it).size() > column_width_set.size())
00035       column_width_set.resize((*row_it).size(), 0);
00036 
00037     int i = 0;
00038     for (Row::const_iterator cell_it = (*row_it).begin(); cell_it != (*row_it).end(); ++cell_it, ++i) {
00039       m_columnWidth[i] = std::max(m_columnWidth[i], (*cell_it).m_string.size());
00040       column_width_set[i] = std::max(column_width_set[i], (*cell_it).m_width);
00041     }
00042   }
00043 
00044   // loop over the table and find the longest field for each column by size and from m_columnWidth
00045   for (Table::const_iterator row_it = m_table.begin(); row_it != m_table.end(); ++row_it) {
00046     if ((*row_it).size() > m_columnWidth.size())
00047       m_columnWidth.resize((*row_it).size(), 0);
00048     if ((*row_it).size() > column_width_set.size())
00049       column_width_set.resize((*row_it).size(), 0);
00050 
00051     int i = 0;
00052     for (Row::const_iterator cell_it = (*row_it).begin(); cell_it != (*row_it).end(); ++cell_it, ++i) {
00053       m_columnWidth[i] = std::max(m_columnWidth[i], (*cell_it).m_string.size());
00054       column_width_set[i] = std::max(column_width_set[i], (*cell_it).m_width);
00055     }
00056   }
00057 
00058   // choose m_width width over size() width for each column
00059   m_tableWidth = 0;
00060   for (ColumnWidthVector::size_type i = 0; i < m_columnWidth.size(); ++i) {
00061     if (column_width_set[i] != 0)
00062       m_columnWidth[i] = column_width_set[i];
00063     m_tableWidth += m_columnWidth[i] + 1;
00064   }
00065 }
00066 
00067 
00068 PrintTable &
00069 PrintTable::at(
00070   size_t        row,
00071   size_t        col)
00072 {
00073   for (Table::size_type i = m_table.size(); i <= row; ++i)
00074     m_table.push_back(Row());
00075   for (Row::size_type i = m_table[row].size(); i <= col; ++i) 
00076     m_table[row].push_back(Cell());
00077   
00078   m_currentCell.m_string = std::string(m_currentCell.m_indent*2, ' ') + m_currentString.str();  
00079   m_table[row][col] = m_currentCell;
00080   
00081   m_currentCell = Cell();
00082   m_currentString.str("");
00083 
00084   return *this;
00085 }
00086 
00087 
00088 PrintTable &
00089 PrintTable::end_col()
00090 {
00091   m_currentCell.m_string = std::string(m_currentCell.m_indent*2, ' ') + m_currentString.str();
00092   m_table.back().push_back(m_currentCell);
00093   if (m_table.size() > 1 && m_table[0].size() <= m_table.back().size()) {
00094     m_currentCell.m_string = "";
00095     m_currentCell.m_flags = 0;
00096     m_currentCell.m_justification = m_table[0][m_table[0].size() - 1].m_justification;
00097     m_currentCell.m_width = m_table[0][m_table[0].size() - 1].m_width;
00098     m_currentCell.m_indent = m_table[0][m_table[0].size() - 1].m_indent;
00099  }
00100   else {
00101     m_currentCell = Cell();
00102   }
00103   m_currentString.str("");
00104 
00105   return *this;
00106 }
00107 
00108 
00109 PrintTable &
00110 PrintTable::end_row()
00111 {
00112   if (!m_currentString.str().empty())
00113     end_col();
00114   m_table.push_back(Row());
00115   return *this;
00116 }
00117 
00118 
00119 std::ostream &
00120 PrintTable::print(
00121   std::ostream &  os) const
00122 {
00123   if (m_flags & COMMA_SEPARATED_VALUES)
00124     csvPrint(os);
00125 
00126   else {
00127     if (m_flags & PRINT_TRANSPOSED)
00128       transpose_table();
00129 
00130     calculate_column_widths();
00131 
00132     if (!m_title.empty()) {
00133       int prespaces = 0;
00134 
00135       if(m_title.length() < m_tableWidth)
00136   prespaces = (m_tableWidth - m_title.length())/2;;
00137 
00138       os << m_commentPrefix;
00139       os << std::left << std::setw(prespaces) << "" << m_title << '\n';
00140     }
00141 
00142     for (Table::const_iterator row_it = m_header.begin(); row_it != m_header.end(); ++row_it) {
00143       os << m_commentPrefix;
00144       printRow(os, *row_it);
00145       os << '\n';
00146     }
00147 
00148     if (m_header.size() > 0) {
00149       os << m_commentPrefix;
00150       printHeaderBar(os);
00151       os << '\n';
00152     }
00153 
00154     for (Table::const_iterator row_it = m_table.begin(); row_it != m_table.end(); ++row_it) {
00155       os << std::left << std::setw(m_commentPrefix.size()) << "";
00156       printRow(os, *row_it);
00157       os << '\n';
00158     }
00159   }
00160 
00161   return os;
00162 }
00163 
00164 
00165 std::ostream &
00166 PrintTable::printRow(
00167   std::ostream &  os,
00168   const Row &   row) const
00169 {
00170   int i = 0;
00171   int postspaces = 0;
00172   for (Row::const_iterator cell_it = row.begin(); cell_it != row.end(); ++cell_it, ++i) {
00173     os // << postspaces << ", "
00174        << std::left << std::setw(postspaces) << "";
00175     postspaces = 0;
00176 
00177     if (cell_it != row.begin())
00178       os << " ";
00179 
00180     if ((*cell_it).m_flags & Cell::SPAN)
00181       os << (*cell_it).m_string;
00182     else if ((*cell_it).m_string.length() > m_columnWidth[i]) {
00183       if ((*cell_it).m_justification & Cell::ENDS) {
00184   int front_end = m_columnWidth[i]/4;
00185   int back_begin = (*cell_it).m_string.size() - (m_columnWidth[i] - front_end);
00186   os << (*cell_it).m_string.substr(0, front_end - 3) + "..." + (*cell_it).m_string.substr(back_begin, (*cell_it).m_string.size());
00187       }
00188       else { // if ((*cell_it).m_justification & Cell::TRUNC) {
00189   os << (*cell_it).m_string.substr(0, m_columnWidth[i]);
00190       }
00191     }
00192     else {
00193       if ((*cell_it).m_string.length() == 0)
00194   postspaces = m_columnWidth[i];
00195       else if (((*cell_it).m_justification & Cell::JUSTIFY_MASK) == Cell::LEFT) {
00196   postspaces = m_columnWidth[i] - (*cell_it).m_string.length();
00197   os // << m_columnWidth[i] << ", " << postspaces << ", "
00198     << std::left << (*cell_it).m_string;
00199       }
00200       else if (((*cell_it).m_justification & Cell::JUSTIFY_MASK) == Cell::CENTER) {
00201   int prespaces = (m_columnWidth[i] - (*cell_it).m_string.length())/2;
00202   postspaces = m_columnWidth[i] - (*cell_it).m_string.length() - prespaces;
00203   os // << prespaces << " " << postspaces << ", "
00204     << std::left << std::setw(prespaces) << "" << (*cell_it).m_string;
00205       }
00206       else // if (((*cell_it).m_justification & Cell::JUSTIFY_MASK) == Cell::RIGHT)
00207   os // << m_columnWidth[i] << ", "
00208      << std::right << std::setw(m_columnWidth[i]) << (*cell_it).m_string;
00209     }
00210   }
00211 
00212   return os;
00213 }
00214 
00215 
00216 std::ostream &
00217 PrintTable::printHeaderBar(
00218   std::ostream &  os) const
00219 {
00220   os << std::setfill('-');
00221 
00222   for (ColumnWidthVector::size_type i = 0; i < m_columnWidth.size(); ++i) {
00223     if (i != 0)
00224       os << " ";
00225     os << std::setw(m_columnWidth[i]) << "";
00226   }
00227   os << std::setfill(' ');
00228 
00229   return os;
00230 }
00231 
00232 
00233 std::ostream &
00234 PrintTable::csvPrint(
00235   std::ostream &  os) const
00236 {
00237   if (!m_title.empty())
00238     os << m_title << '\n';
00239 
00240   for (Table::const_iterator row_it = m_header.begin(); row_it != m_header.end(); ++row_it) {
00241     const Row &row = (*row_it);
00242     for (Row::const_iterator cell_it = row.begin(); cell_it != row.end(); ++cell_it) {
00243       if (cell_it != row.begin())
00244   os << ",";
00245       os << (*cell_it).m_string;
00246     }
00247     os << '\n';
00248   }
00249 
00250   for (Table::const_iterator row_it = m_table.begin(); row_it != m_table.end(); ++row_it) {
00251     const Row &row = (*row_it);
00252     for (Row::const_iterator cell_it = row.begin(); cell_it != row.end(); ++cell_it) {
00253       if (cell_it != row.begin())
00254   os << ",";
00255       os << (*cell_it).m_string;
00256     }
00257     os << '\n';
00258   }
00259 
00260   return os;
00261 }
00262 
00263 
00264 diag::Writer &
00265 PrintTable::verbose_print(
00266   diag::Writer &  dout) const
00267 {
00268 //   const ColumnWidthVector &column_width = calculate_column_widths();
00269 
00270 //   for (Table::const_iterator row_it = m_header.begin(); row_it != m_header.end(); ++row_it) {
00271 //     printRow(os, *row_it);
00272 //     os << '\n';
00273 //   }
00274 
00275 //   if (m_header.size() > 0)
00276 //     printHeaderBar(os);
00277 
00278 //   for (Table::const_iterator row_it = m_table.begin(); row_it != m_table.end(); ++row_it) {
00279 //     int i = 0;
00280 //     for (Row::const_iterator cell_it = (*row_it).begin(); cell_it != (*row_it).end(); ++cell_it, ++i)
00281 //       if ((*cell_it).m_flags & Cell::SPAN)
00282 //  dout << (*cell_it).m_string;
00283 //       else
00284 //  dout << std::setw(column_width[i]) << (*cell_it).m_string;
00285 //     dout << dendl;
00286 //   }
00287 
00288   calculate_column_widths();
00289 
00290   dout << m_title << std::endl;
00291 
00292   for (Table::const_iterator row_it = m_header.begin(); row_it != m_header.end(); ++row_it) {
00293     dout << "";
00294     printRow(dout.getStream(), *row_it);
00295     dout << diag::dendl;
00296   }
00297 
00298   if (m_header.size() > 0) {
00299     dout << "";
00300     printHeaderBar(dout.getStream());
00301     dout << diag::dendl;
00302   }
00303 
00304   for (Table::const_iterator row_it = m_table.begin(); row_it != m_table.end(); ++row_it) {
00305     dout << "";
00306     printRow(dout.getStream(), *row_it);
00307     dout << diag::dendl;
00308   }
00309 
00310   return dout;
00311 }
00312 
00313 } // namespace stk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines