ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4UIbatch.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4UIbatch.cc
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 // G4UIbatch.cc
29 //
30 // ====================================================================
31 #include "G4UIbatch.hh"
32 #include "G4UImanager.hh"
33 #include <vector>
34 #include <string>
35 
37 static void Tokenize(const G4String& str, std::vector<G4String>& tokens)
39 {
40  const char* delimiter= " ";
41 
42  G4String::size_type pos0= str.find_first_not_of(delimiter);
43  G4String::size_type pos = str.find_first_of(delimiter, pos0);
44 
45  while (pos != G4String::npos || pos0 != G4String::npos) {
46  if (str[pos0] == '\"') {
47  pos = str.find_first_of("\"", pos0+1);
48  if(pos != G4String::npos) pos++;
49  }
50  if (str[pos0] == '\'') {
51  pos = str.find_first_of("\'", pos0+1);
52  if(pos != G4String::npos) pos++;
53  }
54 
55  tokens.push_back(str.substr(pos0, pos-pos0));
56  pos0 = str.find_first_not_of(delimiter, pos);
57  pos = str.find_first_of(delimiter, pos0);
58  }
59 }
60 
61 // ====================================================================
62 //
63 // class description
64 //
65 // ====================================================================
66 
68 G4UIbatch::G4UIbatch(const char* fileName, G4UIsession* prevSession)
69  : G4UIsession(1),
70  previousSession(prevSession), isOpened(false)
72 {
73  macroStream.open(fileName, std::ios::in);
74  if(macroStream.fail()) {
75  G4cerr << "ERROR: Can not open a macro file <"
76  << fileName << ">. Set macro path with \"/control/macroPath\" if needed."
77  << G4endl;
79  } else {
80  isOpened= true;
81  }
82 
83  G4UImanager::GetUIpointer()-> SetSession(this);
84 }
85 
86 
90 {
91  if(isOpened) macroStream.close();
92 }
93 
94 
98 {
99  enum { BUFSIZE= 4096 };
100  static G4ThreadLocal char *linebuf = 0 ; if (!linebuf) linebuf = new char [BUFSIZE];
101  const char ctrM = 0x0d;
102 
103  G4String cmdtotal= "";
104  G4bool qcontinued= false;
105  while(macroStream.good()) {
106  macroStream.getline(linebuf, BUFSIZE);
107 
108  G4String cmdline(linebuf);
109 
110  // TAB-> ' ' conversion
111  str_size nb=0;
112  while ((nb= cmdline.find('\t',nb)) != G4String::npos) {
113  cmdline.replace(nb, 1, " ");
114  }
115 
116  // strip
117  cmdline= cmdline.strip(G4String::both);
118  cmdline= cmdline.strip(G4String::trailing, ctrM);
119 
120  // skip null line if single line
121  if(!qcontinued && cmdline.size()==0) continue;
122 
123  // '#' is treated as echoing something
124  if(cmdline[(size_t)0]=='#') return cmdline;
125 
126  // tokenize...
127  std::vector<G4String> tokens;
128  Tokenize(cmdline, tokens);
129  qcontinued= false;
130  for (G4int i=0; i< G4int(tokens.size()); i++) {
131  // string after '#" is ignored
132  if(tokens[i][(size_t)0] == '#' ) break;
133  // '\' or '_' is treated as continued line.
134  if(tokens[i] == '\\' || tokens[i] == '_' ) {
135  qcontinued= true;
136  // check nothing after line continuation character
137  if( i != G4int(tokens.size())-1) {
138  G4Exception("G4UIbatch::ReadCommand","UI0003",
139  JustWarning,
140  "unexpected character after line continuation character");
141  }
142  break; // stop parsing
143  }
144  cmdtotal+= tokens[i];
145  cmdtotal+= " ";
146  }
147 
148  if(qcontinued) continue; // read the next line
149 
150  if(cmdtotal.size() != 0) break;
151  if(macroStream.eof()) break;
152  }
153 
154  // strip again
155  cmdtotal= cmdtotal.strip(G4String::both);
156 
157  // finally,
158  if(macroStream.eof() && cmdtotal.size()==0) {
159  return "exit";
160  }
161 
162  return cmdtotal;
163 }
164 
165 
169 {
171  G4int rc= UI-> ApplyCommand(command);
172 
173  switch(rc) {
174  case fCommandSucceeded:
175  break;
176  case fCommandNotFound:
177  G4cerr << "***** COMMAND NOT FOUND <"
178  << command << "> *****" << G4endl;
179  break;
181  G4cerr << "***** Illegal application state <"
182  << command << "> *****" << G4endl;
183  break;
184  default:
185  G4int pn= rc%100;
186  G4cerr << "***** Illegal parameter (" << pn << ") <"
187  << command << "> *****" << G4endl;
188  }
189 
190  return rc;
191 }
192 
193 
197 {
198  if(!isOpened) return previousSession;
199 
200  while(1) {
201  G4String newCommand = ReadCommand();
202 
203  if(newCommand == "exit") {
204  break;
205  }
206 
207  // just echo something
208  if( newCommand[(size_t)0] == '#') {
209  if(G4UImanager::GetUIpointer()-> GetVerboseLevel()==2) {
210  G4cout << newCommand << G4endl;
211  }
212  continue;
213  }
214 
215  // execute command
216  G4int rc= ExecCommand(newCommand);
217  if(rc != fCommandSucceeded) {
218  G4cerr << G4endl << "***** Batch is interrupted!! *****" << G4endl;
219  lastRC = rc;
220  break;
221  }
222  }
223 
224  return previousSession;
225 }
226 
227 
231 {
232  G4cout << "Pause session <" << Prompt << "> start." << G4endl;
233 
234  SessionStart();
235 
236  G4cout << "Pause session <" << Prompt << "> Terminate." << G4endl;
237 }
238