Public Member Functions | |
Macro (G4String _name, G4String _body) | |
G4String | commandName () |
int | command (BLArgumentVector &argv, BLArgumentMap &namedArgs) |
void | defineNamedArgs () |
Private Attributes | |
G4String | name |
G4String | body |
Static Private Attributes | |
static int | nExpansions = 0 |
Macro::Macro | ( | G4String | _name, | |
G4String | _body | |||
) | [inline] |
References BLCMDTYPE_OTHER, BLCommand::deleteCommand(), name, BLCommand::registerCommand(), and BLCommand::setSynopsis().
00045 : name(_name), body(_body) 00046 { deleteCommand(name); 00047 registerCommand(BLCMDTYPE_OTHER); 00048 setSynopsis("Defined macro."); 00049 }
G4String Macro::commandName | ( | ) | [inline, virtual] |
int Macro::command | ( | BLArgumentVector & | argv, | |
BLArgumentMap & | namedArgs | |||
) | [virtual] |
Implements BLCommand.
References body, commandName(), BLCommand::doCommand(), BLParam::expand(), IndentArg(), name, nExpansions, Param, and BLCommand::printError().
00104 { 00105 ++nExpansions; 00106 00107 if(namedArgs.size() > 0) { 00108 printError("macro: named arguments are ignored"); 00109 } 00110 00111 // print the macro command, with args 00112 G4String line(commandName()); 00113 do { line += " "; } while(line.size() < IndentArg.size()); 00114 for(unsigned i=0; i<argv.size(); ++i) 00115 {line += argv[i]; line += " ";} 00116 printf("%s\n",line.c_str()); 00117 00118 // ensure argv has 10 entries. Changes argv in caller, but nobody cares. 00119 while(argv.size() < 10) 00120 argv.push_back(""); 00121 00122 // replace: $0 with name, $1 - $9 with argv[0-8], $# with nExpansions 00123 G4String tmp = Param.expand(body); 00124 int place=0; 00125 for(;;) { 00126 G4String::size_type i = tmp.find('$',place); 00127 if(i == tmp.npos) break; 00128 if(tmp.c_str()[i+1] == '#') { 00129 char tmp2[16]; 00130 sprintf(tmp2,"%d",nExpansions); 00131 tmp.replace(i,2,tmp2); 00132 } else if(isdigit(tmp.c_str()[i+1])) { 00133 int j = tmp.c_str()[i+1] - '0'; 00134 if(j == 0) 00135 tmp.replace(i,2,name); 00136 else 00137 tmp.replace(i,2,argv[j-1]); 00138 } else { 00139 place = i+1; 00140 } 00141 } 00142 00143 // execute the macro body (now in tmp) 00144 G4String::size_type i = 0; 00145 while(i < tmp.size()) { 00146 G4String::size_type j = tmp.find('\n',i); 00147 if(j == tmp.npos) break; 00148 G4String tmp3(tmp.substr(i,j-i)); 00149 BLCommand::doCommand(tmp3); 00150 i = j + 1; 00151 } 00152 00153 return 0; 00154 }
void Macro::defineNamedArgs | ( | ) | [inline, virtual] |
G4String Macro::name [private] |
Referenced by command(), commandName(), and Macro().
G4String Macro::body [private] |
Referenced by command().
int Macro::nExpansions = 0 [static, private] |
Referenced by command().