Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

# 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: G4UIArrayString.cc,v 1.8 2006/06/29 19:09:43 gunter Exp $
    00028 // GEANT4 tag $Name: geant4-08-01-patch-01 $
    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;  // temporal assignment
    00042
    00043   G4String tmpstr= stream;  // G4String::strip() CONST !!
    00044   G4String astream= tmpstr.strip(G4String::both);
    00045
    00046   // tokenize...
    00047   G4int indx=0;
    00048   while(1) {
    00049     G4int jc= astream.index(" ", indx);
    00050     nElement++;
    00051     if(jc == G4int(G4String::npos)) break;
    00052     jc++; // fix a tiny mistake...
    00053     for(; jc< G4int(astream.length()); ) {  // skip continuing spaces
    00054       if(astream[(size_t)(jc)]==' ') jc++;
    00055       else break;
    00056     }
    00057     indx= jc;
    00058   }
    00059
    00060   // allocate string array
    00061   stringArray= new G4String[nElement];
    00062
    00063   // push...
    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 {  // last token
    00070       jc= astream.length()+1;
    00071       stringArray[i]= astream(indx, jc-indx);
    00072     }
    00073     for(G4int j=1; jc+j< G4int(astream.length()); j++ ) { // skip continuing spaces
    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)) // offset of column/row is "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     // care for color code
    00126     // if(GetElement(icol,iy)-> index(strESC,0) != G4String::npos) {
    00127     // if(strESC == (*GetElement(icol,iy))[0] ) {
    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;  // for space
    00151
    00152   return totalWidth;
    00153 }
    00154
    00156 void G4UIArrayString::Show(G4int ncol)
    00158 {
    00159   // calculate #colums in need...
    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)) { // last row
    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       // care for color code
    00177       G4String colorWord;
    00178       //if(word.index(strESC,0) != G4String::npos) {
    00179       //if(strESC == word[0]) {
    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                 // against problem w/ g++ iostream
    00190       if(ix != nc) G4cout << "  " << std::flush;
    00191       else G4cout << G4endl;
    00192     }
    00193   }
    00194 }
    00195