G4UIArrayString.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
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;  // temporal assignment
00041 
00042   G4String tmpstr= stream;  // G4String::strip() CONST !!
00043   G4String astream= tmpstr.strip(G4String::both);
00044 
00045   // tokenize...
00046   G4int indx=0;
00047   while(1) {
00048     G4int jc= astream.index(" ", indx);
00049     nElement++;
00050     if(jc == G4int(G4String::npos)) break;
00051     jc++; // fix a tiny mistake...
00052     for(; jc< G4int(astream.length()); ) {  // skip continuing spaces
00053       if(astream[(size_t)(jc)]==' ') jc++;
00054       else break;
00055     }
00056     indx= jc;
00057   }
00058 
00059   // allocate string array
00060   stringArray= new G4String[nElement];   
00061 
00062   // push...
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 {  // last token
00069       jc= astream.length()+1;
00070       stringArray[i]= astream(indx, jc-indx);
00071     }
00072     for(G4int j=1; jc+j< G4int(astream.length()); j++ ) { // skip continuing spaces
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)) // offset of column/row is "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     // care for color code
00125     // if(GetElement(icol,iy)-> index(strESC,0) != G4String::npos) {
00126     // if(strESC == (*GetElement(icol,iy))[0] ) {
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;  // for space
00150 
00151   return totalWidth;
00152 }
00153 
00155 void G4UIArrayString::Show(G4int ncol)
00157 {
00158   // calculate #colums in need...
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)) { // last row
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       // care for color code
00176       G4String colorWord;
00177       //if(word.index(strESC,0) != G4String::npos) {
00178       //if(strESC == word[0]) {
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                 // against problem w/ g++ iostream
00189       if(ix != nc) G4cout << "  " << std::flush;
00190       else G4cout << G4endl;      
00191     }
00192   }
00193 }
00194 

Generated on Mon May 27 17:50:05 2013 for Geant4 by  doxygen 1.4.7