// // ******************************************************************** // * License and Disclaimer * // * * // * The Geant4 software is copyright of the Copyright Holders of * // * the Geant4 Collaboration. It is provided under the terms and * // * conditions of the Geant4 Software License, included in the file * // * LICENSE and available at http://cern.ch/geant4/license . These * // * include a list of copyright holders. * // * * // * Neither the authors of this software system, nor their employing * // * institutes,nor the agencies providing financial support for this * // * work make any representation or warranty, express or implied, * // * regarding this software system or assume any liability for its * // * use. Please see the license in the file LICENSE and URL above * // * for the full disclaimer and the limitation of liability. * // * * // * This code implementation is the result of the scientific and * // * technical work of the GEANT4 collaboration. * // * By using, copying, modifying or distributing the software (or * // * any work based on the software) you agree to acknowledge its * // * use in resulting scientific publications, and indicate your * // * acceptance of all terms of the Geant4 Software license. * // ******************************************************************** // // // $Id: G4UIparameter.cc,v 1.14 2006/06/29 19:09:09 gunter Exp $ // GEANT4 tag $Name: geant4-09-02 $ // #include "G4UIparameter.hh" #include "G4UIcommandStatus.hh" #include "G4Tokenizer.hh" #include "G4ios.hh" #include 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() { } G4int G4UIparameter::operator==(const G4UIparameter &right) const { return ( this == &right ); } G4int 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) { std::ostringstream os; os << theDefaultValue; defaultValue = os.str(); } void G4UIparameter::SetDefaultValue(G4double theDefaultValue) { std::ostringstream os; os << theDefaultValue; defaultValue = os.str(); } // ---------- CheckNewValue() related routines ----------- #include #include "G4UItokenNum.hh" //#include "checkNewValue_debug.icc" //#define DEBUG 1 G4int G4UIparameter:: CheckNewValue(const char* 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 } G4int G4UIparameter:: CandidateCheck(const char* newValue) { G4Tokenizer candidateTokenizer(parameterCandidate); G4String aToken; G4int 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; } G4int G4UIparameter:: RangeCheck(const char* newValue) { yystype result; bp = 0; // reset buffer pointer for G4UIpGetc() std::istringstream is(newValue); 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; } G4int G4UIparameter:: TypeCheck(const char* newValue) { G4String newValueString(newValue); char type = toupper( parameterType ); switch(type) { case 'D': if( IsDouble(newValueString.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; } G4int G4UIparameter:: CompareDouble(G4double arg1, G4int op, G4double arg2) { G4int result=-1; 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) G4int 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; std::istringstream is(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; } } G4int G4UIparameter:: Follow(G4int expect, G4int ifyes, G4int ifno) { G4int c = G4UIpGetc(); if ( c== expect) return ifyes; G4UIpUngetc(c); return ifno; } //------------------ low level routines ----------------------------- G4int G4UIparameter:: G4UIpGetc() { // emulation of getc() G4int length = parameterRange.length(); if( bp < length) return parameterRange(bp++); else return EOF; } G4int G4UIparameter:: G4UIpUngetc(G4int c) { // emulation of ungetc() if (c<0) return -1; if (bp >0 && c == parameterRange(bp-1)) { --bp; } else { G4cerr << "G4UIpUngetc() failed." << G4endl; G4cerr << "bp="<