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/util/PrintTable.hpp>
00015 
00016 namespace stk {
00017 
00018 void
00019 PrintTable::transpose_table() const
00020 {}
00021 
00022 
00023 void
00024 PrintTable::calculate_column_widths() const
00025 {
00026   ColumnWidthVector column_width_set;
00027 
00028   // loop over the headers and find the longest field for each column by size and from m_columnWidth
00029   for (Table::const_iterator row_it = m_header.begin(); row_it != m_header.end(); ++row_it) {
00030     if ((*row_it).size() > m_columnWidth.size())
00031       m_columnWidth.resize((*row_it).size(), 0);
00032     if ((*row_it).size() > column_width_set.size())
00033       column_width_set.resize((*row_it).size(), 0);
00034 
00035     int i = 0;
00036     for (Row::const_iterator cell_it = (*row_it).begin(); cell_it != (*row_it).end(); ++cell_it, ++i) {
00037       m_columnWidth[i] = std::max(m_columnWidth[i], (*cell_it).m_string.size());
00038       column_width_set[i] = std::max(column_width_set[i], (*cell_it).m_width);
00039     }
00040   }
00041 
00042   // loop over the table and find the longest field for each column by size and from m_columnWidth
00043   for (Table::const_iterator row_it = m_table.begin(); row_it != m_table.end(); ++row_it) {
00044     if ((*row_it).size() > m_columnWidth.size())
00045       m_columnWidth.resize((*row_it).size(), 0);
00046     if ((*row_it).size() > column_width_set.size())
00047       column_width_set.resize((*row_it).size(), 0);
00048 
00049     int i = 0;
00050     for (Row::const_iterator cell_it = (*row_it).begin(); cell_it != (*row_it).end(); ++cell_it, ++i) {
00051       m_columnWidth[i] = std::max(m_columnWidth[i], (*cell_it).m_string.size());
00052       column_width_set[i] = std::max(column_width_set[i], (*cell_it).m_width);
00053     }
00054   }
00055 
00056   // choose m_width width over size() width for each column
00057   m_tableWidth = 0;
00058   for (ColumnWidthVector::size_type i = 0; i < m_columnWidth.size(); ++i) {
00059     if (column_width_set[i] != 0)
00060       m_columnWidth[i] = column_width_set[i];
00061     m_tableWidth += m_columnWidth[i] + 1;
00062   }
00063 }
00064 
00065 
00066 PrintTable &
00067 PrintTable::end_col()
00068 {
00069   m_currentCell.m_string = std::string(m_currentCell.m_indent*2, ' ') + m_currentString.str();
00070   m_table.back().push_back(m_currentCell);
00071   if (m_table.size() > 1 && m_table[0].size() <= m_table.back().size()) {
00072     m_currentCell.m_string = "";
00073     m_currentCell.m_flags = 0;
00074     m_currentCell.m_justification = m_table[0][m_table[0].size() - 1].m_justification;
00075     m_currentCell.m_width = m_table[0][m_table[0].size() - 1].m_width;
00076     m_currentCell.m_indent = m_table[0][m_table[0].size() - 1].m_indent;
00077  }
00078   else {
00079     m_currentCell = Cell();
00080   }
00081   m_currentString.str("");
00082 
00083   return *this;
00084 }
00085 
00086 
00087 PrintTable &
00088 PrintTable::end_row()
00089 {
00090   if (!m_currentString.str().empty())
00091     end_col();
00092   m_table.push_back(Row());
00093   return *this;
00094 }
00095 
00096 
00097 PrintTable &
00098 PrintTable::at(
00099   size_t        row,
00100   size_t        col)
00101 {
00102   for (Table::size_type i = m_table.size(); i <= row; ++i)
00103     m_table.push_back(Row());
00104   for (Row::size_type i = m_table[row].size(); i <= col; ++i) 
00105     m_table[row].push_back(Cell());
00106   
00107   m_currentCell.m_string = std::string(m_currentCell.m_indent*2, ' ') + m_currentString.str();  
00108   m_table[row][col] = m_currentCell;
00109   
00110   m_currentCell = Cell();
00111   m_currentString.str("");
00112 
00113   return *this;
00114 }
00115 
00116 
00117 std::ostream &
00118 PrintTable::print(
00119   std::ostream &  os) const
00120 {
00121   if (m_flags & COMMA_SEPARATED_VALUES)
00122     csvPrint(os);
00123 
00124   else {
00125     if (m_flags & PRINT_TRANSPOSED)
00126       transpose_table();
00127 
00128     calculate_column_widths();
00129 
00130     if (!m_title.empty()) {
00131       int prespaces = 0;
00132 
00133       if(m_title.length() < m_tableWidth)
00134   prespaces = (m_tableWidth - m_title.length())/2;;
00135 
00136       os << m_commentPrefix;
00137       os << std::left << std::setw(prespaces) << "" << m_title << '\n';
00138     }
00139 
00140     for (Table::const_iterator row_it = m_header.begin(); row_it != m_header.end(); ++row_it) {
00141       os << m_commentPrefix;
00142       printRow(os, *row_it);
00143       os << '\n';
00144     }
00145 
00146     if (m_header.size() > 0) {
00147       os << m_commentPrefix;
00148       printHeaderBar(os);
00149       os << '\n';
00150     }
00151 
00152     for (Table::const_iterator row_it = m_table.begin(); row_it != m_table.end(); ++row_it) {
00153       os << std::left << std::setw(m_commentPrefix.size()) << "";
00154       printRow(os, *row_it);
00155       os << '\n';
00156     }
00157   }
00158 
00159   return os;
00160 }
00161 
00162 
00163 std::ostream &
00164 PrintTable::printRow(
00165   std::ostream &  os,
00166   const Row &   row) const
00167 {
00168   int i = 0;
00169   int postspaces = 0;
00170   for (Row::const_iterator cell_it = row.begin(); cell_it != row.end(); ++cell_it, ++i) {
00171     os // << postspaces << ", "
00172        << std::left << std::setw(postspaces) << "";
00173     postspaces = 0;
00174 
00175     if (cell_it != row.begin())
00176       os << " ";
00177 
00178     if ((*cell_it).m_flags & Cell::SPAN)
00179       os << (*cell_it).m_string;
00180     else if ((*cell_it).m_string.length() > m_columnWidth[i]) {
00181       if ((*cell_it).m_justification & Cell::ENDS) {
00182   int front_end = m_columnWidth[i]/4;
00183   int back_begin = (*cell_it).m_string.size() - (m_columnWidth[i] - front_end);
00184   os << (*cell_it).m_string.substr(0, front_end - 3) + "..." + (*cell_it).m_string.substr(back_begin, (*cell_it).m_string.size());
00185       }
00186       else { // if ((*cell_it).m_justification & Cell::TRUNC) {
00187   os << (*cell_it).m_string.substr(0, m_columnWidth[i]);
00188       }
00189     }
00190     else {
00191       if ((*cell_it).m_string.length() == 0)
00192   postspaces = m_columnWidth[i];
00193       else if (((*cell_it).m_justification & Cell::JUSTIFY_MASK) == Cell::LEFT) {
00194   postspaces = m_columnWidth[i] - (*cell_it).m_string.length();
00195   os // << m_columnWidth[i] << ", " << postspaces << ", "
00196     << std::left << (*cell_it).m_string;
00197       }
00198       else if (((*cell_it).m_justification & Cell::JUSTIFY_MASK) == Cell::CENTER) {
00199   int prespaces = (m_columnWidth[i] - (*cell_it).m_string.length())/2;
00200   postspaces = m_columnWidth[i] - (*cell_it).m_string.length() - prespaces;
00201   os // << prespaces << " " << postspaces << ", "
00202     << std::left << std::setw(prespaces) << "" << (*cell_it).m_string;
00203       }
00204       else // if (((*cell_it).m_justification & Cell::JUSTIFY_MASK) == Cell::RIGHT)
00205   os // << m_columnWidth[i] << ", "
00206      << std::right << std::setw(m_columnWidth[i]) << (*cell_it).m_string;
00207     }
00208   }
00209 
00210   return os;
00211 }
00212 
00213 
00214 std::ostream &
00215 PrintTable::printHeaderBar(
00216   std::ostream &  os) const
00217 {
00218   os << std::setfill('-');
00219 
00220   for (ColumnWidthVector::size_type i = 0; i < m_columnWidth.size(); ++i) {
00221     if (i != 0)
00222       os << " ";
00223     os << std::setw(m_columnWidth[i]) << "";
00224   }
00225   os << std::setfill(' ');
00226 
00227   return os;
00228 }
00229 
00230 
00231 std::ostream &
00232 PrintTable::csvPrint(
00233   std::ostream &  os) const
00234 {
00235   if (!m_title.empty())
00236     os << m_title << '\n';
00237 
00238   for (Table::const_iterator row_it = m_header.begin(); row_it != m_header.end(); ++row_it) {
00239     const Row &row = (*row_it);
00240     for (Row::const_iterator cell_it = row.begin(); cell_it != row.end(); ++cell_it) {
00241       if (cell_it != row.begin())
00242   os << ",";
00243       os << (*cell_it).m_string;
00244     }
00245     os << '\n';
00246   }
00247 
00248   for (Table::const_iterator row_it = m_table.begin(); row_it != m_table.end(); ++row_it) {
00249     const Row &row = (*row_it);
00250     for (Row::const_iterator cell_it = row.begin(); cell_it != row.end(); ++cell_it) {
00251       if (cell_it != row.begin())
00252   os << ",";
00253       os << (*cell_it).m_string;
00254     }
00255     os << '\n';
00256   }
00257 
00258   return os;
00259 }
00260 
00261 } // namespace stk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends