00001
00002
00003 #include "MyBLFuncs.hh"
00004
00005 std::map<G4String,G4String> *MyBLFuncs::paramMap;
00006
00007
00008 MyBLFuncs BLFuncs;
00009
00010 void MyBLFuncs::init() {
00011 if (!paramMap) paramMap = new std::map<G4String,G4String>;
00012
00013 }
00014
00015
00016
00017 int MyBLFuncs::parseArgs(const G4String &line, MyBLArgumentVector &argv,
00018 MyBLArgumentMap &namedArgs) {
00019 G4String::size_type place = 0;
00020 while(place < line.size()) {
00021 TokenType type;
00022 G4String arg = nextToken(line,place,type), val;
00023 switch(type) {
00024 case NONE:
00025 break;
00026 case ARGNAME:
00027 val = nextToken(line,place,type);
00028 if(type != ARGVALUE) {
00029 G4cerr << "Syntax error parsing arguments" << G4endl;
00030 return -1;
00031 }
00032 namedArgs[arg] = expand(val);
00033 break;
00034 case ARGVALUE:
00035 argv.push_back(expand(arg));
00036 break;
00037 }
00038 }
00039 return 0;
00040 }
00041
00042
00043
00044 G4String MyBLFuncs::nextToken(const G4String& line, G4String::size_type& place,
00045 TokenType& type) {
00046
00047 static const char namechars[] = ",+-ABCDEFGHIJKLMNOPQRSTUVWXYZ_"
00048 "abcdefghijklmnopqrstuvwxyz0123456789";
00049 G4String::size_type i;
00050
00051
00052 if(line[place] == '=') {
00053 ++place;
00054 goto value;
00055 }
00056
00057
00058 while(place < line.size() && isspace(line[place])) ++place;
00059
00060
00061 if(isalnum(line[place]) || line[place] == '_') {
00062 i = line.find_first_not_of(namechars,place);
00063 if(i > place && i < line.size() && line[i] == '=' &&
00064 line[i+1] != '=') {
00065 G4String retval = line.substr(place,i-place);
00066 place = i;
00067 type = ARGNAME;
00068 return retval;
00069 }
00070 }
00071 value:
00072 if(line[place] == '"') {
00073 ++place;
00074 i = line.find('"',place);
00075 if(i <line.size()) {
00076 G4String retval = line.substr(place,i-place);
00077 place = i + 1;
00078 type = ARGVALUE;
00079 return retval;
00080 }
00081 }
00082 else if(line[place] == '\'') {
00083 ++place;
00084 i = line.find('\'',place);
00085 if(i <line.size()) {
00086 G4String retval = line.substr(place,i-place);
00087 place = i + 1;
00088 type = ARGVALUE;
00089 return retval;
00090 }
00091 }
00092
00093 if(place >= line.size()) {
00094 type = NONE;
00095 return "";
00096 }
00097
00098
00099 G4String::size_type start = place;
00100 while(place < line.size() && !isspace(line[place])) ++place;
00101
00102 type = ARGVALUE;
00103
00104 return line.substr(start,place-start);
00105 }
00106
00107
00108
00109 G4String MyBLFuncs::expand(G4String str) {
00110 static G4String nameChars("ABCDEFGHIJKLMNOPQRSTUVWXYZ_"
00111 "abcdefghijklmnopqrstuvwxyz0123456789");
00112 G4String out;
00113
00114 G4String::size_type place=0;
00115 while(place < str.size()) {
00116 G4String::size_type i=str.find('$',place);
00117 if(i == str.npos) {
00118 out += str.substr(place);
00119 break;
00120 }
00121 out += str.substr(place,i-place);
00122 place = i + 1;
00123
00124 if(isdigit(str[place]) || str[place] == '#') {
00125 out += "$";
00126 continue;
00127 }
00128
00129 if(str[place] == '$') {
00130 out += "$";
00131 ++place;
00132 continue;
00133 }
00134 G4String::size_type j=str.find_first_not_of(nameChars,place);
00135 if(j == str.npos) j = str.size();
00136 G4String name=str.substr(place,j-place);
00137 if(j == place || isdigit(str[place])) {
00138 G4cerr << "MyBLFuncs::expand ERROR: Invalid parameter name "
00139 << name << G4endl;
00140 }
00141 else {
00142 out += getString(name);
00143 }
00144 place = j;
00145 }
00146
00147 return out;
00148 }
00149
00150
00151
00152 G4String MyBLFuncs::getString(G4String name) {
00153 init();
00154 if((*paramMap).count(name) == 0) {
00155
00156 char *p = getenv(name.c_str());
00157 if(p) {
00158 setParam(name,p);
00159 }
00160 else {
00161 G4cerr << "MyBLFuncs::getString ERROR: Unknown parameter "
00162 << name << G4endl;
00163 }
00164 }
00165 return (*paramMap)[name];
00166 }
00167
00168
00169
00170 void MyBLFuncs::setParam(G4String name, G4String value) {
00171 init();
00172 (*paramMap)[name] = value;
00173 }
00174
00175
00176
00177 void MyBLFuncs::setParam(G4String name, G4double value) {
00178 char tmp[32];
00179 sprintf(tmp,"%g",value);
00180 setParam(name,tmp);
00181 }
00182
00183
00184
00185 void MyBLFuncs::setParam(G4String name, G4int value) {
00186 char tmp[32];
00187 sprintf(tmp,"%d",value);
00188 setParam(name,tmp);
00189 }