Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

G4VUIshell.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id: G4VUIshell.cc,v 1.9 2006/06/29 19:09:59 gunter Exp $
00028 // GEANT4 tag $Name: geant4-08-01-patch-01 $
00029 //
00030 
00031 #include "G4UImanager.hh"
00032 #include "G4UIcommand.hh"
00033 #include "G4UIcommandTree.hh"
00034 #include "G4StateManager.hh"
00035 #include "G4UIcommandStatus.hh"
00036 #include "G4VUIshell.hh"
00037 #include "G4UIArrayString.hh"
00038 
00039 // terminal color string
00040 static const G4String strESC= '\033';
00041 static const G4String TermColorString[8] ={ 
00042   strESC+"[30m", strESC+"[31m", strESC+"[32m", strESC+"[33m", 
00043   strESC+"[34m", strESC+"[35m", strESC+"[36m", strESC+"[37m"
00044 };
00045 
00047 G4VUIshell::G4VUIshell(const G4String& prompt)
00048   : promptSetting(prompt), promptString(""), nColumn(80),  
00049     lsColorFlag(FALSE), directoryColor(BLACK), commandColor(BLACK),
00050     currentCommandDir("/")
00052 {
00053 }
00054 
00056 G4VUIshell::~G4VUIshell()
00058 {
00059 }
00060 
00062 void G4VUIshell::MakePrompt(const char* msg) 
00063 
00064 {
00065   if(promptSetting.length()<=1) {
00066     promptString= promptSetting;
00067     return;
00068   }
00069 
00070   promptString="";
00071   G4int i;
00072   for(i=0; i<G4int(promptSetting.length())-1; i++){
00073     if(promptSetting[(size_t)i]=='%'){
00074       switch (promptSetting[(size_t)(i+1)]) {
00075       case 's':  // current application status
00076         {
00077            G4String stateStr;
00078            if(msg)
00079            { stateStr = msg; }
00080            else
00081            {
00082              G4StateManager* statM= G4StateManager::GetStateManager();
00083              stateStr= statM-> GetStateString(statM->GetCurrentState());
00084            }
00085            promptString.append(stateStr);
00086            i++;
00087         }
00088         break;
00089       case '/':  // current working directory
00090         promptString.append(currentCommandDir);
00091         i++;
00092         break;
00093       default:
00094         promptString.append(G4String(promptSetting[(size_t)i]));
00095         break;
00096       }           
00097     } else {
00098       promptString.append(G4String(promptSetting[(size_t)i]));
00099     }
00100   }
00101 
00102   // append last chaacter
00103   if(i == G4int(promptSetting.length())-1) 
00104     promptString.append(G4String(promptSetting[(size_t)i]));
00105 }
00106 
00107 
00108 // --------------------------------------------------------------------
00109 //      G4command operations
00110 // --------------------------------------------------------------------
00112 G4UIcommandTree* G4VUIshell::GetCommandTree(const G4String& input) const
00114 {
00115   G4UImanager* UI= G4UImanager::GetUIpointer();
00116 
00117   G4UIcommandTree* cmdTree= UI-> GetTree();  // root tree
00118 
00119   G4String absPath= input; // G4String::strip() CONST !!
00120   absPath= GetAbsCommandDirPath(absPath.strip(G4String::both));
00121 
00122   // parsing absolute path ...
00123   if(absPath.length()==0) return NULL;
00124   if(absPath[absPath.length()-1] != '/') return NULL; // error??
00125   if(absPath=="/") return cmdTree;
00126 
00127   for(G4int indx=1; indx<G4int(absPath.length())-1; ) {
00128     G4int jslash= absPath.index("/", indx);  // search index begin with "/" 
00129     if(jslash != G4int(G4String::npos)) {
00130       if(cmdTree != NULL)
00131         cmdTree= cmdTree-> GetTree(G4String(absPath(0,jslash+1)));
00132     }
00133     indx= jslash+1;
00134   }
00135 
00136   if(cmdTree == NULL) return NULL;
00137   else return cmdTree;
00138 }
00139 
00141 G4String G4VUIshell::GetAbsCommandDirPath(const G4String& apath) const
00143 {
00144   if(apath.empty()) return apath;  // null string
00145 
00146   // if "apath" does not start with "/", 
00147   //   then it is treared as relative path
00148   G4String bpath= apath;
00149   if(apath[(size_t)0] != '/') bpath= currentCommandDir + apath;
00150 
00151   // parsing...
00152   G4String absPath= "/";
00153   for(G4int indx=1; indx<=G4int(bpath.length())-1; ) {
00154     G4int jslash= bpath.index("/", indx);  // search index begin with "/"
00155     if(jslash != G4int(G4String::npos)) {
00156       if(bpath(indx,jslash-indx) == ".."){  // directory up
00157         if(absPath.length() >=1) {
00158           absPath.remove(absPath.length()-1);  // remove last  "/"
00159           G4int jpre= absPath.last('/');
00160           if(jpre != G4int(G4String::npos)) absPath.remove(jpre+1);
00161         }
00162       } else if(bpath(indx,jslash-indx) == "."){  // nothing to do
00163       } else { // add
00164         if( !(jslash==indx && bpath(indx)=='/') ) // truncate "////"
00165           absPath+= bpath(indx, jslash-indx+1);
00166           // better to be check directory existence. (it costs!)
00167       }
00168     } else { // directory ONLY (ignore non-"/" terminated string)
00169     }
00170     indx= jslash+1;
00171   }
00172   return  absPath;
00173 }
00174 
00175 
00177 G4String G4VUIshell::GetCommandPathTail(const G4String& apath) const
00179 {   // xxx/xxx/zzz -> zzz, trancate /// -> /
00180   if(apath.empty()) return apath;
00181 
00182   G4int lstr= apath.length();
00183 
00184   // for trancating "/"
00185   G4bool Qsla= FALSE;
00186   if(apath[(size_t)(lstr-1)]=='/') Qsla= TRUE;
00187 
00188   // searching last '/' from tail
00189   G4int indx= -1;
00190   for(G4int i=lstr-1; i>=0; i--) {
00191     if(Qsla && apath[(size_t)i]!='/') Qsla= FALSE; // break "/" flag!!
00192     if(apath[(size_t)i]=='/' && !Qsla) {
00193       indx= i;
00194       break;
00195     } 
00196   }
00197 
00198   if(indx==-1) return apath;  // not found
00199 
00200   if(indx==0  && lstr==1) { // "/"
00201     G4String nullStr;
00202     return nullStr;
00203   } else {  
00204     //G4String newPath= apath(indx+1,lstr-indx-1); 
00205     G4String newPath= apath;
00206     newPath= newPath(indx+1,lstr-indx-1);
00207     return newPath;
00208   }
00209 }
00210 
00211 // --------------------------------------------------------------------
00212 //      shell commands
00213 // --------------------------------------------------------------------
00215 void G4VUIshell::ListCommand(const G4String& dir, 
00216                              const G4String& candidate) const
00218 {
00219   // specified directpry
00220   G4String input= dir; // ...
00221   input= input.strip(G4String::both);
00222 
00223   // command tree of "user specified directory"
00224   G4String vpath= currentCommandDir;
00225   G4String vcmd;
00226 
00227   G4int len= input.length();
00228   if(! input.empty()) {
00229     G4int indx= -1;
00230     for(G4int i=len-1; i>=0; i--) { // search last '/'
00231       if(input[(size_t)i]=='/') {
00232         indx= i;
00233         break;
00234       }   
00235     }
00236     // get abs. path
00237     if(indx != -1) vpath= GetAbsCommandDirPath(input(0,indx+1));
00238     if(!(indx==0  && len==1)) vcmd= input(indx+1,len-indx-1); // care for "/"
00239   }
00240 
00241   // check "vcmd" is directory?
00242   G4String inputpath= vpath+vcmd;
00243   if(! vcmd.empty()){
00244     G4String tmpstr= inputpath + "/";
00245     if(GetCommandTree(tmpstr) != NULL) {
00246       vpath= tmpstr;
00247       vcmd= "";
00248     }
00249   }
00250       
00251   // check "vpath" directory exists?
00252   G4UIcommandTree* atree= GetCommandTree(vpath);  
00253   if(atree == NULL) {
00254     G4cout << "<" << input << ">: No such directory" << G4endl;
00255     return;
00256   }
00257 
00258   // list matched directories/commands
00259   G4String stream;
00260   G4bool isMatch= FALSE;
00261 
00262   G4int Ndir= atree-> GetTreeEntry();
00263   G4int Ncmd= atree-> GetCommandEntry();
00264   if(Ndir==0 && Ncmd==0) return;  // no contents
00265   
00266   // directory ...
00267   for(G4int idir=1; idir<=Ndir; idir++) {
00268     if(idir==1 && lsColorFlag) stream+= TermColorString[directoryColor];
00269     G4String fpdir= atree-> GetTree(idir)-> GetPathName();
00270     // matching test
00271     if(candidate.empty()) { // list all
00272       if(vcmd=="" || fpdir==inputpath) {
00273         stream+= GetCommandPathTail(fpdir); stream+= "  ";
00274         isMatch= TRUE;
00275       }
00276     } else { // list only matched with candidate
00277       if( fpdir.index(candidate, 0) == 0) {
00278         stream+= GetCommandPathTail(fpdir); stream+= "  ";
00279       }
00280     }
00281   }
00282   
00283   // command ...
00284   for(G4int icmd=1; icmd<=Ncmd; icmd++){
00285     if(icmd==1 && lsColorFlag) stream+= TermColorString[commandColor];
00286     G4String fpcmd= atree-> GetPathName() +
00287              atree-> GetCommand(icmd) -> GetCommandName();
00288     // matching test
00289     if(candidate.empty()) { // list all
00290       if(vcmd=="" || fpcmd==inputpath) {
00291         stream+= GetCommandPathTail(fpcmd); stream+= "*  ";
00292         isMatch= TRUE;
00293       }
00294     } else {  // list only matched with candidate
00295       if( fpcmd.index(candidate, 0) == 0) {
00296         stream+= GetCommandPathTail(fpcmd); stream+= "*  ";
00297       }
00298     }
00299   }
00300   
00301   // waring : not matched
00302   if(!isMatch && candidate.empty()) 
00303     G4cout << "<" << input 
00304            << ">: No such directory or command" << std::flush;
00305 
00306   // display
00307   G4UIArrayString arrayString(stream);
00308   arrayString.Show(nColumn);
00309 }

Generated on Fri Jun 22 11:07:02 2007 by doxygen 1.3.4