Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4UIbatch.cc
Go to the documentation of this file.
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 // $Id: G4UIbatch.cc 98731 2016-08-09 10:49:49Z gcosmo $
27 //
28 // ====================================================================
29 // G4UIbatch.cc
30 //
31 // ====================================================================
32 #include "G4UIbatch.hh"
33 #include "G4UImanager.hh"
34 #include <vector>
35 #include <string>
36 
38 static void Tokenize(const G4String& str, std::vector<G4String>& tokens)
40 {
41  const char* delimiter= " ";
42 
43  G4String::size_type pos0= str.find_first_not_of(delimiter);
44  G4String::size_type pos = str.find_first_of(delimiter, pos0);
45 
46  while (pos != G4String::npos || pos0 != G4String::npos) {
47  if (str[pos0] == '\"') {
48  pos = str.find_first_of("\"", pos0+1);
49  if(pos != G4String::npos) pos++;
50  }
51  if (str[pos0] == '\'') {
52  pos = str.find_first_of("\'", pos0+1);
53  if(pos != G4String::npos) pos++;
54  }
55 
56  tokens.push_back(str.substr(pos0, pos-pos0));
57  pos0 = str.find_first_not_of(delimiter, pos);
58  pos = str.find_first_of(delimiter, pos0);
59  }
60 }
61 
62 // ====================================================================
63 //
64 // class description
65 //
66 // ====================================================================
67 
69 G4UIbatch::G4UIbatch(const char* fileName, G4UIsession* prevSession)
70  : G4UIsession(1),
71  previousSession(prevSession), isOpened(false)
73 {
74  macroStream.open(fileName, std::ios::in);
75  if(macroStream.fail()) {
76  G4cerr << "ERROR: Can not open a macro file <"
77  << fileName << ">. Set macro path with \"/control/macroPath\" if needed."
78  << 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 
96 G4String G4UIbatch::ReadCommand()
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 
167 G4int G4UIbatch::ExecCommand(const G4String& command)
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  break;
220  }
221  }
222 
223  return previousSession;
224 }
225 
226 
230 {
231  G4cout << "Pause session <" << Prompt << "> start." << G4endl;
232 
233  SessionStart();
234 
235  G4cout << "Pause session <" << Prompt << "> Terminate." << G4endl;
236 }
237 
G4String strip(G4int strip_Type=trailing, char c=' ')
virtual G4UIsession * SessionStart()
Definition: G4UIbatch.cc:195
static void Tokenize(const G4String &str, std::vector< G4String > &tokens)
Definition: G4UIbatch.cc:38
std::string::size_type str_size
Definition: G4String.hh:57
#define G4ThreadLocal
Definition: tls.hh:89
int G4int
Definition: G4Types.hh:78
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:59
G4GLOB_DLL std::ostream G4cout
bool G4bool
Definition: G4Types.hh:79
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4UIbatch(const char *fileName, G4UIsession *prevSession=0)
Definition: G4UIbatch.cc:69
virtual void PauseSessionStart(const G4String &Prompt)
Definition: G4UIbatch.cc:228
#define G4endl
Definition: G4ios.hh:61
~G4UIbatch()
Definition: G4UIbatch.cc:88
#define BUFSIZE
Definition: liblist.c:41
static const G4double pos
G4GLOB_DLL std::ostream G4cerr