Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4VUIshell.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 "G4UImanager.hh"
30#include "G4UIcommand.hh"
31#include "G4UIcommandTree.hh"
32#include "G4StateManager.hh"
33#include "G4UIcommandStatus.hh"
34#include "G4VUIshell.hh"
35#include "G4UIArrayString.hh"
36
37// terminal color string
38static const G4String strESC= '\033';
39static const G4String TermColorString[8] ={
40 strESC+"[30m", strESC+"[31m", strESC+"[32m", strESC+"[33m",
41 strESC+"[34m", strESC+"[35m", strESC+"[36m", strESC+"[37m"
42};
43
44///////////////////////////////////////////////////////////////////
46 : promptSetting(prompt), promptString(""), nColumn(80),
47 lsColorFlag(FALSE), directoryColor(BLACK), commandColor(BLACK),
48 currentCommandDir("/")
49///////////////////////////////////////////////////////////////////
50{
51}
52
53/////////////////////////
55/////////////////////////
56{
57}
58
59////////////////////////////////////////////
60void G4VUIshell::MakePrompt(const char* msg)
61////////////////////////////////////////////
62{
63 if(promptSetting.length()<=1) {
65 return;
66 }
67
68 promptString="";
69 G4int i;
70 for(i=0; i<G4int(promptSetting.length())-1; i++){
71 if(promptSetting[(size_t)i]=='%'){
72 switch (promptSetting[(size_t)(i+1)]) {
73 case 's': // current application status
74 {
75 G4String stateStr;
76 if(msg)
77 { stateStr = msg; }
78 else
79 {
81 stateStr= statM-> GetStateString(statM->GetCurrentState());
82 }
83 promptString.append(stateStr);
84 i++;
85 }
86 break;
87 case '/': // current working directory
89 i++;
90 break;
91 default:
93 break;
94 }
95 } else {
97 }
98 }
99
100 // append last chaacter
101 if(i == G4int(promptSetting.length())-1)
103}
104
105
106////////////////////////////////
108////////////////////////////////
109{
110
111}
112
113// --------------------------------------------------------------------
114// G4command operations
115// --------------------------------------------------------------------
116////////////////////////////////////////////////////////////////////////
118////////////////////////////////////////////////////////////////////////
119{
121
122 G4UIcommandTree* cmdTree= UI-> GetTree(); // root tree
123
124 G4String absPath= input; // G4String::strip() CONST !!
125 absPath= GetAbsCommandDirPath(absPath.strip(G4String::both));
126
127 // parsing absolute path ...
128 if(absPath.length()==0) return NULL;
129 if(absPath[absPath.length()-1] != '/') return NULL; // error??
130 if(absPath=="/") return cmdTree;
131
132 for(G4int indx=1; indx<G4int(absPath.length())-1; ) {
133 G4int jslash= absPath.index("/", indx); // search index begin with "/"
134 if(jslash != G4int(G4String::npos)) {
135 if(cmdTree != NULL)
136 cmdTree= cmdTree-> GetTree(G4String(absPath(0,jslash+1)));
137 }
138 indx= jslash+1;
139 }
140
141 if(cmdTree == NULL) return NULL;
142 else return cmdTree;
143}
144
145//////////////////////////////////////////////////////////////////////
147//////////////////////////////////////////////////////////////////////
148{
149 if(apath.empty()) return apath; // null string
150
151 // if "apath" does not start with "/",
152 // then it is treared as relative path
153 G4String bpath= apath;
154 if(apath[(size_t)0] != '/') bpath= currentCommandDir + apath;
155
156 // parsing...
157 G4String absPath= "/";
158 for(G4int indx=1; indx<=G4int(bpath.length())-1; ) {
159 G4int jslash= bpath.index("/", indx); // search index begin with "/"
160 if(indx == jslash) { // skip first '///'
161 indx++;
162 continue;
163 }
164 if(jslash != G4int(G4String::npos)) {
165 if(bpath.substr(indx,jslash-indx) == ".."){ // directory up
166 if(absPath == "/") {
167 indx = jslash+1;
168 continue;
169 }
170 if(absPath.length() >= 2) {
171 absPath.remove(absPath.length()-1); // remove last "/"
172 G4int jpre= absPath.last('/');
173 if(jpre != G4int(G4String::npos)) absPath.remove(jpre+1);
174 }
175 } else if(bpath.substr(indx,jslash-indx) == "."){ // nothing to do
176 } else { // add
177 if( !(jslash==indx && bpath(indx)=='/') ) // truncate "////"
178 absPath+= bpath(indx, jslash-indx+1);
179 // better to be check directory existence. (it costs!)
180 }
181 indx= jslash+1;
182 } else { // directory ONLY (ignore non-"/" terminated string)
183 break;
184 }
185 }
186
187 return absPath;
188}
189
190
191////////////////////////////////////////////////////////////////////
193////////////////////////////////////////////////////////////////////
194{ // xxx/xxx/zzz -> zzz, trancate /// -> /
195 if(apath.empty()) return apath;
196
197 G4int lstr= apath.length();
198
199 // for trancating "/"
200 G4bool Qsla= FALSE;
201 if(apath[(size_t)(lstr-1)]=='/') Qsla= TRUE;
202
203 // searching last '/' from tail
204 G4int indx= -1;
205 for(G4int i=lstr-1; i>=0; i--) {
206 if(Qsla && apath[(size_t)i]!='/') Qsla= FALSE; // break "/" flag!!
207 if(apath[(size_t)i]=='/' && !Qsla) {
208 indx= i;
209 break;
210 }
211 }
212
213 if(indx==-1) return apath; // not found
214
215 if(indx==0 && lstr==1) { // "/"
216 G4String nullStr;
217 return nullStr;
218 } else {
219 //G4String newPath= apath(indx+1,lstr-indx-1);
220 G4String newPath= apath;
221 newPath= newPath(indx+1,lstr-indx-1);
222 return newPath;
223 }
224}
225
226// --------------------------------------------------------------------
227// shell commands
228// --------------------------------------------------------------------
229/////////////////////////////////////////////////////////////
231 const G4String& candidate) const
232/////////////////////////////////////////////////////////////
233{
234 // specified directpry
235 G4String input= dir; // ...
236 input= input.strip(G4String::both);
237
238 // command tree of "user specified directory"
239 G4String vpath= currentCommandDir;
240 G4String vcmd;
241
242 G4int len= input.length();
243 if(! input.empty()) {
244 G4int indx= -1;
245 for(G4int i=len-1; i>=0; i--) { // search last '/'
246 if(input[(size_t)i]=='/') {
247 indx= i;
248 break;
249 }
250 }
251 // get abs. path
252 if(indx != -1) vpath= GetAbsCommandDirPath(input(0,indx+1));
253 if(!(indx==0 && len==1)) vcmd= input(indx+1,len-indx-1); // care for "/"
254 }
255
256 // check "vcmd" is directory?
257 G4String inputpath= vpath+vcmd;
258 if(! vcmd.empty()){
259 G4String tmpstr= inputpath + "/";
260 if(GetCommandTree(tmpstr) != NULL) {
261 vpath= tmpstr;
262 vcmd= "";
263 }
264 }
265
266 // check "vpath" directory exists?
267 G4UIcommandTree* atree= GetCommandTree(vpath);
268 if(atree == NULL) {
269 G4cout << "<" << input << ">: No such directory" << G4endl;
270 return;
271 }
272
273 // list matched directories/commands
274 G4String stream;
275 G4bool isMatch= FALSE;
276
277 G4int Ndir= atree-> GetTreeEntry();
278 G4int Ncmd= atree-> GetCommandEntry();
279 if(Ndir==0 && Ncmd==0) return; // no contents
280
281 // directory ...
282 for(G4int idir=1; idir<=Ndir; idir++) {
283 if(idir==1 && lsColorFlag) stream+= TermColorString[directoryColor];
284 G4String fpdir= atree-> GetTree(idir)-> GetPathName();
285 // matching test
286 if(candidate.empty()) { // list all
287 if(vcmd=="" || fpdir==inputpath) {
288 stream+= GetCommandPathTail(fpdir); stream+= " ";
289 isMatch= TRUE;
290 }
291 } else { // list only matched with candidate
292 if( fpdir.index(candidate, 0) == 0) {
293 stream+= GetCommandPathTail(fpdir); stream+= " ";
294 }
295 }
296 }
297
298 // command ...
299 for(G4int icmd=1; icmd<=Ncmd; icmd++){
300 if(icmd==1 && lsColorFlag) stream+= TermColorString[commandColor];
301 G4String fpcmd= atree-> GetPathName() +
302 atree-> GetCommand(icmd) -> GetCommandName();
303 // matching test
304 if(candidate.empty()) { // list all
305 if(vcmd=="" || fpcmd==inputpath) {
306 stream+= GetCommandPathTail(fpcmd); stream+= "* ";
307 isMatch= TRUE;
308 }
309 } else { // list only matched with candidate
310 if( fpcmd.index(candidate, 0) == 0) {
311 stream+= GetCommandPathTail(fpcmd); stream+= "* ";
312 }
313 }
314 }
315
316 // waring : not matched
317 if(!isMatch && candidate.empty())
318 G4cout << "<" << input
319 << ">: No such directory or command" << std::flush;
320
321 // display
322 G4UIArrayString arrayString(stream);
323 arrayString.Show(nColumn);
324}
325
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
@ BLACK
Definition: G4VUIshell.hh:53
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
#define TRUE
Definition: Globals.hh:27
#define FALSE
Definition: Globals.hh:23
const G4ApplicationState & GetCurrentState() const
static G4StateManager * GetStateManager()
G4String & remove(str_size)
G4String & append(const G4String &)
str_size index(const char *, G4int pos=0) const
std::size_t last(char) const
G4String strip(G4int strip_Type=trailing, char c=' ')
void Show(G4int ncol)
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:77
virtual void ResetTerminal()
Definition: G4VUIshell.cc:107
virtual void MakePrompt(const char *msg=0)
Definition: G4VUIshell.cc:60
virtual void ListCommand(const G4String &input, const G4String &candidate="") const
Definition: G4VUIshell.cc:230
G4UIcommandTree * GetCommandTree(const G4String &dir) const
Definition: G4VUIshell.cc:117
G4String currentCommandDir
Definition: G4VUIshell.hh:72
G4VUIshell(const G4String &prompt="> ")
Definition: G4VUIshell.cc:45
G4String promptString
Definition: G4VUIshell.hh:61
G4String promptSetting
Definition: G4VUIshell.hh:60
G4String GetCommandPathTail(const G4String &apath) const
Definition: G4VUIshell.cc:192
virtual ~G4VUIshell()
Definition: G4VUIshell.cc:54
G4String GetAbsCommandDirPath(const G4String &apath) const
Definition: G4VUIshell.cc:146