// This code implementation is the intellectual property of // the GEANT4 collaboration. // // By copying, distributing or modifying the Program (or any work // based on the Program) you indicate your acceptance of this statement, // and all its terms. // // $Id: G4UIparameter.cc,v 1.4 1999/12/15 14:50:42 gunter Exp $ // GEANT4 tag $Name: geant4-02-00 $ // #include "G4UIparameter.hh" #include "G4UIcommandStatus.hh" #include "g4rw/ctoken.h" #include "G4ios.hh" G4UIparameter::G4UIparameter():paramERR(0) { G4String nullString; parameterName = nullString; parameterType = '\0'; omittable = false; parameterGuidance = nullString; defaultValue = nullString; parameterRange = nullString; currentAsDefaultFlag = false; parameterCandidate = nullString; widget = 0; } G4UIparameter::G4UIparameter(char theType):paramERR(0) { G4String nullString; parameterName = nullString; parameterType = theType; omittable = false; parameterGuidance = nullString; defaultValue = nullString; parameterRange = nullString; currentAsDefaultFlag = false; parameterCandidate = nullString; widget = 0; } G4UIparameter::G4UIparameter(const char * theName, char theType, G4bool theOmittable):paramERR(0) { parameterName = theName; parameterType = theType; omittable = theOmittable; G4String nullString; parameterGuidance = nullString; defaultValue = nullString; parameterRange = nullString; currentAsDefaultFlag = false; parameterCandidate = nullString; widget = 0; } G4UIparameter::~G4UIparameter() { } int G4UIparameter::operator==(const G4UIparameter &right) const { return ( this == &right ); } int G4UIparameter::operator!=(const G4UIparameter &right) const { return ( this != &right ); } void G4UIparameter::List() { G4cout << G4endl << "Parameter : " << parameterName << G4endl; if( ! parameterGuidance.isNull() ) G4cout << parameterGuidance << G4endl ; G4cout << " Parameter type : " << parameterType << G4endl; if(omittable) { G4cout << " Omittable : True" << G4endl; } else { G4cout << " Omittable : False" << G4endl; } if( currentAsDefaultFlag ) { G4cout << " Default value : taken from the current value" << G4endl; } else if( ! defaultValue.isNull() ) { G4cout << " Default value : " << defaultValue << G4endl; } if( ! parameterRange.isNull() ) G4cout << " Parameter range : " << parameterRange << G4endl; if( ! parameterCandidate.isNull() ) G4cout << " Candidates : " << parameterCandidate << G4endl; } void G4UIparameter::SetDefaultValue(G4int theDefaultValue) { char defVal[20]; G4std::ostrstream os(defVal,20); os << theDefaultValue << '\0'; defaultValue = defVal; } void G4UIparameter::SetDefaultValue(G4double theDefaultValue) { char defVal[20]; G4std::ostrstream os(defVal,20); os << theDefaultValue << '\0'; defaultValue = defVal; } // ---------- CheckNewValue() related routines ----------- #include #include "G4UItokenNum.hh" //#include "checkNewValue_debug.icc" //#define DEBUG 1 int G4UIparameter:: CheckNewValue( G4String newValue ) { if( TypeCheck(newValue) == 0) return fParameterUnreadable; if( ! parameterRange.isNull() ) { if( RangeCheck(newValue) == 0 ) return fParameterOutOfRange; } if( ! parameterCandidate.isNull() ) { if( CandidateCheck(newValue) == 0 ) return fParameterOutOfCandidates; } return 0; // succeeded } int G4UIparameter:: CandidateCheck(G4String newValue) { G4Tokenizer candidateTokenizer(parameterCandidate); G4String aToken; int iToken = 0; while( ! (aToken=candidateTokenizer()).isNull() ) { iToken++; if(aToken==newValue) return iToken; } G4cerr << "parameter value is not listed in the candidate List." << G4endl; return 0; } int G4UIparameter:: RangeCheck(G4String newValue) { yystype result; bp = 0; // reset buffer pointer for G4UIpGetc() const char* t = newValue; G4std::istrstream is((char*)t); char type = toupper( parameterType ); switch (type) { case 'D': { is >> newVal.D; } break; case 'I': { is >> newVal.I; } break; default: ; } // PrintToken(); // Print tokens (consumes all tokens) token= Yylex(); result = Expression(); if( paramERR == 1 ) return 0; if( result.type != CONSTINT) { G4cerr << "Illegal Expression in parameter range." << G4endl; return 0; } if ( result.I ) return 1; G4cerr << "parameter out of range: "<< parameterRange << G4endl; return 0; } int G4UIparameter:: TypeCheck(G4String newValue) { char type = toupper( parameterType ); switch(type) { case 'D': if( IsDouble(newValue.data())== 0) { G4cerr< maxDigits) { G4cerr <<"digit length exceeds"< arg2); opr= ">" ; break; case GE: result = ( arg1 >= arg2); opr= ">="; break; case LT: result = ( arg1 < arg2); opr= "<" ; break; case LE: result = ( arg1 <= arg2); opr= "<="; break; case EQ: result = ( arg1 == arg2); opr= "=="; break; case NE: result = ( arg1 != arg2); opr= "!="; break; default: G4cerr << "Parameter range: error at CompareInt" << G4endl; paramERR = 1; } #ifdef DEBUG G4cerr << "CompareInt " << arg1 << " " << opr << arg2 << " result: " << result << G4endl; #endif return result; } int G4UIparameter:: CompareDouble(double arg1, int op, double arg2) { int result; G4String opr; switch (op) { case GT: result = ( arg1 > arg2); opr= ">"; break; case GE: result = ( arg1 >= arg2); opr= ">="; break; case LT: result = ( arg1 < arg2); opr= "<"; break; case LE: result = ( arg1 <= arg2); opr= "<="; break; case EQ: result = ( arg1 == arg2); opr= "=="; break; case NE: result = ( arg1 != arg2); opr= "!="; break; default: G4cerr << "Parameter range: error at CompareDouble" << G4endl; paramERR = 1; } #ifdef DEBUG G4cerr << "CompareDouble " << arg1 <<" " << opr << " "<< arg2 << " result: " << result << G4endl; #endif return result; } // --------------------- utility functions -------------------------- tokenNum G4UIparameter:: Yylex() // reads input and returns token number KR486 { // (returns EOF) int c; G4String buf; while(( c= G4UIpGetc())==' '|| c=='\t' || c== '\n' ) ; if (c== EOF) return (tokenNum)EOF; // KR488 buf= ""; if (isdigit(c) || c== '.') { // I or D do { buf += G4String((unsigned char)c); c=G4UIpGetc(); } while (c=='.' || isdigit(c) || c=='e' || c=='E' || c=='+' || c=='-'); G4UIpUngetc(c); const char* t = buf; G4std::istrstream is((char*)t); if ( IsInt(buf.data(),20) ) { is >> yylval.I; return CONSTINT; } else if ( IsDouble(buf.data()) ) { is >> yylval.D; return CONSTDOUBLE; } else { G4cerr << buf<<": numeric format error."<': return (tokenNum) Follow('=', GE, GT); case '<': return (tokenNum) Follow('=', LE, LT); case '=': return (tokenNum) Follow('=', EQ, '='); case '!': return (tokenNum) Follow('=', NE, '!'); case '|': return (tokenNum) Follow('|', LOGICALOR, '|'); case '&': return (tokenNum) Follow('&', LOGICALAND, '&'); default: return (tokenNum) c; } } int G4UIparameter:: Follow(int expect, int ifyes, int ifno) { int c = G4UIpGetc(); if ( c== expect) return ifyes; G4UIpUngetc(c); return ifno; } //------------------ low level routines ----------------------------- int G4UIparameter:: G4UIpGetc() { // emulation of getc() int length = parameterRange.length(); if( bp < length) return parameterRange(bp++); else return EOF; } int G4UIparameter:: G4UIpUngetc(int c) { // emulation of ungetc() if (c<0) return -1; if (bp >0 && c == parameterRange(bp-1)) { --bp; } else { G4cerr << "G4UIpUngetc() failed." << G4endl; G4cerr << "bp="<