Geant4-11
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//
28
29#include <iomanip>
30#include "G4UIArrayString.hh"
31
32static const char strESC= '\033';
33
37{
38 nElement=0;
39 nColumn=5; // temporal assignment
40
41 G4String astream = G4StrUtil::strip_copy(stream);
42
43 // tokenize...
44 G4int indx=0;
45 while(1) {
46 G4int jc= astream.find(" ", indx);
47 nElement++;
48 if(jc == G4int(G4String::npos)) break;
49 jc++; // fix a tiny mistake...
50 for(; jc< G4int(astream.length()); ) { // skip continuing spaces
51 if(astream[(size_t)(jc)]==' ') jc++;
52 else break;
53 }
54 indx= jc;
55 }
56
57 // allocate string array
59
60 // push...
61 indx=0;
62 for(G4int i=0; i<nElement; i++){
63 G4int jc= astream.find(" ", indx);
64 if(jc != G4int(G4String::npos))
65 stringArray[i]= astream.substr(indx, jc-indx);
66 else { // last token
67 jc= astream.length()+1;
68 stringArray[i]= astream.substr(indx, jc-indx);
69 }
70 for(G4int j=1; jc+j< G4int(astream.length()); j++ ) { // skip continuing spaces
71 if(astream[jc+j]==' ') jc++;
72 else break;
73 }
74 indx= jc+1;
75 }
76}
77
81{
82 delete [] stringArray;
83}
84
88{
89 if( !(icol>=1 && irow>=1)) // offset of column/row is "1".
90 G4cerr << "G4UIArrayString: overrange" << G4endl;
91 if(icol>nColumn) G4cerr << "G4UIArrayString: overrange" << G4endl;
92
93 G4int jq= (irow-1)*nColumn + icol;
94 if(jq> nElement) G4cerr << "G4UIArrayString: overrange" << G4endl;
95
96 jq--;
97 return &stringArray[jq];
98}
99
103{
104 G4int ni;
105 if(nElement%nColumn ==0) ni= nElement/nColumn;
106 else ni= nElement/nColumn + 1;
107
108 G4int nn= nElement%nColumn;
109 if(nn==0) nn= nColumn;
110
111 if(icol<= nn) return ni;
112 else return ni-1;
113}
114
118{
119 G4int maxWidth=0;
120 for (G4int iy=1; iy<= GetNRow(icol); iy++) {
121 G4int ilen= GetElement(icol,iy)-> length();
122 // care for color code
123 // if(GetElement(icol,iy)-> index(strESC,0) != G4String::npos) {
124 // if(strESC == (*GetElement(icol,iy))[0] ) {
125 const char tgt = (*GetElement(icol,iy))[(size_t)0];
126 if(strESC == tgt) {
127 ilen-= 5;
128 if(ilen<0) G4cout << "length(c) cal. error." << G4endl;
129 }
130 if(ilen> maxWidth) maxWidth= ilen;
131 }
132
133 return maxWidth;
134}
135
139{
140 G4int totalWidth= 0;
141
142 for(G4int ix=1; ix<= nColumn; ix++) {
143 totalWidth+= GetNField(ix);
144 }
145
146 const G4int nwSpace= 2;
147 totalWidth+= (nColumn-1)*nwSpace; // for space
148
149 return totalWidth;
150}
151
155{
156 // calculate #colums in need...
157 while( CalculateColumnWidth()< ncol ) {
158 nColumn++;
159 }
160 while( CalculateColumnWidth()> ncol && nColumn>1 ) {
161 nColumn--;
162 }
163
164 for(G4int iy=1; iy<= GetNRow(1); iy++) {
165 G4int nc= nColumn;
166 if(iy == GetNRow(1)) { // last row
167 nc= nElement%nColumn;
168 if(nc==0) nc= nColumn;
169 }
170 for(G4int ix=1; ix<=nc; ix++) {
171 G4String word= GetElement(ix,iy)-> data();
172
173 // care for color code
174 G4String colorWord;
175 const char tgt = word[(size_t)0];
176 if(strESC == tgt) {
177 colorWord= word.substr(0,5);
178 word.erase(0,5);
179 }
180 if(!colorWord.empty()) G4cout << colorWord << std::flush;
181
182 G4cout << std::setiosflags(std::ios::left) << std::setw(GetNField(ix))
183 << word.c_str() << std::flush;
184 // against problem w/ g++ iostream
185 if(ix != nc) G4cout << " " << std::flush;
186 else G4cout << G4endl;
187 }
188 }
189}
190
int G4int
Definition: G4Types.hh:85
static const char strESC
G4GLOB_DLL std::ostream G4cerr
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
G4String * GetElement(G4int icol, G4int irow) const
G4int GetNRow(G4int icol) const
void Show(G4int ncol)
G4int GetNField(G4int icol) const
G4int CalculateColumnWidth() const
G4UIArrayString(const G4String &stream)
G4String * stringArray
G4String strip_copy(G4String str, char c=' ')
Return copy of string with leading and trailing characters removed.