Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // G4UIbatch 26 // G4UIbatch 27 // 27 // 28 // Author: M.Asai, 2000 28 // Author: M.Asai, 2000 29 // ------------------------------------------- 29 // -------------------------------------------------------------------- 30 30 31 #include "G4UIbatch.hh" 31 #include "G4UIbatch.hh" 32 << 33 #include "G4UImanager.hh" 32 #include "G4UImanager.hh" 34 << 35 #include <string> << 36 #include <vector> 33 #include <vector> >> 34 #include <string> 37 35 38 // ------------------------------------------- 36 // -------------------------------------------------------------------- 39 static void Tokenize(const G4String& str, std: 37 static void Tokenize(const G4String& str, std::vector<G4String>& tokens) 40 { 38 { 41 const char* delimiter = " "; 39 const char* delimiter = " "; 42 40 43 G4String::size_type pos0 = str.find_first_no 41 G4String::size_type pos0 = str.find_first_not_of(delimiter); 44 G4String::size_type pos = str.find_first_of( << 42 G4String::size_type pos = str.find_first_of(delimiter, pos0); 45 43 46 while (pos != G4String::npos || pos0 != G4St << 44 while(pos != G4String::npos || pos0 != G4String::npos) 47 if (str[(G4int)pos0] == '\"') { << 45 { 48 pos = str.find_first_of('\"', pos0 + 1); << 46 if(str[(G4int)pos0] == '\"') 49 if (pos != G4String::npos) { << 47 { >> 48 pos = str.find_first_of("\"", pos0 + 1); >> 49 if(pos != G4String::npos) >> 50 { 50 pos++; 51 pos++; 51 } 52 } 52 } 53 } 53 if (str[(G4int)pos0] == '\'') { << 54 if(str[(G4int)pos0] == '\'') 54 pos = str.find_first_of('\'', pos0 + 1); << 55 { 55 if (pos != G4String::npos) { << 56 pos = str.find_first_of("\'", pos0 + 1); >> 57 if(pos != G4String::npos) >> 58 { 56 pos++; 59 pos++; 57 } 60 } 58 } 61 } 59 62 60 tokens.emplace_back(str.substr(pos0, pos - 63 tokens.emplace_back(str.substr(pos0, pos - pos0)); 61 pos0 = str.find_first_not_of(delimiter, po 64 pos0 = str.find_first_not_of(delimiter, pos); 62 pos = str.find_first_of(delimiter, pos0); << 65 pos = str.find_first_of(delimiter, pos0); 63 } 66 } 64 } 67 } 65 68 66 // ------------------------------------------- 69 // -------------------------------------------------------------------- 67 G4UIbatch::G4UIbatch(const char* fileName, G4U 70 G4UIbatch::G4UIbatch(const char* fileName, G4UIsession* prevSession) 68 : G4UIsession(1), previousSession(prevSessio << 71 : G4UIsession(1) >> 72 , previousSession(prevSession) 69 { 73 { 70 macroStream.open(fileName, std::ios::in); 74 macroStream.open(fileName, std::ios::in); 71 if (macroStream.fail()) { << 75 if(macroStream.fail()) >> 76 { 72 G4cerr << "ERROR: Can not open a macro fil 77 G4cerr << "ERROR: Can not open a macro file <" << fileName 73 << ">. Set macro path with \"/contr << 78 << ">. Set macro path with \"/control/macroPath\" if needed." >> 79 << G4endl; 74 lastRC = fParameterUnreadable; 80 lastRC = fParameterUnreadable; 75 } 81 } 76 else { << 82 else >> 83 { 77 isOpened = true; 84 isOpened = true; 78 } 85 } 79 86 80 G4UImanager::GetUIpointer()->SetSession(this 87 G4UImanager::GetUIpointer()->SetSession(this); 81 } 88 } 82 89 83 // ------------------------------------------- 90 // -------------------------------------------------------------------- 84 G4UIbatch::~G4UIbatch() 91 G4UIbatch::~G4UIbatch() 85 { 92 { 86 if (isOpened) { << 93 if(isOpened) >> 94 { 87 macroStream.close(); 95 macroStream.close(); 88 } 96 } 89 } 97 } 90 98 91 // ------------------------------------------- 99 // -------------------------------------------------------------------- 92 G4String G4UIbatch::ReadCommand() 100 G4String G4UIbatch::ReadCommand() 93 { 101 { 94 enum 102 enum 95 { 103 { 96 BUFSIZE = 4096 104 BUFSIZE = 4096 97 }; 105 }; 98 static G4ThreadLocal char* linebuf = nullptr 106 static G4ThreadLocal char* linebuf = nullptr; 99 if (linebuf == nullptr) { << 107 if(linebuf == nullptr) >> 108 { 100 linebuf = new char[BUFSIZE]; 109 linebuf = new char[BUFSIZE]; 101 } 110 } 102 const char ctrM = 0x0d; 111 const char ctrM = 0x0d; 103 112 104 G4String cmdtotal = ""; 113 G4String cmdtotal = ""; 105 G4bool qcontinued = false; 114 G4bool qcontinued = false; 106 while (macroStream.good()) { << 115 while(macroStream.good()) >> 116 { 107 macroStream.getline(linebuf, BUFSIZE); 117 macroStream.getline(linebuf, BUFSIZE); 108 118 109 G4String cmdline(linebuf); 119 G4String cmdline(linebuf); 110 120 111 // TAB-> ' ' conversion 121 // TAB-> ' ' conversion 112 G4String::size_type nb = 0; 122 G4String::size_type nb = 0; 113 while ((nb = cmdline.find('\t', nb)) != G4 << 123 while((nb = cmdline.find('\t', nb)) != G4String::npos) >> 124 { 114 cmdline.replace(nb, 1, " "); 125 cmdline.replace(nb, 1, " "); 115 } 126 } 116 127 117 // strip 128 // strip 118 G4StrUtil::strip(cmdline); 129 G4StrUtil::strip(cmdline); 119 G4StrUtil::rstrip(cmdline, ctrM); 130 G4StrUtil::rstrip(cmdline, ctrM); 120 131 121 // skip null line if single line 132 // skip null line if single line 122 if (!qcontinued && cmdline.empty()) { << 133 if(!qcontinued && cmdline.empty()) >> 134 { 123 continue; 135 continue; 124 } 136 } 125 137 126 // '#' is treated as echoing something 138 // '#' is treated as echoing something 127 if (cmdline[(std::size_t)0] == '#') { << 139 if(cmdline[(std::size_t) 0] == '#') >> 140 { 128 return cmdline; 141 return cmdline; 129 } 142 } 130 143 131 // tokenize... 144 // tokenize... 132 std::vector<G4String> tokens; 145 std::vector<G4String> tokens; 133 Tokenize(cmdline, tokens); 146 Tokenize(cmdline, tokens); 134 qcontinued = false; 147 qcontinued = false; 135 for (G4int i = 0; i < G4int(tokens.size()) << 148 for(G4int i = 0; i < G4int(tokens.size()); ++i) >> 149 { 136 // string after '#" is ignored 150 // string after '#" is ignored 137 if (tokens[i][(std::size_t)0] == '#') { << 151 if(tokens[i][(std::size_t) 0] == '#') >> 152 { 138 break; 153 break; 139 } 154 } 140 // '\' or '_' is treated as continued li 155 // '\' or '_' is treated as continued line. 141 if (tokens[i] == "\\" || tokens[i] == "_ << 156 if(tokens[i] == "\\" || tokens[i] == "_") >> 157 { 142 qcontinued = true; 158 qcontinued = true; 143 // check nothing after line continuati 159 // check nothing after line continuation character 144 if (i != G4int(tokens.size()) - 1) { << 160 if(i != G4int(tokens.size()) - 1) >> 161 { 145 G4Exception("G4UIbatch::ReadCommand" 162 G4Exception("G4UIbatch::ReadCommand", "UI0003", JustWarning, 146 "unexpected character af 163 "unexpected character after line continuation character"); 147 } 164 } 148 break; // stop parsing 165 break; // stop parsing 149 } 166 } 150 cmdtotal += tokens[i]; 167 cmdtotal += tokens[i]; 151 cmdtotal += " "; 168 cmdtotal += " "; 152 } 169 } 153 170 154 if (qcontinued) { << 171 if(qcontinued) >> 172 { 155 continue; // read the next line 173 continue; // read the next line 156 } 174 } 157 175 158 if (!cmdtotal.empty()) { << 176 if(!cmdtotal.empty()) >> 177 { 159 break; 178 break; 160 } 179 } 161 if (macroStream.eof()) { << 180 if(macroStream.eof()) >> 181 { 162 break; 182 break; 163 } 183 } 164 } 184 } 165 185 166 // strip again 186 // strip again 167 G4StrUtil::strip(cmdtotal); 187 G4StrUtil::strip(cmdtotal); 168 188 169 // finally, 189 // finally, 170 if (macroStream.eof() && cmdtotal.empty()) { << 190 if(macroStream.eof() && cmdtotal.empty()) >> 191 { 171 return "exit"; 192 return "exit"; 172 } 193 } 173 194 174 return cmdtotal; 195 return cmdtotal; 175 } 196 } 176 197 177 // ------------------------------------------- 198 // -------------------------------------------------------------------- 178 G4int G4UIbatch::ExecCommand(const G4String& c 199 G4int G4UIbatch::ExecCommand(const G4String& command) 179 { 200 { 180 G4UImanager* UI = G4UImanager::GetUIpointer( 201 G4UImanager* UI = G4UImanager::GetUIpointer(); 181 G4int rc = UI->ApplyCommand(command); << 202 G4int rc = UI->ApplyCommand(command); 182 203 183 switch (rc) { << 204 switch(rc) >> 205 { 184 case fCommandSucceeded: 206 case fCommandSucceeded: 185 break; 207 break; 186 case fCommandNotFound: 208 case fCommandNotFound: 187 G4cerr << "***** COMMAND NOT FOUND <" << 209 G4cerr << "***** COMMAND NOT FOUND <" << command << "> *****" << G4endl; 188 break; 210 break; 189 case fIllegalApplicationState: 211 case fIllegalApplicationState: 190 G4cerr << "***** Illegal application sta << 212 G4cerr << "***** Illegal application state <" << command << "> *****" >> 213 << G4endl; 191 break; 214 break; 192 default: 215 default: 193 G4int pn = rc % 100; 216 G4int pn = rc % 100; 194 G4cerr << "***** Illegal parameter (" << << 217 G4cerr << "***** Illegal parameter (" << pn << ") <" << command >> 218 << "> *****" << G4endl; 195 } 219 } 196 220 197 return rc; 221 return rc; 198 } 222 } 199 223 200 // ------------------------------------------- 224 // -------------------------------------------------------------------- 201 G4UIsession* G4UIbatch::SessionStart() 225 G4UIsession* G4UIbatch::SessionStart() 202 { 226 { 203 if (!isOpened) { << 227 if(!isOpened) >> 228 { 204 return previousSession; 229 return previousSession; 205 } 230 } 206 231 207 while (true) { << 232 while(true) >> 233 { 208 G4String newCommand = ReadCommand(); 234 G4String newCommand = ReadCommand(); 209 235 210 if (newCommand == "exit") { << 236 if(newCommand == "exit") >> 237 { 211 break; 238 break; 212 } 239 } 213 240 214 // just echo something 241 // just echo something 215 if (newCommand[(std::size_t)0] == '#') { << 242 if(newCommand[(std::size_t) 0] == '#') 216 if (G4UImanager::GetUIpointer()->GetVerb << 243 { >> 244 if(G4UImanager::GetUIpointer()->GetVerboseLevel() == 2) >> 245 { 217 G4cout << newCommand << G4endl; 246 G4cout << newCommand << G4endl; 218 } 247 } 219 continue; 248 continue; 220 } 249 } 221 250 222 // execute command 251 // execute command 223 G4int rc = ExecCommand(newCommand); 252 G4int rc = ExecCommand(newCommand); 224 if (rc != fCommandSucceeded) { << 253 if(rc != fCommandSucceeded) >> 254 { 225 G4cerr << G4endl << "***** Batch is inte 255 G4cerr << G4endl << "***** Batch is interrupted!! *****" << G4endl; 226 lastRC = rc; 256 lastRC = rc; 227 break; 257 break; 228 } 258 } 229 } 259 } 230 260 231 return previousSession; 261 return previousSession; 232 } 262 } 233 263 234 // ------------------------------------------- 264 // -------------------------------------------------------------------- 235 void G4UIbatch::PauseSessionStart(const G4Stri 265 void G4UIbatch::PauseSessionStart(const G4String& Prompt) 236 { 266 { 237 G4cout << "Pause session <" << Prompt << "> 267 G4cout << "Pause session <" << Prompt << "> start." << G4endl; 238 268 239 SessionStart(); 269 SessionStart(); 240 270 241 G4cout << "Pause session <" << Prompt << "> 271 G4cout << "Pause session <" << Prompt << "> Terminate." << G4endl; 242 } 272 } 243 273