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 #include <iomanip>
00031 #include "G4UIArrayString.hh"
00032
00033 static const char strESC= '\033';
00034
00036 G4UIArrayString::G4UIArrayString(const G4String& stream)
00038 {
00039 nElement=0;
00040 nColumn=5;
00041
00042 G4String tmpstr= stream;
00043 G4String astream= tmpstr.strip(G4String::both);
00044
00045
00046 G4int indx=0;
00047 while(1) {
00048 G4int jc= astream.index(" ", indx);
00049 nElement++;
00050 if(jc == G4int(G4String::npos)) break;
00051 jc++;
00052 for(; jc< G4int(astream.length()); ) {
00053 if(astream[(size_t)(jc)]==' ') jc++;
00054 else break;
00055 }
00056 indx= jc;
00057 }
00058
00059
00060 stringArray= new G4String[nElement];
00061
00062
00063 indx=0;
00064 for(G4int i=0; i<nElement; i++){
00065 G4int jc= astream.index(" ", indx);
00066 if(jc != G4int(G4String::npos))
00067 stringArray[i]= astream(indx, jc-indx);
00068 else {
00069 jc= astream.length()+1;
00070 stringArray[i]= astream(indx, jc-indx);
00071 }
00072 for(G4int j=1; jc+j< G4int(astream.length()); j++ ) {
00073 if(astream(jc+j)==' ') jc++;
00074 else break;
00075 }
00076 indx= jc+1;
00077 }
00078 }
00079
00081 G4UIArrayString::~G4UIArrayString()
00083 {
00084 delete [] stringArray;
00085 }
00086
00088 G4String* G4UIArrayString::GetElement(G4int icol, G4int irow) const
00090 {
00091 if( !(icol>=1 && irow>=1))
00092 G4cerr << "G4UIArrayString: overrange" << G4endl;
00093 if(icol>nColumn) G4cerr << "G4UIArrayString: overrange" << G4endl;
00094
00095 G4int jq= (irow-1)*nColumn + icol;
00096 if(jq> nElement) G4cerr << "G4UIArrayString: overrange" << G4endl;
00097
00098 jq--;
00099 return &stringArray[jq];
00100 }
00101
00103 G4int G4UIArrayString::GetNRow(int icol) const
00105 {
00106 G4int ni;
00107 if(nElement%nColumn ==0) ni= nElement/nColumn;
00108 else ni= nElement/nColumn + 1;
00109
00110 G4int nn= nElement%nColumn;
00111 if(nn==0) nn= nColumn;
00112
00113 if(icol<= nn) return ni;
00114 else return ni-1;
00115 }
00116
00118 G4int G4UIArrayString::GetNField(int icol) const
00120 {
00121 G4int maxWidth=0;
00122 for (G4int iy=1; iy<= GetNRow(icol); iy++) {
00123 G4int ilen= GetElement(icol,iy)-> length();
00124
00125
00126
00127 const char tgt = (*GetElement(icol,iy))[(size_t)0];
00128 if(strESC == tgt) {
00129 ilen-= 5;
00130 if(ilen<0) G4cout << "length(c) cal. error." << G4endl;
00131 }
00132 if(ilen> maxWidth) maxWidth= ilen;
00133 }
00134
00135 return maxWidth;
00136 }
00137
00139 int G4UIArrayString::CalculateColumnWidth() const
00141 {
00142 G4int totalWidth= 0;
00143
00144 for(G4int ix=1; ix<= nColumn; ix++) {
00145 totalWidth+= GetNField(ix);
00146 }
00147
00148 const G4int nwSpace= 2;
00149 totalWidth+= (nColumn-1)*nwSpace;
00150
00151 return totalWidth;
00152 }
00153
00155 void G4UIArrayString::Show(G4int ncol)
00157 {
00158
00159 while( CalculateColumnWidth()< ncol ) {
00160 nColumn++;
00161 }
00162 while( CalculateColumnWidth()> ncol && nColumn>1 ) {
00163 nColumn--;
00164 }
00165
00166 for(G4int iy=1; iy<= GetNRow(1); iy++) {
00167 G4int nc= nColumn;
00168 if(iy == GetNRow(1)) {
00169 nc= nElement%nColumn;
00170 if(nc==0) nc= nColumn;
00171 }
00172 for(G4int ix=1; ix<=nc; ix++) {
00173 G4String word= GetElement(ix,iy)-> data();
00174
00175
00176 G4String colorWord;
00177
00178
00179 const char tgt = word[(size_t)0];
00180 if(strESC == tgt) {
00181 colorWord= word(0,5);
00182 word.erase(0,5);
00183 }
00184 if(!colorWord.empty()) G4cout << colorWord << std::flush;
00185
00186 G4cout << std::setiosflags(std::ios::left) << std::setw(GetNField(ix))
00187 << word.c_str() << std::flush;
00188
00189 if(ix != nc) G4cout << " " << std::flush;
00190 else G4cout << G4endl;
00191 }
00192 }
00193 }
00194