Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-23 09:21:49

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 // This example is provided by the Geant4-DNA collaboration
0027 // Any report or published results obtained using the Geant4-DNA software
0028 // shall cite the following Geant4-DNA collaboration publication:
0029 // Med. Phys. 37 (2010) 4692-4708
0030 // J. Comput. Phys. 274 (2014) 841-882
0031 // The Geant4-DNA web site is available at http://geant4-dna.org
0032 //
0033 // Author: Mathieu Karamitros
0034 //
0035 //
0036 /// \file CommandLineParser.cc
0037 /// \brief Implementation of the CommandLineParser class
0038 
0039 #include "CommandLineParser.hh"
0040 
0041 #include <iomanip>
0042 
0043 using namespace std;
0044 using namespace G4DNAPARSER;
0045 
0046 CommandLineParser* CommandLineParser::fpInstance(0);
0047 G4String Command::fNoOption = "NoOption";
0048 
0049 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0050 
0051 inline bool MATCH(const char* a, const char* b)
0052 {
0053   return strcmp(a, b) == 0;
0054 }
0055 
0056 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0057 
0058 CommandLineParser::CommandLineParser()
0059 {
0060   // G4cout << "############ NEW PARSE ##########" << G4endl;
0061   fpInstance = this;
0062   fOptionsWereSetup = false;
0063   fMaxMarkerLength = 0;
0064   fMaxOptionNameLength = 0;
0065   AddCommand("--help", Command::WithoutOption, "Print this help");
0066   AddCommand("-h", Command::WithoutOption, "Print this help");
0067   AddCommand("&", Command::WithoutOption);
0068 
0069   fVerbose = 0;
0070 }
0071 
0072 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0073 
0074 CommandLineParser* CommandLineParser::GetParser()
0075 {
0076   if (!fpInstance) new CommandLineParser;
0077   return fpInstance;
0078 }
0079 
0080 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0081 
0082 CommandLineParser::~CommandLineParser()
0083 {
0084   std::map<G4String, Command*>::iterator it = fCommandMap.begin();
0085   for (; it != fCommandMap.end(); it++) {
0086     if (it->second) delete it->second;
0087   }
0088 }
0089 
0090 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0091 
0092 void CommandLineParser::DeleteInstance()
0093 {
0094   if (fpInstance) {
0095     delete fpInstance;
0096     fpInstance = 0;
0097   }
0098 }
0099 
0100 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0101 
0102 Command::Command(Command::Type commandType, const G4String& description)
0103 {
0104   fType = commandType;
0105   fDescription = description;
0106   fActive = false;
0107 }
0108 
0109 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0110 
0111 CommandWithOption::CommandWithOption(Command::Type commandType, const G4String& description,
0112                                      const G4String& defaultOption, const G4String& optionName)
0113   : Command(commandType, description)
0114 {
0115   fDefaultOption = defaultOption;
0116   fOptionName = optionName;
0117   fOption = "";
0118 }
0119 
0120 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0121 
0122 int CommandLineParser::Parse(int& argc, char** argv)
0123 {
0124   //    G4cout << "Parse " << G4endl;
0125   static char null[1] = {""};
0126   int firstArgc = argc;
0127 
0128   for (int i = 1; i < firstArgc; i++) {
0129     Command* command = FindCommand(argv[i]);
0130     if (command == 0) continue;
0131 
0132     if (fVerbose) G4cout << "Command : " << argv[i] << G4endl;
0133 
0134     fOptionsWereSetup = true;
0135     command->fActive = true;
0136 
0137     G4String marker(argv[i]);
0138 
0139     if (strcmp(argv[i], "-h") != 0 && strcmp(argv[i], "--help") != 0) {
0140       argv[i] = null;
0141     }
0142 
0143     if (command->fType == Command::WithOption) {
0144       if (fVerbose) G4cout << "WithOption" << G4endl;
0145 
0146       if (i + 1 > firstArgc || argv[i + 1] == 0 || argv[i + 1][0] == '-') {
0147         G4cerr << "An command line option is missing for " << marker << G4endl;
0148         abort();
0149       }
0150 
0151       command->SetOption((const char*)strdup(argv[i + 1]));
0152       argv[i + 1] = null;
0153       i++;
0154     }
0155     else if (command->fType == Command::OptionNotCompulsory) {
0156       if (fVerbose) G4cout << "OptionNotCompulsory" << G4endl;
0157 
0158       if (i + 1 < firstArgc) {
0159         G4String buffer = (const char*)strdup(argv[i + 1]);
0160 
0161         if (buffer.empty() == false) {
0162           if (buffer.at(0) != '-' && buffer.at(0) != '&' && buffer.at(0) != '>'
0163               && buffer.at(0) != '|')
0164           {
0165             if (fVerbose) {
0166               G4cout << "facultative option is : " << buffer << G4endl;
0167             }
0168 
0169             command->SetOption((const char*)strdup(argv[i + 1]));
0170             argv[i + 1] = null;
0171             i++;
0172             continue;
0173           }
0174         }
0175       }
0176 
0177       if (fVerbose) G4cout << "Option not set" << G4endl;
0178 
0179       command->SetOption("");
0180     }
0181   }
0182   CorrectRemainingOptions(argc, argv);
0183 
0184   Command* commandLine(0);
0185   if ((commandLine = GetCommandIfActive("--help")) || (commandLine = GetCommandIfActive("-h"))) {
0186     G4cout << "Usage : " << argv[0] << " [OPTIONS]" << G4endl;
0187     PrintHelp();
0188     return 1;
0189   }
0190 
0191   return 0;
0192 }
0193 
0194 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0195 
0196 void CommandLineParser::PrintHelp()
0197 {
0198   std::map<G4String, Command*>::iterator it;
0199 
0200   int maxFieldLength = fMaxMarkerLength + fMaxOptionNameLength + 4;
0201 
0202   G4cout << "Options: " << G4endl;
0203 
0204   for (it = fCommandMap.begin(); it != fCommandMap.end(); it++) {
0205     Command* command = it->second;
0206     if (command) {
0207       G4cout << setw(maxFieldLength) << left;
0208 
0209       G4String toPrint = it->first;
0210 
0211       if (toPrint == "&") {
0212         continue;
0213       }
0214       else if (toPrint == "-h")
0215         continue;
0216       else if (toPrint == "--help") {
0217         toPrint += ", -h";
0218       }
0219 
0220       if (command->GetDefaultOption() != "") {
0221         toPrint += " \"" + command->GetDefaultOption() + "\"";
0222       }
0223 
0224       G4cout << toPrint;
0225 
0226       G4cout << command->GetDescription() << G4endl;
0227     }
0228   }
0229 }
0230 
0231 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0232 
0233 void CommandLineParser::CorrectRemainingOptions(int& argc, char** argv)
0234 {
0235   // remove handled arguments from argument array
0236   int j = 0;
0237   for (int i = 0; i < argc; i++) {
0238     if (strcmp(argv[i], "")) {
0239       argv[j] = argv[i];
0240       j++;
0241     }
0242   }
0243   argc = j;
0244 }
0245 
0246 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0247 
0248 void CommandLineParser::AddCommand(const G4String& marker, Command::Type type,
0249                                    const G4String& description, const G4String& defaultOption,
0250                                    const G4String& optionName)
0251 {
0252   // G4cout << "Add command : "<< marker << G4endl;
0253 
0254   Command* command = 0;
0255   switch (type) {
0256     case Command::WithoutOption:
0257       command = new Command(type, description);
0258       break;
0259 
0260     default:
0261       command = new CommandWithOption(type, description, defaultOption, optionName);
0262       if ((int)defaultOption.length() > fMaxOptionNameLength)
0263         fMaxOptionNameLength = defaultOption.length();
0264       break;
0265   }
0266 
0267   if ((int)marker.length() > fMaxMarkerLength) fMaxMarkerLength = marker.length();
0268   fCommandMap.insert(make_pair(marker, command));
0269 }
0270 
0271 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0272 
0273 /*
0274 // Add one command but multiple markers
0275 void Parser::AddCommand(vector<G4String> markers,
0276                         CommandType type,
0277                         const G4String& description,
0278                         const G4String& optionName)
0279 {
0280   // G4cout << "Add command : "<< marker << G4endl;
0281   Command* command = new Command(type, description, optionName);
0282 
0283   for (size_t i = 0; i < markers.size; i++)
0284   {
0285     G4String marker = markers[i];
0286     if ((int) marker.length() > fMaxMarkerLength)
0287     {
0288       fMaxMarkerLength = marker.length();
0289     }
0290     if ((int) optionName.length() > fMaxOptionNameLength)
0291     {
0292       fMaxOptionNameLength = optionName.length();
0293     }
0294     fCommandMap.insert(make_pair(marker, command));
0295   }
0296 }
0297 */
0298 
0299 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0300 
0301 Command* CommandLineParser::FindCommand(const G4String& marker)
0302 {
0303   std::map<G4String, Command*>::iterator it = fCommandMap.find(marker);
0304   if (it == fCommandMap.end()) {
0305     // G4cerr << "command not found" << G4endl;
0306     return 0;
0307   }
0308   return it->second;
0309 }
0310 
0311 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0312 
0313 Command* CommandLineParser::GetCommandIfActive(const G4String& marker)
0314 {
0315   Command* command = FindCommand(marker);
0316   if (command) {
0317     // G4cout << "Command found : "<< marker << G4endl;
0318 
0319     if (command->fActive) {
0320       // G4cout << "Command Active" << G4endl;
0321       return command;
0322     }
0323     // else
0324     //  G4cout <<"Command not active" << G4endl;
0325   }
0326   else {
0327     G4ExceptionDescription description;
0328     description << "You try to retrieve a command that was not registered : " << marker << G4endl;
0329     G4Exception("CommandLineParser::GetCommandIfActive", "COMMAND LINE NOT DEFINED", FatalException,
0330                 description, "");
0331     // If you are using this class outside of Geant4, use exit(-1) instead
0332     // exit(-1);
0333   }
0334   return 0;
0335 }
0336 
0337 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0338 
0339 bool CommandLineParser::CheckIfNotHandledOptionsExists(int& argc, char** argv)
0340 {
0341   if (argc > 0) {
0342     G4bool kill = false;
0343     for (G4int i = 1; i < argc; i++) {
0344       if (strcmp(argv[i], "")) {
0345         kill = true;
0346         G4cerr << "Unknown argument : " << argv[i] << "\n";
0347       }
0348     }
0349     if (kill) {
0350       G4cerr << "The option " << argv[0] << " is not handled this programme." << G4endl;
0351       G4cout << "Usage : " << argv[0] << " [OPTIONS]" << G4endl;
0352       PrintHelp();
0353       return true;  // KILL APPLICATION
0354     }
0355   }
0356   return false;
0357 }