Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4UIArrayString.cc
Go to the documentation of this file.
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 66892 2013-01-17 10:57:59Z gunter $
28 //
29 
30 #include <iomanip>
31 #include "G4UIArrayString.hh"
32 
33 static const char strESC= '\033';
34 
35 ////////////////////////////////////////////////////////
37 ////////////////////////////////////////////////////////
38 {
39  nElement=0;
40  nColumn=5; // temporal assignment
41 
42  G4String tmpstr= stream; // G4String::strip() CONST !!
43  G4String astream= tmpstr.strip(G4String::both);
44 
45  // tokenize...
46  G4int indx=0;
47  while(1) {
48  G4int jc= astream.index(" ", indx);
49  nElement++;
50  if(jc == G4int(G4String::npos)) break;
51  jc++; // fix a tiny mistake...
52  for(; jc< G4int(astream.length()); ) { // skip continuing spaces
53  if(astream[(size_t)(jc)]==' ') jc++;
54  else break;
55  }
56  indx= jc;
57  }
58 
59  // allocate string array
60  stringArray= new G4String[nElement];
61 
62  // push...
63  indx=0;
64  for(G4int i=0; i<nElement; i++){
65  G4int jc= astream.index(" ", indx);
66  if(jc != G4int(G4String::npos))
67  stringArray[i]= astream(indx, jc-indx);
68  else { // last token
69  jc= astream.length()+1;
70  stringArray[i]= astream(indx, jc-indx);
71  }
72  for(G4int j=1; jc+j< G4int(astream.length()); j++ ) { // skip continuing spaces
73  if(astream(jc+j)==' ') jc++;
74  else break;
75  }
76  indx= jc+1;
77  }
78 }
79 
80 ///////////////////////////////////
82 ///////////////////////////////////
83 {
84  delete [] stringArray;
85 }
86 
87 ///////////////////////////////////////////////////////////////////
88 G4String* G4UIArrayString::GetElement(G4int icol, G4int irow) const
89 ///////////////////////////////////////////////////////////////////
90 {
91  if( !(icol>=1 && irow>=1)) // offset of column/row is "1".
92  G4cerr << "G4UIArrayString: overrange" << G4endl;
93  if(icol>nColumn) G4cerr << "G4UIArrayString: overrange" << G4endl;
94 
95  G4int jq= (irow-1)*nColumn + icol;
96  if(jq> nElement) G4cerr << "G4UIArrayString: overrange" << G4endl;
97 
98  jq--;
99  return &stringArray[jq];
100 }
101 
102 ////////////////////////////////////////////
103 G4int G4UIArrayString::GetNRow(int icol) const
104 ////////////////////////////////////////////
105 {
106  G4int ni;
107  if(nElement%nColumn ==0) ni= nElement/nColumn;
108  else ni= nElement/nColumn + 1;
109 
110  G4int nn= nElement%nColumn;
111  if(nn==0) nn= nColumn;
112 
113  if(icol<= nn) return ni;
114  else return ni-1;
115 }
116 
117 ////////////////////////////////////////////////
118 G4int G4UIArrayString::GetNField(int icol) const
119 ////////////////////////////////////////////////
120 {
121  G4int maxWidth=0;
122  for (G4int iy=1; iy<= GetNRow(icol); iy++) {
123  G4int ilen= GetElement(icol,iy)-> length();
124  // care for color code
125  // if(GetElement(icol,iy)-> index(strESC,0) != G4String::npos) {
126  // if(strESC == (*GetElement(icol,iy))[0] ) {
127  const char tgt = (*GetElement(icol,iy))[(size_t)0];
128  if(strESC == tgt) {
129  ilen-= 5;
130  if(ilen<0) G4cout << "length(c) cal. error." << G4endl;
131  }
132  if(ilen> maxWidth) maxWidth= ilen;
133  }
134 
135  return maxWidth;
136 }
137 
138 /////////////////////////////////////////////////
139 int G4UIArrayString::CalculateColumnWidth() const
140 /////////////////////////////////////////////////
141 {
142  G4int totalWidth= 0;
143 
144  for(G4int ix=1; ix<= nColumn; ix++) {
145  totalWidth+= GetNField(ix);
146  }
147 
148  const G4int nwSpace= 2;
149  totalWidth+= (nColumn-1)*nwSpace; // for space
150 
151  return totalWidth;
152 }
153 
154 //////////////////////////////////////
156 //////////////////////////////////////
157 {
158  // calculate #colums in need...
159  while( CalculateColumnWidth()< ncol ) {
160  nColumn++;
161  }
162  while( CalculateColumnWidth()> ncol && nColumn>1 ) {
163  nColumn--;
164  }
165 
166  for(G4int iy=1; iy<= GetNRow(1); iy++) {
167  G4int nc= nColumn;
168  if(iy == GetNRow(1)) { // last row
169  nc= nElement%nColumn;
170  if(nc==0) nc= nColumn;
171  }
172  for(G4int ix=1; ix<=nc; ix++) {
173  G4String word= GetElement(ix,iy)-> data();
174 
175  // care for color code
176  G4String colorWord;
177  //if(word.index(strESC,0) != G4String::npos) {
178  //if(strESC == word[0]) {
179  const char tgt = word[(size_t)0];
180  if(strESC == tgt) {
181  colorWord= word(0,5);
182  word.erase(0,5);
183  }
184  if(!colorWord.empty()) G4cout << colorWord << std::flush;
185 
186  G4cout << std::setiosflags(std::ios::left) << std::setw(GetNField(ix))
187  << word.c_str() << std::flush;
188  // against problem w/ g++ iostream
189  if(ix != nc) G4cout << " " << std::flush;
190  else G4cout << G4endl;
191  }
192  }
193 }
194 
G4String strip(G4int strip_Type=trailing, char c=' ')
int G4int
Definition: G4Types.hh:78
void Show(G4int ncol)
G4GLOB_DLL std::ostream G4cout
str_size index(const char *, G4int pos=0) const
#define G4endl
Definition: G4ios.hh:61
const XML_Char const XML_Char * data
G4UIArrayString(const G4String &stream)
G4GLOB_DLL std::ostream G4cerr