source: trunk/geant4/interfaces/basic/src/G4VUIshell.cc @ 483

Last change on this file since 483 was 483, checked in by garnier, 17 years ago

r569@mac-90108: laurentgarnier | 2007-06-05 15:53:34 +0200
version contre geant4.8.2.p01

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