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,v 1.17 2008/11/21 10:54:16 kmura Exp $ |
---|
27 | // GEANT4 tag $Name: geant4-09-03 $ |
---|
28 | // |
---|
29 | // ==================================================================== |
---|
30 | // G4UIbatch.cc |
---|
31 | // |
---|
32 | // ==================================================================== |
---|
33 | #include "G4UIbatch.hh" |
---|
34 | #include "G4UImanager.hh" |
---|
35 | #include <vector> |
---|
36 | |
---|
37 | //////////////////////////////////////////////////////////////////////// |
---|
38 | static void Tokenize(const G4String& str, std::vector<G4String>& tokens) |
---|
39 | //////////////////////////////////////////////////////////////////////// |
---|
40 | { |
---|
41 | const char* delimiter= " "; |
---|
42 | |
---|
43 | str_size pos0= str.find_first_not_of(delimiter); |
---|
44 | str_size 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 | //////////////////////////////////////////////////////////////////// |
---|
69 | G4UIbatch::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 | /////////////////////// |
---|
87 | G4UIbatch::~G4UIbatch() |
---|
88 | /////////////////////// |
---|
89 | { |
---|
90 | if(isOpened) macroStream.close(); |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | ///////////////////////////////// |
---|
95 | G4String G4UIbatch::ReadCommand() |
---|
96 | ///////////////////////////////// |
---|
97 | { |
---|
98 | enum { BUFSIZE= 4096 }; |
---|
99 | static char linebuf[BUFSIZE]; |
---|
100 | |
---|
101 | G4String cmdtotal= ""; |
---|
102 | G4bool qcontinued= false; |
---|
103 | while(macroStream.good()) { |
---|
104 | macroStream.getline(linebuf, BUFSIZE); |
---|
105 | |
---|
106 | G4String cmdline(linebuf); |
---|
107 | |
---|
108 | // TAB-> ' ' conversion |
---|
109 | str_size nb=0; |
---|
110 | while ((nb= cmdline.find('\t',nb)) != G4String::npos) { |
---|
111 | cmdline.replace(nb, 1, " "); |
---|
112 | } |
---|
113 | |
---|
114 | // strip |
---|
115 | cmdline= cmdline.strip(G4String::both); |
---|
116 | |
---|
117 | // skip null line if single line |
---|
118 | if(!qcontinued && cmdline.size()==0) continue; |
---|
119 | |
---|
120 | // '#' is treated as echoing something |
---|
121 | if(cmdline[(size_t)0]=='#') return cmdline; |
---|
122 | |
---|
123 | // tokenize... |
---|
124 | std::vector<G4String> tokens; |
---|
125 | Tokenize(cmdline, tokens); |
---|
126 | qcontinued= false; |
---|
127 | for (G4int i=0; i< G4int(tokens.size()); i++) { |
---|
128 | // string after '#" is ignored |
---|
129 | if(tokens[i][(size_t)0] == '#' ) break; |
---|
130 | // '\' or '_' is treated as continued line. |
---|
131 | if(tokens[i] == '\\' || tokens[i] == '_' ) { |
---|
132 | qcontinued= true; |
---|
133 | // check nothing after line continuation character |
---|
134 | if( i != G4int(tokens.size())-1) { |
---|
135 | G4Exception("unexpected character after " |
---|
136 | "line continuation character"); |
---|
137 | } |
---|
138 | break; // stop parsing |
---|
139 | } |
---|
140 | cmdtotal+= tokens[i]; |
---|
141 | cmdtotal+= " "; |
---|
142 | } |
---|
143 | |
---|
144 | if(qcontinued) continue; // read the next line |
---|
145 | |
---|
146 | if(cmdtotal.size() != 0) break; |
---|
147 | if(macroStream.eof()) break; |
---|
148 | } |
---|
149 | |
---|
150 | // strip again |
---|
151 | cmdtotal= cmdtotal.strip(G4String::both); |
---|
152 | |
---|
153 | // finally, |
---|
154 | if(macroStream.eof() && cmdtotal.size()==0) { |
---|
155 | return "exit"; |
---|
156 | } |
---|
157 | |
---|
158 | return cmdtotal; |
---|
159 | } |
---|
160 | |
---|
161 | |
---|
162 | ///////////////////////////////////////////////////// |
---|
163 | G4int G4UIbatch::ExecCommand(const G4String& command) |
---|
164 | ///////////////////////////////////////////////////// |
---|
165 | { |
---|
166 | G4UImanager* UI= G4UImanager::GetUIpointer(); |
---|
167 | G4int rc= UI-> ApplyCommand(command); |
---|
168 | |
---|
169 | switch(rc) { |
---|
170 | case fCommandSucceeded: |
---|
171 | break; |
---|
172 | case fCommandNotFound: |
---|
173 | G4cerr << "***** COMMAND NOT FOUND <" |
---|
174 | << command << "> *****" << G4endl; |
---|
175 | break; |
---|
176 | case fIllegalApplicationState: |
---|
177 | G4cerr << "***** Illegal application state <" |
---|
178 | << command << "> *****" << G4endl; |
---|
179 | break; |
---|
180 | default: |
---|
181 | G4int pn= rc%100; |
---|
182 | G4cerr << "***** Illegal parameter (" << pn << ") <" |
---|
183 | << command << "> *****" << G4endl; |
---|
184 | } |
---|
185 | |
---|
186 | return rc; |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | /////////////////////////////////////// |
---|
191 | G4UIsession * G4UIbatch::SessionStart() |
---|
192 | /////////////////////////////////////// |
---|
193 | { |
---|
194 | if(!isOpened) return previousSession; |
---|
195 | |
---|
196 | while(1) { |
---|
197 | G4String newCommand = ReadCommand(); |
---|
198 | |
---|
199 | if(newCommand == "exit") { |
---|
200 | break; |
---|
201 | } |
---|
202 | |
---|
203 | // just echo something |
---|
204 | if( newCommand[(size_t)0] == '#') { |
---|
205 | if(G4UImanager::GetUIpointer()-> GetVerboseLevel()==2) { |
---|
206 | G4cout << newCommand << G4endl; |
---|
207 | } |
---|
208 | continue; |
---|
209 | } |
---|
210 | |
---|
211 | // execute command |
---|
212 | G4int rc= ExecCommand(newCommand); |
---|
213 | if(rc != fCommandSucceeded) { |
---|
214 | G4cerr << G4endl << "***** Batch is interupted!! *****" << G4endl; |
---|
215 | break; |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | return previousSession; |
---|
220 | } |
---|
221 | |
---|
222 | |
---|
223 | ////////////////////////////////////////////////// |
---|
224 | void G4UIbatch::PauseSessionStart(G4String Prompt) |
---|
225 | ////////////////////////////////////////////////// |
---|
226 | { |
---|
227 | G4cout << "Pause session <" << Prompt << "> start." << G4endl; |
---|
228 | |
---|
229 | SessionStart(); |
---|
230 | |
---|
231 | G4cout << "Pause session <" << Prompt << "> Terminate." << G4endl; |
---|
232 | } |
---|
233 | |
---|