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