Geant4 9.6.0
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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$
27//
28// ====================================================================
29// G4UIbatch.cc
30//
31// ====================================================================
32#include "G4UIbatch.hh"
33#include "G4UImanager.hh"
34#include <vector>
35#include <string>
36
37////////////////////////////////////////////////////////////////////////
38static void Tokenize(const G4String& str, std::vector<G4String>& tokens)
39////////////////////////////////////////////////////////////////////////
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
68////////////////////////////////////////////////////////////////////
69G4UIbatch::G4UIbatch(const char* fileName, G4UIsession* prevSession)
70 : previousSession(prevSession), isOpened(false)
71////////////////////////////////////////////////////////////////////
72{
73 macroStream.open(fileName, std::ios::in);
74 if(macroStream.fail()) {
75 G4cerr << "***** Can not open a macro file <"
76 << fileName << ">"
77 << G4endl;
78 } else {
79 isOpened= true;
80 }
81
82 G4UImanager::GetUIpointer()-> SetSession(this);
83}
84
85
86///////////////////////
88///////////////////////
89{
90 if(isOpened) macroStream.close();
91}
92
93
94/////////////////////////////////
95G4String G4UIbatch::ReadCommand()
96/////////////////////////////////
97{
98 enum { BUFSIZE= 4096 };
99 static char linebuf[BUFSIZE];
100 const char ctrM = 0x0d;
101
102 G4String cmdtotal= "";
103 G4bool qcontinued= false;
104 while(macroStream.good()) {
105 macroStream.getline(linebuf, BUFSIZE);
106
107 G4String cmdline(linebuf);
108
109 // TAB-> ' ' conversion
110 str_size nb=0;
111 while ((nb= cmdline.find('\t',nb)) != G4String::npos) {
112 cmdline.replace(nb, 1, " ");
113 }
114
115 // strip
116 cmdline= cmdline.strip(G4String::both);
117 cmdline= cmdline.strip(G4String::trailing, ctrM);
118
119 // skip null line if single line
120 if(!qcontinued && cmdline.size()==0) continue;
121
122 // '#' is treated as echoing something
123 if(cmdline[(size_t)0]=='#') return cmdline;
124
125 // tokenize...
126 std::vector<G4String> tokens;
127 Tokenize(cmdline, tokens);
128 qcontinued= false;
129 for (G4int i=0; i< G4int(tokens.size()); i++) {
130 // string after '#" is ignored
131 if(tokens[i][(size_t)0] == '#' ) break;
132 // '\' or '_' is treated as continued line.
133 if(tokens[i] == '\\' || tokens[i] == '_' ) {
134 qcontinued= true;
135 // check nothing after line continuation character
136 if( i != G4int(tokens.size())-1) {
137 G4Exception("G4UIbatch::ReadCommand","UI0003",
139 "unexpected character after line continuation character");
140 }
141 break; // stop parsing
142 }
143 cmdtotal+= tokens[i];
144 cmdtotal+= " ";
145 }
146
147 if(qcontinued) continue; // read the next line
148
149 if(cmdtotal.size() != 0) break;
150 if(macroStream.eof()) break;
151 }
152
153 // strip again
154 cmdtotal= cmdtotal.strip(G4String::both);
155
156 // finally,
157 if(macroStream.eof() && cmdtotal.size()==0) {
158 return "exit";
159 }
160
161 return cmdtotal;
162}
163
164
165/////////////////////////////////////////////////////
166G4int G4UIbatch::ExecCommand(const G4String& command)
167/////////////////////////////////////////////////////
168{
170 G4int rc= UI-> ApplyCommand(command);
171
172 switch(rc) {
174 break;
175 case fCommandNotFound:
176 G4cerr << "***** COMMAND NOT FOUND <"
177 << command << "> *****" << G4endl;
178 break;
180 G4cerr << "***** Illegal application state <"
181 << command << "> *****" << G4endl;
182 break;
183 default:
184 G4int pn= rc%100;
185 G4cerr << "***** Illegal parameter (" << pn << ") <"
186 << command << "> *****" << G4endl;
187 }
188
189 return rc;
190}
191
192
193///////////////////////////////////////
195///////////////////////////////////////
196{
197 if(!isOpened) return previousSession;
198
199 while(1) {
200 G4String newCommand = ReadCommand();
201
202 if(newCommand == "exit") {
203 break;
204 }
205
206 // just echo something
207 if( newCommand[(size_t)0] == '#') {
208 if(G4UImanager::GetUIpointer()-> GetVerboseLevel()==2) {
209 G4cout << newCommand << G4endl;
210 }
211 continue;
212 }
213
214 // execute command
215 G4int rc= ExecCommand(newCommand);
216 if(rc != fCommandSucceeded) {
217 G4cerr << G4endl << "***** Batch is interrupted!! *****" << G4endl;
218 break;
219 }
220 }
221
222 return previousSession;
223}
224
225
226/////////////////////////////////////////////////////////
228/////////////////////////////////////////////////////////
229{
230 G4cout << "Pause session <" << Prompt << "> start." << G4endl;
231
232 SessionStart();
233
234 G4cout << "Pause session <" << Prompt << "> Terminate." << G4endl;
235}
236
@ JustWarning
std::string::size_type str_size
Definition: G4String.hh:57
int G4int
Definition: G4Types.hh:66
bool G4bool
Definition: G4Types.hh:67
@ fCommandNotFound
@ fIllegalApplicationState
@ fCommandSucceeded
#define G4endl
Definition: G4ios.hh:52
G4DLLIMPORT std::ostream G4cerr
G4DLLIMPORT std::ostream G4cout
@ trailing
Definition: G4String.hh:112
G4String strip(G4int strip_Type=trailing, char c=' ')
~G4UIbatch()
Definition: G4UIbatch.cc:87
virtual G4UIsession * SessionStart()
Definition: G4UIbatch.cc:194
G4UIbatch(const char *fileName, G4UIsession *prevSession=0)
Definition: G4UIbatch.cc:69
virtual void PauseSessionStart(const G4String &Prompt)
Definition: G4UIbatch.cc:227
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:51
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41