Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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// G4UIbatch
27//
28// Author: M.Asai, 2000
29// --------------------------------------------------------------------
30
31#include "G4UIbatch.hh"
32#include "G4UImanager.hh"
33#include <vector>
34#include <string>
35
36// --------------------------------------------------------------------
37static void Tokenize(const G4String& str, std::vector<G4String>& tokens)
38{
39 const char* delimiter = " ";
40
41 G4String::size_type pos0 = str.find_first_not_of(delimiter);
42 G4String::size_type pos = str.find_first_of(delimiter, pos0);
43
44 while(pos != G4String::npos || pos0 != G4String::npos)
45 {
46 if(str[(G4int)pos0] == '\"')
47 {
48 pos = str.find_first_of("\"", pos0 + 1);
49 if(pos != G4String::npos)
50 {
51 pos++;
52 }
53 }
54 if(str[(G4int)pos0] == '\'')
55 {
56 pos = str.find_first_of("\'", pos0 + 1);
57 if(pos != G4String::npos)
58 {
59 pos++;
60 }
61 }
62
63 tokens.emplace_back(str.substr(pos0, pos - pos0));
64 pos0 = str.find_first_not_of(delimiter, pos);
65 pos = str.find_first_of(delimiter, pos0);
66 }
67}
68
69// --------------------------------------------------------------------
70G4UIbatch::G4UIbatch(const char* fileName, G4UIsession* prevSession)
71 : G4UIsession(1)
72 , previousSession(prevSession)
73{
74 macroStream.open(fileName, std::ios::in);
75 if(macroStream.fail())
76 {
77 G4cerr << "ERROR: Can not open a macro file <" << fileName
78 << ">. Set macro path with \"/control/macroPath\" if needed."
79 << G4endl;
81 }
82 else
83 {
84 isOpened = true;
85 }
86
88}
89
90// --------------------------------------------------------------------
92{
93 if(isOpened)
94 {
95 macroStream.close();
96 }
97}
98
99// --------------------------------------------------------------------
100G4String G4UIbatch::ReadCommand()
101{
102 enum
103 {
104 BUFSIZE = 4096
105 };
106 static G4ThreadLocal char* linebuf = nullptr;
107 if(linebuf == nullptr)
108 {
109 linebuf = new char[BUFSIZE];
110 }
111 const char ctrM = 0x0d;
112
113 G4String cmdtotal = "";
114 G4bool qcontinued = false;
115 while(macroStream.good())
116 {
117 macroStream.getline(linebuf, BUFSIZE);
118
119 G4String cmdline(linebuf);
120
121 // TAB-> ' ' conversion
122 G4String::size_type nb = 0;
123 while((nb = cmdline.find('\t', nb)) != G4String::npos)
124 {
125 cmdline.replace(nb, 1, " ");
126 }
127
128 // strip
129 G4StrUtil::strip(cmdline);
130 G4StrUtil::rstrip(cmdline, ctrM);
131
132 // skip null line if single line
133 if(!qcontinued && cmdline.empty())
134 {
135 continue;
136 }
137
138 // '#' is treated as echoing something
139 if(cmdline[(std::size_t) 0] == '#')
140 {
141 return cmdline;
142 }
143
144 // tokenize...
145 std::vector<G4String> tokens;
146 Tokenize(cmdline, tokens);
147 qcontinued = false;
148 for(G4int i = 0; i < G4int(tokens.size()); ++i)
149 {
150 // string after '#" is ignored
151 if(tokens[i][(std::size_t) 0] == '#')
152 {
153 break;
154 }
155 // '\' or '_' is treated as continued line.
156 if(tokens[i] == "\\" || tokens[i] == "_")
157 {
158 qcontinued = true;
159 // check nothing after line continuation character
160 if(i != G4int(tokens.size()) - 1)
161 {
162 G4Exception("G4UIbatch::ReadCommand", "UI0003", JustWarning,
163 "unexpected character after line continuation character");
164 }
165 break; // stop parsing
166 }
167 cmdtotal += tokens[i];
168 cmdtotal += " ";
169 }
170
171 if(qcontinued)
172 {
173 continue; // read the next line
174 }
175
176 if(!cmdtotal.empty())
177 {
178 break;
179 }
180 if(macroStream.eof())
181 {
182 break;
183 }
184 }
185
186 // strip again
187 G4StrUtil::strip(cmdtotal);
188
189 // finally,
190 if(macroStream.eof() && cmdtotal.empty())
191 {
192 return "exit";
193 }
194
195 return cmdtotal;
196}
197
198// --------------------------------------------------------------------
199G4int G4UIbatch::ExecCommand(const G4String& command)
200{
202 G4int rc = UI->ApplyCommand(command);
203
204 switch(rc)
205 {
207 break;
208 case fCommandNotFound:
209 G4cerr << "***** COMMAND NOT FOUND <" << command << "> *****" << G4endl;
210 break;
212 G4cerr << "***** Illegal application state <" << command << "> *****"
213 << G4endl;
214 break;
215 default:
216 G4int pn = rc % 100;
217 G4cerr << "***** Illegal parameter (" << pn << ") <" << command
218 << "> *****" << G4endl;
219 }
220
221 return rc;
222}
223
224// --------------------------------------------------------------------
226{
227 if(!isOpened)
228 {
229 return previousSession;
230 }
231
232 while(true)
233 {
234 G4String newCommand = ReadCommand();
235
236 if(newCommand == "exit")
237 {
238 break;
239 }
240
241 // just echo something
242 if(newCommand[(std::size_t) 0] == '#')
243 {
244 if(G4UImanager::GetUIpointer()->GetVerboseLevel() == 2)
245 {
246 G4cout << newCommand << G4endl;
247 }
248 continue;
249 }
250
251 // execute command
252 G4int rc = ExecCommand(newCommand);
253 if(rc != fCommandSucceeded)
254 {
255 G4cerr << G4endl << "***** Batch is interrupted!! *****" << G4endl;
256 lastRC = rc;
257 break;
258 }
259 }
260
261 return previousSession;
262}
263
264// --------------------------------------------------------------------
266{
267 G4cout << "Pause session <" << Prompt << "> start." << G4endl;
268
269 SessionStart();
270
271 G4cout << "Pause session <" << Prompt << "> Terminate." << G4endl;
272}
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:59
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
@ fCommandNotFound
@ fIllegalApplicationState
@ fParameterUnreadable
@ fCommandSucceeded
G4GLOB_DLL std::ostream G4cerr
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
void PauseSessionStart(const G4String &Prompt) override
Definition: G4UIbatch.cc:265
~G4UIbatch() override
Definition: G4UIbatch.cc:91
G4UIsession * SessionStart() override
Definition: G4UIbatch.cc:225
G4UIbatch(const char *fileName, G4UIsession *prevSession=nullptr)
Definition: G4UIbatch.cc:70
G4int ApplyCommand(const char *aCommand)
Definition: G4UImanager.cc:495
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:77
void SetSession(G4UIsession *const value)
Definition: G4UImanager.hh:190
G4int lastRC
Definition: G4UIsession.hh:69
void Tokenize(const G4String &line, std::vector< G4String > &tokens)
void strip(G4String &str, char ch=' ')
Remove leading and trailing characters from string.
void rstrip(G4String &str, char ch=' ')
Remove trailing characters from string.
#define G4ThreadLocal
Definition: tls.hh:77