G4UIbatch.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 // $Id$
00027 //
00028 // ====================================================================
00029 //   G4UIbatch.cc
00030 //
00031 // ====================================================================
00032 #include "G4UIbatch.hh"
00033 #include "G4UImanager.hh"
00034 #include <vector>
00035 #include <string>
00036 
00038 static void Tokenize(const G4String& str, std::vector<G4String>& tokens)
00040 {
00041   const char* delimiter= " ";
00042 
00043   G4String::size_type pos0= str.find_first_not_of(delimiter);
00044   G4String::size_type pos = str.find_first_of(delimiter, pos0);
00045 
00046   while (pos != G4String::npos || pos0 != G4String::npos) {
00047     if (str[pos0] == '\"') {
00048       pos = str.find_first_of("\"", pos0+1);
00049       if(pos != G4String::npos) pos++;
00050     }
00051     if (str[pos0] == '\'') {
00052       pos = str.find_first_of("\'", pos0+1);
00053       if(pos != G4String::npos) pos++;
00054     }
00055 
00056     tokens.push_back(str.substr(pos0, pos-pos0));
00057     pos0 = str.find_first_not_of(delimiter, pos);
00058     pos = str.find_first_of(delimiter, pos0);
00059   }
00060 }
00061 
00062 // ====================================================================
00063 //
00064 // class description
00065 //
00066 // ====================================================================
00067 
00069 G4UIbatch::G4UIbatch(const char* fileName, G4UIsession* prevSession)
00070   : previousSession(prevSession), isOpened(false)
00072 {
00073   macroStream.open(fileName, std::ios::in);
00074   if(macroStream.fail()) {
00075     G4cerr << "***** Can not open a macro file <"
00076            << fileName << ">"
00077            << G4endl;
00078   } else {
00079     isOpened= true;
00080   }
00081 
00082   G4UImanager::GetUIpointer()-> SetSession(this);
00083 }
00084 
00085 
00087 G4UIbatch::~G4UIbatch()
00089 {
00090   if(isOpened) macroStream.close();
00091 }
00092 
00093 
00095 G4String G4UIbatch::ReadCommand()
00097 {
00098   enum { BUFSIZE= 4096 };
00099   static char linebuf[BUFSIZE];
00100   const char ctrM = 0x0d;
00101 
00102   G4String cmdtotal= "";
00103   G4bool qcontinued= false;
00104   while(macroStream.good()) {
00105     macroStream.getline(linebuf, BUFSIZE);
00106 
00107     G4String cmdline(linebuf);
00108 
00109     // TAB-> ' ' conversion
00110     str_size nb=0;
00111     while ((nb= cmdline.find('\t',nb)) != G4String::npos) {
00112       cmdline.replace(nb, 1, " ");
00113     }
00114 
00115     // strip
00116     cmdline= cmdline.strip(G4String::both);
00117     cmdline= cmdline.strip(G4String::trailing, ctrM);
00118 
00119     // skip null line if single line
00120     if(!qcontinued && cmdline.size()==0) continue;
00121 
00122     // '#' is treated as echoing something
00123     if(cmdline[(size_t)0]=='#') return cmdline;
00124 
00125     // tokenize...
00126     std::vector<G4String> tokens;
00127     Tokenize(cmdline, tokens);
00128     qcontinued= false;
00129     for (G4int i=0; i< G4int(tokens.size()); i++) {
00130       // string after '#" is ignored
00131       if(tokens[i][(size_t)0] == '#' ) break;
00132       // '\' or '_' is treated as continued line.
00133       if(tokens[i] == '\\' || tokens[i] == '_' ) {
00134         qcontinued= true;
00135         // check nothing after line continuation character
00136         if( i != G4int(tokens.size())-1) {
00137           G4Exception("G4UIbatch::ReadCommand","UI0003",
00138              JustWarning,
00139              "unexpected character after line continuation character");
00140         }
00141         break; // stop parsing
00142       }
00143       cmdtotal+= tokens[i];
00144       cmdtotal+= " ";
00145     }
00146 
00147     if(qcontinued) continue; // read the next line
00148 
00149     if(cmdtotal.size() != 0) break;
00150     if(macroStream.eof()) break;
00151   }
00152 
00153   // strip again
00154   cmdtotal= cmdtotal.strip(G4String::both);
00155 
00156   // finally,
00157   if(macroStream.eof() && cmdtotal.size()==0) {
00158     return "exit";
00159   }
00160 
00161   return cmdtotal;
00162 }
00163 
00164 
00166 G4int G4UIbatch::ExecCommand(const G4String& command)
00168 {
00169   G4UImanager* UI= G4UImanager::GetUIpointer();
00170   G4int rc= UI-> ApplyCommand(command);
00171 
00172   switch(rc) {
00173   case fCommandSucceeded:
00174     break;
00175   case fCommandNotFound:
00176     G4cerr << "***** COMMAND NOT FOUND <"
00177            << command << "> *****" << G4endl;
00178     break;
00179   case fIllegalApplicationState:
00180     G4cerr << "***** Illegal application state <"
00181            << command << "> *****" << G4endl;
00182     break;
00183   default:
00184     G4int pn= rc%100;
00185     G4cerr << "***** Illegal parameter (" << pn << ") <"
00186            << command << "> *****" << G4endl;
00187   }
00188 
00189   return rc;
00190 }
00191 
00192 
00194 G4UIsession * G4UIbatch::SessionStart()
00196 {
00197   if(!isOpened) return previousSession;
00198 
00199   while(1) {
00200     G4String newCommand = ReadCommand();
00201 
00202     if(newCommand == "exit") {
00203       break;
00204     }
00205 
00206     // just echo something
00207     if( newCommand[(size_t)0] == '#') {
00208       if(G4UImanager::GetUIpointer()-> GetVerboseLevel()==2) {
00209         G4cout << newCommand << G4endl;
00210       }
00211       continue;
00212     }
00213 
00214     // execute command
00215     G4int rc= ExecCommand(newCommand);
00216     if(rc != fCommandSucceeded) {
00217       G4cerr << G4endl << "***** Batch is interrupted!! *****" << G4endl;
00218       break;
00219     }
00220   }
00221 
00222   return previousSession;
00223 }
00224 
00225 
00227 void G4UIbatch::PauseSessionStart(const G4String& Prompt)
00229 {
00230   G4cout << "Pause session <" << Prompt << "> start." << G4endl;
00231 
00232   SessionStart();
00233 
00234   G4cout << "Pause session <" << Prompt << "> Terminate." << G4endl;
00235 }
00236 

Generated on Mon May 27 17:50:05 2013 for Geant4 by  doxygen 1.4.7