00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include <iomanip>
00032 #include "G4UIArrayString.hh"
00033
00034 static const char strESC= '\033';
00035
00037 G4UIArrayString::G4UIArrayString(const G4String& stream)
00039 {
00040 nElement=0;
00041 nColumn=5;
00042
00043 G4String tmpstr= stream;
00044 G4String astream= tmpstr.strip(G4String::both);
00045
00046
00047 G4int indx=0;
00048 while(1) {
00049 G4int jc= astream.index(" ", indx);
00050 nElement++;
00051 if(jc == G4int(G4String::npos)) break;
00052 jc++;
00053 for(; jc< G4int(astream.length()); ) {
00054 if(astream[(size_t)(jc)]==' ') jc++;
00055 else break;
00056 }
00057 indx= jc;
00058 }
00059
00060
00061 stringArray= new G4String[nElement];
00062
00063
00064 indx=0;
00065 for(G4int i=0; i<nElement; i++){
00066 G4int jc= astream.index(" ", indx);
00067 if(jc != G4int(G4String::npos))
00068 stringArray[i]= astream(indx, jc-indx);
00069 else {
00070 jc= astream.length()+1;
00071 stringArray[i]= astream(indx, jc-indx);
00072 }
00073 for(G4int j=1; jc+j< G4int(astream.length()); j++ ) {
00074 if(astream(jc+j)==' ') jc++;
00075 else break;
00076 }
00077 indx= jc+1;
00078 }
00079 }
00080
00082 G4UIArrayString::~G4UIArrayString()
00084 {
00085 delete [] stringArray;
00086 }
00087
00089 G4String* G4UIArrayString::GetElement(G4int icol, G4int irow) const
00091 {
00092 if( !(icol>=1 && irow>=1))
00093 G4cerr << "G4UIArrayString: overrange" << G4endl;
00094 if(icol>nColumn) G4cerr << "G4UIArrayString: overrange" << G4endl;
00095
00096 G4int jq= (irow-1)*nColumn + icol;
00097 if(jq> nElement) G4cerr << "G4UIArrayString: overrange" << G4endl;
00098
00099 jq--;
00100 return &stringArray[jq];
00101 }
00102
00104 G4int G4UIArrayString::GetNRow(int icol) const
00106 {
00107 G4int ni;
00108 if(nElement%nColumn ==0) ni= nElement/nColumn;
00109 else ni= nElement/nColumn + 1;
00110
00111 G4int nn= nElement%nColumn;
00112 if(nn==0) nn= nColumn;
00113
00114 if(icol<= nn) return ni;
00115 else return ni-1;
00116 }
00117
00119 G4int G4UIArrayString::GetNField(int icol) const
00121 {
00122 G4int maxWidth=0;
00123 for (G4int iy=1; iy<= GetNRow(icol); iy++) {
00124 G4int ilen= GetElement(icol,iy)-> length();
00125
00126
00127
00128 const char tgt = (*GetElement(icol,iy))[(size_t)0];
00129 if(strESC == tgt) {
00130 ilen-= 5;
00131 if(ilen<0) G4cout << "length(c) cal. error." << G4endl;
00132 }
00133 if(ilen> maxWidth) maxWidth= ilen;
00134 }
00135
00136 return maxWidth;
00137 }
00138
00140 int G4UIArrayString::CalculateColumnWidth() const
00142 {
00143 G4int totalWidth= 0;
00144
00145 for(G4int ix=1; ix<= nColumn; ix++) {
00146 totalWidth+= GetNField(ix);
00147 }
00148
00149 const G4int nwSpace= 2;
00150 totalWidth+= (nColumn-1)*nwSpace;
00151
00152 return totalWidth;
00153 }
00154
00156 void G4UIArrayString::Show(G4int ncol)
00158 {
00159
00160 while( CalculateColumnWidth()< ncol ) {
00161 nColumn++;
00162 }
00163 while( CalculateColumnWidth()> ncol && nColumn>1 ) {
00164 nColumn--;
00165 }
00166
00167 for(G4int iy=1; iy<= GetNRow(1); iy++) {
00168 G4int nc= nColumn;
00169 if(iy == GetNRow(1)) {
00170 nc= nElement%nColumn;
00171 if(nc==0) nc= nColumn;
00172 }
00173 for(G4int ix=1; ix<=nc; ix++) {
00174 G4String word= GetElement(ix,iy)-> data();
00175
00176
00177 G4String colorWord;
00178
00179
00180 const char tgt = word[(size_t)0];
00181 if(strESC == tgt) {
00182 colorWord= word(0,5);
00183 word.erase(0,5);
00184 }
00185 if(!colorWord.empty()) G4cout << colorWord << std::flush;
00186
00187 G4cout << std::setiosflags(std::ios::left) << std::setw(GetNField(ix))
00188 << word.c_str() << std::flush;
00189
00190 if(ix != nc) G4cout << " " << std::flush;
00191 else G4cout << G4endl;
00192 }
00193 }
00194 }
00195