[821] | 1 | // |
---|
| 2 | // ******************************************************************** |
---|
| 3 | // * License and Disclaimer * |
---|
| 4 | // * * |
---|
| 5 | // * The Geant4 software is copyright of the Copyright Holders of * |
---|
| 6 | // * the Geant4 Collaboration. It is provided under the terms and * |
---|
| 7 | // * conditions of the Geant4 Software License, included in the file * |
---|
| 8 | // * LICENSE and available at http://cern.ch/geant4/license . These * |
---|
| 9 | // * include a list of copyright holders. * |
---|
| 10 | // * * |
---|
| 11 | // * Neither the authors of this software system, nor their employing * |
---|
| 12 | // * institutes,nor the agencies providing financial support for this * |
---|
| 13 | // * work make any representation or warranty, express or implied, * |
---|
| 14 | // * regarding this software system or assume any liability for its * |
---|
| 15 | // * use. Please see the license in the file LICENSE and URL above * |
---|
| 16 | // * for the full disclaimer and the limitation of liability. * |
---|
| 17 | // * * |
---|
| 18 | // * This code implementation is the result of the scientific and * |
---|
| 19 | // * technical work of the GEANT4 collaboration. * |
---|
| 20 | // * By using, copying, modifying or distributing the software (or * |
---|
| 21 | // * any work based on the software) you agree to acknowledge its * |
---|
| 22 | // * use in resulting scientific publications, and indicate your * |
---|
| 23 | // * acceptance of all terms of the Geant4 Software license. * |
---|
| 24 | // ******************************************************************** |
---|
| 25 | // |
---|
| 26 | // $Id: G4UIbatch.cc,v 1.16 2007/08/10 09:46:10 kmura Exp $ |
---|
| 27 | // GEANT4 tag $Name: $ |
---|
| 28 | // |
---|
| 29 | // ==================================================================== |
---|
| 30 | // G4UIbatch.cc |
---|
| 31 | // |
---|
| 32 | // ==================================================================== |
---|
| 33 | #include "G4UIbatch.hh" |
---|
| 34 | #include "G4UImanager.hh" |
---|
| 35 | #include <vector> |
---|
| 36 | |
---|
| 37 | //////////////////////////////////////////////////////////////////////// |
---|
| 38 | static void Tokenize(const G4String& str, std::vector<G4String>& tokens) |
---|
| 39 | //////////////////////////////////////////////////////////////////////// |
---|
| 40 | { |
---|
| 41 | const char* delimiter= " "; |
---|
| 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 != G4String::npos) { |
---|
| 47 | tokens.push_back(str.substr(pos0, pos-pos0)); |
---|
| 48 | |
---|
| 49 | pos0 = str.find_first_not_of(delimiter, pos); |
---|
| 50 | pos = str.find_first_of(delimiter, pos0); |
---|
| 51 | } |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | // ==================================================================== |
---|
| 55 | // |
---|
| 56 | // class description |
---|
| 57 | // |
---|
| 58 | // ==================================================================== |
---|
| 59 | |
---|
| 60 | //////////////////////////////////////////////////////////////////// |
---|
| 61 | G4UIbatch::G4UIbatch(const char* fileName, G4UIsession* prevSession) |
---|
| 62 | : previousSession(prevSession), isOpened(false) |
---|
| 63 | //////////////////////////////////////////////////////////////////// |
---|
| 64 | { |
---|
| 65 | macroStream.open(fileName, std::ios::in); |
---|
| 66 | if(macroStream.fail()) { |
---|
| 67 | G4cerr << "***** Can not open a macro file <" |
---|
| 68 | << fileName << ">" |
---|
| 69 | << G4endl; |
---|
| 70 | } else { |
---|
| 71 | isOpened= true; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | G4UImanager::GetUIpointer()-> SetSession(this); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | /////////////////////// |
---|
| 79 | G4UIbatch::~G4UIbatch() |
---|
| 80 | /////////////////////// |
---|
| 81 | { |
---|
| 82 | if(isOpened) macroStream.close(); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | ///////////////////////////////// |
---|
| 87 | G4String G4UIbatch::ReadCommand() |
---|
| 88 | ///////////////////////////////// |
---|
| 89 | { |
---|
| 90 | enum { BUFSIZE= 4096 }; |
---|
| 91 | static char linebuf[BUFSIZE]; |
---|
| 92 | |
---|
| 93 | G4String cmdtotal= ""; |
---|
| 94 | G4bool qcontinued= false; |
---|
| 95 | while(macroStream.good()) { |
---|
| 96 | macroStream.getline(linebuf, BUFSIZE); |
---|
| 97 | |
---|
| 98 | G4String cmdline(linebuf); |
---|
| 99 | |
---|
| 100 | // TAB-> ' ' conversion |
---|
| 101 | str_size nb=0; |
---|
| 102 | while ((nb= cmdline.find('\t',nb)) != G4String::npos) { |
---|
| 103 | cmdline.replace(nb, 1, " "); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | // strip |
---|
| 107 | cmdline= cmdline.strip(G4String::both); |
---|
| 108 | |
---|
| 109 | // skip null line if single line |
---|
| 110 | if(!qcontinued && cmdline.size()==0) continue; |
---|
| 111 | |
---|
| 112 | // tokenize... |
---|
| 113 | std::vector<G4String> tokens; |
---|
| 114 | Tokenize(cmdline, tokens); |
---|
| 115 | qcontinued= false; |
---|
| 116 | for (G4int i=0; i< G4int(tokens.size()); i++) { |
---|
| 117 | // '\' or '_' is treated as continued line. |
---|
| 118 | if(tokens[i] == '\\' || tokens[i] == '_' ) { |
---|
| 119 | qcontinued= true; |
---|
| 120 | // check nothing after line continuation character |
---|
| 121 | if( i != G4int(tokens.size())-1) { |
---|
| 122 | G4Exception("unexpected character after " |
---|
| 123 | "line continuation character"); |
---|
| 124 | } |
---|
| 125 | break; // stop parsing |
---|
| 126 | } |
---|
| 127 | cmdtotal+= tokens[i]; |
---|
| 128 | cmdtotal+= " "; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | if(qcontinued) continue; // read` the next line |
---|
| 132 | |
---|
| 133 | if(cmdtotal.size() != 0) break; |
---|
| 134 | if(macroStream.eof()) break; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | // strip again |
---|
| 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 | } |
---|
| 148 | |
---|
| 149 | // finally, |
---|
| 150 | if(macroStream.eof() && cmdtotal.size()==0) { |
---|
| 151 | return "exit"; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | return cmdtotal; |
---|
| 155 | |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | ///////////////////////////////////////////////////// |
---|
| 160 | G4int G4UIbatch::ExecCommand(const G4String& command) |
---|
| 161 | ///////////////////////////////////////////////////// |
---|
| 162 | { |
---|
| 163 | G4UImanager* UI= G4UImanager::GetUIpointer(); |
---|
| 164 | G4int rc= UI-> ApplyCommand(command); |
---|
| 165 | |
---|
| 166 | switch(rc) { |
---|
| 167 | case fCommandSucceeded: |
---|
| 168 | break; |
---|
| 169 | case fCommandNotFound: |
---|
| 170 | G4cerr << "***** COMMAND NOT FOUND <" |
---|
| 171 | << command << "> *****" << G4endl; |
---|
| 172 | break; |
---|
| 173 | case fIllegalApplicationState: |
---|
| 174 | G4cerr << "***** Illegal application state <" |
---|
| 175 | << command << "> *****" << G4endl; |
---|
| 176 | break; |
---|
| 177 | default: |
---|
| 178 | G4int pn= rc%100; |
---|
| 179 | G4cerr << "***** Illegal parameter (" << pn << ") <" |
---|
| 180 | << command << "> *****" << G4endl; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | return rc; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | |
---|
| 187 | /////////////////////////////////////// |
---|
| 188 | G4UIsession * G4UIbatch::SessionStart() |
---|
| 189 | /////////////////////////////////////// |
---|
| 190 | { |
---|
| 191 | if(!isOpened) return previousSession; |
---|
| 192 | |
---|
| 193 | while(1) { |
---|
| 194 | G4String newCommand = ReadCommand(); |
---|
| 195 | |
---|
| 196 | if(newCommand == "exit") { |
---|
| 197 | break; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | // just echo something |
---|
| 201 | if( newCommand[(size_t)0] == '#') { |
---|
| 202 | if(G4UImanager::GetUIpointer()-> GetVerboseLevel()==2) { |
---|
| 203 | G4cout << newCommand << G4endl; |
---|
| 204 | } |
---|
| 205 | continue; |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | // execute command |
---|
| 209 | G4int rc= ExecCommand(newCommand); |
---|
| 210 | if(rc != fCommandSucceeded) { |
---|
| 211 | G4cerr << G4endl << "***** Batch is interupted!! *****" << G4endl; |
---|
| 212 | break; |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | return previousSession; |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | |
---|
| 220 | ////////////////////////////////////////////////// |
---|
| 221 | void G4UIbatch::PauseSessionStart(G4String Prompt) |
---|
| 222 | ////////////////////////////////////////////////// |
---|
| 223 | { |
---|
| 224 | G4cout << "Pause session <" << Prompt << "> start." << G4endl; |
---|
| 225 | |
---|
| 226 | SessionStart(); |
---|
| 227 | |
---|
| 228 | G4cout << "Pause session <" << Prompt << "> Terminate." << G4endl; |
---|
| 229 | } |
---|
| 230 | |
---|