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