MyBLFuncs Class Reference

#include <MyBLFuncs.hh>

List of all members.

Public Member Functions

 MyBLFuncs ()
int parseArgs (const G4String &line, MyBLArgumentVector &argv, MyBLArgumentMap &namedArgs)
G4String getString (G4String name)
void setParam (G4String name, G4String value)
void setParam (G4String name, G4double value)
void setParam (G4String name, G4int value)
G4String expand (G4String str)

Private Types

enum  TokenType { NONE, ARGNAME, ARGVALUE }

Static Private Member Functions

static void init ()
static G4String nextToken (const G4String &line, G4String::size_type &place, TokenType &type)

Static Private Attributes

static std::map< G4String,
G4String > * 
paramMap

Detailed Description

Definition at line 20 of file MyBLFuncs.hh.


Member Enumeration Documentation

enum MyBLFuncs::TokenType [private]
Enumerator:
NONE 
ARGNAME 
ARGVALUE 

Definition at line 37 of file MyBLFuncs.hh.

00037 { NONE, ARGNAME, ARGVALUE };


Constructor & Destructor Documentation

MyBLFuncs::MyBLFuncs (  )  [inline]

Definition at line 25 of file MyBLFuncs.hh.

References init().

00025 { init();}


Member Function Documentation

G4String MyBLFuncs::expand ( G4String  str  ) 

Definition at line 109 of file MyBLFuncs.cc.

References getString().

Referenced by parseArgs().

00109                                        {
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         // leave $1 - $9 and $# alone (for define command)
00124         if(isdigit(str[place]) || str[place] == '#') {
00125             out += "$";
00126             continue;
00127         }
00128         // replace $$ with $ (delayed evaluation of $param)
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 }

G4String MyBLFuncs::getString ( G4String  name  ) 

Definition at line 152 of file MyBLFuncs.cc.

References init(), paramMap, and setParam().

Referenced by expand().

00152                                            {
00153     init();
00154     if((*paramMap).count(name) == 0) {
00155         // define it from the environment, if possible
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];                         // "" if not found
00166 }

void MyBLFuncs::init (  )  [static, private]

Definition at line 10 of file MyBLFuncs.cc.

References paramMap.

Referenced by getString(), MyBLFuncs(), and setParam().

00010                      {
00011     if (!paramMap) paramMap = new std::map<G4String,G4String>;
00012     //if (!paramHelpText) paramHelpText = new std::map<G4String,G4String>;
00013 }

G4String MyBLFuncs::nextToken ( const G4String &  line,
G4String::size_type &  place,
TokenType type 
) [static, private]

Definition at line 44 of file MyBLFuncs.cc.

References ARGNAME, ARGVALUE, and NONE.

Referenced by parseArgs().

00045                  {
00046     // (add +- for particlecolor pi+=1,1,1)
00047     static const char namechars[] = ",+-ABCDEFGHIJKLMNOPQRSTUVWXYZ_"
00048         "abcdefghijklmnopqrstuvwxyz0123456789";
00049     G4String::size_type i;
00050 
00051     // check if previous token was ARGNAME
00052     if(line[place] == '=') {
00053         ++place;
00054         goto value;
00055     }
00056 
00057     // skip initial whitespace
00058     while(place < line.size() && isspace(line[place])) ++place;
00059 
00060     // check for ARGNAME
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     // find next whitespace
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 }

int MyBLFuncs::parseArgs ( const G4String &  line,
MyBLArgumentVector argv,
MyBLArgumentMap namedArgs 
)

Definition at line 17 of file MyBLFuncs.cc.

References ARGNAME, ARGVALUE, expand(), nextToken(), and NONE.

Referenced by MyBLFieldMap::readFile().

00018                             {
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 }

void MyBLFuncs::setParam ( G4String  name,
G4int  value 
)

Definition at line 185 of file MyBLFuncs.cc.

References setParam().

00185                                                    {
00186     char tmp[32];
00187     sprintf(tmp,"%d",value);
00188     setParam(name,tmp);
00189 }

void MyBLFuncs::setParam ( G4String  name,
G4double  value 
)

Definition at line 177 of file MyBLFuncs.cc.

References setParam().

00177                                                       {
00178     char tmp[32];
00179     sprintf(tmp,"%g",value);
00180     setParam(name,tmp);
00181 }

void MyBLFuncs::setParam ( G4String  name,
G4String  value 
)

Definition at line 170 of file MyBLFuncs.cc.

References init().

Referenced by getString(), and setParam().

00170                                                       {
00171     init();
00172     (*paramMap)[name] = value;
00173 }


Member Data Documentation

std::map< G4String, G4String > * MyBLFuncs::paramMap [static, private]

Definition at line 21 of file MyBLFuncs.hh.

Referenced by getString(), and init().


The documentation for this class was generated from the following files:

Generated on 15 Jun 2016 for g4sim by  doxygen 1.6.1