Geant4 11.1.1
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(1,'\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[i]=='%'){
72 switch (promptSetting[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 = GetAbsCommandDirPath(G4StrUtil::strip_copy(input));
125
126 // parsing absolute path ...
127 if(absPath.length()==0) return NULL;
128 if(absPath[G4int(absPath.length()-1)] != '/') return NULL; // error??
129 if(absPath=="/") return cmdTree;
130
131 for(std::size_t indx=1; indx<absPath.length()-1; ) {
132 std::size_t jslash= absPath.find("/", indx); // search index begin with "/"
133 if(jslash != G4String::npos) {
134 if(cmdTree != NULL)
135 cmdTree= cmdTree-> GetTree(G4String(absPath.substr(0,jslash+1)));
136 }
137 indx= jslash+1;
138 }
139
140 if(cmdTree == NULL) return NULL;
141 else return cmdTree;
142}
143
144//////////////////////////////////////////////////////////////////////
146//////////////////////////////////////////////////////////////////////
147{
148 if(apath.empty()) return apath; // null string
149
150 // if "apath" does not start with "/",
151 // then it is treared as relative path
152 G4String bpath= apath;
153 if(apath[(std::size_t)0] != '/') bpath= currentCommandDir + apath;
154
155 // parsing...
156 G4String absPath= "/";
157 for(std::size_t indx=1; indx<=bpath.length()-1; ) {
158 std::size_t jslash= bpath.find("/", indx); // search index begin with "/"
159 if(indx == jslash) { // skip first '///'
160 ++indx;
161 continue;
162 }
163 if(jslash != G4String::npos) {
164 if(bpath.substr(indx,jslash-indx) == ".."){ // directory up
165 if(absPath == "/") {
166 indx = jslash+1;
167 continue;
168 }
169 if(absPath.length() >= 2) {
170 absPath.erase(absPath.length()-1); // remove last "/"
171 auto jpre= absPath.rfind('/');
172 if(jpre != G4String::npos) absPath.erase(jpre+1);
173 }
174 } else if(bpath.substr(indx,jslash-indx) == "."){ // nothing to do
175 } else { // add
176 if( !(jslash==indx && bpath[(G4int)indx]=='/') ) // truncate "////"
177 absPath+= bpath.substr(indx, jslash-indx+1);
178 // better to be check directory existence. (it costs!)
179 }
180 indx= jslash+1;
181 } else { // directory ONLY (ignore non-"/" terminated string)
182 break;
183 }
184 }
185
186 return absPath;
187}
188
189
190////////////////////////////////////////////////////////////////////
192////////////////////////////////////////////////////////////////////
193{ // xxx/xxx/zzz -> zzz, trancate /// -> /
194 if(apath.empty()) return apath;
195
196 G4int lstr= (G4int)apath.length();
197
198 // for trancating "/"
199 G4bool Qsla= FALSE;
200 if(apath[lstr-1]=='/') Qsla= TRUE;
201
202 // searching last '/' from tail
203 G4int indx= -1;
204 for(G4int i=lstr-1; i>=0; --i) {
205 if(Qsla && apath[i]!='/') Qsla= FALSE; // break "/" flag!!
206 if(apath[i]=='/' && !Qsla) {
207 indx= i;
208 break;
209 }
210 }
211
212 if(indx==-1) return apath; // not found
213
214 if(indx==0 && lstr==1) { // "/"
215 G4String nullStr;
216 return nullStr;
217 } else {
218 //G4String newPath= apath(indx+1,lstr-indx-1);
219 G4String newPath= apath;
220 newPath= newPath.substr(indx+1,lstr-indx-1);
221 return newPath;
222 }
223}
224
225// --------------------------------------------------------------------
226// shell commands
227// --------------------------------------------------------------------
228/////////////////////////////////////////////////////////////
230 const G4String& candidate) const
231/////////////////////////////////////////////////////////////
232{
233 // specified directpry
234 G4String input = G4StrUtil::strip_copy(dir);
235
236 // command tree of "user specified directory"
237 G4String vpath= currentCommandDir;
238 G4String vcmd;
239
240 G4int len= (G4int)input.length();
241 if(! input.empty()) {
242 G4int indx= -1;
243 for(G4int i=len-1; i>=0; --i) { // search last '/'
244 if(input[i]=='/') {
245 indx= i;
246 break;
247 }
248 }
249 // get abs. path
250 if(indx != -1) vpath= GetAbsCommandDirPath(input.substr(0,indx+1));
251 if(!(indx==0 && len==1)) vcmd= input.substr(indx+1,len-indx-1); // care for "/"
252 }
253
254 // check "vcmd" is directory?
255 G4String inputpath= vpath+vcmd;
256 if(! vcmd.empty()){
257 G4String tmpstr= inputpath + "/";
258 if(GetCommandTree(tmpstr) != NULL) {
259 vpath= tmpstr;
260 vcmd= "";
261 }
262 }
263
264 // check "vpath" directory exists?
265 G4UIcommandTree* atree= GetCommandTree(vpath);
266 if(atree == NULL) {
267 G4cout << "<" << input << ">: No such directory" << G4endl;
268 return;
269 }
270
271 // list matched directories/commands
272 G4String stream;
273 G4bool isMatch= FALSE;
274
275 G4int Ndir= atree-> GetTreeEntry();
276 G4int Ncmd= atree-> GetCommandEntry();
277 if(Ndir==0 && Ncmd==0) return; // no contents
278
279 // directory ...
280 for(G4int idir=1; idir<=Ndir; idir++) {
281 if(idir==1 && lsColorFlag) stream+= TermColorString[directoryColor];
282 G4String fpdir= atree-> GetTree(idir)-> GetPathName();
283 // matching test
284 if(candidate.empty()) { // list all
285 if(vcmd=="" || fpdir==inputpath) {
286 stream+= GetCommandPathTail(fpdir); stream+= " ";
287 isMatch= TRUE;
288 }
289 } else { // list only matched with candidate
290 if( fpdir.find(candidate, 0) == 0) {
291 stream+= GetCommandPathTail(fpdir); stream+= " ";
292 }
293 }
294 }
295
296 // command ...
297 for(G4int icmd=1; icmd<=Ncmd; icmd++){
298 if(icmd==1 && lsColorFlag) stream+= TermColorString[commandColor];
299 G4String fpcmd= atree-> GetPathName() +
300 atree-> GetCommand(icmd) -> GetCommandName();
301 // matching test
302 if(candidate.empty()) { // list all
303 if(vcmd=="" || fpcmd==inputpath) {
304 stream+= GetCommandPathTail(fpcmd); stream+= "* ";
305 isMatch= TRUE;
306 }
307 } else { // list only matched with candidate
308 if( fpcmd.find(candidate, 0) == 0) {
309 stream+= GetCommandPathTail(fpcmd); stream+= "* ";
310 }
311 }
312 }
313
314 // waring : not matched
315 if(!isMatch && candidate.empty())
316 G4cout << "<" << input
317 << ">: No such directory or command" << std::flush;
318
319 // display
320 G4UIArrayString arrayString(stream);
321 arrayString.Show(nColumn);
322}
323
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
const G4ApplicationState & GetCurrentState() const
static G4StateManager * GetStateManager()
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:229
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:191
virtual ~G4VUIshell()
Definition: G4VUIshell.cc:54
G4String GetAbsCommandDirPath(const G4String &apath) const
Definition: G4VUIshell.cc:145
#define TRUE
Definition: globals.hh:41
#define FALSE
Definition: globals.hh:38
G4String strip_copy(G4String str, char ch=' ')
Return copy of string with leading and trailing characters removed.