Teuchos - Trilinos Tools Package Version of the Day
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 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 
00043 #include "Teuchos_TableFormat.hpp"
00044 #include "Teuchos_Assert.hpp"
00045 
00046 
00047 namespace Teuchos {
00048 
00049 
00050 std::string TableFormat::thinline() const
00051 {
00052   std::ostringstream toss;
00053   for (int i=0; i<pageWidth_; i++)
00054   {
00055     toss << "-";
00056   }
00057   return toss.str();
00058 }
00059 
00060 
00061 
00062 std::string TableFormat::thickline() const
00063 {
00064   std::ostringstream toss;
00065   for (int i=0; i<pageWidth_; i++)
00066   {
00067     toss << "=";
00068   }
00069   return toss.str();
00070 }
00071 
00072 
00073 std::string TableFormat::blanks(int size) const
00074 {
00075   std::ostringstream toss;
00076   for (int i=0; i<size; i++)
00077   {
00078     toss << " ";
00079   }
00080   return toss.str();
00081 }
00082 
00083 
00084 int TableFormat::computeRequiredColumnWidth(
00085   const std::string& name,
00086   const TableColumn& column
00087   ) const
00088 {
00089   int rtn = name.length();
00090   
00091   for (int i=0; i<column.numRows(); i++)
00092   {
00093     int x = column.entry(i)->toString().length();
00094     rtn = std::max(rtn, x);
00095   }
00096   
00097   return rtn + columnSpacing_;
00098 }
00099 
00100 
00101 void TableFormat::writeRow(
00102   std::ostream& out,
00103   const Array<RCP<TableEntry> >& entries
00104   ) const
00105 {
00106   TEUCHOS_TEST_FOR_EXCEPT(entries.size() != columnWidths_.size() 
00107     && columnWidths_.size() != 0);
00108 
00109   for (Array<RCP<TableEntry> >::size_type i=0; i<entries.size(); i++)
00110   {
00111     int cw = defaultColumnWidth();
00112     if (columnWidths_.size() != 0) cw = columnWidths_[i];
00113 
00114     out << std::left << std::setw(cw) << entries[i]->toString();
00115   }
00116   out << std::endl;
00117 }
00118 
00119 
00120 void TableFormat::writeRow(
00121   std::ostream& out,
00122   int rowIndex,
00123   const Array<TableColumn>& columns
00124   ) const
00125 {
00126   Array<RCP<TableEntry> > entries(columns.size());
00127   for (Array<TableColumn>::size_type i=0; i<columns.size(); i++)
00128   {
00129     entries[i] = columns[i].entry(rowIndex);
00130   }
00131   
00132   writeRow(out, entries);
00133 }
00134 
00135 
00136 void TableFormat::writeWholeTable(
00137   std::ostream& out,
00138   const std::string& header,
00139   const Array<std::string>& columnNames,
00140   const Array<TableColumn>& columns
00141   ) const
00142 {
00143 
00144   /* compute the total width */
00145   int pgWidth = 0;
00146   for (Array<TableColumn>::size_type i=0; i<columnNames.size(); i++)
00147   {
00148     int cw = defaultColumnWidth();
00149     if (columnWidths_.size() != 0) cw = columnWidths_[i];
00150     pgWidth += cw;
00151   }
00152   setPageWidth(std::max(pageWidth_, pgWidth));
00153   
00154   /* write the header */
00155   out << thickline() << std::endl;
00156   out << std::endl;
00157   int numBlanks = (pageWidth_ - header.length())/2;
00158   out << blanks(numBlanks) << header << std::endl;
00159   out << std::endl;
00160 
00161   /* write the column titles */
00162   for (Array<std::string>::size_type i=0; i<columnNames.size(); i++)
00163   {
00164     int cw = defaultColumnWidth();
00165     if (columnWidths_.size() != 0) cw = columnWidths_[i];
00166 
00167     out << std::left << std::setw(cw) << columnNames[i];
00168   }
00169   out << std::endl;
00170 
00171   /* ensure that all columns have the same number of rows */
00172   int numRows = columns[0].numRows();
00173   for (Array<TableColumn>::size_type i=1; i<columns.size(); i++)
00174   {
00175     TEUCHOS_ASSERT_EQUALITY(columns[i].numRows(), numRows);
00176   }
00177   
00178   /* write the table data */
00179   for (int i=0; i<numRows; i++)
00180   {
00181     if (i % lineInterval_ == 0)
00182       out << std::left << thinline() << std::endl;   
00183     writeRow(out, i, columns);
00184   }
00185   
00186   /* write the footer */
00187   out << thickline() << std::endl;
00188 
00189 }
00190 
00191 
00192 } // namespace Teuchos
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines