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