00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "BaBar/BaBar.hh"
00010
00011 #include "CdbTable/CdbNTuplePrint.hh"
00012
00013 #include "BbrStdUtils/BbrIOStreamUtils.hh"
00014 using std::endl;
00015 using std::ios;
00016 using std::ostream;
00017 using std::setw;
00018
00019 template < class NTUPLE,
00020 class CONVERTER >
00021 CdbStatus
00022 CdbNTuplePrintImpl<NTUPLE,CONVERTER>::print( ostream& theOutputStream,
00023 const CdbCPtr<NTUPLE>& thePtr,
00024 const unsigned int theColumnWidth,
00025 const char* theIndent,
00026 const CdbNTuplePrintTypes::FrameType theFrameType,
00027 const CdbNTuplePrintTypes::FrameBorders theFrameBorders )
00028 {
00029 const char* errorStr = "CdbNTuplePrintImpl<NTUPLE,CONVERTER>::print() -- ERROR.";
00030
00031 if( thePtr.isNull( )) return CdbStatus::IllegalParameters;
00032
00033
00034
00035 const unsigned int nColumns = thePtr->columns( );
00036 const unsigned int nRows = thePtr->rows( );
00037
00038
00039
00040 unsigned int verticalBorderWidth = 0;
00041 std::string borderSymbol = "";
00042
00043 switch( theFrameBorders ) {
00044
00045 case CdbNTuplePrintTypes::DoubleHashes:
00046
00047 verticalBorderWidth = 2;
00048 borderSymbol = "/";
00049
00050 break;
00051
00052 case CdbNTuplePrintTypes::SingleXes:
00053
00054 default:
00055
00056 verticalBorderWidth = 1;
00057 borderSymbol = "x";
00058
00059 break;
00060 }
00061
00062 std::string left1 = borderSymbol;
00063 std::string left2 = borderSymbol;
00064 std::string outerRight1 = "";
00065 std::string outerRight2 = "";
00066 std::string innerRight1 = " ";
00067 std::string innerRight2 = borderSymbol;
00068
00069 for( unsigned int i = 1; i < verticalBorderWidth; ++i ) {
00070 left1 += borderSymbol;
00071 left2 += borderSymbol;
00072 innerRight1 += borderSymbol;
00073 innerRight2 += borderSymbol;
00074 }
00075
00076 left1 += " ";
00077 left2 += borderSymbol;
00078 innerRight1 += borderSymbol;
00079 innerRight2 += borderSymbol;
00080
00081 std::string outerSeparator = "";
00082 std::string outerEmptySeparator = "";
00083
00084 switch( theFrameType ) {
00085
00086 case CdbNTuplePrintTypes::OpenFrame:
00087
00088 break;
00089
00090 case CdbNTuplePrintTypes::ClosedFrame:
00091
00092 default:
00093
00094 outerRight1 = innerRight1;
00095 outerRight2 = innerRight2;
00096
00097 outerSeparator += borderSymbol;
00098 outerEmptySeparator += " ";
00099
00100 for( unsigned int i = 0; i < 12; ++i ) {
00101 outerSeparator += borderSymbol;
00102 outerEmptySeparator += " ";
00103 }
00104
00105 outerSeparator += borderSymbol;
00106 outerEmptySeparator += " ";
00107
00108 for( unsigned int col = 0; col < nColumns; ++col ) {
00109
00110 outerSeparator += borderSymbol + borderSymbol;
00111 outerEmptySeparator += " ";
00112
00113 for( unsigned int i = 0; i < theColumnWidth; ++i ) {
00114 outerSeparator += borderSymbol;
00115 outerEmptySeparator += " ";
00116 }
00117
00118 outerSeparator += borderSymbol;
00119 outerEmptySeparator += " ";
00120 }
00121 break;
00122 }
00123
00124 std::string name = thePtr->name( );
00125 std::string description = thePtr->description( );
00126
00127 std::string innerSeparator = borderSymbol;
00128 std::string innerEmptySeparator = " ";
00129
00130 for( unsigned int i = 0; i < 12; ++i ) {
00131 innerSeparator += borderSymbol;
00132 innerEmptySeparator += " ";
00133 }
00134
00135 innerSeparator += borderSymbol;
00136 innerEmptySeparator += " ";
00137
00138 std::string headerSeparator = "--------------";
00139
00140 for( unsigned int col = 0; col < nColumns; ++col ) {
00141
00142 innerSeparator += borderSymbol + borderSymbol;
00143 innerEmptySeparator += " ";
00144 headerSeparator += "+-";
00145
00146 for( unsigned int i = 0; i < theColumnWidth; ++i ) {
00147 innerSeparator += borderSymbol;
00148 innerEmptySeparator += " ";
00149 headerSeparator += "-";
00150 }
00151
00152 innerSeparator += borderSymbol;
00153 innerEmptySeparator += " ";
00154 headerSeparator += "-";
00155 }
00156
00157
00158
00159 if( CdbNTuplePrintTypes::ClosedFrame == theFrameType ) {
00160 theOutputStream << theIndent << left2.c_str( ) << outerSeparator.c_str( ) << outerRight2.c_str( ) << endl;
00161 }
00162 {
00163 unsigned int metedataWidth = 1 + 12 + 1 + nColumns * ( 2 + theColumnWidth + 1 ) - 14;
00164
00165 theOutputStream << theIndent << left1.c_str( ) << outerEmptySeparator.c_str( ) << outerRight1.c_str( ) << endl
00166 << theIndent << left1.c_str( ) << "Name : ";
00167 {
00168 babar::IOStream::all_format_guard guard( theOutputStream );
00169 theOutputStream.setf( ios::left, ios::adjustfield );
00170 theOutputStream << setw( metedataWidth ) << name.c_str( );
00171 }
00172 theOutputStream << outerRight1.c_str( ) << endl
00173 << theIndent << left1.c_str( ) << "Description : ";
00174 {
00175 babar::IOStream::all_format_guard guard( theOutputStream );
00176 theOutputStream.setf( ios::left, ios::adjustfield );
00177 theOutputStream << setw( metedataWidth ) << description.c_str( );
00178 }
00179 theOutputStream << outerRight1.c_str( ) << endl
00180 << theIndent << left1.c_str( ) << outerEmptySeparator.c_str( ) << outerRight1.c_str( ) << endl
00181 << theIndent << left2.c_str( ) << innerSeparator.c_str( ) << innerRight2.c_str( ) << endl
00182 << theIndent << left1.c_str( ) << innerEmptySeparator.c_str( ) << innerRight1.c_str( ) << endl;
00183 }
00184
00185
00186
00187
00188 theOutputStream << theIndent << left1.c_str( ) << " #ROW ";
00189
00190 for( unsigned int col = 0; col < nColumns; ++col ) {
00191
00192 std::string name;
00193 if( CdbStatus::Success != thePtr->column_name( col, name )) {
00194 theOutputStream << errorStr << endl
00195 << " Failed to get the name of a column number " << col << "." << endl
00196 << " The tuple may be corrupted or have incorrect implementation." << endl;
00197 return CdbStatus::Error;
00198 }
00199
00200 theOutputStream << "| ";
00201 {
00202 babar::IOStream::all_format_guard guard( theOutputStream );
00203 theOutputStream.setf( ios::left, ios::adjustfield );
00204 theOutputStream << setw( theColumnWidth ) << name.c_str( );
00205 }
00206 theOutputStream << " ";
00207 }
00208 theOutputStream << innerRight1.c_str( ) << endl
00209 << theIndent << left1.c_str( ) << headerSeparator.c_str( ) << innerRight1.c_str( ) << endl;
00210
00211
00212
00213 for( unsigned int row = 0; row < nRows; ++row ) {
00214
00215 theOutputStream << theIndent << left1.c_str( ) << " " << setw( 12 ) << row << " ";
00216
00217 for( unsigned int col = 0; col < nColumns; ++col ) {
00218
00219 typename NTUPLE::element_type value;
00220
00221 if( CdbStatus::Success != thePtr->get_element( value, row, col )) {
00222 theOutputStream << errorStr << endl
00223 << " Failed to get an element at row " << row << " and column " << col << "." << endl
00224 << " The tuple may be corrupted or have incorrect implementation." << endl;
00225 return CdbStatus::Error;
00226 }
00227
00228 theOutputStream << "| ";
00229 CONVERTER::convertAndPrint( theOutputStream, theColumnWidth, value );
00230 theOutputStream << " ";
00231 }
00232 theOutputStream << innerRight1.c_str( ) << endl;
00233 }
00234
00235
00236
00237 theOutputStream << theIndent << left1.c_str( ) << innerEmptySeparator.c_str( ) << innerRight1.c_str( ) << endl
00238 << theIndent << left2.c_str( ) << innerSeparator.c_str( ) << innerRight2.c_str( ) << endl
00239 << theIndent << left1.c_str( ) << outerEmptySeparator.c_str( ) << outerRight1.c_str( ) << endl
00240 << theIndent << left1.c_str( ) << "Total of " << setw( 12 ) << nRows << " rows in the table.";
00241 {
00242 unsigned int trailerWidth = 1 + 12 + 1 + nColumns * ( 2 + theColumnWidth + 1 ) - 40;
00243 theOutputStream << setw( trailerWidth ) << " ";
00244 }
00245 theOutputStream << outerRight1.c_str( ) << endl
00246 << theIndent << left1.c_str( ) << outerEmptySeparator.c_str( ) << outerRight1.c_str( ) << endl;
00247
00248 if( CdbNTuplePrintTypes::ClosedFrame == theFrameType ) {
00249 theOutputStream << theIndent << left2.c_str( ) << outerSeparator.c_str( ) << outerRight2.c_str( ) << endl;
00250 }
00251
00252 return CdbStatus::Success;
00253 }
00254
00255
00256
00257