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 // G4UIparameter << 8 // $Id: G4UIparameter.cc,v 1.3.2.1 1999/12/07 20:49:04 gunter Exp $ >> 9 // GEANT4 tag $Name: geant4-01-00 $ 27 // 10 // 28 // Author: Makoto Asai, 1997 << 29 // ------------------------------------------- << 30 11 31 #include "G4UIparameter.hh" 12 #include "G4UIparameter.hh" 32 << 33 #include "G4Tokenizer.hh" << 34 #include "G4UIcommand.hh" << 35 #include "G4UIcommandStatus.hh" 13 #include "G4UIcommandStatus.hh" 36 #include "G4UIparsing.hh" << 14 >> 15 #include "g4rw/ctoken.h" >> 16 37 #include "G4ios.hh" 17 #include "G4ios.hh" >> 18 G4UIparameter::G4UIparameter():paramERR(0) >> 19 { >> 20 G4String nullString; >> 21 parameterName = nullString; >> 22 parameterType = '\0'; >> 23 omittable = false; >> 24 parameterGuidance = nullString; >> 25 defaultValue = nullString; >> 26 parameterRange = nullString; >> 27 currentAsDefaultFlag = false; >> 28 parameterCandidate = nullString; >> 29 widget = 0; >> 30 } 38 31 39 // ------------------------------------------- << 32 G4UIparameter::G4UIparameter(char theType):paramERR(0) 40 G4UIparameter::G4UIparameter(char theType) << 41 { 33 { >> 34 G4String nullString; >> 35 parameterName = nullString; 42 parameterType = theType; 36 parameterType = theType; >> 37 omittable = false; >> 38 parameterGuidance = nullString; >> 39 defaultValue = nullString; >> 40 parameterRange = nullString; >> 41 currentAsDefaultFlag = false; >> 42 parameterCandidate = nullString; >> 43 widget = 0; 43 } 44 } 44 45 45 // ------------------------------------------- << 46 G4UIparameter::G4UIparameter(const char * theName, char theType, G4bool theOmittable):paramERR(0) 46 G4UIparameter::G4UIparameter(const char* theNa << 47 { 47 { 48 parameterName = theName; 48 parameterName = theName; 49 parameterType = theType; 49 parameterType = theType; 50 omittable = theOmittable; 50 omittable = theOmittable; >> 51 G4String nullString; >> 52 parameterGuidance = nullString; >> 53 defaultValue = nullString; >> 54 parameterRange = nullString; >> 55 currentAsDefaultFlag = false; >> 56 parameterCandidate = nullString; >> 57 widget = 0; 51 } 58 } 52 59 53 // ------------------------------------------- << 60 G4UIparameter::~G4UIparameter() 54 G4UIparameter::~G4UIparameter() = default; << 61 { } >> 62 >> 63 int G4UIparameter::operator==(const G4UIparameter &right) const >> 64 { >> 65 return ( this == &right ); >> 66 } >> 67 >> 68 int G4UIparameter::operator!=(const G4UIparameter &right) const >> 69 { >> 70 return ( this != &right ); >> 71 } 55 72 56 // ------------------------------------------- << 57 void G4UIparameter::List() 73 void G4UIparameter::List() 58 { 74 { 59 G4cout << G4endl << "Parameter : " << parame << 75 G4cout << endl << "Parameter : " << parameterName << endl; 60 if (!parameterGuidance.empty()) { << 76 if( ! parameterGuidance.isNull() ) 61 G4cout << parameterGuidance << G4endl; << 77 G4cout << parameterGuidance << endl ; 62 } << 78 G4cout << " Parameter type : " << parameterType << endl; 63 G4cout << " Parameter type : " << parameter << 79 if(omittable) 64 if (omittable) { << 80 { G4cout << " Omittable : True" << endl; } 65 G4cout << " Omittable : True" << G4e << 81 else 66 } << 82 { G4cout << " Omittable : False" << endl; } 67 else { << 83 if( currentAsDefaultFlag ) 68 G4cout << " Omittable : False" << G4 << 84 { G4cout << " Default value : taken from the current value" << endl; } 69 } << 85 else if( ! defaultValue.isNull() ) 70 if (currentAsDefaultFlag) { << 86 { G4cout << " Default value : " << defaultValue << endl; } 71 G4cout << " Default value : taken from t << 87 if( ! parameterRange.isNull() ) 72 } << 88 G4cout << " Parameter range : " << parameterRange << endl; 73 else if (!defaultValue.empty()) { << 89 if( ! parameterCandidate.isNull() ) 74 G4cout << " Default value : " << default << 90 G4cout << " Candidates : " << parameterCandidate << endl; 75 } << 76 if (!rangeExpression.empty()) { << 77 G4cout << " Parameter range : " << rangeEx << 78 } << 79 if (!parameterCandidate.empty()) { << 80 G4cout << " Candidates : " << paramet << 81 } << 82 } 91 } 83 92 84 // ------------------------------------------- << 85 void G4UIparameter::SetDefaultValue(G4int theD 93 void G4UIparameter::SetDefaultValue(G4int theDefaultValue) 86 { 94 { 87 defaultValue = G4UIparsing::TtoS(theDefaultV << 95 char defVal[20]; >> 96 ostrstream os(defVal,20); >> 97 os << theDefaultValue << '\0'; >> 98 defaultValue = defVal; 88 } 99 } 89 100 90 // ------------------------------------------- << 101 void G4UIparameter::SetDefaultValue(G4double theDefaultValue) 91 void G4UIparameter::SetDefaultValue(G4long the << 92 { 102 { 93 defaultValue = G4UIparsing::TtoS(theDefaultV << 103 char defVal[20]; >> 104 ostrstream os(defVal,20); >> 105 os << theDefaultValue << '\0'; >> 106 defaultValue = defVal; 94 } 107 } 95 108 96 // ------------------------------------------- << 109 97 void G4UIparameter::SetDefaultValue(G4double t << 110 // ---------- CheckNewValue() related routines ----------- >> 111 #include <ctype.h> >> 112 #include "G4UItokenNum.hh" >> 113 >> 114 //#include "checkNewValue_debug.icc" >> 115 //#define DEBUG 1 >> 116 >> 117 int G4UIparameter:: >> 118 CheckNewValue( G4String newValue ) { >> 119 if( TypeCheck(newValue) == 0) return fParameterUnreadable; >> 120 if( ! parameterRange.isNull() ) >> 121 { if( RangeCheck(newValue) == 0 ) return fParameterOutOfRange; } >> 122 if( ! parameterCandidate.isNull() ) >> 123 { if( CandidateCheck(newValue) == 0 ) return fParameterOutOfCandidates; } >> 124 return 0; // succeeded >> 125 } >> 126 >> 127 int G4UIparameter:: >> 128 CandidateCheck(G4String newValue) { >> 129 G4Tokenizer candidateTokenizer(parameterCandidate); >> 130 G4String aToken; >> 131 int iToken = 0; >> 132 while( ! (aToken=candidateTokenizer()).isNull() ) >> 133 { >> 134 iToken++; >> 135 if(aToken==newValue) return iToken; >> 136 } >> 137 G4cerr << "parameter value is not listed in the candidate List." << endl; >> 138 return 0; >> 139 } >> 140 >> 141 int G4UIparameter:: >> 142 RangeCheck(G4String newValue) { >> 143 yystype result; >> 144 bp = 0; // reset buffer pointer for G4UIpGetc() >> 145 const char* t = newValue; >> 146 istrstream is((char*)t); >> 147 char type = toupper( parameterType ); >> 148 switch (type) { >> 149 case 'D': { is >> newVal.D; } break; >> 150 case 'I': { is >> newVal.I; } break; >> 151 default: ; >> 152 } >> 153 // PrintToken(); // Print tokens (consumes all tokens) >> 154 token= Yylex(); >> 155 result = Expression(); >> 156 if( paramERR == 1 ) return 0; >> 157 if( result.type != CONSTINT) { >> 158 G4cerr << "Illegal Expression in parameter range." << endl; >> 159 return 0; >> 160 } >> 161 if ( result.I ) return 1; >> 162 G4cerr << "parameter out of range: "<< parameterRange << endl; >> 163 return 0; >> 164 } >> 165 >> 166 >> 167 int G4UIparameter:: >> 168 TypeCheck(G4String newValue) 98 { 169 { 99 defaultValue = G4UIparsing::TtoS(theDefaultV << 170 char type = toupper( parameterType ); >> 171 switch(type) { >> 172 case 'D': >> 173 if( IsDouble(newValue.data())== 0) { >> 174 G4cerr<<newValue<<": double value expected." >> 175 << endl; >> 176 return 0; >> 177 } break; >> 178 case 'I': >> 179 if( IsInt(newValue.data(),20)== 0) { >> 180 G4cerr<<newValue<<": integer expected." >> 181 << endl; >> 182 return 0; >> 183 } break; >> 184 case 'S': break; >> 185 case 'B': >> 186 newValue.toUpper(); >> 187 if ( newValue == "Y" || newValue == "N" >> 188 ||newValue == "YES" || newValue == "NO" >> 189 ||newValue == "1" || newValue == "0" >> 190 ||newValue == "T" || newValue == "F" >> 191 ||newValue == "TRUE" || newValue == "FALSE") >> 192 return 1; >> 193 else { >> 194 G4cerr<<newValue<<": bool expected." << endl; >> 195 return 0; >> 196 } >> 197 default: ; >> 198 } >> 199 return 1; 100 } 200 } 101 201 102 // ------------------------------------------- << 202 103 void G4UIparameter::SetDefaultUnit(const char* << 203 int G4UIparameter:: >> 204 IsInt(const char* buf, short maxDigits) // do not allow any ws 104 { 205 { 105 char type = (char)std::toupper(parameterType << 206 const char* p= buf; 106 if (type != 'S') { << 207 int length=0; 107 G4ExceptionDescription ed; << 208 if( *p == '+' || *p == '-') { ++p; } 108 ed << "This method can be used only for a << 209 if( isdigit( (int)(*p) )) { 109 "used to specify a unit.\n" << 210 while( isdigit( (int)(*p) )) { ++p; ++length; } 110 << "This parameter <" << parameterName << 211 if( *p == '\0' ) { 111 switch (type) { << 212 if( length > maxDigits) { 112 case 'D': << 213 G4cerr <<"digit length exceeds"<<endl; 113 ed << "double."; << 214 return 0; 114 break; << 215 } 115 case 'I': << 216 return 1; 116 ed << "integer."; << 217 } else { 117 break; << 218 // G4cerr <<"illegal character after int:"<<buf<<endl; 118 case 'L': << 219 } 119 ed << "long int."; << 220 } else { 120 break; << 221 // G4cerr <<"illegal int:"<<buf<<endl; 121 case 'B': << 222 } 122 ed << "bool."; << 223 return 0; 123 break; << 224 } >> 225 >> 226 >> 227 int G4UIparameter:: >> 228 ExpectExponent(const char* str) // used only by IsDouble() >> 229 { >> 230 int maxExplength; >> 231 if( IsInt( str, maxExplength=7 )) return 1; >> 232 else return 0; >> 233 } >> 234 >> 235 int G4UIparameter:: >> 236 IsDouble(const char* buf) // see state diagram for this spec. >> 237 { >> 238 const char* p= buf; >> 239 switch( *p) { >> 240 case '+': case '-': ++p; >> 241 if( isdigit(*p) ) { >> 242 while( isdigit( (int)(*p) )) { ++p; } >> 243 switch ( *p ) { >> 244 case '\0': return 1; //break; >> 245 case 'E': case 'e': >> 246 return ExpectExponent(++p ); //break; >> 247 case '.': ++p; >> 248 if( *p == '\0' ) return 1; >> 249 if( *p == 'e' || *p =='E' ) return ExpectExponent(++p ); >> 250 if( isdigit(*p) ) { >> 251 while( isdigit( (int)(*p) )) { ++p; } >> 252 if( *p == '\0' ) return 1; >> 253 if( *p == 'e' || *p =='E') return ExpectExponent(++p); >> 254 } else return 0; break; >> 255 default: return 0; >> 256 } >> 257 } >> 258 if( *p == '.' ) { ++p; >> 259 if( isdigit(*p) ) { >> 260 while( isdigit( (int)(*p) )) { ++p; } >> 261 if( *p == '\0' ) return 1; >> 262 if( *p == 'e' || *p =='E') return ExpectExponent(++p); >> 263 } >> 264 } >> 265 break; >> 266 case '.': ++p; >> 267 if( isdigit(*p) ) { >> 268 while( isdigit( (int)(*p) )) { ++p; } >> 269 if( *p == '\0' ) return 1; >> 270 if( *p == 'e' || *p =='E' ) return ExpectExponent(++p); >> 271 } break; >> 272 default: // digit is expected >> 273 if( isdigit(*p) ) { >> 274 while( isdigit( (int)(*p) )) { ++p; } >> 275 if( *p == '\0' ) return 1; >> 276 if( *p == 'e' || *p =='E') return ExpectExponent(++p); >> 277 if( *p == '.' ) { ++p; >> 278 if( *p == '\0' ) return 1; >> 279 if( *p == 'e' || *p =='E') return ExpectExponent(++p); >> 280 if( isdigit(*p) ) { >> 281 while( isdigit( (int)(*p) )) { ++p; } >> 282 if( *p == '\0' ) return 1; >> 283 if( *p == 'e' || *p =='E') return ExpectExponent(++p); >> 284 } >> 285 } >> 286 } >> 287 } >> 288 return 0; >> 289 } >> 290 >> 291 >> 292 // ------------------ syntax node functions ------------------ >> 293 >> 294 yystype G4UIparameter:: >> 295 Expression(void) >> 296 { >> 297 yystype result; >> 298 #ifdef DEBUG >> 299 G4cerr << " Expression()" << endl; >> 300 #endif >> 301 result = LogicalORExpression(); >> 302 return result; >> 303 } >> 304 >> 305 yystype G4UIparameter:: >> 306 LogicalORExpression(void) >> 307 { >> 308 yystype result; >> 309 yystype p; >> 310 p = LogicalANDExpression(); >> 311 if( token != LOGICALOR) return p; >> 312 if( p.type == CONSTSTRING || p.type == IDENTIFIER ) { >> 313 G4cerr << "Parameter range: illegal type at '||'" << endl; >> 314 paramERR = 1; >> 315 } >> 316 result.I = p.I; >> 317 while (token == LOGICALOR) >> 318 { >> 319 token = Yylex(); >> 320 p = LogicalANDExpression(); >> 321 if( p.type == CONSTSTRING || p.type == IDENTIFIER ) { >> 322 G4cerr << "Parameter range: illegal type at '||'" <<endl; >> 323 paramERR = 1; >> 324 } >> 325 switch (p.type) { >> 326 case CONSTINT: >> 327 result.I += p.I; >> 328 result.type = CONSTINT; break; >> 329 case CONSTDOUBLE: >> 330 result.I += (p.D != 0.0); >> 331 result.type = CONSTINT; break; >> 332 default: >> 333 G4cerr << "Parameter range: unknown type"<<endl; >> 334 paramERR = 1; >> 335 } >> 336 } >> 337 return result; >> 338 } >> 339 >> 340 yystype G4UIparameter:: >> 341 LogicalANDExpression(void) >> 342 { >> 343 yystype result; >> 344 yystype p; >> 345 p = EqualityExpression(); >> 346 if( token != LOGICALAND) return p; >> 347 if( p.type == CONSTSTRING || p.type == IDENTIFIER ) { >> 348 G4cerr << "Parameter range: illegal type at '&&'" << endl; >> 349 paramERR = 1; >> 350 } >> 351 result.I = p.I; >> 352 while (token == LOGICALAND) >> 353 { >> 354 token = Yylex(); >> 355 p = EqualityExpression(); >> 356 if( p.type == CONSTSTRING || p.type == IDENTIFIER ) { >> 357 G4cerr << "Parameter range: illegal type at '&&'" << endl; >> 358 paramERR = 1; >> 359 } >> 360 switch (p.type) { >> 361 case CONSTINT: >> 362 result.I *= p.I; >> 363 result.type = CONSTINT; break; >> 364 case CONSTDOUBLE: >> 365 result.I *= (p.D != 0.0); >> 366 result.type = CONSTINT; break; >> 367 default: >> 368 G4cerr << "Parameter range: unknown type."<< endl; >> 369 paramERR = 1; >> 370 } >> 371 } >> 372 return result; >> 373 } >> 374 >> 375 >> 376 yystype G4UIparameter:: >> 377 EqualityExpression(void) >> 378 { >> 379 yystype arg1, arg2; >> 380 int operat; >> 381 yystype result; >> 382 #ifdef DEBUG >> 383 G4cerr << " EqualityExpression()" <<endl; >> 384 #endif >> 385 result = RelationalExpression(); >> 386 if( token==EQ || token==NE ) { >> 387 operat = token; >> 388 token = Yylex(); >> 389 arg1 = result; >> 390 arg2 = RelationalExpression(); >> 391 result.I = Eval2( arg1, operat, arg2 ); // semantic action >> 392 result.type = CONSTINT; >> 393 #ifdef DEBUG >> 394 G4cerr << " return code of Eval2(): " << result.I <<endl; >> 395 #endif >> 396 } else { >> 397 if (result.type != CONSTINT && result.type != CONSTDOUBLE) { >> 398 G4cerr << "Parameter range: error at EqualityExpression" >> 399 << endl; >> 400 paramERR = 1; >> 401 } >> 402 } >> 403 return result; >> 404 } >> 405 >> 406 >> 407 yystype G4UIparameter:: >> 408 RelationalExpression(void) >> 409 { >> 410 yystype arg1, arg2; >> 411 int operat; >> 412 yystype result; >> 413 #ifdef DEBUG >> 414 G4cerr << " RelationalExpression()" <<endl; >> 415 #endif >> 416 >> 417 arg1 = AdditiveExpression(); >> 418 if( token==GT || token==GE || token==LT || token==LE ) { >> 419 operat = token; >> 420 token = Yylex(); >> 421 arg2 = AdditiveExpression(); >> 422 result.I = Eval2( arg1, operat, arg2 ); // semantic action >> 423 result.type = CONSTINT; >> 424 #ifdef DEBUG >> 425 G4cerr << " return Eval2(): " << endl; >> 426 #endif >> 427 } else { >> 428 result = arg1; >> 429 } >> 430 #ifdef DEBUG >> 431 G4cerr <<" return RelationalExpression()" <<endl; >> 432 #endif >> 433 return result; >> 434 } >> 435 >> 436 yystype G4UIparameter:: >> 437 AdditiveExpression(void) >> 438 { yystype result; >> 439 result = MultiplicativeExpression(); >> 440 if( token != '+' && token != '-' ) return result; >> 441 G4cerr << "Parameter range: operator " >> 442 << (char)token >> 443 << " is not supported." << endl; >> 444 paramERR = 1; >> 445 return result; >> 446 } >> 447 >> 448 yystype G4UIparameter:: >> 449 MultiplicativeExpression(void) >> 450 { yystype result; >> 451 result = UnaryExpression(); >> 452 if( token != '*' && token != '/' && token != '%' ) return result; >> 453 G4cerr << "Parameter range: operator " >> 454 << (char)token >> 455 << " is not supported." << endl; >> 456 paramERR = 1; >> 457 return result; >> 458 } >> 459 >> 460 yystype G4UIparameter:: >> 461 UnaryExpression(void) >> 462 { >> 463 yystype result; >> 464 yystype p; >> 465 #ifdef DEBUG >> 466 G4cerr <<" UnaryExpression"<< endl; >> 467 #endif >> 468 switch(token) { >> 469 case '-': >> 470 token = Yylex(); >> 471 p = UnaryExpression(); >> 472 if (p.type == CONSTINT) { >> 473 result.I = - p.I; >> 474 result.type = CONSTINT; >> 475 } >> 476 if (p.type == CONSTDOUBLE) { >> 477 result.D = - p.D; >> 478 result.type = CONSTDOUBLE; >> 479 } break; >> 480 case '+': >> 481 token = Yylex(); >> 482 result = UnaryExpression(); break; >> 483 case '!': >> 484 token = Yylex(); >> 485 G4cerr << "Parameter range error: " >> 486 << "operator '!' is not supported (sorry)." >> 487 << endl; >> 488 paramERR = 1; >> 489 result = UnaryExpression(); break; >> 490 default: >> 491 result = PrimaryExpression(); >> 492 } >> 493 return result; >> 494 } >> 495 >> 496 >> 497 yystype G4UIparameter:: >> 498 PrimaryExpression(void) >> 499 { >> 500 yystype result; >> 501 #ifdef DEBUG >> 502 G4cerr <<" PrimaryExpression" << endl; >> 503 #endif >> 504 switch (token) { >> 505 case IDENTIFIER: >> 506 result.S = yylval.S; >> 507 result.type = token; >> 508 token = Yylex(); break; >> 509 case CONSTINT: >> 510 result.I = yylval.I; >> 511 result.type = token; >> 512 token= Yylex(); break; >> 513 case CONSTDOUBLE: >> 514 result.D = yylval.D; >> 515 result.type = token; >> 516 token = Yylex(); break; >> 517 case '(' : >> 518 token= Yylex(); >> 519 result = Expression(); >> 520 if( token != ')' ) { >> 521 G4cerr << " ')' expected" << endl; >> 522 paramERR = 1; >> 523 } >> 524 token = Yylex(); >> 525 break; >> 526 default: >> 527 return result; >> 528 } >> 529 return result; // never executed >> 530 } >> 531 >> 532 //---------------- semantic routines --------------------------------- >> 533 >> 534 int G4UIparameter:: >> 535 Eval2(yystype arg1, int op, yystype arg2) >> 536 { >> 537 if( (arg1.type != IDENTIFIER) && (arg2.type != IDENTIFIER)) { >> 538 G4cerr << parameterName >> 539 << ": meaningless comparison " >> 540 << arg1.type << " " << arg2.type << endl; >> 541 paramERR = 1; >> 542 } >> 543 char type = toupper( parameterType ); >> 544 if( arg1.type == IDENTIFIER) { >> 545 switch (type) { >> 546 case 'I': >> 547 if ( arg2.type == CONSTINT ) { >> 548 return CompareInt( newVal.I, op, arg2.I ); >> 549 } else { >> 550 G4cerr << "integer operand expected for " >> 551 << parameterRange << '.' >> 552 << endl; >> 553 } >> 554 break; >> 555 case 'D': >> 556 if ( arg2.type == CONSTDOUBLE ) { >> 557 return CompareDouble( newVal.D, op, arg2.D ); >> 558 } else >> 559 if ( arg2.type == CONSTINT ) { // integral promotion >> 560 return CompareDouble( newVal.D, op, arg2.I ); >> 561 } break; >> 562 default: ; >> 563 } >> 564 } >> 565 if( arg2.type == IDENTIFIER) { >> 566 switch (type) { >> 567 case 'I': >> 568 if ( arg1.type == CONSTINT ) { >> 569 return CompareInt( arg1.I, op, newVal.I ); >> 570 } else { >> 571 G4cerr << "integer operand expected for " >> 572 << parameterRange << '.' >> 573 << endl; >> 574 } >> 575 break; >> 576 case 'D': >> 577 if ( arg1.type == CONSTDOUBLE ) { >> 578 return CompareDouble( arg1.D, op, newVal.D ); >> 579 } else >> 580 if ( arg1.type == CONSTINT ) { // integral promotion >> 581 return CompareDouble( arg1.I, op, newVal.D ); >> 582 } break; >> 583 default: ; >> 584 } >> 585 } >> 586 G4cerr << "no param name is specified at the param range."<<endl; >> 587 return 0; >> 588 } >> 589 >> 590 int G4UIparameter:: >> 591 CompareInt(int arg1, int op, int arg2) >> 592 { >> 593 int result; >> 594 G4String opr; >> 595 switch (op) { >> 596 case GT: result = ( arg1 > arg2); opr= ">" ; break; >> 597 case GE: result = ( arg1 >= arg2); opr= ">="; break; >> 598 case LT: result = ( arg1 < arg2); opr= "<" ; break; >> 599 case LE: result = ( arg1 <= arg2); opr= "<="; break; >> 600 case EQ: result = ( arg1 == arg2); opr= "=="; break; >> 601 case NE: result = ( arg1 != arg2); opr= "!="; break; >> 602 default: >> 603 G4cerr << "Parameter range: error at CompareInt" << endl; >> 604 paramERR = 1; >> 605 } >> 606 #ifdef DEBUG >> 607 G4cerr << "CompareInt " >> 608 << arg1 << " " << opr << arg2 >> 609 << " result: " << result >> 610 << endl; >> 611 #endif >> 612 return result; >> 613 } >> 614 >> 615 int G4UIparameter:: >> 616 CompareDouble(double arg1, int op, double arg2) >> 617 { >> 618 int result; >> 619 G4String opr; >> 620 switch (op) { >> 621 case GT: result = ( arg1 > arg2); opr= ">"; break; >> 622 case GE: result = ( arg1 >= arg2); opr= ">="; break; >> 623 case LT: result = ( arg1 < arg2); opr= "<"; break; >> 624 case LE: result = ( arg1 <= arg2); opr= "<="; break; >> 625 case EQ: result = ( arg1 == arg2); opr= "=="; break; >> 626 case NE: result = ( arg1 != arg2); opr= "!="; break; >> 627 default: >> 628 G4cerr << "Parameter range: error at CompareDouble" << endl; >> 629 paramERR = 1; >> 630 } >> 631 #ifdef DEBUG >> 632 G4cerr << "CompareDouble " >> 633 << arg1 <<" " << opr << " "<< arg2 >> 634 << " result: " << result >> 635 << endl; >> 636 #endif >> 637 return result; >> 638 } >> 639 >> 640 // --------------------- utility functions -------------------------- >> 641 >> 642 tokenNum G4UIparameter:: >> 643 Yylex() // reads input and returns token number KR486 >> 644 { // (returns EOF) >> 645 int c; >> 646 G4String buf; >> 647 >> 648 while(( c= G4UIpGetc())==' '|| c=='\t' || c== '\n' ) >> 649 ; >> 650 if (c== EOF) >> 651 return (tokenNum)EOF; // KR488 >> 652 buf= ""; >> 653 if (isdigit(c) || c== '.') { // I or D >> 654 do { >> 655 buf += G4String((unsigned char)c); >> 656 c=G4UIpGetc(); >> 657 } while (c=='.' || isdigit(c) || >> 658 c=='e' || c=='E' || c=='+' || c=='-'); >> 659 G4UIpUngetc(c); >> 660 const char* t = buf; >> 661 istrstream is((char*)t); >> 662 if ( IsInt(buf.data(),20) ) { >> 663 is >> yylval.I; >> 664 return CONSTINT; >> 665 } else >> 666 if ( IsDouble(buf.data()) ) { >> 667 is >> yylval.D; >> 668 return CONSTDOUBLE; >> 669 } else { >> 670 G4cerr << buf<<": numeric format error."<<endl; >> 671 } >> 672 } >> 673 buf=""; >> 674 if (isalpha(c)|| c=='_') { // IDENTIFIER >> 675 do { >> 676 buf += G4String((unsigned char)c); >> 677 } while ((c=G4UIpGetc()) != EOF && (isalnum(c) || c=='_')); >> 678 G4UIpUngetc(c); >> 679 if( buf == parameterName ) { >> 680 yylval.S =buf; >> 681 return IDENTIFIER; >> 682 } else { >> 683 G4cerr << buf << " is not a parameter name."<< endl; >> 684 paramERR = 1; >> 685 } >> 686 } >> 687 switch (c) { >> 688 case '>': return (tokenNum) Follow('=', GE, GT); >> 689 case '<': return (tokenNum) Follow('=', LE, LT); >> 690 case '=': return (tokenNum) Follow('=', EQ, '='); >> 691 case '!': return (tokenNum) Follow('=', NE, '!'); >> 692 case '|': return (tokenNum) Follow('|', LOGICALOR, '|'); >> 693 case '&': return (tokenNum) Follow('&', LOGICALAND, '&'); 124 default: 694 default: 125 ed << "undefined."; << 695 return (tokenNum) c; >> 696 } >> 697 } >> 698 >> 699 >> 700 int G4UIparameter:: >> 701 Follow(int expect, int ifyes, int ifno) >> 702 { >> 703 int c = G4UIpGetc(); >> 704 if ( c== expect) >> 705 return ifyes; >> 706 G4UIpUngetc(c); >> 707 return ifno; >> 708 } >> 709 >> 710 //------------------ low level routines ----------------------------- >> 711 int G4UIparameter:: >> 712 G4UIpGetc() { // emulation of getc() >> 713 int length = parameterRange.length(); >> 714 if( bp < length) >> 715 return parameterRange(bp++); >> 716 else >> 717 return EOF; >> 718 } >> 719 int G4UIparameter:: >> 720 G4UIpUngetc(int c) { // emulation of ungetc() >> 721 if (c<0) return -1; >> 722 if (bp >0 && c == parameterRange(bp-1)) { >> 723 --bp; >> 724 } else { >> 725 G4cerr << "G4UIpUngetc() failed." << endl; >> 726 G4cerr << "bp="<<bp <<" c="<<c >> 727 << " pR(bp-1)=" << parameterRange(bp-1) >> 728 << endl; >> 729 paramERR = 1; >> 730 return -1; 126 } 731 } 127 G4Exception("G4UIparameter::SetDefaultUnit << 732 return 0; 128 } << 129 SetDefaultValue(theDefaultUnit); << 130 SetParameterCandidates(G4UIcommand::UnitsLis << 131 } << 132 << 133 // ---------- CheckNewValue() related routines << 134 << 135 G4int G4UIparameter::CheckNewValue(const char* << 136 { << 137 if (!TypeCheck(newValue)) { << 138 return fParameterUnreadable; << 139 } << 140 if (!G4UIparsing::RangeCheck(*this, newValue << 141 return fParameterOutOfRange; << 142 } << 143 if (!CandidateCheck(newValue)) { << 144 return fParameterOutOfCandidates; << 145 } << 146 return 0; // succeeded << 147 } << 148 << 149 // ------------------------------------------- << 150 G4bool G4UIparameter::CandidateCheck(const cha << 151 { << 152 if (parameterCandidate.empty()) { << 153 return true; << 154 } << 155 << 156 G4Tokenizer candidateTokenizer(parameterCand << 157 G4String aToken; << 158 while (!(aToken = candidateTokenizer()).empt << 159 if (aToken == newValue) { << 160 return true; << 161 } << 162 } << 163 G4cerr << "parameter value (" << newValue << << 164 G4cerr << " Candidates are:"; << 165 G4Tokenizer candidateListTokenizer(parameter << 166 while (!(aToken = candidateListTokenizer()). << 167 G4cerr << ' ' << aToken; << 168 } << 169 G4cerr << G4endl; << 170 << 171 return false; << 172 } << 173 << 174 // ------------------------------------------- << 175 G4bool G4UIparameter::TypeCheck(const char* ne << 176 { << 177 G4String newValueString(newValue); << 178 char type = (char)std::toupper(parameterType << 179 switch (type) { << 180 case 'D': << 181 if (!G4UIparsing::IsDouble(newValueStrin << 182 G4cerr << newValue << ": double value << 183 return false; << 184 } << 185 break; << 186 case 'I': << 187 if (!G4UIparsing::IsInt(newValueString.d << 188 G4cerr << newValue << ": integer expec << 189 return false; << 190 } << 191 break; << 192 case 'L': << 193 if (!G4UIparsing::IsInt(newValueString.d << 194 G4cerr << newValue << ": long int expe << 195 return false; << 196 } << 197 break; << 198 case 'S': << 199 break; << 200 case 'B': << 201 G4StrUtil::to_upper(newValueString); << 202 if (newValueString == "Y" || newValueStr << 203 || newValueString == "NO" || newValu << 204 || newValueString == "T" || newValue << 205 || newValueString == "FALSE") << 206 { << 207 return true; << 208 } << 209 << 210 G4cerr << newValue << ": bool expected." << 211 return false; << 212 << 213 default:; << 214 } << 215 return true; << 216 } 733 } >> 734 // ***** end of CheckNewValue() related code ****** >> 735 217 736