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 << 27 // 26 // 28 // Author: M.Asai, 2000 << 27 // $Id: G4UIbatch.cc,v 1.14 2006/06/29 19:08:35 gunter Exp $ 29 // ------------------------------------------- << 28 // GEANT4 tag $Name: geant4-08-03-patch-02 $ >> 29 // 30 30 31 #include "G4UIbatch.hh" 31 #include "G4UIbatch.hh" 32 << 33 #include "G4UImanager.hh" 32 #include "G4UImanager.hh" >> 33 #include "G4ios.hh" 34 34 35 #include <string> << 35 G4bool G4UIbatch::commandFailed = false; 36 #include <vector> << 37 << 38 // ------------------------------------------- << 39 static void Tokenize(const G4String& str, std: << 40 { << 41 const char* delimiter = " "; << 42 << 43 G4String::size_type pos0 = str.find_first_no << 44 G4String::size_type pos = str.find_first_of( << 45 36 46 while (pos != G4String::npos || pos0 != G4St << 37 G4UIbatch::G4UIbatch(const char* fileName,G4UIsession* prevSession) 47 if (str[(G4int)pos0] == '\"') { << 38 : previousSession(prevSession), macroFileName(fileName), 48 pos = str.find_first_of('\"', pos0 + 1); << 39 openFailed(false) 49 if (pos != G4String::npos) { << 40 { 50 pos++; << 41 UImanager = G4UImanager::GetUIpointer(); 51 } << 42 UImanager->SetSession(this); 52 } << 43 macroFile.open((char*)fileName); 53 if (str[(G4int)pos0] == '\'') { << 44 if(macroFile.fail()) 54 pos = str.find_first_of('\'', pos0 + 1); << 45 { 55 if (pos != G4String::npos) { << 46 G4cerr << "macro file <" << fileName << "> could not open." 56 pos++; << 47 << G4endl; 57 } << 48 openFailed = true; 58 } << 49 commandFailed = true; 59 << 60 tokens.emplace_back(str.substr(pos0, pos - << 61 pos0 = str.find_first_not_of(delimiter, po << 62 pos = str.find_first_of(delimiter, pos0); << 63 } << 64 } << 65 << 66 // ------------------------------------------- << 67 G4UIbatch::G4UIbatch(const char* fileName, G4U << 68 : G4UIsession(1), previousSession(prevSessio << 69 { << 70 macroStream.open(fileName, std::ios::in); << 71 if (macroStream.fail()) { << 72 G4cerr << "ERROR: Can not open a macro fil << 73 << ">. Set macro path with \"/contr << 74 lastRC = fParameterUnreadable; << 75 } 50 } 76 else { << 51 else 77 isOpened = true; << 52 { >> 53 commandFailed = false; 78 } 54 } 79 << 80 G4UImanager::GetUIpointer()->SetSession(this << 81 } 55 } 82 56 83 // ------------------------------------------- << 57 G4UIbatch::~G4UIbatch() 84 G4UIbatch::~G4UIbatch() << 85 { 58 { 86 if (isOpened) { << 59 if(!openFailed) macroFile.close(); 87 macroStream.close(); << 88 } << 89 } 60 } 90 61 91 // ------------------------------------------- << 62 G4UIsession * G4UIbatch::SessionStart() 92 G4String G4UIbatch::ReadCommand() << 93 { 63 { 94 enum << 64 if(!openFailed) 95 { 65 { 96 BUFSIZE = 4096 << 66 char commandLine[256]; 97 }; << 67 G4int lineLength = 255; 98 static G4ThreadLocal char* linebuf = nullptr << 99 if (linebuf == nullptr) { << 100 linebuf = new char[BUFSIZE]; << 101 } << 102 const char ctrM = 0x0d; << 103 << 104 G4String cmdtotal = ""; << 105 G4bool qcontinued = false; << 106 while (macroStream.good()) { << 107 macroStream.getline(linebuf, BUFSIZE); << 108 << 109 G4String cmdline(linebuf); << 110 << 111 // TAB-> ' ' conversion << 112 G4String::size_type nb = 0; << 113 while ((nb = cmdline.find('\t', nb)) != G4 << 114 cmdline.replace(nb, 1, " "); << 115 } << 116 << 117 // strip << 118 G4StrUtil::strip(cmdline); << 119 G4StrUtil::rstrip(cmdline, ctrM); << 120 << 121 // skip null line if single line << 122 if (!qcontinued && cmdline.empty()) { << 123 continue; << 124 } << 125 68 126 // '#' is treated as echoing something << 69 while(1) 127 if (cmdline[(std::size_t)0] == '#') { << 70 { 128 return cmdline; << 71 macroFile.getline( commandLine, lineLength ); 129 } << 72 if( macroFile.bad() ) 130 << 73 { 131 // tokenize... << 74 G4cout << "Cannot read " << macroFileName << "." << G4endl; 132 std::vector<G4String> tokens; << 75 commandFailed = true; 133 Tokenize(cmdline, tokens); << 134 qcontinued = false; << 135 for (G4int i = 0; i < G4int(tokens.size()) << 136 // string after '#" is ignored << 137 if (tokens[i][(std::size_t)0] == '#') { << 138 break; 76 break; 139 } 77 } 140 // '\' or '_' is treated as continued li << 78 if( macroFile.eof() ) break; 141 if (tokens[i] == "\\" || tokens[i] == "_ << 79 commandLine[lineLength] = '\0'; 142 qcontinued = true; << 80 G4String commandString = commandLine; 143 // check nothing after line continuati << 81 G4String nC= commandString.strip(G4String::both); 144 if (i != G4int(tokens.size()) - 1) { << 82 if( commandLine[0] == '#') 145 G4Exception("G4UIbatch::ReadCommand" << 83 { if(G4UImanager::GetUIpointer()->GetVerboseLevel()==2) 146 "unexpected character af << 84 { G4cout << commandLine << G4endl; } 147 } << 148 break; // stop parsing << 149 } 85 } 150 cmdtotal += tokens[i]; << 86 else if( nC.length() == 0 ) 151 cmdtotal += " "; << 87 { continue; } 152 } << 88 else if(nC == "exit") 153 << 89 { break; } 154 if (qcontinued) { << 90 else 155 continue; // read the next line << 91 { 156 } << 92 G4int rc = UImanager->ApplyCommand(commandLine); 157 << 93 if(rc) 158 if (!cmdtotal.empty()) { << 94 { 159 break; << 95 switch(rc) 160 } << 96 { 161 if (macroStream.eof()) { << 97 case fCommandNotFound: 162 break; << 98 G4cerr << "***** COMMAND NOT FOUND <" 163 } << 99 << commandLine << "> *****" << G4endl; 164 } << 100 break; 165 << 101 case fIllegalApplicationState: 166 // strip again << 102 G4cerr << "***** Illegal application state <" 167 G4StrUtil::strip(cmdtotal); << 103 << commandLine << "> *****" << G4endl; 168 << 104 break; 169 // finally, << 105 default: 170 if (macroStream.eof() && cmdtotal.empty()) { << 106 G4int pn = rc%100; 171 return "exit"; << 107 G4cerr << "***** Illegal parameter (" << pn << ") <" 172 } << 108 << commandLine << "> *****" << G4endl; 173 << 109 } 174 return cmdtotal; << 110 G4cerr << "***** Command ignored *****" << G4endl; 175 } << 111 commandFailed = true; 176 << 112 } 177 // ------------------------------------------- << 113 if(commandFailed) break; 178 G4int G4UIbatch::ExecCommand(const G4String& c << 179 { << 180 G4UImanager* UI = G4UImanager::GetUIpointer( << 181 G4int rc = UI->ApplyCommand(command); << 182 << 183 switch (rc) { << 184 case fCommandSucceeded: << 185 break; << 186 case fCommandNotFound: << 187 G4cerr << "***** COMMAND NOT FOUND <" << << 188 break; << 189 case fIllegalApplicationState: << 190 G4cerr << "***** Illegal application sta << 191 break; << 192 default: << 193 G4int pn = rc % 100; << 194 G4cerr << "***** Illegal parameter (" << << 195 } << 196 << 197 return rc; << 198 } << 199 << 200 // ------------------------------------------- << 201 G4UIsession* G4UIbatch::SessionStart() << 202 { << 203 if (!isOpened) { << 204 return previousSession; << 205 } << 206 << 207 while (true) { << 208 G4String newCommand = ReadCommand(); << 209 << 210 if (newCommand == "exit") { << 211 break; << 212 } << 213 << 214 // just echo something << 215 if (newCommand[(std::size_t)0] == '#') { << 216 if (G4UImanager::GetUIpointer()->GetVerb << 217 G4cout << newCommand << G4endl; << 218 } 114 } 219 continue; << 220 } << 221 << 222 // execute command << 223 G4int rc = ExecCommand(newCommand); << 224 if (rc != fCommandSucceeded) { << 225 G4cerr << G4endl << "***** Batch is inte << 226 lastRC = rc; << 227 break; << 228 } 115 } 229 } 116 } 230 << 231 return previousSession; 117 return previousSession; 232 } 118 } 233 119 234 // ------------------------------------------- << 120 void G4UIbatch::PauseSessionStart(G4String Prompt) 235 void G4UIbatch::PauseSessionStart(const G4Stri << 236 { 121 { 237 G4cout << "Pause session <" << Prompt << "> 122 G4cout << "Pause session <" << Prompt << "> start." << G4endl; 238 << 239 SessionStart(); 123 SessionStart(); 240 << 241 G4cout << "Pause session <" << Prompt << "> 124 G4cout << "Pause session <" << Prompt << "> Terminate." << G4endl; 242 } 125 } >> 126 >> 127 243 128