Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

DbmCmdOptions.cxx

Go to the documentation of this file.
00001 #include <cstring>
00003 // DbmCmdOptions                                                     //
00004 //                                                                    //
00005 // Package: Dbm (Database Maintenance).                               //
00006 //                                                                    //
00007 // N. West 12/2001                                                    //
00008 //                                                                    //
00009 // Concept: Collection of command options.                            //
00010 //                                                                    //
00011 // Purpose: To simplify command option processing; options come both  //
00012 //          from user and DbmLogFile.                                 //
00013 //                                                                    //
00015 
00016 #include <cstdlib>
00017 #include <cstring>
00018 
00019 #include "DatabaseMaintenance/DbmCmdOptions.h"
00020 #include "JobControl/JobCommand.h" 
00021 #include "LeakChecker/Lea.h"
00022 #include "Util/UtilString.h"
00023 #include "MessageService/MsgService.h"
00024 
00025 using namespace std;
00026 
00027 ClassImp(DbmCmdOptions)
00028 
00029 
00030 //   Definition of static data members
00031 //   *********************************
00032 
00033 CVSID("$Id: DbmCmdOptions.cxx,v 1.7 2009/02/28 21:46:12 gmieg Exp $");
00034 
00035 // Definition of member functions (alphabetical order)
00036 // ***************************************************
00037 
00038 
00039 //.....................................................................
00040 
00041 DbmCmdOptions::DbmCmdOptions(const string& validOpts,
00042                              JobCommand* jcmd,
00043                              const std::list<std::string>* defaultOpts) :
00044 fIsValid(kTRUE),
00045 fValidOpts(":")
00046 {
00047 //
00048 //
00049 //  Purpose:  Default constructor
00050 //
00051 //  Arguments:
00052 //    validOpts    in    Option list of colon separated valid options
00053 //                       e.g."--Since:--File".  Default = "".
00054 //    jcmd         in    JobCommand to process. Optional, default = 0.
00055 //    defaultOpts  in    Ordered list of default options.
00056 //                       If none supplied, use --Table --File (see 
00057 //                       ProcessCmd).
00058 //
00059 //  Return:    n/a
00060 //
00061 //  Contact:   N. West
00062 //
00063 //  Specification:-
00064 //  =============
00065 //
00066 //  o  Create a DbmCmdOptions.
00067 
00068 
00069 //  Program Notes:-
00070 //  =============
00071 
00072 //  None.
00073 
00074   LEA_CTOR    //Leak Checker
00075 
00076   MSG("Dbm", Msg::kVerbose) << "Creating DbmCmdOptions" << endl;
00077   
00078 //  Add leading and trailing colons to simplify searching.
00079   fValidOpts += validOpts;
00080   fValidOpts += ':';
00081   if ( jcmd ) ProcessCmd( *jcmd, defaultOpts );
00082 
00083 }
00084 
00085 //.....................................................................
00086 
00087 DbmCmdOptions::~DbmCmdOptions() {
00088 //
00089 //
00090 //  Purpose: Destructor
00091 //
00092 //  Arguments: 
00093 //    None.
00094 //
00095 //  Return:    n/a
00096 //
00097 //  Contact:   N. West
00098 //
00099 //  Specification:-
00100 //  =============
00101 //
00102 //  o  Destroy DbmCmdOptions.
00103 
00104 
00105 //  Program Notes:-
00106 //  =============
00107 
00108 //  None.
00109 
00110   LEA_DTOR    //Leak Checker
00111 
00112   MSG("Dbm", Msg::kVerbose) << "Destroying DbmCmdOptions" << endl;
00113 
00114 }
00115 
00116 //.....................................................................
00117 
00118 Bool_t DbmCmdOptions::AddOpt(const string& opt, JobCommand& jcmd) {
00119 //
00120 //
00121 //  Purpose:  Add option, plus associated value, if valid.
00122 //
00123 //  Arguments: 
00124 //    opt          in    Option to be stored.
00125 //    jcmd         in    JobCommand holding option value, if any.
00126 //                 out   Option removed, if any.
00127 //
00128 //  Return:   kTRUE if option permitted 
00129 //
00130 //  Contact:   N. West
00131 //
00132 //  Specification:-
00133 //  =============
00134 //
00135 //  o Add option plus associated value if valid.  If no associated
00136 //    value, stored "yes".
00137 
00138 //  Program Notes:-
00139 //  =============
00140 
00141 //  None.
00142 
00143 
00144 //  Collect option if any.
00145   string val("yes");
00146   Bool_t hasOpt = kFALSE;
00147 
00148   if ( opt == "--Date"           ) hasOpt = kTRUE;
00149   if ( opt == "--DatabaseNumber" ) hasOpt = kTRUE;
00150   if ( opt == "--Detector"       ) hasOpt = kTRUE;
00151   if ( opt == "--File"           ) hasOpt = kTRUE;
00152   if ( opt == "--LogFile"        ) hasOpt = kTRUE;
00153   if ( opt == "--NumSeqNo"       ) hasOpt = kTRUE;
00154   if ( opt == "--Reason"         ) hasOpt = kTRUE;
00155   if ( opt == "--SimFlag"        ) hasOpt = kTRUE;
00156   if ( opt == "--SeqNo"          ) hasOpt = kTRUE;
00157   if ( opt == "--SeqNoMin"       ) hasOpt = kTRUE;
00158   if ( opt == "--SeqNoMax"       ) hasOpt = kTRUE;
00159   if ( opt == "--Since"          ) hasOpt = kTRUE;
00160   if ( opt == "--Table"          ) hasOpt = kTRUE;
00161   if ( opt == "--Task"           ) hasOpt = kTRUE;
00162   if ( opt == "--UpdateTime"     ) hasOpt = kTRUE;
00163 
00164   if ( hasOpt ) {
00165     const char* valPtr = jcmd.PopOpt();
00166     if ( ! valPtr) {
00167       MSG("Dbm",Msg::kError) << "Option " << opt 
00168                              << " must be followed by a value" << endl;
00169       fIsValid = kFALSE;
00170       return kFALSE;
00171     }
00172   else val = valPtr;
00173   }
00174   return AddOpt(opt,val);
00175 }
00176 Bool_t DbmCmdOptions::AddOpt(const string& opt, const string& val) {
00177 //
00178 //
00179 //  Purpose:  Add option, plus associated value, if valid.
00180 //
00181 //  Arguments: 
00182 //    opt          in    Option to be stored.
00183 //    jcmd         in    JobCommand holding option value, if any.
00184 //                 out   Option removed, if any.
00185 //
00186 //  Return:   kTRUE if option permitted 
00187 //
00188 //  Contact:   N. West
00189 //
00190 //  Specification:-
00191 //  =============
00192 //
00193 //  o Add option plus associated value if valid. 
00194 
00195 //  Program Notes:-
00196 //  =============
00197 
00198 //  None.
00199 
00200 // Store if valid.
00201   if ( ! IsValid( opt ) ) {
00202     MSG("Dbm",Msg::kError) << "Option " << opt 
00203                            << " not allowed for this command" << endl;
00204     fIsValid = kFALSE;
00205     return kFALSE;
00206   }
00207   if ( TestOpt(opt) 
00208      ) MSG("Dbm",Msg::kWarning) << "Option " << opt 
00209                                 << " has been duplicated" << endl;
00210   fOptsMap[opt] = val;
00211   return fIsValid;
00212 
00213 }
00214 //.....................................................................
00215 
00216 void DbmCmdOptions::GetOptIntList(const std::string& opt, std::list<int>& nos) const {
00217 //
00218 //
00219 //  Purpose:  Parse and return a comma separated list.
00220 //
00221 //  Arguments: 
00222 //    opt          in    The option whose value is the comma separated list.
00223 //    nos          in    The initial list
00224 //                 out   Updated to include the list of numbers.
00225 //                       If any error, the list will be cleared to zero 
00226 //                        (including anything that pre-existed the call to this function).
00228 //  Contact:   N. West
00229 //
00230 //  Specification:-
00231 //  =============
00232 //
00233 //  o Treat option value associated with supplied option as a comma separated
00234 //    list of integers and stored them in the supplied list.
00235 
00236   string optValue = this->GetOptString(opt);
00237   bool fail = (opt == "no");
00238 
00239   if ( ! fail ) {
00240     std::vector<std::string> ls;
00241     UtilString::StringTok(ls,optValue,",");
00242     if ( ls.size() == 0 ) fail = true;
00243     else {
00244       std::vector<std::string>::iterator    itr = ls.begin();
00245       std::vector<std::string>::iterator itrEnd = ls.end();
00246       while ( itr != itrEnd ) {
00247         if ( UtilString::IsInt(itr->c_str()) ) nos.push_back(atoi(itr->c_str()));
00248         else fail = true;
00249         ++itr;
00250       }
00251     }
00252   }
00253 
00254   if ( fail ) nos.clear();
00255 
00256 }
00257 
00258 //.....................................................................
00259 
00260 Int_t DbmCmdOptions::GetOptInt(const string& opt) const {
00261 //
00262 //
00263 //  Purpose:  Return associated option value as an integer
00264 //
00265 //  Arguments: 
00266 //    opt          in    Option whose value is to be returned.
00267 //
00268 //  Return:    0 if option not currently stored
00269 //             otherwise the option value or 1 if none.
00270 //
00271 //  Contact:   N. West
00272 //
00273 //  Specification:-
00274 //  =============
00275 //
00276 //  o  Return associated option value as an integer.
00277 
00278   string value = this->GetOptString(opt);
00279   if ( opt == "no"  ) return 0;
00280   if ( opt == "yes" ) return 1;
00281   return atoi(value.c_str());
00282 
00283 
00284 }
00285 
00286 //.....................................................................
00287 
00288 string DbmCmdOptions::GetOptString(const string& opt) const {
00289 //
00290 //
00291 //  Purpose:  Return associated option value as a string.
00292 //
00293 //  Arguments: 
00294 //    opt          in    Option whose value is to be returned.
00295 //
00296 //  Return:    "no" if option not currently stored
00297 //             otherwise the option value or "yes" if none.
00298 //
00299 //  Contact:   N. West
00300 //
00301 //  Specification:-
00302 //  =============
00303 //
00304 //  o  Return associated option value as a string.
00305 
00306 //  Program Notes:-
00307 //  =============
00308 
00309 //  As we are careful to check that key exists before using
00310 //  the non-const operator[], its O.K. to cast away const.
00311 
00312   DbmCmdOptions* me = const_cast<DbmCmdOptions*>(this);
00313   return TestOpt(opt) ? me->fOptsMap[opt] : "no";
00314 
00315 }
00316 //.....................................................................
00317 
00318 string DbmCmdOptions::GetOpts() const {
00319 //
00320 //
00321 //  Purpose:  Return string containing all options.
00322 //
00323 //  Arguments: None.
00324 //
00325 //  Return:  String containing all options.  
00326 //
00327 //  Contact:   N. West
00328 //
00329 //  Specification:-
00330 //  =============
00331 //
00332 //  o Return string containing all options.
00333 
00334 //  Program Notes:-
00335 //  =============
00336 
00337 //  None.
00338 
00339   string opts;
00340   for ( map<string,string>::const_iterator itr = fOptsMap.begin();
00341         itr != fOptsMap.end();
00342         ++itr ) {
00343     opts += (*itr).first + " " + (*itr).second + " ";
00344   }
00345   return opts;
00346 
00347 }
00348 Bool_t DbmCmdOptions::IsValid(const string& opt) const {
00349 //
00350 //
00351 //  Purpose:  Test to see if option is valid.
00352 //
00353 //  Arguments: 
00354 //    opt          in    Option to be tested.
00355 //
00356 //  Return:   kTRUE if option permitted 
00357 //
00358 //  Contact:   N. West
00359 //
00360 //  Specification:-
00361 //  =============
00362 //
00363 //  o Test to see if option is valid.
00364 
00365 //  Program Notes:-
00366 //  =============
00367 
00368 //  None.
00369 
00370 // Look up option is valid list.
00371   string optStr(":");
00372   optStr += opt;
00373   optStr += ':';
00374   return  fValidOpts.find(optStr) != string::npos;
00375 
00376 }
00377 
00378 //.....................................................................
00379 
00380 Bool_t DbmCmdOptions::ProcessCmd(JobCommand& jcmd,
00381                                  const std::list<std::string>* defaultOpts) {
00382 //
00383 //
00384 //  Purpose:  Process JobCommand storing all options.
00385 //
00386 //  Arguments: 
00387 //    jcmd         in    JobCommand to be processed.
00388 //                 out   Exhausted JobCommand.
00389 //    defaultOpts  in    Ordered list of default options.
00390 //                       If none supplied, use --Table --File
00391 //
00392 //  Return:    
00393 //
00394 //  Contact:   N. West
00395 //
00396 //  Specification:-
00397 //  =============
00398 //
00399 //  o Process JobCommand storing all options.
00400 //  
00401 //  o Supply default options to the first  parameters 
00402 //    i.e. options that do not start "-" and are permitted.
00403 
00404 
00405 //  Program Notes:-
00406 //  =============
00407 
00408 //  None.
00409 
00410   // Provide a default for the default!
00411   std::list<std::string> defDefOPts;
00412   defDefOPts.push_back("--Table");
00413   defDefOPts.push_back("--File");
00414 
00415   if ( defaultOpts == 0 ) defaultOpts = &defDefOPts;
00416   
00417   std::list<std::string>::const_iterator 
00418           defOptsItr    = defaultOpts->begin();
00419   std::list<std::string>::const_iterator 
00420           defOptsItrEnd = defaultOpts->end();
00421 
00422   const char* opt = jcmd.PopOpt();
00423   while (opt) {
00424 
00425 //  Deal with option.
00426     if ( opt[0] == '-' ) {
00427 
00428       string option = opt;
00429       AddOpt(opt, jcmd);
00430     }
00431  
00432 // Deal with parameter.
00433    else {
00434 
00435      // Pick up next default option, if any.
00436      std::string defaultOpt = "";
00437      if ( defOptsItr != defOptsItrEnd ) {
00438        defaultOpt = *defOptsItr;
00439        ++defOptsItr;
00440      }
00441 
00442      // Permit default if defined, not yet used and is valid.
00443       if (  defaultOpt != "" && ! TestOpt(defaultOpt) && IsValid(defaultOpt) 
00444           ) AddOpt(defaultOpt, opt);
00445 
00446       else {
00447         MSG("Dbm",Msg::kWarning) << "Unknown parameter " << opt << endl;
00448         fIsValid = kFALSE;
00449       }
00450     }
00451 
00452     opt = jcmd.PopOpt(); 
00453   }   // loop over options
00454 
00455   return fIsValid;
00456 
00457 }
00458 
00459 //.....................................................................
00460 
00461 Bool_t DbmCmdOptions::TestOpt(const string& opt) const {
00462 //
00463 //
00464 //  Purpose:  Test existence of option.
00465 //
00466 //  Arguments: 
00467 //    opt          in    Option whose value is to be tested.
00468 //
00469 //  Return:    kTRUE if option exists, otherwise kFALSE.
00470 //
00471 //  Contact:   N. West
00472 //
00473 //  Specification:-
00474 //  =============
00475 //
00476 //  o Test existence of option.
00477 
00478 //  Program Notes:-
00479 //  =============
00480 
00481 //  None.
00482 
00483   map<string,string>::const_iterator loc = fOptsMap.find(opt);
00484   return loc != fOptsMap.end();
00485 
00486 }
00487 
00488 /*    Template for New Member Function
00489 
00490 //.....................................................................
00491 
00492 DbmCmdOptions:: {
00493 //
00494 //
00495 //  Purpose:  
00496 //
00497 //  Arguments: 
00498 //    xxxxxxxxx    in    yyyyyy
00499 //
00500 //  Return:    
00501 //
00502 //  Contact:   N. West
00503 //
00504 //  Specification:-
00505 //  =============
00506 //
00507 //  o 
00508 
00509 //  Program Notes:-
00510 //  =============
00511 
00512 //  None.
00513 
00514 
00515 }
00516 
00517 */
00518 

Generated on Mon Feb 15 11:06:35 2010 for loon by  doxygen 1.3.9.1