Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // This example is provided by the Geant4-DNA 26 // This example is provided by the Geant4-DNA collaboration 27 // Any report or published results obtained us 27 // Any report or published results obtained using the Geant4-DNA software 28 // shall cite the following Geant4-DNA collabo 28 // shall cite the following Geant4-DNA collaboration publication: 29 // Med. Phys. 37 (2010) 4692-4708 29 // Med. Phys. 37 (2010) 4692-4708 30 // The Geant4-DNA web site is available at htt 30 // The Geant4-DNA web site is available at http://geant4-dna.org 31 // 31 // 32 // Author: Mathieu Karamitros 32 // Author: Mathieu Karamitros 33 // 33 // >> 34 // $Id$ 34 // 35 // 35 /// \file CommandLineParser.cc 36 /// \file CommandLineParser.cc 36 /// \brief Implementation of the CommandLinePa 37 /// \brief Implementation of the CommandLineParser class 37 38 38 #include "CommandLineParser.hh" << 39 << 40 #include <iomanip> 39 #include <iomanip> >> 40 #include "CommandLineParser.hh" 41 41 42 using namespace std; 42 using namespace std; 43 using namespace G4DNAPARSER; 43 using namespace G4DNAPARSER; 44 44 45 CommandLineParser* CommandLineParser::fpInstan 45 CommandLineParser* CommandLineParser::fpInstance(0); 46 G4String Command::fNoOption = "NoOption"; 46 G4String Command::fNoOption = "NoOption"; 47 47 >> 48 48 //....oooOO0OOooo........oooOO0OOooo........oo 49 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 49 50 50 inline bool MATCH(const char* a, const char* b << 51 inline bool MATCH(const char *a, const char *b) 51 { 52 { 52 return strcmp(a, b) == 0; 53 return strcmp(a, b) == 0; 53 } 54 } 54 55 55 //....oooOO0OOooo........oooOO0OOooo........oo 56 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 56 57 57 CommandLineParser::CommandLineParser() 58 CommandLineParser::CommandLineParser() 58 { 59 { 59 // G4cout << "############ NEW PARSE ####### 60 // G4cout << "############ NEW PARSE ##########" << G4endl; 60 fpInstance = this; 61 fpInstance = this; 61 fOptionsWereSetup = false; 62 fOptionsWereSetup = false; 62 fMaxMarkerLength = 0; 63 fMaxMarkerLength = 0; 63 fMaxOptionNameLength = 0; 64 fMaxOptionNameLength = 0; 64 AddCommand("--help", Command::WithoutOption, 65 AddCommand("--help", Command::WithoutOption, "Print this help"); 65 AddCommand("-h", Command::WithoutOption, "Pr 66 AddCommand("-h", Command::WithoutOption, "Print this help"); 66 AddCommand("&", Command::WithoutOption); 67 AddCommand("&", Command::WithoutOption); 67 68 68 fVerbose = 0; 69 fVerbose = 0; 69 } 70 } 70 71 71 //....oooOO0OOooo........oooOO0OOooo........oo 72 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 72 73 73 CommandLineParser* CommandLineParser::GetParse 74 CommandLineParser* CommandLineParser::GetParser() 74 { 75 { 75 if (!fpInstance) new CommandLineParser; 76 if (!fpInstance) new CommandLineParser; 76 return fpInstance; 77 return fpInstance; 77 } 78 } 78 79 79 //....oooOO0OOooo........oooOO0OOooo........oo 80 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 80 81 81 CommandLineParser::~CommandLineParser() 82 CommandLineParser::~CommandLineParser() 82 { 83 { 83 std::map<G4String, Command*>::iterator it = 84 std::map<G4String, Command*>::iterator it = fCommandMap.begin(); 84 for (; it != fCommandMap.end(); it++) { << 85 for (; it != fCommandMap.end(); it++) >> 86 { 85 if (it->second) delete it->second; 87 if (it->second) delete it->second; 86 } 88 } 87 } 89 } 88 90 89 //....oooOO0OOooo........oooOO0OOooo........oo 91 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 90 92 91 void CommandLineParser::DeleteInstance() 93 void CommandLineParser::DeleteInstance() 92 { 94 { 93 if (fpInstance) { << 95 if (fpInstance) >> 96 { 94 delete fpInstance; 97 delete fpInstance; 95 fpInstance = 0; 98 fpInstance = 0; 96 } 99 } 97 } 100 } 98 101 99 //....oooOO0OOooo........oooOO0OOooo........oo 102 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 100 103 101 Command::Command(Command::Type commandType, co << 104 Command::Command(Command::Type commandType, >> 105 const G4String& description) 102 { 106 { 103 fType = commandType; 107 fType = commandType; 104 fDescription = description; 108 fDescription = description; 105 fActive = false; 109 fActive = false; 106 } 110 } 107 111 108 CommandWithOption::CommandWithOption(Command:: << 112 CommandWithOption::CommandWithOption(Command::Type commandType, 109 const G4S << 113 const G4String& description, 110 : Command(commandType, description) << 114 const G4String& defaultOption, >> 115 const G4String& optionName) : >> 116 Command(commandType, description) 111 { 117 { 112 fDefaultOption = defaultOption; 118 fDefaultOption = defaultOption; 113 fOptionName = optionName; 119 fOptionName = optionName; 114 fOption = ""; 120 fOption = ""; 115 } 121 } 116 122 >> 123 117 //....oooOO0OOooo........oooOO0OOooo........oo 124 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 118 125 119 int CommandLineParser::Parse(int& argc, char** << 126 int CommandLineParser::Parse(int& argc, char **argv) 120 { 127 { 121 // G4cout << "Parse " << G4endl; 128 // G4cout << "Parse " << G4endl; 122 static char null[1] = {""}; << 129 static char null[1] = { "" }; 123 int firstArgc = argc; 130 int firstArgc = argc; 124 131 125 for (int i = 1; i < firstArgc; i++) { << 132 for (int i = 1; i < firstArgc; i++) >> 133 { 126 Command* command = FindCommand(argv[i]); 134 Command* command = FindCommand(argv[i]); 127 if (command == 0) continue; 135 if (command == 0) continue; 128 136 129 if (fVerbose) G4cout << "Command : " << ar 137 if (fVerbose) G4cout << "Command : " << argv[i] << G4endl; 130 138 131 fOptionsWereSetup = true; 139 fOptionsWereSetup = true; 132 command->fActive = true; 140 command->fActive = true; 133 141 134 G4String marker(argv[i]); 142 G4String marker(argv[i]); 135 143 136 if (strcmp(argv[i], "-h") != 0 && strcmp(a << 144 if (strcmp(argv[i], "-h") != 0 && strcmp(argv[i], "--help") != 0) >> 145 { 137 argv[i] = null; 146 argv[i] = null; 138 } 147 } 139 148 140 if (command->fType == Command::WithOption) << 149 if (command->fType == Command::WithOption) >> 150 { 141 if (fVerbose) G4cout << "WithOption" << 151 if (fVerbose) G4cout << "WithOption" << G4endl; 142 152 143 if (i + 1 > firstArgc || argv[i + 1] == << 153 if(i+1 > firstArgc || argv[i+1]==0 || argv[i+1][0]=='-') 144 G4cerr << "An command line option is m << 154 { >> 155 G4cerr << "An command line option is missing for " >> 156 << marker << G4endl; 145 abort(); 157 abort(); 146 } 158 } 147 159 148 command->SetOption((const char*)strdup(a << 160 command->SetOption( (const char*) strdup(argv[i+1]) ); 149 argv[i + 1] = null; << 161 argv[i+1] = null; 150 i++; 162 i++; 151 } 163 } 152 else if (command->fType == Command::Option << 164 else if(command->fType == Command::OptionNotCompulsory) 153 if (fVerbose) G4cout << "OptionNotCompul << 165 { 154 << 166 if(fVerbose) 155 if (i + 1 < firstArgc) { << 167 G4cout <<"OptionNotCompulsory"<<G4endl; 156 G4String buffer = (const char*)strdup( << 157 168 158 if (buffer.empty() == false) { << 169 if(i+1 < firstArgc) 159 if (buffer.at(0) != '-' && buffer.at << 170 { 160 && buffer.at(0) != '|') << 171 G4String buffer = (const char*) strdup(argv[i+1]); >> 172 >> 173 if(buffer.empty() == false) >> 174 { >> 175 if(buffer.at(0) != '-' >> 176 && buffer.at(0) != '&' >> 177 && buffer.at(0) != '>' >> 178 && buffer.at(0) != '|') 161 { 179 { 162 if (fVerbose) { << 180 if(fVerbose) >> 181 { 163 G4cout << "facultative option is 182 G4cout << "facultative option is : " << buffer << G4endl; 164 } 183 } 165 184 166 command->SetOption((const char*)st << 185 command->SetOption( (const char*) strdup(argv[i+1]) ); 167 argv[i + 1] = null; << 186 argv[i+1] = null; 168 i++; 187 i++; 169 continue; 188 continue; 170 } 189 } 171 } 190 } 172 } 191 } 173 192 174 if (fVerbose) G4cout << "Option not set" << 193 if(fVerbose) >> 194 G4cout << "Option not set" << G4endl; 175 195 176 command->SetOption(""); 196 command->SetOption(""); 177 } 197 } 178 } 198 } 179 CorrectRemainingOptions(argc, argv); 199 CorrectRemainingOptions(argc, argv); 180 200 181 Command* commandLine(0); 201 Command* commandLine(0); 182 if ((commandLine = GetCommandIfActive("--hel << 202 if ((commandLine = GetCommandIfActive("--help")) || (commandLine = >> 203 GetCommandIfActive("-h"))) >> 204 { 183 G4cout << "Usage : " << argv[0] << " [OPTI 205 G4cout << "Usage : " << argv[0] << " [OPTIONS]" << G4endl; 184 PrintHelp(); 206 PrintHelp(); 185 return 1; 207 return 1; 186 } 208 } 187 209 188 return 0; 210 return 0; 189 } 211 } 190 212 191 //....oooOO0OOooo........oooOO0OOooo........oo 213 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 192 214 193 void CommandLineParser::PrintHelp() 215 void CommandLineParser::PrintHelp() 194 { 216 { 195 std::map<G4String, Command*>::iterator it; 217 std::map<G4String, Command*>::iterator it; 196 218 197 int maxFieldLength = fMaxMarkerLength + fMax 219 int maxFieldLength = fMaxMarkerLength + fMaxOptionNameLength + 4; 198 220 199 G4cout << "Options: " << G4endl; 221 G4cout << "Options: " << G4endl; 200 222 201 for (it = fCommandMap.begin(); it != fComman << 223 for (it = fCommandMap.begin(); it != fCommandMap.end(); it++) >> 224 { 202 Command* command = it->second; 225 Command* command = it->second; 203 if (command) { << 226 if (command) >> 227 { 204 G4cout << setw(maxFieldLength) << left; 228 G4cout << setw(maxFieldLength) << left; 205 229 206 G4String toPrint = it->first; 230 G4String toPrint = it->first; 207 231 208 if (toPrint == "&") { << 232 if (toPrint == "&") >> 233 { 209 continue; 234 continue; 210 } 235 } 211 else if (toPrint == "-h") << 236 else if (toPrint == "-h") continue; 212 continue; << 237 else if (toPrint == "--help") 213 else if (toPrint == "--help") { << 238 { 214 toPrint += ", -h"; 239 toPrint += ", -h"; 215 } 240 } 216 241 217 if (command->GetDefaultOption() != "") { << 242 if (command->GetDefaultOption() != "") >> 243 { 218 toPrint += " \"" + command->GetDefault 244 toPrint += " \"" + command->GetDefaultOption() + "\""; 219 } 245 } 220 246 221 G4cout << toPrint; 247 G4cout << toPrint; 222 248 223 G4cout << command->GetDescription() << G 249 G4cout << command->GetDescription() << G4endl; 224 } 250 } 225 } 251 } 226 } 252 } 227 253 228 //....oooOO0OOooo........oooOO0OOooo........oo 254 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 229 255 230 void CommandLineParser::CorrectRemainingOption << 256 void CommandLineParser::CorrectRemainingOptions(int& argc, char **argv) 231 { 257 { 232 // remove handled arguments from argument ar 258 // remove handled arguments from argument array 233 int j = 0; 259 int j = 0; 234 for (int i = 0; i < argc; i++) { << 260 for (int i = 0; i < argc; i++) 235 if (strcmp(argv[i], "")) { << 261 { >> 262 if (strcmp(argv[i], "")) >> 263 { 236 argv[j] = argv[i]; 264 argv[j] = argv[i]; 237 j++; 265 j++; 238 } 266 } 239 } 267 } 240 argc = j; 268 argc = j; 241 } 269 } 242 270 243 //....oooOO0OOooo........oooOO0OOooo........oo 271 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 244 272 245 void CommandLineParser::AddCommand(const G4Str << 273 void CommandLineParser::AddCommand(const G4String& marker, 246 const G4Str << 274 Command::Type type, 247 const G4Str << 275 const G4String& description, >> 276 const G4String& defaultOption, >> 277 const G4String& optionName) 248 { 278 { 249 // G4cout << "Add command : "<< marker << G4 279 // G4cout << "Add command : "<< marker << G4endl; 250 280 251 Command* command = 0; 281 Command* command = 0; 252 switch (type) { << 282 switch(type) >> 283 { 253 case Command::WithoutOption: 284 case Command::WithoutOption: 254 command = new Command(type, description) 285 command = new Command(type, description); 255 break; 286 break; 256 287 257 default: 288 default: 258 command = new CommandWithOption(type, de << 289 command = new CommandWithOption(type, 259 if ((int)defaultOption.length() > fMaxOp << 290 description, >> 291 defaultOption, >> 292 optionName); >> 293 if ((int) defaultOption.length() > fMaxOptionNameLength) 260 fMaxOptionNameLength = defaultOption.l 294 fMaxOptionNameLength = defaultOption.length(); 261 break; 295 break; 262 } 296 } 263 297 264 if ((int)marker.length() > fMaxMarkerLength) << 298 if ((int) marker.length() > fMaxMarkerLength) fMaxMarkerLength = >> 299 marker.length(); 265 fCommandMap.insert(make_pair(marker, command 300 fCommandMap.insert(make_pair(marker, command)); 266 } 301 } 267 302 268 /* 303 /* 269 // Add one command but multiple markers 304 // Add one command but multiple markers 270 void Parser::AddCommand(vector<G4String> marke 305 void Parser::AddCommand(vector<G4String> markers, 271 CommandType type, 306 CommandType type, 272 const G4String& descri 307 const G4String& description, 273 const G4String& option 308 const G4String& optionName) 274 { 309 { 275 // G4cout << "Add command : "<< marker << G4 310 // G4cout << "Add command : "<< marker << G4endl; 276 Command* command = new Command(type, descrip 311 Command* command = new Command(type, description, optionName); 277 312 278 for (size_t i = 0; i < markers.size; i++) 313 for (size_t i = 0; i < markers.size; i++) 279 { 314 { 280 G4String marker = markers[i]; 315 G4String marker = markers[i]; 281 if ((int) marker.length() > fMaxMarkerLeng 316 if ((int) marker.length() > fMaxMarkerLength) 282 { 317 { 283 fMaxMarkerLength = marker.length(); 318 fMaxMarkerLength = marker.length(); 284 } 319 } 285 if ((int) optionName.length() > fMaxOption 320 if ((int) optionName.length() > fMaxOptionNameLength) 286 { 321 { 287 fMaxOptionNameLength = optionName.length 322 fMaxOptionNameLength = optionName.length(); 288 } 323 } 289 fCommandMap.insert(make_pair(marker, comma 324 fCommandMap.insert(make_pair(marker, command)); 290 } 325 } 291 } 326 } 292 */ << 327 */ 293 328 294 //....oooOO0OOooo........oooOO0OOooo........oo 329 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 295 330 296 Command* CommandLineParser::FindCommand(const 331 Command* CommandLineParser::FindCommand(const G4String& marker) 297 { 332 { 298 std::map<G4String, Command*>::iterator it = 333 std::map<G4String, Command*>::iterator it = fCommandMap.find(marker); 299 if (it == fCommandMap.end()) { << 334 if (it == fCommandMap.end()) >> 335 { 300 // G4cerr << "command not found" << G4endl 336 // G4cerr << "command not found" << G4endl; 301 return 0; 337 return 0; 302 } 338 } 303 return it->second; 339 return it->second; 304 } 340 } 305 341 306 //....oooOO0OOooo........oooOO0OOooo........oo 342 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 307 343 308 Command* CommandLineParser::GetCommandIfActive << 344 Command* CommandLineParser::GetCommandIfActive(const G4String &marker) 309 { 345 { 310 Command* command = FindCommand(marker); 346 Command* command = FindCommand(marker); 311 if (command) { << 347 if (command) >> 348 { 312 // G4cout << "Command found : "<< marker < 349 // G4cout << "Command found : "<< marker << G4endl; 313 350 314 if (command->fActive) { << 351 if (command->fActive) >> 352 { 315 // G4cout << "Command Active" << G4endl; 353 // G4cout << "Command Active" << G4endl; 316 return command; 354 return command; 317 } 355 } 318 // else 356 // else 319 // G4cout <<"Command not active" << G4end 357 // G4cout <<"Command not active" << G4endl; 320 } 358 } 321 else { << 359 else >> 360 { 322 G4ExceptionDescription description; 361 G4ExceptionDescription description; 323 description << "You try to retrieve a comm << 362 description << "You try to retrieve a command that was not registered : " 324 G4Exception("CommandLineParser::GetCommand << 363 << marker << G4endl; 325 description, ""); << 364 G4Exception("CommandLineParser::GetCommandIfActive", >> 365 "COMMAND LINE NOT DEFINED", FatalException, description, ""); 326 // If you are using this class outside of 366 // If you are using this class outside of Geant4, use exit(-1) instead 327 // exit(-1); << 367 //exit(-1); 328 } 368 } 329 return 0; 369 return 0; 330 } 370 } 331 371 332 //....oooOO0OOooo........oooOO0OOooo........oo 372 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 333 373 334 bool CommandLineParser::CheckIfNotHandledOptio 374 bool CommandLineParser::CheckIfNotHandledOptionsExists(int& argc, char** argv) 335 { 375 { 336 if (argc > 0) { << 376 if (argc > 0) >> 377 { 337 G4bool kill = false; 378 G4bool kill = false; 338 for (G4int i = 1; i < argc; i++) { << 379 for (G4int i = 1; i < argc; i++) 339 if (strcmp(argv[i], "")) { << 380 { >> 381 if (strcmp(argv[i], "")) >> 382 { 340 kill = true; 383 kill = true; 341 G4cerr << "Unknown argument : " << arg 384 G4cerr << "Unknown argument : " << argv[i] << "\n"; 342 } 385 } 343 } 386 } 344 if (kill) { << 387 if (kill) 345 G4cerr << "The option " << argv[0] << " << 388 { >> 389 G4cerr << "The option " << argv[0] >> 390 << " is not handled this programme." >> 391 << G4endl; 346 G4cout << "Usage : " << argv[0] << " [OP 392 G4cout << "Usage : " << argv[0] << " [OPTIONS]" << G4endl; 347 PrintHelp(); 393 PrintHelp(); 348 return true; // KILL APPLICATION << 394 return true; // KILL APPLICATION 349 } 395 } 350 } 396 } 351 return false; 397 return false; 352 } 398 } 353 399