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 // G4UIcommand << 27 // 26 // 28 // Author: Makoto Asai (SLAC), 1998 << 27 // $Id: G4UIcommand.cc 89953 2015-05-06 08:09:12Z gcosmo $ 29 // ------------------------------------------- << 28 // >> 29 // 30 30 31 #include "G4UIcommand.hh" 31 #include "G4UIcommand.hh" 32 << 33 #include "G4StateManager.hh" << 34 #include "G4Threading.hh" << 35 #include "G4Tokenizer.hh" << 36 #include "G4UIcommandStatus.hh" << 37 #include "G4UImanager.hh" << 38 #include "G4UImessenger.hh" 32 #include "G4UImessenger.hh" 39 #include "G4UIparsing.hh" << 33 #include "G4UImanager.hh" >> 34 #include "G4UIcommandStatus.hh" >> 35 #include "G4StateManager.hh" 40 #include "G4UnitsTable.hh" 36 #include "G4UnitsTable.hh" >> 37 #include "G4Tokenizer.hh" 41 #include "G4ios.hh" 38 #include "G4ios.hh" >> 39 #include <sstream> 42 40 43 // ------------------------------------------- << 41 G4UIcommand::G4UIcommand() 44 G4UIcommand::G4UIcommand(const char* theComman << 42 : messenger(0), toBeBroadcasted(false), toBeFlushed(false), workerThreadOnly(false), 45 : toBeBroadcasted(tBB), messenger(theMesseng << 43 bp(0), token(IDENTIFIER), paramERR(0) 46 { 44 { 47 G4String comStr = theCommandPath; << 48 G4UIcommandCommonConstructorCode(comStr); << 49 availabelStateList = {G4State_PreInit, G4 << 50 G4State_GeomClosed, G4 << 51 } 45 } 52 46 53 // ------------------------------------------- << 47 G4UIcommand::G4UIcommand(const char * theCommandPath, 54 void G4UIcommand::G4UIcommandCommonConstructor << 48 G4UImessenger * theMessenger, G4bool tBB) >> 49 :messenger(theMessenger),toBeBroadcasted(tBB),toBeFlushed(false), workerThreadOnly(false), >> 50 token(IDENTIFIER),paramERR(0) 55 { 51 { >> 52 G4String comStr = theCommandPath; >> 53 if(!theMessenger) >> 54 { // this must be a directory >> 55 if(comStr(comStr.length()-1)!='/') >> 56 { >> 57 G4cerr << "G4UIcommand Warning : " << G4endl; >> 58 G4cerr << " <" << theCommandPath << "> must be a directory." << G4endl; >> 59 G4cerr << " '/' is appended." << G4endl; >> 60 comStr += "/"; >> 61 } >> 62 } >> 63 G4UIcommandCommonConstructorCode (comStr); >> 64 G4String nullString; >> 65 availabelStateList.clear(); >> 66 availabelStateList.push_back(G4State_PreInit); >> 67 availabelStateList.push_back(G4State_Init); >> 68 availabelStateList.push_back(G4State_Idle); >> 69 availabelStateList.push_back(G4State_GeomClosed); >> 70 availabelStateList.push_back(G4State_EventProc); >> 71 availabelStateList.push_back(G4State_Abort); >> 72 } >> 73 >> 74 #ifdef G4MULTITHREADED >> 75 #include "G4Threading.hh" >> 76 #endif >> 77 >> 78 void G4UIcommand::G4UIcommandCommonConstructorCode >> 79 (const char * theCommandPath) >> 80 { 56 commandPath = theCommandPath; 81 commandPath = theCommandPath; 57 commandName = theCommandPath; 82 commandName = theCommandPath; 58 auto commandNameIndex = (G4int)commandName.r << 83 G4int commandNameIndex = commandName.last('/'); 59 commandName.erase(0, commandNameIndex + 1); << 84 commandName.remove(0,commandNameIndex+1); 60 #ifdef G4MULTITHREADED 85 #ifdef G4MULTITHREADED 61 if ((messenger != nullptr) && messenger->Com << 86 if(messenger && messenger->CommandsShouldBeInMaster() 62 && G4Threading::IsWorkerThread()) << 87 && G4Threading::IsWorkerThread()) 63 { 88 { 64 toBeBroadcasted = false; 89 toBeBroadcasted = false; 65 G4UImanager::GetMasterUIpointer()->AddNewC 90 G4UImanager::GetMasterUIpointer()->AddNewCommand(this); 66 } 91 } 67 else { << 92 else 68 G4UImanager::GetUIpointer()->AddNewCommand << 93 { G4UImanager::GetUIpointer()->AddNewCommand(this); } 69 } << 70 #else 94 #else 71 G4UImanager::GetUIpointer()->AddNewCommand(t 95 G4UImanager::GetUIpointer()->AddNewCommand(this); 72 #endif 96 #endif 73 } 97 } 74 98 75 // ------------------------------------------- << 76 void G4UIcommand::SetCommandType(CommandType t << 77 { << 78 if (messenger == nullptr) { // this must be << 79 if (typ != CmdDirectory) { << 80 G4ExceptionDescription ed; << 81 ed << "A UI command <" << commandPath << << 82 G4Exception("G4UIcommand::SetCommandType << 83 } << 84 else if (commandPath.back() != '/') { << 85 G4ExceptionDescription ed; << 86 ed << "G4UIcommand Warning : \n" << 87 << " <" << commandPath << "> must be << 88 << " '/' is appended."; << 89 G4Exception("G4UIcommand::SetCommandType << 90 commandPath += "/"; << 91 } << 92 } << 93 commandType = typ; << 94 } << 95 << 96 // ------------------------------------------- << 97 G4UIcommand::~G4UIcommand() 99 G4UIcommand::~G4UIcommand() 98 { 100 { 99 G4UImanager* fUImanager = G4UImanager::GetUI 101 G4UImanager* fUImanager = G4UImanager::GetUIpointer(); 100 if (fUImanager != nullptr) { << 102 if(fUImanager) fUImanager->RemoveCommand(this); 101 fUImanager->RemoveCommand(this); << 103 102 } << 104 G4int n_parameterEntry = parameter.size(); 103 << 105 for( G4int i_thParameter=0; i_thParameter < n_parameterEntry; i_thParameter++ ) 104 for (const auto& p : parameter) { << 106 { delete parameter[i_thParameter]; } 105 delete p; << 107 parameter.clear(); 106 } << 107 } 108 } 108 109 109 // ------------------------------------------- << 110 G4int G4UIcommand::operator==(const G4UIcommand &right) const 110 G4bool G4UIcommand::operator==(const G4UIcomma << 111 { 111 { 112 return (commandPath == right.GetCommandPath( << 112 return ( commandPath == right.GetCommandPath() ); 113 } 113 } 114 114 115 // ------------------------------------------- << 115 G4int G4UIcommand::operator!=(const G4UIcommand &right) const 116 G4bool G4UIcommand::operator!=(const G4UIcomma << 117 { 116 { 118 return (commandPath != right.GetCommandPath( << 117 return ( commandPath != right.GetCommandPath() ); 119 } 118 } 120 119 121 // ------------------------------------------- << 120 #include "G4Threading.hh" 122 G4int G4UIcommand::DoIt(const G4String& parame << 121 >> 122 G4int G4UIcommand::DoIt(G4String parameterList) 123 { 123 { 124 G4String correctParameters; 124 G4String correctParameters; 125 std::size_t n_parameterEntry = parameter.siz << 125 G4int n_parameterEntry = parameter.size(); 126 if (n_parameterEntry != 0) { << 126 if( n_parameterEntry != 0 ) >> 127 { 127 G4String aToken; 128 G4String aToken; 128 G4String correctToken; 129 G4String correctToken; 129 G4Tokenizer parameterToken(parameterList); << 130 G4Tokenizer parameterToken( parameterList ); 130 for (std::size_t i_thParameter = 0; i_thPa << 131 for( G4int i_thParameter=0; i_thParameter<n_parameterEntry; i_thParameter++ ) 131 if (i_thParameter > 0) { << 132 { >> 133 if(i_thParameter > 0) >> 134 { 132 correctParameters.append(" "); 135 correctParameters.append(" "); 133 } 136 } 134 aToken = parameterToken(); 137 aToken = parameterToken(); 135 if (aToken.length() > 0 && aToken[0] == << 138 if( aToken.length()>0 && aToken(0)=='"' ) 136 while (aToken.back() != '"' || (aToken << 139 { >> 140 while( aToken(aToken.length()-1) != '"' >> 141 || ( aToken.length()==1 && aToken(0)=='"' )) >> 142 { 137 G4String additionalToken = parameter 143 G4String additionalToken = parameterToken(); 138 if (additionalToken.empty()) { << 144 if( additionalToken.isNull() ) 139 return G4int(fParameterUnreadable << 145 { return fParameterUnreadable+i_thParameter; } 140 } << 141 aToken += " "; 146 aToken += " "; 142 aToken += additionalToken; 147 aToken += additionalToken; 143 } 148 } 144 } 149 } 145 else if (i_thParameter == n_parameterEnt << 150 else if(i_thParameter==n_parameterEntry-1 && parameter[i_thParameter]->GetParameterType()=='s') 146 && parameter[i_thParameter]->Ge << 147 { 151 { 148 G4String anotherToken; 152 G4String anotherToken; 149 while (!((anotherToken = parameterToke << 153 while(!((anotherToken=parameterToken()).isNull())) 150 std::size_t idxs = anotherToken.find << 154 { 151 if (idxs == std::string::npos) { << 155 G4int idxs = anotherToken.index("#"); >> 156 if(idxs==G4int(std::string::npos)) >> 157 { 152 aToken += " "; 158 aToken += " "; 153 aToken += anotherToken; 159 aToken += anotherToken; 154 } 160 } 155 else if (idxs > 0) { << 161 else if(idxs>0) >> 162 { 156 aToken += " "; 163 aToken += " "; 157 aToken += anotherToken.substr(0, i << 164 aToken += anotherToken(0,idxs); 158 break; << 159 } << 160 else { << 161 break; 165 break; 162 } 166 } >> 167 else >> 168 { break; } 163 } 169 } 164 } 170 } 165 171 166 if (aToken.empty() || aToken == "!") { << 172 if( aToken.isNull() || aToken == "!" ) 167 if (parameter[i_thParameter]->IsOmitta << 173 { 168 if (parameter[i_thParameter]->GetCur << 174 if(parameter[i_thParameter]->IsOmittable()) >> 175 { >> 176 if(parameter[i_thParameter]->GetCurrentAsDefault()) >> 177 { 169 G4Tokenizer cvSt(messenger->GetCur 178 G4Tokenizer cvSt(messenger->GetCurrentValue(this)); 170 G4String parVal; 179 G4String parVal; 171 for (std::size_t ii = 0; ii < i_th << 180 for(G4int ii=0;ii<i_thParameter;ii++) 172 parVal = cvSt(); << 181 { 173 if (parVal[0] == '"') { << 182 parVal = cvSt(); 174 while (parVal.back() != '"') { << 183 if (parVal(0)=='"') 175 G4String additionalToken = c << 184 { 176 if (additionalToken.empty()) << 185 while( parVal(parVal.length()-1) != '"' ) 177 return G4int(fParameterUnr << 186 { 178 } << 187 G4String additionalToken = cvSt(); 179 parVal += " "; << 188 if( additionalToken.isNull() ) 180 parVal += additionalToken; << 189 { return fParameterUnreadable+i_thParameter; } 181 } << 190 parVal += " "; 182 } << 191 parVal += additionalToken; 183 } << 192 } 184 G4String aCVToken = cvSt(); << 193 } 185 if (aCVToken[0] == '"') { << 194 } 186 while (aCVToken.back() != '"') { << 195 G4String aCVToken = cvSt(); 187 G4String additionalToken = cvS << 196 if (aCVToken(0)=='"') 188 if (additionalToken.empty()) { << 197 { 189 return G4int(fParameterUnrea << 198 while( aCVToken(aCVToken.length()-1) != '"' ) 190 } << 199 { 191 aCVToken += " "; << 200 G4String additionalToken = cvSt(); 192 aCVToken += additionalToken; << 201 if( additionalToken.isNull() ) 193 } << 202 { return fParameterUnreadable+i_thParameter; } 194 } << 203 aCVToken += " "; >> 204 aCVToken += additionalToken; >> 205 } >> 206 // aCVToken.strip(G4String::both,'"'); >> 207 } 195 correctParameters.append(aCVToken) 208 correctParameters.append(aCVToken); 196 } 209 } 197 else { << 210 else 198 correctParameters.append(parameter << 211 { correctParameters.append(parameter[i_thParameter]->GetDefaultValue()); } 199 } << 200 } << 201 else { << 202 return G4int(fParameterUnreadable + << 203 } 212 } >> 213 else >> 214 { return fParameterUnreadable+i_thParameter; } 204 } 215 } 205 else { << 216 else 206 G4int stat = parameter[i_thParameter]- << 217 { 207 if (stat != 0) { << 218 G4int stat = parameter[i_thParameter]->CheckNewValue( aToken ); 208 return stat + G4int(i_thParameter); << 219 if(stat) return stat+i_thParameter; 209 } << 210 correctParameters.append(aToken); 220 correctParameters.append(aToken); 211 } 221 } 212 } 222 } 213 } 223 } 214 224 215 if (CheckNewValue(correctParameters) != 0) { << 225 if(CheckNewValue( correctParameters )) 216 return fParameterOutOfRange + 99; << 226 { return fParameterOutOfRange+99; } 217 } << 218 227 219 if (workerThreadOnly && G4Threading::IsMaste << 228 if(workerThreadOnly && G4Threading::IsMasterThread()) return 0; 220 return 0; << 221 } << 222 229 223 messenger->SetNewValue(this, std::move(corre << 230 messenger->SetNewValue( this, correctParameters ); 224 return 0; 231 return 0; 225 } 232 } 226 233 227 // ------------------------------------------- << 228 G4String G4UIcommand::GetCurrentValue() 234 G4String G4UIcommand::GetCurrentValue() 229 { 235 { 230 return messenger->GetCurrentValue(this); 236 return messenger->GetCurrentValue(this); 231 } 237 } 232 238 233 // ------------------------------------------- << 234 void G4UIcommand::AvailableForStates(G4Applica 239 void G4UIcommand::AvailableForStates(G4ApplicationState s1) 235 { 240 { 236 availabelStateList = {s1}; << 241 availabelStateList.clear(); >> 242 availabelStateList.push_back(s1); 237 } 243 } 238 244 239 // ------------------------------------------- << 245 void G4UIcommand::AvailableForStates(G4ApplicationState s1, 240 void G4UIcommand::AvailableForStates(G4Applica << 246 G4ApplicationState s2) 241 { 247 { 242 availabelStateList = {s1, s2}; << 248 availabelStateList.clear(); >> 249 availabelStateList.push_back(s1); >> 250 availabelStateList.push_back(s2); 243 } 251 } 244 252 245 // ------------------------------------------- << 253 void G4UIcommand::AvailableForStates(G4ApplicationState s1, 246 void G4UIcommand::AvailableForStates(G4Applica << 254 G4ApplicationState s2, 247 G4Applica 255 G4ApplicationState s3) 248 { 256 { 249 availabelStateList = {s1, s2, s3}; << 257 availabelStateList.clear(); 250 } << 258 availabelStateList.push_back(s1); 251 << 259 availabelStateList.push_back(s2); 252 // ------------------------------------------- << 260 availabelStateList.push_back(s3); 253 void G4UIcommand::AvailableForStates(G4Applica << 261 } 254 G4Applica << 262 255 { << 263 void G4UIcommand::AvailableForStates(G4ApplicationState s1, 256 availabelStateList = {s1, s2, s3, s4}; << 264 G4ApplicationState s2, 257 } << 265 G4ApplicationState s3, 258 << 266 G4ApplicationState s4) 259 // ------------------------------------------- << 267 { 260 void G4UIcommand::AvailableForStates(G4Applica << 268 availabelStateList.clear(); 261 G4Applica << 269 availabelStateList.push_back(s1); >> 270 availabelStateList.push_back(s2); >> 271 availabelStateList.push_back(s3); >> 272 availabelStateList.push_back(s4); >> 273 } >> 274 >> 275 void G4UIcommand::AvailableForStates(G4ApplicationState s1, >> 276 G4ApplicationState s2, >> 277 G4ApplicationState s3, >> 278 G4ApplicationState s4, 262 G4Applica 279 G4ApplicationState s5) 263 { 280 { 264 availabelStateList = {s1, s2, s3, s4, s5}; << 281 availabelStateList.clear(); >> 282 availabelStateList.push_back(s1); >> 283 availabelStateList.push_back(s2); >> 284 availabelStateList.push_back(s3); >> 285 availabelStateList.push_back(s4); >> 286 availabelStateList.push_back(s5); 265 } 287 } 266 288 267 // ------------------------------------------- << 268 G4bool G4UIcommand::IsAvailable() 289 G4bool G4UIcommand::IsAvailable() 269 { 290 { 270 G4ApplicationState currentState = G4StateMan << 291 G4bool av = false; 271 << 292 G4ApplicationState currentState 272 for (const auto& s : availabelStateList) { << 293 = G4StateManager::GetStateManager()->GetCurrentState(); 273 if (s == currentState) { << 294 274 return true; << 295 G4int nState = availabelStateList.size(); 275 } << 296 for(G4int i=0;i<nState;i++) >> 297 { >> 298 if(availabelStateList[i]==currentState) >> 299 { >> 300 av = true; >> 301 break; >> 302 } 276 } 303 } 277 304 278 return false; << 305 return av; 279 } 306 } 280 307 281 // ------------------------------------------- << 282 G4double G4UIcommand::ValueOf(const char* unit 308 G4double G4UIcommand::ValueOf(const char* unitName) 283 { 309 { 284 return G4UnitDefinition::GetValueOf(unitName << 310 G4double value = 0.; >> 311 value = G4UnitDefinition::GetValueOf(unitName); >> 312 return value; 285 } 313 } 286 314 287 // ------------------------------------------- << 288 G4String G4UIcommand::CategoryOf(const char* u 315 G4String G4UIcommand::CategoryOf(const char* unitName) 289 { 316 { 290 return G4UnitDefinition::GetCategory(unitNam << 317 return G4UnitDefinition::GetCategory(unitName); 291 } 318 } 292 319 293 // ------------------------------------------- << 294 G4String G4UIcommand::UnitsList(const char* un 320 G4String G4UIcommand::UnitsList(const char* unitCategory) 295 { 321 { >> 322 G4String retStr; 296 G4UnitsTable& UTbl = G4UnitDefinition::GetUn 323 G4UnitsTable& UTbl = G4UnitDefinition::GetUnitsTable(); 297 << 324 size_t i; 298 auto ucatIter = std::find_if(std::cbegin(UTb << 325 for(i=0;i<UTbl.size();i++) 299 return ud->GetName() == unitCategory; << 326 { if(UTbl[i]->GetName()==unitCategory) break; } 300 }); << 327 if(i==UTbl.size()) 301 << 328 { 302 if (ucatIter == std::cend(UTbl)) { << 303 G4cerr << "Unit category <" << unitCategor 329 G4cerr << "Unit category <" << unitCategory << "> is not defined." << G4endl; 304 return G4String(); << 330 return retStr; 305 } 331 } 306 << 332 G4UnitsContainer& UCnt = UTbl[i]->GetUnitsList(); 307 G4String symList; << 333 retStr = UCnt[0]->GetSymbol(); 308 G4String nameList; << 334 G4int je = UCnt.size(); 309 G4UnitsContainer& UCnt = (*ucatIter)->GetUni << 335 for(G4int j=1;j<je;j++) 310 << 336 { 311 for (const auto& uDef : UCnt) { << 337 retStr += " "; 312 symList += uDef->GetSymbol(); << 338 retStr += UCnt[j]->GetSymbol(); 313 symList += " "; << 339 } 314 nameList += uDef->GetName(); << 340 for(G4int k=0;k<je;k++) 315 nameList += " "; << 341 { >> 342 retStr += " "; >> 343 retStr += UCnt[k]->GetName(); 316 } 344 } 317 symList += nameList; << 345 return retStr; 318 G4StrUtil::rstrip(symList); << 319 return symList; << 320 } 346 } 321 << 347 322 // ------------------------------------------- << 323 void G4UIcommand::List() 348 void G4UIcommand::List() 324 { 349 { 325 G4cout << G4endl; 350 G4cout << G4endl; 326 G4cout << G4endl; 351 G4cout << G4endl; 327 if (commandPath.back() != '/') { << 352 if(commandPath(commandPath.length()-1)!='/') 328 G4cout << "Command " << commandPath << G4e << 353 { G4cout << "Command " << commandPath << G4endl; } 329 } << 354 if(workerThreadOnly) 330 if (workerThreadOnly) { << 355 { G4cout << " ---- available only in worker thread" << G4endl; } 331 G4cout << " ---- available only in work << 332 } << 333 << 334 G4cout << "Guidance :" << G4endl; 356 G4cout << "Guidance :" << G4endl; 335 for (const auto& i_thGuidance : commandGuida << 357 G4int n_guidanceEntry = commandGuidance.size(); 336 G4cout << i_thGuidance << G4endl; << 358 for( G4int i_thGuidance=0; i_thGuidance < n_guidanceEntry; i_thGuidance++ ) 337 } << 359 { G4cout << commandGuidance[i_thGuidance] << G4endl; } 338 << 360 if( ! rangeString.isNull() ) 339 if (!rangeExpression.empty()) { << 361 { G4cout << " Range of parameters : " << rangeString << G4endl; } 340 G4cout << " Range of parameters : " << ran << 362 G4int n_parameterEntry = parameter.size(); 341 } << 363 if( n_parameterEntry > 0 ) 342 << 364 { 343 for (const auto& i_thParameter : parameter) << 365 for( G4int i_thParameter=0; i_thParameter<n_parameterEntry; i_thParameter++ ) 344 i_thParameter->List(); << 366 { parameter[i_thParameter]->List(); } 345 } 367 } 346 G4cout << G4endl; 368 G4cout << G4endl; 347 } 369 } 348 370 349 // ------------------------------------------- << 350 G4String G4UIcommand::ConvertToString(G4bool b 371 G4String G4UIcommand::ConvertToString(G4bool boolVal) 351 { 372 { 352 return boolVal ? "1" : "0"; << 373 G4String vl = "0"; >> 374 if(boolVal) vl = "1"; >> 375 return vl; 353 } 376 } 354 377 355 // ------------------------------------------- << 356 G4String G4UIcommand::ConvertToString(G4int in 378 G4String G4UIcommand::ConvertToString(G4int intValue) 357 { 379 { 358 return G4UIparsing::TtoS(intValue); << 380 std::ostringstream os; 359 } << 381 os << intValue; 360 << 382 G4String vl = os.str(); 361 // ------------------------------------------- << 383 return vl; 362 G4String G4UIcommand::ConvertToString(G4long l << 363 { << 364 return G4UIparsing::TtoS(longValue); << 365 } 384 } 366 385 367 // ------------------------------------------- << 368 G4String G4UIcommand::ConvertToString(G4double 386 G4String G4UIcommand::ConvertToString(G4double doubleValue) 369 { 387 { 370 std::ostringstream os; 388 std::ostringstream os; 371 if (G4UImanager::DoublePrecisionStr()) { << 372 os << std::setprecision(17); << 373 } << 374 os << doubleValue; 389 os << doubleValue; 375 return os.str(); << 390 G4String vl = os.str(); >> 391 return vl; 376 } 392 } 377 393 378 // ------------------------------------------- << 394 G4String G4UIcommand::ConvertToString(G4double doubleValue,const char* unitName) 379 G4String G4UIcommand::ConvertToString(G4double << 380 { 395 { >> 396 G4String unt = unitName; >> 397 G4double uv = ValueOf(unitName); >> 398 381 std::ostringstream os; 399 std::ostringstream os; 382 if (G4UImanager::DoublePrecisionStr()) { << 400 os << doubleValue/uv << " " << unitName; 383 os << std::setprecision(17); << 401 G4String vl = os.str(); 384 } << 402 return vl; 385 os << doubleValue / ValueOf(unitName) << " " << 386 return os.str(); << 387 } 403 } 388 404 389 // ------------------------------------------- << 405 G4String G4UIcommand::ConvertToString(G4ThreeVector vec) 390 G4String G4UIcommand::ConvertToString(const G4 << 391 { 406 { 392 std::ostringstream os; 407 std::ostringstream os; 393 if (G4UImanager::DoublePrecisionStr()) { << 394 os << std::setprecision(17); << 395 } << 396 os << vec.x() << " " << vec.y() << " " << ve 408 os << vec.x() << " " << vec.y() << " " << vec.z(); 397 return os.str(); << 409 G4String vl = os.str(); >> 410 return vl; 398 } 411 } 399 412 400 // ------------------------------------------- << 413 G4String G4UIcommand::ConvertToString(G4ThreeVector vec,const char* unitName) 401 G4String G4UIcommand::ConvertToString(const G4 << 402 { 414 { >> 415 G4String unt = unitName; 403 G4double uv = ValueOf(unitName); 416 G4double uv = ValueOf(unitName); 404 417 405 std::ostringstream os; 418 std::ostringstream os; 406 if (G4UImanager::DoublePrecisionStr()) { << 419 os << vec.x()/uv << " " << vec.y()/uv << " " << vec.z()/uv 407 os << std::setprecision(17); << 420 << " " << unitName; 408 } << 421 G4String vl = os.str(); 409 os << vec.x() / uv << " " << vec.y() / uv << << 422 return vl; 410 return os.str(); << 411 } 423 } 412 424 413 // ------------------------------------------- << 414 G4bool G4UIcommand::ConvertToBool(const char* 425 G4bool G4UIcommand::ConvertToBool(const char* st) 415 { 426 { 416 G4String v = G4StrUtil::to_upper_copy(st); << 427 G4String v = st; 417 return (v == "Y" || v == "YES" || v == "1" | << 428 v.toUpper(); >> 429 G4bool vl = false; >> 430 if( v=="Y" || v=="YES" || v=="1" || v=="T" || v=="TRUE" ) >> 431 { vl = true; } >> 432 return vl; 418 } 433 } 419 434 420 // ------------------------------------------- << 421 G4int G4UIcommand::ConvertToInt(const char* st 435 G4int G4UIcommand::ConvertToInt(const char* st) 422 { 436 { 423 return G4UIparsing::StoT<G4int>(st); << 437 G4int vl; 424 } << 438 std::istringstream is(st); 425 << 439 is >> vl; 426 // ------------------------------------------- << 440 return vl; 427 G4long G4UIcommand::ConvertToLongInt(const cha << 428 { << 429 return G4UIparsing::StoT<G4long>(st); << 430 } 441 } 431 442 432 // ------------------------------------------- << 433 G4double G4UIcommand::ConvertToDouble(const ch 443 G4double G4UIcommand::ConvertToDouble(const char* st) 434 { 444 { 435 return G4UIparsing::StoT<G4double>(st); << 445 G4double vl; >> 446 std::istringstream is(st); >> 447 is >> vl; >> 448 return vl; 436 } 449 } 437 450 438 // ------------------------------------------- << 439 G4double G4UIcommand::ConvertToDimensionedDoub 451 G4double G4UIcommand::ConvertToDimensionedDouble(const char* st) 440 { 452 { 441 G4double vl; 453 G4double vl; 442 char unts[30]; 454 char unts[30]; 443 455 444 std::istringstream is(st); 456 std::istringstream is(st); 445 is >> vl >> unts; 457 is >> vl >> unts; 446 G4String unt = unts; 458 G4String unt = unts; 447 459 448 return (vl * ValueOf(unt)); << 460 return (vl*ValueOf(unt)); 449 } 461 } 450 462 451 // ------------------------------------------- << 452 G4ThreeVector G4UIcommand::ConvertTo3Vector(co 463 G4ThreeVector G4UIcommand::ConvertTo3Vector(const char* st) 453 { 464 { 454 G4double vx; 465 G4double vx; 455 G4double vy; 466 G4double vy; 456 G4double vz; 467 G4double vz; 457 std::istringstream is(st); 468 std::istringstream is(st); 458 is >> vx >> vy >> vz; 469 is >> vx >> vy >> vz; 459 return G4ThreeVector(vx, vy, vz); << 470 return G4ThreeVector(vx,vy,vz); 460 } 471 } 461 472 462 // ------------------------------------------- << 463 G4ThreeVector G4UIcommand::ConvertToDimensione 473 G4ThreeVector G4UIcommand::ConvertToDimensioned3Vector(const char* st) 464 { 474 { 465 G4double vx; 475 G4double vx; 466 G4double vy; 476 G4double vy; 467 G4double vz; 477 G4double vz; 468 char unts[30]; 478 char unts[30]; 469 std::istringstream is(st); 479 std::istringstream is(st); 470 is >> vx >> vy >> vz >> unts; 480 is >> vx >> vy >> vz >> unts; 471 G4String unt = unts; 481 G4String unt = unts; 472 G4double uv = ValueOf(unt); 482 G4double uv = ValueOf(unt); 473 return G4ThreeVector(vx * uv, vy * uv, vz * << 483 return G4ThreeVector(vx*uv,vy*uv,vz*uv); 474 } 484 } 475 485 476 G4int G4UIcommand::CheckNewValue(const char* n << 486 >> 487 // ----- the following is used by CheckNewValue() ------------ >> 488 >> 489 >> 490 #include <ctype.h> // isalpha(), toupper() >> 491 >> 492 //#include "checkNewValue_debug.icc" >> 493 //#define DEBUG 1 >> 494 >> 495 G4int G4UIcommand:: >> 496 CheckNewValue(const char * newValue) >> 497 { >> 498 yystype result; >> 499 // if( TypeCheck(newValue) == 0 ) return 1; >> 500 if( ! rangeString.isNull() ) >> 501 { if( RangeCheck(newValue) == 0 ) return fParameterOutOfRange; } >> 502 return 0; // succeeded >> 503 } >> 504 >> 505 // ------------------ type check routines ------------------- >> 506 >> 507 G4int G4UIcommand:: >> 508 TypeCheck(const char * t) >> 509 { >> 510 G4String aNewValue; >> 511 char type; >> 512 std::istringstream is(t); >> 513 for (unsigned i=0; i< parameter.size(); i++) { >> 514 is >> aNewValue; >> 515 type = toupper(parameter[i]->GetParameterType()); >> 516 switch ( type ) { >> 517 case 'D': >> 518 if( IsDouble(aNewValue)==0 ){ >> 519 G4cerr << aNewValue << ": double value expected." >> 520 << G4endl; >> 521 return 0; >> 522 } break; >> 523 case 'I': >> 524 if( IsInt(aNewValue,20)==0 ){ >> 525 G4cerr <<aNewValue<<": integer expected." >> 526 <<G4endl; >> 527 return 0; >> 528 } break; >> 529 case 'S': >> 530 break; >> 531 case 'B': >> 532 aNewValue.toUpper(); >> 533 if (aNewValue == "Y" || aNewValue == "N" >> 534 ||aNewValue == "YES" || aNewValue == "NO" >> 535 ||aNewValue == "1" || aNewValue == "0" >> 536 ||aNewValue == "T" || aNewValue == "F" >> 537 ||aNewValue == "TRUE" || aNewValue == "FALSE") >> 538 return 1; >> 539 else return 0; >> 540 break; >> 541 default: ; >> 542 } >> 543 } >> 544 return 1; >> 545 } >> 546 >> 547 >> 548 G4int G4UIcommand:: >> 549 IsInt(const char* buf, short maxDigits) 477 { 550 { 478 if (!G4UIparsing::RangeCheck(*this, newValue << 551 const char* p= buf; 479 return fParameterOutOfRange; << 552 G4int length=0; 480 } << 553 if( *p == '+' || *p == '-') { ++p; } 481 return 0; // succeeded << 554 if( isdigit( (G4int)(*p) )) { >> 555 while( isdigit( (G4int)(*p) )) { ++p; ++length; } >> 556 if( *p == '\0' ) { >> 557 if( length > maxDigits) { >> 558 G4cerr <<"digit length exceeds"<<G4endl; >> 559 return 0; >> 560 } >> 561 return 1; >> 562 } else { >> 563 // G4cerr <<"illegal character after int:"<<buf<<G4endl; >> 564 } >> 565 } else { >> 566 // G4cerr <<"illegal int:"<<buf<<G4endl; >> 567 } >> 568 return 0; >> 569 } >> 570 >> 571 >> 572 G4int G4UIcommand:: >> 573 ExpectExponent(const char* str) // used only by IsDouble() >> 574 { >> 575 G4int maxExplength; >> 576 if( IsInt( str, maxExplength=7 )) return 1; >> 577 else return 0; >> 578 } >> 579 >> 580 >> 581 G4int G4UIcommand:: >> 582 IsDouble(const char* buf) >> 583 { >> 584 const char* p= buf; >> 585 switch( *p) { >> 586 case '+': case '-': ++p; >> 587 if( isdigit(*p) ) { >> 588 while( isdigit( (G4int)(*p) )) { ++p; } >> 589 switch ( *p ) { >> 590 case '\0': return 1; >> 591 // break; >> 592 case 'E': case 'e': >> 593 return ExpectExponent(++p ); >> 594 // break; >> 595 case '.': ++p; >> 596 if( *p == '\0' ) return 1; >> 597 if( *p == 'e' || *p =='E' ) return ExpectExponent(++p ); >> 598 if( isdigit(*p) ) { >> 599 while( isdigit( (G4int)(*p) )) { ++p; } >> 600 if( *p == '\0' ) return 1; >> 601 if( *p == 'e' || *p =='E') return ExpectExponent(++p); >> 602 } else return 0; break; >> 603 default: return 0; >> 604 } >> 605 } >> 606 if( *p == '.' ) { ++p; >> 607 if( isdigit(*p) ) { >> 608 while( isdigit( (G4int)(*p) )) { ++p; } >> 609 if( *p == '\0' ) return 1; >> 610 if( *p == 'e' || *p =='E') return ExpectExponent(++p); >> 611 } >> 612 } >> 613 break; >> 614 case '.': ++p; >> 615 if( isdigit(*p) ) { >> 616 while( isdigit( (G4int)(*p) )) { ++p; } >> 617 if( *p == '\0' ) return 1; >> 618 if( *p == 'e' || *p =='E' ) return ExpectExponent(++p); >> 619 } break; >> 620 default: // digit is expected >> 621 if( isdigit(*p) ) { >> 622 while( isdigit( (G4int)(*p) )) { ++p; } >> 623 if( *p == '\0' ) return 1; >> 624 if( *p == 'e' || *p =='E') return ExpectExponent(++p); >> 625 if( *p == '.' ) { ++p; >> 626 if( *p == '\0' ) return 1; >> 627 if( *p == 'e' || *p =='E') return ExpectExponent(++p); >> 628 if( isdigit(*p) ) { >> 629 while( isdigit( (G4int)(*p) )) { ++p; } >> 630 if( *p == '\0' ) return 1; >> 631 if( *p == 'e' || *p =='E') return ExpectExponent(++p); >> 632 } >> 633 } >> 634 } >> 635 } >> 636 return 0; >> 637 } >> 638 >> 639 >> 640 // ------------------ range Check routines ------------------- >> 641 G4int G4UIcommand:: >> 642 RangeCheck(const char* t) { >> 643 yystype result; >> 644 char type; >> 645 bp = 0; // reset buffer pointer for G4UIpGetc() >> 646 std::istringstream is(t); >> 647 for (unsigned i=0; i< parameter.size(); i++) { >> 648 type= toupper(parameter[i]->GetParameterType()); >> 649 switch ( type ) { >> 650 case 'D': is >> newVal[i].D; break; >> 651 case 'I': is >> newVal[i].I; break; >> 652 case 'S': >> 653 case 'B': >> 654 default: ; >> 655 } >> 656 } >> 657 // PrintToken(); // Print tokens (consumes all tokens) >> 658 token= Yylex(); >> 659 result = Expression(); >> 660 >> 661 if( paramERR == 1 ) return 0; >> 662 if( result.type != CONSTINT) { >> 663 G4cerr << "Illegal Expression in parameter range." << G4endl; >> 664 return 0; >> 665 } >> 666 if ( result.I ) return 1; >> 667 G4cerr << "parameter out of range: "<< rangeString << G4endl; >> 668 return 0; >> 669 } >> 670 >> 671 // ------------------ syntax node functions ------------------ >> 672 yystype G4UIcommand:: >> 673 Expression(void) >> 674 { >> 675 yystype result; >> 676 #ifdef DEBUG >> 677 G4cerr << " Expression()" << G4endl; >> 678 #endif >> 679 result = LogicalORExpression(); >> 680 return result; >> 681 } >> 682 >> 683 yystype G4UIcommand:: >> 684 LogicalORExpression(void) >> 685 { >> 686 yystype result; >> 687 yystype p; >> 688 p = LogicalANDExpression(); >> 689 if( token != LOGICALOR) return p; >> 690 if( p.type == CONSTSTRING || p.type == IDENTIFIER ) { >> 691 G4cerr << "Parameter range: illegal type at '||'" << G4endl; >> 692 paramERR = 1; >> 693 } >> 694 result.I = p.I; >> 695 while (token == LOGICALOR) >> 696 { >> 697 token = Yylex(); >> 698 p = LogicalANDExpression(); >> 699 if( p.type == CONSTSTRING || p.type == IDENTIFIER ) { >> 700 G4cerr << "Parameter range: illegal type at '||'" <<G4endl; >> 701 paramERR = 1; >> 702 } >> 703 switch (p.type) { >> 704 case CONSTINT: >> 705 result.I += p.I; >> 706 result.type = CONSTINT; break; >> 707 case CONSTDOUBLE: >> 708 result.I += (p.D != 0.0); >> 709 result.type = CONSTINT; break; >> 710 default: >> 711 G4cerr << "Parameter range: unknown type"<<G4endl; >> 712 paramERR = 1; >> 713 } >> 714 } >> 715 return result; >> 716 } >> 717 >> 718 yystype G4UIcommand:: >> 719 LogicalANDExpression(void) >> 720 { >> 721 yystype result; >> 722 yystype p; >> 723 p = EqualityExpression(); >> 724 if( token != LOGICALAND) return p; >> 725 if( p.type == CONSTSTRING || p.type == IDENTIFIER ) { >> 726 G4cerr << "Parameter range: illegal type at '&&'" << G4endl; >> 727 paramERR = 1; >> 728 } >> 729 result.I = p.I; >> 730 while (token == LOGICALAND) >> 731 { >> 732 token = Yylex(); >> 733 p = EqualityExpression(); >> 734 if( p.type == CONSTSTRING || p.type == IDENTIFIER ) { >> 735 G4cerr << "Parameter range: illegal type at '&&'" << G4endl; >> 736 paramERR = 1; >> 737 } >> 738 switch (p.type) { >> 739 case CONSTINT: >> 740 result.I *= p.I; >> 741 result.type = CONSTINT; break; >> 742 case CONSTDOUBLE: >> 743 result.I *= (p.D != 0.0); >> 744 result.type = CONSTINT; break; >> 745 default: >> 746 G4cerr << "Parameter range: unknown type."<< G4endl; >> 747 paramERR = 1; >> 748 } >> 749 } >> 750 return result; >> 751 } >> 752 >> 753 >> 754 yystype G4UIcommand:: >> 755 EqualityExpression(void) >> 756 { >> 757 yystype arg1, arg2; >> 758 G4int operat; >> 759 yystype result; >> 760 #ifdef DEBUG >> 761 G4cerr << " EqualityExpression()" <<G4endl; >> 762 #endif >> 763 result = RelationalExpression(); >> 764 if( token==EQ || token==NE ) { >> 765 operat = token; >> 766 token = Yylex(); >> 767 arg1 = result; >> 768 arg2 = RelationalExpression(); >> 769 result.I = Eval2( arg1, operat, arg2 ); // semantic action >> 770 result.type = CONSTINT; >> 771 #ifdef DEBUG >> 772 G4cerr << " return code of Eval2(): " << result.I <<G4endl; >> 773 #endif >> 774 } else { >> 775 if (result.type != CONSTINT && result.type != CONSTDOUBLE) { >> 776 G4cerr << "Parameter range: error at EqualityExpression" >> 777 << G4endl; >> 778 paramERR = 1; >> 779 } >> 780 } >> 781 return result; >> 782 } >> 783 >> 784 >> 785 yystype G4UIcommand:: >> 786 RelationalExpression(void) >> 787 { >> 788 yystype arg1, arg2; >> 789 G4int operat; >> 790 yystype result; >> 791 #ifdef DEBUG >> 792 G4cerr << " RelationalExpression()" <<G4endl; >> 793 #endif >> 794 >> 795 arg1 = AdditiveExpression(); >> 796 if( token==GT || token==GE || token==LT || token==LE ) { >> 797 operat = token; >> 798 token = Yylex(); >> 799 arg2 = AdditiveExpression(); >> 800 result.I = Eval2( arg1, operat, arg2 ); // semantic action >> 801 result.type = CONSTINT; >> 802 #ifdef DEBUG >> 803 G4cerr << " return code of Eval2(): " << result.I << G4endl; >> 804 #endif >> 805 } else { >> 806 result = arg1; >> 807 } >> 808 #ifdef DEBUG >> 809 G4cerr <<" return RelationalExpression()"<< G4endl; >> 810 #endif >> 811 return result; >> 812 } >> 813 >> 814 yystype G4UIcommand:: >> 815 AdditiveExpression(void) >> 816 { yystype result; >> 817 result = MultiplicativeExpression(); >> 818 if( token != '+' && token != '-' ) return result; >> 819 G4cerr << "Parameter range: operator " >> 820 << (char)token >> 821 << " is not supported." << G4endl; >> 822 paramERR = 1; >> 823 return result; >> 824 } >> 825 >> 826 yystype G4UIcommand:: >> 827 MultiplicativeExpression(void) >> 828 { yystype result; >> 829 result = UnaryExpression(); >> 830 if( token != '*' && token != '/' && token != '%' ) return result; >> 831 G4cerr << "Parameter range: operator " >> 832 << (char)token >> 833 << " is not supported." << G4endl; >> 834 paramERR = 1; >> 835 return result; >> 836 } >> 837 >> 838 yystype G4UIcommand:: >> 839 UnaryExpression(void) >> 840 { >> 841 yystype result; >> 842 yystype p; >> 843 #ifdef DEBUG >> 844 G4cerr <<" UnaryExpression"<< G4endl; >> 845 #endif >> 846 switch(token) { >> 847 case '-': >> 848 token = Yylex(); >> 849 p = UnaryExpression(); >> 850 if (p.type == CONSTINT) { >> 851 result.I = - p.I; >> 852 result.type = CONSTINT; >> 853 } >> 854 if (p.type == CONSTDOUBLE) { >> 855 result.D = - p.D; >> 856 result.type = CONSTDOUBLE; >> 857 } break; >> 858 case '+': >> 859 token = Yylex(); >> 860 result = UnaryExpression(); break; >> 861 case '!': >> 862 token = Yylex(); >> 863 G4cerr << "Parameter range error: " >> 864 << "operator '!' is not supported (sorry)." >> 865 << G4endl; >> 866 paramERR = 1; >> 867 result = UnaryExpression(); break; >> 868 default: >> 869 result = PrimaryExpression(); >> 870 } >> 871 return result; >> 872 } >> 873 >> 874 >> 875 yystype G4UIcommand:: >> 876 PrimaryExpression(void) >> 877 { >> 878 yystype result; >> 879 #ifdef DEBUG >> 880 G4cerr <<" primary_exp"<<G4endl; >> 881 #endif >> 882 switch (token) { >> 883 case IDENTIFIER: >> 884 result.S = yylval.S; >> 885 result.type = token; >> 886 token = Yylex(); break; >> 887 case CONSTINT: >> 888 result.I = yylval.I; >> 889 result.type = token; >> 890 token= Yylex(); break; >> 891 case CONSTDOUBLE: >> 892 result.D = yylval.D; >> 893 result.type = token; >> 894 token = Yylex(); break; >> 895 case '(' : >> 896 token= Yylex(); >> 897 result = Expression(); >> 898 if( token != ')' ) { >> 899 G4cerr << " ')' expected" << G4endl; >> 900 paramERR = 1; >> 901 } >> 902 token = Yylex(); >> 903 break; >> 904 default: >> 905 return result; >> 906 } >> 907 return result; // never executed >> 908 } >> 909 >> 910 //---------------- semantic routines --------------------------------- >> 911 >> 912 G4int G4UIcommand:: >> 913 Eval2(yystype arg1, G4int op, yystype arg2) >> 914 { >> 915 char newValtype; >> 916 if( (arg1.type != IDENTIFIER) && (arg2.type != IDENTIFIER)) { >> 917 G4cerr << commandName >> 918 << ": meaningless comparison" >> 919 << G4endl; >> 920 paramERR = 1; >> 921 } >> 922 >> 923 if( arg1.type == IDENTIFIER) { >> 924 unsigned i = IndexOf( arg1.S ); >> 925 newValtype = toupper(parameter[i]->GetParameterType()); >> 926 switch ( newValtype ) { >> 927 case 'I': >> 928 if( arg2.type == CONSTINT ) { >> 929 return CompareInt( newVal[i].I, op, arg2.I ); >> 930 } else { >> 931 G4cerr << "integer operand expected for " >> 932 << rangeString >> 933 << '.' << G4endl; >> 934 } break; >> 935 case 'D': >> 936 if( arg2.type == CONSTDOUBLE ) { >> 937 return CompareDouble( newVal[i].D, op, arg2.D ); >> 938 } else >> 939 if ( arg2.type == CONSTINT ) { // integral promotion >> 940 return CompareDouble( newVal[i].D, op, arg2.I ); >> 941 } break; >> 942 default: ; >> 943 } >> 944 } >> 945 if( arg2.type == IDENTIFIER) { >> 946 unsigned i = IndexOf( arg2.S ); >> 947 newValtype = toupper(parameter[i]->GetParameterType()); >> 948 switch ( newValtype ) { >> 949 case 'I': >> 950 if( arg1.type == CONSTINT ) { >> 951 return CompareInt( arg1.I, op, newVal[i].I ); >> 952 } else { >> 953 G4cerr << "integer operand expected for " >> 954 << rangeString >> 955 << '.' << G4endl; >> 956 } break; >> 957 case 'D': >> 958 if( arg1.type == CONSTDOUBLE ) { >> 959 return CompareDouble( arg1.D, op, newVal[i].D ); >> 960 } else >> 961 if ( arg1.type == CONSTINT ) { // integral promotion >> 962 return CompareDouble( arg1.I, op, newVal[i].D ); >> 963 } break; >> 964 default: ; >> 965 } >> 966 } >> 967 return 0; >> 968 } >> 969 >> 970 G4int G4UIcommand:: >> 971 CompareInt(G4int arg1, G4int op, G4int arg2) >> 972 { >> 973 G4int result=-1; >> 974 G4String opr; >> 975 switch (op) { >> 976 case GT: result = ( arg1 > arg2); opr= ">" ; break; >> 977 case GE: result = ( arg1 >= arg2); opr= ">="; break; >> 978 case LT: result = ( arg1 < arg2); opr= "<" ; break; >> 979 case LE: result = ( arg1 <= arg2); opr= "<="; break; >> 980 case EQ: result = ( arg1 == arg2); opr= "=="; break; >> 981 case NE: result = ( arg1 != arg2); opr= "!="; break; >> 982 default: >> 983 G4cerr << "Parameter range: error at CompareInt" << G4endl; >> 984 paramERR = 1; >> 985 } >> 986 #ifdef DEBUG >> 987 G4cerr << "CompareInt " >> 988 << arg1 << " " << opr << arg2 >> 989 << " result: " << result >> 990 << G4endl; >> 991 #endif >> 992 return result; >> 993 } >> 994 >> 995 G4int G4UIcommand:: >> 996 CompareDouble(G4double arg1, G4int op, G4double arg2) >> 997 { >> 998 G4int result=-1; >> 999 G4String opr; >> 1000 switch (op) { >> 1001 case GT: result = ( arg1 > arg2); opr= ">"; break; >> 1002 case GE: result = ( arg1 >= arg2); opr= ">="; break; >> 1003 case LT: result = ( arg1 < arg2); opr= "<"; break; >> 1004 case LE: result = ( arg1 <= arg2); opr= "<="; break; >> 1005 case EQ: result = ( arg1 == arg2); opr= "=="; break; >> 1006 case NE: result = ( arg1 != arg2); opr= "!="; break; >> 1007 default: >> 1008 G4cerr << "Parameter range: error at CompareDouble" >> 1009 << G4endl; >> 1010 paramERR = 1; >> 1011 } >> 1012 #ifdef DEBUG >> 1013 G4cerr << "CompareDouble " >> 1014 << arg1 <<" " << opr << " "<< arg2 >> 1015 << " result: " << result >> 1016 << G4endl; >> 1017 #endif >> 1018 return result; >> 1019 } >> 1020 >> 1021 unsigned G4UIcommand:: >> 1022 IndexOf(const char* nam) >> 1023 { >> 1024 unsigned i; >> 1025 G4String pname; >> 1026 for( i=0; i<parameter.size(); i++) >> 1027 { >> 1028 pname = parameter[i]-> GetParameterName(); >> 1029 if( pname == nam ) { >> 1030 return i; >> 1031 } >> 1032 } >> 1033 paramERR = 1; >> 1034 G4cerr << "parameter name:"<<nam<<" not found."<< G4endl; >> 1035 return 0; >> 1036 } >> 1037 >> 1038 >> 1039 unsigned G4UIcommand:: >> 1040 IsParameter(const char* nam) >> 1041 { >> 1042 G4String pname; >> 1043 for(unsigned i=0; i<parameter.size(); i++) >> 1044 { >> 1045 pname = parameter[i]-> GetParameterName(); >> 1046 if( pname == nam ) return 1; >> 1047 } >> 1048 return 0; >> 1049 } >> 1050 >> 1051 >> 1052 // --------------------- utility functions -------------------------- >> 1053 >> 1054 tokenNum G4UIcommand:: >> 1055 Yylex() // reads input and returns token number, KR486 >> 1056 { // (returns EOF) >> 1057 G4int c; >> 1058 G4String buf; >> 1059 >> 1060 while(( c= G4UIpGetc())==' '|| c=='\t' || c== '\n' ) >> 1061 ; >> 1062 if (c== EOF) >> 1063 return (tokenNum)EOF; // KR488 >> 1064 buf= ""; >> 1065 if (isdigit(c) || c== '.') { // I or D >> 1066 do { >> 1067 buf += G4String((unsigned char)c); >> 1068 c=G4UIpGetc(); >> 1069 } while (c=='.' || isdigit(c) || >> 1070 c=='e' || c=='E' || c=='+' || c=='-'); >> 1071 G4UIpUngetc(c); >> 1072 const char* t = buf; >> 1073 std::istringstream is(t); >> 1074 if ( IsInt(buf.data(),20) ) { >> 1075 is >> yylval.I; >> 1076 return CONSTINT; >> 1077 } else >> 1078 if ( IsDouble(buf.data()) ) { >> 1079 is >> yylval.D; >> 1080 return CONSTDOUBLE; >> 1081 } else { >> 1082 G4cerr << buf<<": numeric format error."<<G4endl; >> 1083 } >> 1084 } >> 1085 buf=""; >> 1086 if (isalpha(c)|| c=='_') { // IDENTIFIER >> 1087 do { >> 1088 buf += G4String((unsigned char)c); >> 1089 } while ((c=G4UIpGetc()) != EOF && (isalnum(c) || c=='_')); >> 1090 G4UIpUngetc(c); >> 1091 if( IsParameter(buf) ) { >> 1092 yylval.S =buf; >> 1093 return IDENTIFIER; >> 1094 } else { >> 1095 G4cerr << buf << " is not a parameter name."<< G4endl; >> 1096 paramERR = 1; >> 1097 } >> 1098 } >> 1099 switch (c) { >> 1100 case '>': return (tokenNum) Follow('=', GE, GT); >> 1101 case '<': return (tokenNum) Follow('=', LE, LT); >> 1102 case '=': return (tokenNum) Follow('=', EQ, '='); >> 1103 case '!': return (tokenNum) Follow('=', NE, '!'); >> 1104 case '|': return (tokenNum) Follow('|', LOGICALOR, '|'); >> 1105 case '&': return (tokenNum) Follow('&', LOGICALAND, '&'); >> 1106 default: >> 1107 return (tokenNum) c; >> 1108 } >> 1109 } >> 1110 >> 1111 >> 1112 G4int G4UIcommand:: >> 1113 Follow(G4int expect, G4int ifyes, G4int ifno) >> 1114 { >> 1115 G4int c = G4UIpGetc(); >> 1116 if ( c== expect) >> 1117 return ifyes; >> 1118 G4UIpUngetc(c); >> 1119 return ifno; >> 1120 } >> 1121 >> 1122 //------------------ low level routines ----------------------------- >> 1123 G4int G4UIcommand:: >> 1124 G4UIpGetc() { // emulation of getc() >> 1125 G4int length = rangeString.length(); >> 1126 if( bp < length) >> 1127 return rangeString(bp++); >> 1128 else >> 1129 return EOF; >> 1130 } >> 1131 G4int G4UIcommand:: >> 1132 G4UIpUngetc(G4int c) { // emulation of ungetc() >> 1133 if (c<0) return -1; >> 1134 if (bp >0 && c == rangeString(bp-1)) { >> 1135 --bp; >> 1136 } else { >> 1137 G4cerr << "G4UIpUngetc() failed." << G4endl; >> 1138 G4cerr << "bp="<<bp <<" c="<<c >> 1139 << " pR(bp-1)=" << rangeString(bp-1) >> 1140 << G4endl; >> 1141 paramERR = 1; >> 1142 return -1; >> 1143 } >> 1144 return 0; 482 } 1145 } 483 1146