00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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
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
00110 str_size nb=0;
00111 while ((nb= cmdline.find('\t',nb)) != G4String::npos) {
00112 cmdline.replace(nb, 1, " ");
00113 }
00114
00115
00116 cmdline= cmdline.strip(G4String::both);
00117 cmdline= cmdline.strip(G4String::trailing, ctrM);
00118
00119
00120 if(!qcontinued && cmdline.size()==0) continue;
00121
00122
00123 if(cmdline[(size_t)0]=='#') return cmdline;
00124
00125
00126 std::vector<G4String> tokens;
00127 Tokenize(cmdline, tokens);
00128 qcontinued= false;
00129 for (G4int i=0; i< G4int(tokens.size()); i++) {
00130
00131 if(tokens[i][(size_t)0] == '#' ) break;
00132
00133 if(tokens[i] == '\\' || tokens[i] == '_' ) {
00134 qcontinued= true;
00135
00136 if( i != G4int(tokens.size())-1) {
00137 G4Exception("G4UIbatch::ReadCommand","UI0003",
00138 JustWarning,
00139 "unexpected character after line continuation character");
00140 }
00141 break;
00142 }
00143 cmdtotal+= tokens[i];
00144 cmdtotal+= " ";
00145 }
00146
00147 if(qcontinued) continue;
00148
00149 if(cmdtotal.size() != 0) break;
00150 if(macroStream.eof()) break;
00151 }
00152
00153
00154 cmdtotal= cmdtotal.strip(G4String::both);
00155
00156
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
00207 if( newCommand[(size_t)0] == '#') {
00208 if(G4UImanager::GetUIpointer()-> GetVerboseLevel()==2) {
00209 G4cout << newCommand << G4endl;
00210 }
00211 continue;
00212 }
00213
00214
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