Back to home page

EIC code displayed by LXR

 
 

    


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

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 // The Geant4-DNA web site is available at http://geant4-dna.org
0031 //
0032 // Author: Mathieu Karamitros
0033 //
0034 //
0035 /// \file CommandLineParser.cc
0036 /// \brief Implementation of the CommandLineParser class
0037 
0038 #include "CommandLineParser.hh"
0039 
0040 #include <iomanip>
0041 
0042 using namespace std;
0043 using namespace G4DNAPARSER;
0044 
0045 CommandLineParser* CommandLineParser::fpInstance(0);
0046 G4String Command::fNoOption = "NoOption";
0047 
0048 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0049 
0050 inline bool MATCH(const char* a, const char* b)
0051 {
0052   return strcmp(a, b) == 0;
0053 }
0054 
0055 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0056 
0057 CommandLineParser::CommandLineParser()
0058 {
0059   // G4cout << "############ NEW PARSE ##########" << G4endl;
0060   fpInstance = this;
0061   fOptionsWereSetup = false;
0062   fMaxMarkerLength = 0;
0063   fMaxOptionNameLength = 0;
0064   AddCommand("--help", Command::WithoutOption, "Print this help");
0065   AddCommand("-h", Command::WithoutOption, "Print this help");
0066   AddCommand("&", Command::WithoutOption);
0067 
0068   fVerbose = 0;
0069 }
0070 
0071 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0072 
0073 CommandLineParser* CommandLineParser::GetParser()
0074 {
0075   if (!fpInstance) new CommandLineParser;
0076   return fpInstance;
0077 }
0078 
0079 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0080 
0081 CommandLineParser::~CommandLineParser()
0082 {
0083   std::map<G4String, Command*>::iterator it = fCommandMap.begin();
0084   for (; it != fCommandMap.end(); it++) {
0085     if (it->second) delete it->second;
0086   }
0087 }
0088 
0089 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0090 
0091 void CommandLineParser::DeleteInstance()
0092 {
0093   if (fpInstance) {
0094     delete fpInstance;
0095     fpInstance = 0;
0096   }
0097 }
0098 
0099 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0100 
0101 Command::Command(Command::Type commandType, const G4String& description)
0102 {
0103   fType = commandType;
0104   fDescription = description;
0105   fActive = false;
0106 }
0107 
0108 CommandWithOption::CommandWithOption(Command::Type commandType, const G4String& description,
0109                                      const G4String& defaultOption, const G4String& optionName)
0110   : Command(commandType, description)
0111 {
0112   fDefaultOption = defaultOption;
0113   fOptionName = optionName;
0114   fOption = "";
0115 }
0116 
0117 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0118 
0119 int CommandLineParser::Parse(int& argc, char** argv)
0120 {
0121   //    G4cout << "Parse " << G4endl;
0122   static char null[1] = {""};
0123   int firstArgc = argc;
0124 
0125   for (int i = 1; i < firstArgc; i++) {
0126     Command* command = FindCommand(argv[i]);
0127     if (command == 0) continue;
0128 
0129     if (fVerbose) G4cout << "Command : " << argv[i] << G4endl;
0130 
0131     fOptionsWereSetup = true;
0132     command->fActive = true;
0133 
0134     G4String marker(argv[i]);
0135 
0136     if (strcmp(argv[i], "-h") != 0 && strcmp(argv[i], "--help") != 0) {
0137       argv[i] = null;
0138     }
0139 
0140     if (command->fType == Command::WithOption) {
0141       if (fVerbose) G4cout << "WithOption" << G4endl;
0142 
0143       if (i + 1 > firstArgc || argv[i + 1] == 0 || argv[i + 1][0] == '-') {
0144         G4cerr << "An command line option is missing for " << marker << G4endl;
0145         abort();
0146       }
0147 
0148       command->SetOption((const char*)strdup(argv[i + 1]));
0149       argv[i + 1] = null;
0150       i++;
0151     }
0152     else if (command->fType == Command::OptionNotCompulsory) {
0153       if (fVerbose) G4cout << "OptionNotCompulsory" << G4endl;
0154 
0155       if (i + 1 < firstArgc) {
0156         G4String buffer = (const char*)strdup(argv[i + 1]);
0157 
0158         if (buffer.empty() == false) {
0159           if (buffer.at(0) != '-' && buffer.at(0) != '&' && buffer.at(0) != '>'
0160               && buffer.at(0) != '|')
0161           {
0162             if (fVerbose) {
0163               G4cout << "facultative option is : " << buffer << G4endl;
0164             }
0165 
0166             command->SetOption((const char*)strdup(argv[i + 1]));
0167             argv[i + 1] = null;
0168             i++;
0169             continue;
0170           }
0171         }
0172       }
0173 
0174       if (fVerbose) G4cout << "Option not set" << G4endl;
0175 
0176       command->SetOption("");
0177     }
0178   }
0179   CorrectRemainingOptions(argc, argv);
0180 
0181   Command* commandLine(0);
0182   if ((commandLine = GetCommandIfActive("--help")) || (commandLine = GetCommandIfActive("-h"))) {
0183     G4cout << "Usage : " << argv[0] << " [OPTIONS]" << G4endl;
0184     PrintHelp();
0185     return 1;
0186   }
0187 
0188   return 0;
0189 }
0190 
0191 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0192 
0193 void CommandLineParser::PrintHelp()
0194 {
0195   std::map<G4String, Command*>::iterator it;
0196 
0197   int maxFieldLength = fMaxMarkerLength + fMaxOptionNameLength + 4;
0198 
0199   G4cout << "Options: " << G4endl;
0200 
0201   for (it = fCommandMap.begin(); it != fCommandMap.end(); it++) {
0202     Command* command = it->second;
0203     if (command) {
0204       G4cout << setw(maxFieldLength) << left;
0205 
0206       G4String toPrint = it->first;
0207 
0208       if (toPrint == "&") {
0209         continue;
0210       }
0211       else if (toPrint == "-h")
0212         continue;
0213       else if (toPrint == "--help") {
0214         toPrint += ", -h";
0215       }
0216 
0217       if (command->GetDefaultOption() != "") {
0218         toPrint += " \"" + command->GetDefaultOption() + "\"";
0219       }
0220 
0221       G4cout << toPrint;
0222 
0223       G4cout << command->GetDescription() << G4endl;
0224     }
0225   }
0226 }
0227 
0228 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0229 
0230 void CommandLineParser::CorrectRemainingOptions(int& argc, char** argv)
0231 {
0232   // remove handled arguments from argument array
0233   int j = 0;
0234   for (int i = 0; i < argc; i++) {
0235     if (strcmp(argv[i], "")) {
0236       argv[j] = argv[i];
0237       j++;
0238     }
0239   }
0240   argc = j;
0241 }
0242 
0243 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0244 
0245 void CommandLineParser::AddCommand(const G4String& marker, Command::Type type,
0246                                    const G4String& description, const G4String& defaultOption,
0247                                    const G4String& optionName)
0248 {
0249   // G4cout << "Add command : "<< marker << G4endl;
0250 
0251   Command* command = 0;
0252   switch (type) {
0253     case Command::WithoutOption:
0254       command = new Command(type, description);
0255       break;
0256 
0257     default:
0258       command = new CommandWithOption(type, description, defaultOption, optionName);
0259       if ((int)defaultOption.length() > fMaxOptionNameLength)
0260         fMaxOptionNameLength = defaultOption.length();
0261       break;
0262   }
0263 
0264   if ((int)marker.length() > fMaxMarkerLength) fMaxMarkerLength = marker.length();
0265   fCommandMap.insert(make_pair(marker, command));
0266 }
0267 
0268 /*
0269 // Add one command but multiple markers
0270 void Parser::AddCommand(vector<G4String> markers,
0271                         CommandType type,
0272                         const G4String& description,
0273                         const G4String& optionName)
0274 {
0275   // G4cout << "Add command : "<< marker << G4endl;
0276   Command* command = new Command(type, description, optionName);
0277 
0278   for (size_t i = 0; i < markers.size; i++)
0279   {
0280     G4String marker = markers[i];
0281     if ((int) marker.length() > fMaxMarkerLength)
0282     {
0283       fMaxMarkerLength = marker.length();
0284     }
0285     if ((int) optionName.length() > fMaxOptionNameLength)
0286     {
0287       fMaxOptionNameLength = optionName.length();
0288     }
0289     fCommandMap.insert(make_pair(marker, command));
0290   }
0291 }
0292 */
0293 
0294 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0295 
0296 Command* CommandLineParser::FindCommand(const G4String& marker)
0297 {
0298   std::map<G4String, Command*>::iterator it = fCommandMap.find(marker);
0299   if (it == fCommandMap.end()) {
0300     // G4cerr << "command not found" << G4endl;
0301     return 0;
0302   }
0303   return it->second;
0304 }
0305 
0306 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0307 
0308 Command* CommandLineParser::GetCommandIfActive(const G4String& marker)
0309 {
0310   Command* command = FindCommand(marker);
0311   if (command) {
0312     // G4cout << "Command found : "<< marker << G4endl;
0313 
0314     if (command->fActive) {
0315       // G4cout << "Command Active" << G4endl;
0316       return command;
0317     }
0318     // else
0319     //  G4cout <<"Command not active" << G4endl;
0320   }
0321   else {
0322     G4ExceptionDescription description;
0323     description << "You try to retrieve a command that was not registered : " << marker << G4endl;
0324     G4Exception("CommandLineParser::GetCommandIfActive", "COMMAND LINE NOT DEFINED", FatalException,
0325                 description, "");
0326     // If you are using this class outside of Geant4, use exit(-1) instead
0327     // exit(-1);
0328   }
0329   return 0;
0330 }
0331 
0332 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0333 
0334 bool CommandLineParser::CheckIfNotHandledOptionsExists(int& argc, char** argv)
0335 {
0336   if (argc > 0) {
0337     G4bool kill = false;
0338     for (G4int i = 1; i < argc; i++) {
0339       if (strcmp(argv[i], "")) {
0340         kill = true;
0341         G4cerr << "Unknown argument : " << argv[i] << "\n";
0342       }
0343     }
0344     if (kill) {
0345       G4cerr << "The option " << argv[0] << " is not handled this programme." << G4endl;
0346       G4cout << "Usage : " << argv[0] << " [OPTIONS]" << G4endl;
0347       PrintHelp();
0348       return true;  // KILL APPLICATION
0349     }
0350   }
0351   return false;
0352 }