00001 #include <cstring>
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
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
00031
00032
00033 CVSID("$Id: DbmCmdOptions.cxx,v 1.7 2009/02/28 21:46:12 gmieg Exp $");
00034
00035
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
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 LEA_CTOR
00075
00076 MSG("Dbm", Msg::kVerbose) << "Creating DbmCmdOptions" << endl;
00077
00078
00079 fValidOpts += validOpts;
00080 fValidOpts += ':';
00081 if ( jcmd ) ProcessCmd( *jcmd, defaultOpts );
00082
00083 }
00084
00085
00086
00087 DbmCmdOptions::~DbmCmdOptions() {
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 LEA_DTOR
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
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
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
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
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
00220
00221
00222
00223
00224
00225
00226
00228
00229
00230
00231
00232
00233
00234
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
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
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
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
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
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
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
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
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
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
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
00426 if ( opt[0] == '-' ) {
00427
00428 string option = opt;
00429 AddOpt(opt, jcmd);
00430 }
00431
00432
00433 else {
00434
00435
00436 std::string defaultOpt = "";
00437 if ( defOptsItr != defOptsItrEnd ) {
00438 defaultOpt = *defOptsItr;
00439 ++defOptsItr;
00440 }
00441
00442
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 }
00454
00455 return fIsValid;
00456
00457 }
00458
00459
00460
00461 Bool_t DbmCmdOptions::TestOpt(const string& opt) const {
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483 map<string,string>::const_iterator loc = fOptsMap.find(opt);
00484 return loc != fOptsMap.end();
00485
00486 }
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518