// AK20100820 Various functions from G4beamline v2.03 #include "IBLFuncs.hxx" namespace COMET { std::map *IBLFuncs::paramMap; IBLFuncs BLFuncs; // the global BLFuncs object void IBLFuncs::init() { if (!paramMap) paramMap = new std::map; } // AK20100820 parseArgs is from BLCommand.cc int IBLFuncs::parseArgs(const std::string &line, MyBLArgumentVector &argv, MyBLArgumentMap &namedArgs) { std::string::size_type place = 0; while(place < line.size()) { TokenType type; std::string arg = nextToken(line,place,type), val; switch(type) { case NONE: break; case ARGNAME: val = nextToken(line,place,type); if(type != ARGVALUE) { COMETError("Syntax error parsing arguments"); return -1; } namedArgs[arg] = expand(val); break; case ARGVALUE: argv.push_back(expand(arg)); break; } } return 0; } // AK20100820 nextToken is from BLCommand.cc std::string IBLFuncs::nextToken(const std::string& line, std::string::size_type& place, TokenType& type) { // (add +- for particlecolor pi+=1,1,1) static const char namechars[] = ",+-ABCDEFGHIJKLMNOPQRSTUVWXYZ_" "abcdefghijklmnopqrstuvwxyz0123456789"; std::string::size_type i; // check if previous token was ARGNAME if(line[place] == '=') { ++place; goto value; } // skip initial whitespace while(place < line.size() && isspace(line[place])) ++place; // check for ARGNAME if(isalnum(line[place]) || line[place] == '_') { i = line.find_first_not_of(namechars,place); if(i > place && i < line.size() && line[i] == '=' && line[i+1] != '=') { std::string retval = line.substr(place,i-place); place = i; type = ARGNAME; return retval; } } value: if(line[place] == '"') { ++place; i = line.find('"',place); if(i = line.size()) { type = NONE; return ""; } // find next whitespace std::string::size_type start = place; while(place < line.size() && !isspace(line[place])) ++place; type = ARGVALUE; return line.substr(start,place-start); } // AK20100820 expand is from BLParam.cc std::string IBLFuncs::expand(std::string str) { static std::string nameChars("ABCDEFGHIJKLMNOPQRSTUVWXYZ_" "abcdefghijklmnopqrstuvwxyz0123456789"); std::string out; std::string::size_type place=0; while(place < str.size()) { std::string::size_type i=str.find('$',place); if(i == str.npos) { out += str.substr(place); break; } out += str.substr(place,i-place); place = i + 1; // leave $1 - $9 and $# alone (for define command) if(isdigit(str[place]) || str[place] == '#') { out += "$"; continue; } // replace $$ with $ (delayed evaluation of $param) if(str[place] == '$') { out += "$"; ++place; continue; } std::string::size_type j=str.find_first_not_of(nameChars,place); if(j == str.npos) j = str.size(); std::string name=str.substr(place,j-place); if(j == place || isdigit(str[place])) { COMETError("IBLFuncs::expand ERROR: Invalid parameter name " << name); } else { out += getString(name); } place = j; } return out; } // AK20100820 getString is from BLParam.cc std::string IBLFuncs::getString(std::string name) { init(); if((*paramMap).count(name) == 0) { // define it from the environment, if possible char *p = getenv(name.c_str()); if(p) { setParam(name,p); } else { COMETError( "IBLFuncs::getString ERROR: Unknown parameter " << name); } } return (*paramMap)[name]; // "" if not found } // AK20100820 setParam is from BLParam.cc void IBLFuncs::setParam(std::string name, std::string value) { init(); (*paramMap)[name] = value; } // AK20100820 setParam is from BLParam.cc void IBLFuncs::setParam(std::string name, Double_t value) { char tmp[32]; sprintf(tmp,"%g",value); setParam(name,tmp); } // AK20100820 setParam is from BLParam.cc void IBLFuncs::setParam(std::string name, Int_t value) { char tmp[32]; sprintf(tmp,"%d",value); setParam(name,tmp); } }