Teuchos_TableFormat.cpp

00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #include "Teuchos_TableFormat.hpp"
00030 
00031 using namespace Teuchos;
00032 
00033 
00034 string TableFormat::thinline() const
00035 {
00036   TeuchosOStringStream toss;
00037   for (int i=0; i<pageWidth_; i++)
00038     {
00039       toss << "-";
00040     }
00041   return toss.str();
00042 }
00043 
00044 
00045 
00046 string TableFormat::thickline() const
00047 {
00048   TeuchosOStringStream toss;
00049   for (int i=0; i<pageWidth_; i++)
00050     {
00051       toss << "=";
00052     }
00053   return toss.str();
00054 }
00055 
00056 
00057 string TableFormat::blanks(int size) const
00058 {
00059   TeuchosOStringStream toss;
00060   for (int i=0; i<size; i++)
00061     {
00062       toss << " ";
00063     }
00064   return toss.str();
00065 }
00066 
00067 
00068 
00069 int TableFormat
00070 ::computeRequiredColumnWidth(const string& name,
00071                              const TableColumn& column) const
00072 {
00073   int rtn = name.length();
00074   
00075   for (int i=0; i<column.numRows(); i++)
00076     {
00077       int x = column.entry(i)->toString().length();
00078       rtn = max(rtn, x);
00079     }
00080   
00081   return rtn + columnSpacing_;
00082 }
00083 
00084 
00085 
00086 
00087 void TableFormat::writeRow(RefCountPtr<std::ostream>& out,
00088                            const Array<RefCountPtr<TableEntry> >& entries) const
00089 {
00090   TEST_FOR_EXCEPT(entries.size() != columnWidths_.size() 
00091                   && columnWidths_.size() != 0);
00092 
00093   for (unsigned int i=0; i<entries.size(); i++)
00094     {
00095       int cw = defaultColumnWidth();
00096       if (columnWidths_.size() != 0) cw = columnWidths_[i];
00097 
00098       *out << std::left << std::setw(cw) << entries[i]->toString();
00099     }
00100   *out << std::endl;
00101 }
00102 
00103 
00104 void TableFormat::writeRow(RefCountPtr<std::ostream>& out,
00105                            int rowIndex,
00106                            const Array<TableColumn>& columns) const
00107 {
00108   Array<RefCountPtr<TableEntry> > entries(columns.size());
00109   for (unsigned int i=0; i<columns.size(); i++)
00110     {
00111       entries[i] = columns[i].entry(rowIndex);
00112     }
00113   
00114   writeRow(out, entries);
00115 }
00116 
00117 
00118 void TableFormat::writeWholeTable(RefCountPtr<std::ostream>& out,
00119                                   const string& header,
00120                                   const Array<string>& columnNames,
00121                                   const Array<TableColumn>& columns) const
00122 {
00123   /* compute the total width */
00124   int pageWidth = 0;
00125   for (unsigned int i=0; i<columnNames.size(); i++)
00126     {
00127       int cw = defaultColumnWidth();
00128       if (columnWidths_.size() != 0) cw = columnWidths_[i];
00129       pageWidth += cw;
00130     }
00131   setPageWidth(max(pageWidth_, pageWidth));
00132   
00133   /* write the header */
00134   *out << thickline() << endl;
00135   *out << endl;
00136   int numBlanks = (pageWidth_ - header.length())/2;
00137   *out << blanks(numBlanks) << header << endl;
00138   *out << endl;
00139 
00140   /* write the column titles */
00141   for (unsigned int i=0; i<columnNames.size(); i++)
00142     {
00143       int cw = defaultColumnWidth();
00144       if (columnWidths_.size() != 0) cw = columnWidths_[i];
00145 
00146       *out << std::left << std::setw(cw) << columnNames[i];
00147     }
00148   *out << endl;
00149 
00150   /* ensure that all columns have the same number of rows */
00151   int numRows = columns[0].numRows();
00152   for (unsigned int i=1; i<columns.size(); i++)
00153     {
00154       TEST_FOR_EXCEPTION(columns[i].numRows() != numRows, runtime_error,
00155                          "inconsistent column sizes");
00156     }
00157   
00158   /* write the table data */
00159   for (int i=0; i<numRows; i++)
00160     {
00161       if (i % lineInterval_ == 0) *out << std::left << thinline() << endl;   
00162       writeRow(out, i, columns);
00163     }
00164   
00165   /* write the footer */
00166   *out << thickline() << endl;
00167 }
00168 
00169 
00170 

Generated on Thu Sep 18 12:30:31 2008 for Teuchos - Trilinos Tools Package by doxygen 1.3.9.1