|
Teuchos - Trilinos Tools Package Version of the Day
|
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 00030 #include "Teuchos_TableFormat.hpp" 00031 #include "Teuchos_Assert.hpp" 00032 00033 00034 namespace Teuchos { 00035 00036 00037 std::string TableFormat::thinline() const 00038 { 00039 std::ostringstream toss; 00040 for (int i=0; i<pageWidth_; i++) 00041 { 00042 toss << "-"; 00043 } 00044 return toss.str(); 00045 } 00046 00047 00048 00049 std::string TableFormat::thickline() const 00050 { 00051 std::ostringstream toss; 00052 for (int i=0; i<pageWidth_; i++) 00053 { 00054 toss << "="; 00055 } 00056 return toss.str(); 00057 } 00058 00059 00060 std::string TableFormat::blanks(int size) const 00061 { 00062 std::ostringstream toss; 00063 for (int i=0; i<size; i++) 00064 { 00065 toss << " "; 00066 } 00067 return toss.str(); 00068 } 00069 00070 00071 int TableFormat::computeRequiredColumnWidth( 00072 const std::string& name, 00073 const TableColumn& column 00074 ) const 00075 { 00076 int rtn = name.length(); 00077 00078 for (int i=0; i<column.numRows(); i++) 00079 { 00080 int x = column.entry(i)->toString().length(); 00081 rtn = std::max(rtn, x); 00082 } 00083 00084 return rtn + columnSpacing_; 00085 } 00086 00087 00088 void TableFormat::writeRow( 00089 std::ostream& out, 00090 const Array<RCP<TableEntry> >& entries 00091 ) const 00092 { 00093 TEST_FOR_EXCEPT(entries.size() != columnWidths_.size() 00094 && columnWidths_.size() != 0); 00095 00096 for (Array<RCP<TableEntry> >::size_type i=0; i<entries.size(); i++) 00097 { 00098 int cw = defaultColumnWidth(); 00099 if (columnWidths_.size() != 0) cw = columnWidths_[i]; 00100 00101 out << std::left << std::setw(cw) << entries[i]->toString(); 00102 } 00103 out << std::endl; 00104 } 00105 00106 00107 void TableFormat::writeRow( 00108 std::ostream& out, 00109 int rowIndex, 00110 const Array<TableColumn>& columns 00111 ) const 00112 { 00113 Array<RCP<TableEntry> > entries(columns.size()); 00114 for (Array<TableColumn>::size_type i=0; i<columns.size(); i++) 00115 { 00116 entries[i] = columns[i].entry(rowIndex); 00117 } 00118 00119 writeRow(out, entries); 00120 } 00121 00122 00123 void TableFormat::writeWholeTable( 00124 std::ostream& out, 00125 const std::string& header, 00126 const Array<std::string>& columnNames, 00127 const Array<TableColumn>& columns 00128 ) const 00129 { 00130 00131 /* compute the total width */ 00132 int pgWidth = 0; 00133 for (Array<TableColumn>::size_type i=0; i<columnNames.size(); i++) 00134 { 00135 int cw = defaultColumnWidth(); 00136 if (columnWidths_.size() != 0) cw = columnWidths_[i]; 00137 pgWidth += cw; 00138 } 00139 setPageWidth(std::max(pageWidth_, pgWidth)); 00140 00141 /* write the header */ 00142 out << thickline() << std::endl; 00143 out << std::endl; 00144 int numBlanks = (pageWidth_ - header.length())/2; 00145 out << blanks(numBlanks) << header << std::endl; 00146 out << std::endl; 00147 00148 /* write the column titles */ 00149 for (Array<std::string>::size_type i=0; i<columnNames.size(); i++) 00150 { 00151 int cw = defaultColumnWidth(); 00152 if (columnWidths_.size() != 0) cw = columnWidths_[i]; 00153 00154 out << std::left << std::setw(cw) << columnNames[i]; 00155 } 00156 out << std::endl; 00157 00158 /* ensure that all columns have the same number of rows */ 00159 int numRows = columns[0].numRows(); 00160 for (Array<TableColumn>::size_type i=1; i<columns.size(); i++) 00161 { 00162 TEUCHOS_ASSERT_EQUALITY(columns[i].numRows(), numRows); 00163 } 00164 00165 /* write the table data */ 00166 for (int i=0; i<numRows; i++) 00167 { 00168 if (i % lineInterval_ == 0) 00169 out << std::left << thinline() << std::endl; 00170 writeRow(out, i, columns); 00171 } 00172 00173 /* write the footer */ 00174 out << thickline() << std::endl; 00175 00176 } 00177 00178 00179 } // namespace Teuchos
1.7.4