source: trunk/source/interfaces/basic/src/G4UIArrayString.cc @ 1261

Last change on this file since 1261 was 1156, checked in by garnier, 15 years ago

append qt3 fix

File size: 6.1 KB
Line 
1//
2// ********************************************************************
3// * License and Disclaimer                                           *
4// *                                                                  *
5// * The  Geant4 software  is  copyright of the Copyright Holders  of *
6// * the Geant4 Collaboration.  It is provided  under  the terms  and *
7// * conditions of the Geant4 Software License,  included in the file *
8// * LICENSE and available at  http://cern.ch/geant4/license .  These *
9// * include a list of copyright holders.                             *
10// *                                                                  *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work  make  any representation or  warranty, express or implied, *
14// * regarding  this  software system or assume any liability for its *
15// * use.  Please see the license in the file  LICENSE  and URL above *
16// * for the full disclaimer and the limitation of liability.         *
17// *                                                                  *
18// * This  code  implementation is the result of  the  scientific and *
19// * technical work of the GEANT4 collaboration.                      *
20// * By using,  copying,  modifying or  distributing the software (or *
21// * any work based  on the software)  you  agree  to acknowledge its *
22// * use  in  resulting  scientific  publications,  and indicate your *
23// * acceptance of all terms of the Geant4 Software license.          *
24// ********************************************************************
25//
26//
27// $Id: G4UIArrayString.cc,v 1.8 2006/06/29 19:09:43 gunter Exp $
28// GEANT4 tag $Name:  $
29//
30
31#include <iomanip>
32#include "G4UIArrayString.hh"
33
34static const char strESC= '\033';
35
36////////////////////////////////////////////////////////
37G4UIArrayString::G4UIArrayString(const G4String& stream)
38////////////////////////////////////////////////////////
39{
40  nElement=0;
41  nColumn=5;  // temporal assignment
42
43  G4String tmpstr= stream;  // G4String::strip() CONST !!
44  G4String astream= tmpstr.strip(G4String::both);
45
46  // tokenize...
47  G4int indx=0;
48  while(1) {
49    G4int jc= astream.index(" ", indx);
50    nElement++;
51    if(jc == G4int(G4String::npos)) break;
52    jc++; // fix a tiny mistake...
53    for(; jc< G4int(astream.length()); ) {  // skip continuing spaces
54      if(astream[(size_t)(jc)]==' ') jc++;
55      else break;
56    }
57    indx= jc;
58  }
59
60  // allocate string array
61  stringArray= new G4String[nElement];   
62
63  // push...
64  indx=0;
65  for(G4int i=0; i<nElement; i++){
66    G4int jc= astream.index(" ", indx);
67    if(jc != G4int(G4String::npos))
68      stringArray[i]= astream(indx, jc-indx);
69    else {  // last token
70      jc= astream.length()+1;
71      stringArray[i]= astream(indx, jc-indx);
72    }
73    for(G4int j=1; jc+j< G4int(astream.length()); j++ ) { // skip continuing spaces
74      if(astream(jc+j)==' ') jc++;
75      else break;
76    }
77    indx= jc+1;
78  }
79}
80
81///////////////////////////////////
82G4UIArrayString::~G4UIArrayString()
83///////////////////////////////////
84{ 
85  delete [] stringArray;
86}
87
88///////////////////////////////////////////////////////////////////
89G4String* G4UIArrayString::GetElement(G4int icol, G4int irow) const
90///////////////////////////////////////////////////////////////////
91{ 
92  if( !(icol>=1 && irow>=1)) // offset of column/row is "1".
93    G4cerr << "G4UIArrayString: overrange" << G4endl;
94  if(icol>nColumn) G4cerr << "G4UIArrayString: overrange" << G4endl;
95
96  G4int jq= (irow-1)*nColumn + icol;
97  if(jq> nElement) G4cerr << "G4UIArrayString: overrange" << G4endl;
98
99  jq--;
100  return &stringArray[jq];
101}
102
103////////////////////////////////////////////
104G4int G4UIArrayString::GetNRow(int icol) const
105////////////////////////////////////////////
106{
107  G4int ni;
108  if(nElement%nColumn ==0) ni= nElement/nColumn;
109  else ni= nElement/nColumn + 1;
110
111  G4int nn= nElement%nColumn;
112  if(nn==0) nn= nColumn;
113
114  if(icol<= nn) return ni;
115  else return ni-1;
116}
117
118////////////////////////////////////////////////
119G4int G4UIArrayString::GetNField(int icol) const
120////////////////////////////////////////////////
121{
122  G4int maxWidth=0;
123  for (G4int iy=1; iy<= GetNRow(icol); iy++) {
124    G4int ilen= GetElement(icol,iy)-> length();
125    // care for color code
126    // if(GetElement(icol,iy)-> index(strESC,0) != G4String::npos) {
127    // if(strESC == (*GetElement(icol,iy))[0] ) {
128    const char tgt = (*GetElement(icol,iy))[(size_t)0];
129    if(strESC == tgt) {
130      ilen-= 5;
131      if(ilen<0) G4cout << "length(c) cal. error." << G4endl;
132    }
133    if(ilen> maxWidth) maxWidth= ilen;
134  }
135
136  return maxWidth;
137}
138
139/////////////////////////////////////////////////
140int G4UIArrayString::CalculateColumnWidth() const
141/////////////////////////////////////////////////
142{
143  G4int totalWidth= 0;
144
145  for(G4int ix=1; ix<= nColumn; ix++) {
146    totalWidth+= GetNField(ix);
147  }
148
149  const G4int nwSpace= 2;
150  totalWidth+= (nColumn-1)*nwSpace;  // for space
151
152  return totalWidth;
153}
154
155//////////////////////////////////////
156void G4UIArrayString::Show(G4int ncol)
157//////////////////////////////////////
158{
159  // calculate #colums in need...
160  while( CalculateColumnWidth()< ncol ) {
161    nColumn++;
162  }
163  while( CalculateColumnWidth()> ncol && nColumn>1 ) {
164    nColumn--;
165  }
166 
167  for(G4int iy=1; iy<= GetNRow(1); iy++) {
168    G4int nc= nColumn;
169    if(iy == GetNRow(1)) { // last row
170      nc= nElement%nColumn;
171      if(nc==0) nc= nColumn;
172    }
173    for(G4int ix=1; ix<=nc; ix++) {
174      G4String word= GetElement(ix,iy)-> data();
175
176      // care for color code
177      G4String colorWord;
178      //if(word.index(strESC,0) != G4String::npos) {
179      //if(strESC == word[0]) {
180      const char tgt = word[(size_t)0];
181      if(strESC == tgt) {
182        colorWord= word(0,5);
183        word.erase(0,5);
184      }
185      if(!colorWord.empty()) G4cout << colorWord << std::flush;
186
187      G4cout << std::setiosflags(std::ios::left) << std::setw(GetNField(ix)) 
188             << word.c_str() << std::flush; 
189                // against problem w/ g++ iostream
190      if(ix != nc) G4cout << "  " << std::flush;
191      else G4cout << G4endl;     
192    }
193  }
194}
195
Note: See TracBrowser for help on using the repository browser.