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