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