Geant4 Cross Reference |
>> 1 // This code implementation is the intellectual property of >> 2 // the GEANT4 collaboration. 1 // 3 // 2 // ******************************************* << 4 // By copying, distributing or modifying the Program (or any work 3 // * License and Disclaimer << 5 // based on the Program) you indicate your acceptance of this statement, 4 // * << 6 // and all its terms. 5 // * The Geant4 software is copyright of th << 6 // * the Geant4 Collaboration. It is provided << 7 // * conditions of the Geant4 Software License << 8 // * LICENSE and available at http://cern.ch/ << 9 // * include a list of copyright holders. << 10 // * << 11 // * Neither the authors of this software syst << 12 // * institutes,nor the agencies providing fin << 13 // * work make any representation or warran << 14 // * regarding this software system or assum << 15 // * use. Please see the license in the file << 16 // * for the full disclaimer and the limitatio << 17 // * << 18 // * This code implementation is the result << 19 // * technical work of the GEANT4 collaboratio << 20 // * By using, copying, modifying or distri << 21 // * any work based on the software) you ag << 22 // * use in resulting scientific publicati << 23 // * acceptance of all terms of the Geant4 Sof << 24 // ******************************************* << 25 // 7 // 26 // G4UIbatch << 8 // $Id: G4UIbatch.cc,v 1.2 1999/12/15 14:50:40 gunter Exp $ >> 9 // GEANT4 tag $Name: geant4-02-00 $ 27 // 10 // 28 // Author: M.Asai, 2000 << 29 // ------------------------------------------- << 30 11 31 #include "G4UIbatch.hh" 12 #include "G4UIbatch.hh" 32 << 33 #include "G4UImanager.hh" 13 #include "G4UImanager.hh" >> 14 #include "G4ios.hh" 34 15 35 #include <string> << 16 G4UIbatch::G4UIbatch(G4String fileName,G4UIsession* prevSession) 36 #include <vector> << 17 :macroFileName(fileName),previousSession(prevSession), 37 << 18 openFailed(false) 38 // ------------------------------------------- << 19 { 39 static void Tokenize(const G4String& str, std: << 20 const char* theFileName = fileName; 40 { << 21 UImanager = G4UImanager::GetUIpointer(); 41 const char* delimiter = " "; << 22 UImanager->SetSession(this); 42 << 23 macroFile.open((char*)theFileName); 43 G4String::size_type pos0 = str.find_first_no << 24 if(macroFile.fail()) 44 G4String::size_type pos = str.find_first_of( << 25 { 45 << 26 G4cerr << "macro file <" << fileName << "> could not open." 46 while (pos != G4String::npos || pos0 != G4St << 27 << G4endl; 47 if (str[(G4int)pos0] == '\"') { << 28 openFailed = true; 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 << 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 } << 76 else { << 77 isOpened = true; << 78 } 29 } 79 << 80 G4UImanager::GetUIpointer()->SetSession(this << 81 } 30 } 82 31 83 // ------------------------------------------- << 32 G4UIbatch::~G4UIbatch() 84 G4UIbatch::~G4UIbatch() << 85 { 33 { 86 if (isOpened) { << 34 if(!openFailed) macroFile.close(); 87 macroStream.close(); << 88 } << 89 } 35 } 90 36 91 // ------------------------------------------- << 37 G4UIsession * G4UIbatch::SessionStart() 92 G4String G4UIbatch::ReadCommand() << 93 { 38 { 94 enum << 39 if(!openFailed) 95 { 40 { 96 BUFSIZE = 4096 << 41 char commandLine[256]; 97 }; << 42 int 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 43 126 // '#' is treated as echoing something << 44 while(1) 127 if (cmdline[(std::size_t)0] == '#') { << 45 { 128 return cmdline; << 46 macroFile.getline( commandLine, lineLength ); 129 } << 47 if( macroFile.bad() ) 130 << 48 { 131 // tokenize... << 49 G4cout << "Cannot read " << macroFileName << "." << G4endl; 132 std::vector<G4String> tokens; << 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; 50 break; 139 } 51 } 140 // '\' or '_' is treated as continued li << 52 if( macroFile.eof() ) break; 141 if (tokens[i] == "\\" || tokens[i] == "_ << 53 commandLine[lineLength] = '\0'; 142 qcontinued = true; << 54 if( commandLine[0] != '#' ) 143 // check nothing after line continuati << 55 { UImanager->ApplyCommand(commandLine); } 144 if (i != G4int(tokens.size()) - 1) { << 56 else 145 G4Exception("G4UIbatch::ReadCommand" << 57 { G4cout << commandLine << G4endl; } 146 "unexpected character af << 147 } << 148 break; // stop parsing << 149 } << 150 cmdtotal += tokens[i]; << 151 cmdtotal += " "; << 152 } << 153 << 154 if (qcontinued) { << 155 continue; // read the next line << 156 } << 157 << 158 if (!cmdtotal.empty()) { << 159 break; << 160 } 58 } 161 if (macroStream.eof()) { << 162 break; << 163 } << 164 } << 165 << 166 // strip again << 167 G4StrUtil::strip(cmdtotal); << 168 << 169 // finally, << 170 if (macroStream.eof() && cmdtotal.empty()) { << 171 return "exit"; << 172 } << 173 << 174 return cmdtotal; << 175 } << 176 << 177 // ------------------------------------------- << 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 } 59 } 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 } << 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 } << 229 } << 230 << 231 return previousSession; 60 return previousSession; 232 } 61 } 233 62 234 // ------------------------------------------- << 63 void G4UIbatch::PauseSessionStart(G4String Prompt) 235 void G4UIbatch::PauseSessionStart(const G4Stri << 236 { 64 { 237 G4cout << "Pause session <" << Prompt << "> 65 G4cout << "Pause session <" << Prompt << "> start." << G4endl; 238 << 239 SessionStart(); 66 SessionStart(); 240 << 241 G4cout << "Pause session <" << Prompt << "> 67 G4cout << "Pause session <" << Prompt << "> Terminate." << G4endl; 242 } 68 } >> 69 >> 70 243 71