Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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//
28
29#include <iomanip>
30#include "G4UIArrayString.hh"
31
32static const char strESC= '\033';
33
34////////////////////////////////////////////////////////
36////////////////////////////////////////////////////////
37{
38 nElement=0;
39 nColumn=5; // temporal assignment
40
41 G4String astream = G4StrUtil::strip_copy(stream);
42
43 // tokenize...
44 std::size_t indx=0;
45 while(1) {
46 std::size_t jc= astream.find(" ", indx);
47 nElement++;
48 if(jc == G4String::npos) break;
49 jc++; // fix a tiny mistake...
50 for(; jc< astream.length(); ) { // skip continuing spaces
51 if(astream[(G4int)jc]==' ') jc++;
52 else break;
53 }
54 indx= jc;
55 }
56
57 // allocate string array
58 stringArray= new G4String[nElement];
59
60 // push...
61 indx=0;
62 for(G4int i=0; i<nElement; ++i){
63 std::size_t jc= astream.find(" ", indx);
64 if(jc != 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(std::size_t j=1; jc+j< astream.length(); ++j ) { // skip continuing spaces
71 if(astream[G4int(jc+j)]==' ') jc++;
72 else break;
73 }
74 indx= jc+1;
75 }
76}
77
78///////////////////////////////////
80///////////////////////////////////
81{
82 delete [] stringArray;
83}
84
85///////////////////////////////////////////////////////////////////
86G4String* G4UIArrayString::GetElement(G4int icol, G4int irow) const
87///////////////////////////////////////////////////////////////////
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
100////////////////////////////////////////////
101G4int G4UIArrayString::GetNRow(int icol) const
102////////////////////////////////////////////
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
115////////////////////////////////////////////////
116G4int G4UIArrayString::GetNField(int icol) const
117////////////////////////////////////////////////
118{
119 std::size_t maxWidth=0;
120 for (G4int iy=1; iy<= GetNRow(icol); iy++) {
121 std::size_t 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))[(std::size_t)0];
126 if(strESC == tgt) {
127 ilen-= 5;
128 }
129 if(ilen> maxWidth) maxWidth= ilen;
130 }
131
132 return (G4int)maxWidth;
133}
134
135/////////////////////////////////////////////////
136int G4UIArrayString::CalculateColumnWidth() const
137/////////////////////////////////////////////////
138{
139 G4int totalWidth= 0;
140
141 for(G4int ix=1; ix<= nColumn; ix++) {
142 totalWidth+= GetNField(ix);
143 }
144
145 const G4int nwSpace= 2;
146 totalWidth+= (nColumn-1)*nwSpace; // for space
147
148 return totalWidth;
149}
150
151//////////////////////////////////////
153//////////////////////////////////////
154{
155 // calculate #colums in need...
156 while( CalculateColumnWidth()< ncol ) {
157 nColumn++;
158 }
159 while( CalculateColumnWidth()> ncol && nColumn>1 ) {
160 nColumn--;
161 }
162
163 for(G4int iy=1; iy<= GetNRow(1); iy++) {
164 G4int nc= nColumn;
165 if(iy == GetNRow(1)) { // last row
166 nc= nElement%nColumn;
167 if(nc==0) nc= nColumn;
168 }
169 for(G4int ix=1; ix<=nc; ix++) {
170 G4String word= GetElement(ix,iy)-> data();
171
172 // care for color code
173 G4String colorWord;
174 const char tgt = word[(std::size_t)0];
175 if(strESC == tgt) {
176 colorWord= word.substr(0,5);
177 word.erase(0,5);
178 }
179 if(!colorWord.empty()) G4cout << colorWord << std::flush;
180
181 G4cout << std::setiosflags(std::ios::left) << std::setw(GetNField(ix))
182 << word.c_str() << std::flush;
183 // against problem w/ g++ iostream
184 if(ix != nc) G4cout << " " << std::flush;
185 else G4cout << G4endl;
186 }
187 }
188}
189
int G4int
Definition: G4Types.hh:85
G4GLOB_DLL std::ostream G4cerr
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
void Show(G4int ncol)
G4UIArrayString(const G4String &stream)
G4String strip_copy(G4String str, char ch=' ')
Return copy of string with leading and trailing characters removed.