| 1 | /* ======================================================================== *\
|
|---|
| 2 | !
|
|---|
| 3 | ! *
|
|---|
| 4 | ! * This file is part of MARS, the MAGIC Analysis and Reconstruction
|
|---|
| 5 | ! * Software. It is distributed to you in the hope that it can be a useful
|
|---|
| 6 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
|
|---|
| 7 | ! * It is distributed WITHOUT ANY WARRANTY.
|
|---|
| 8 | ! *
|
|---|
| 9 | ! * Permission to use, copy, modify and distribute this software and its
|
|---|
| 10 | ! * documentation for any purpose is hereby granted without fee,
|
|---|
| 11 | ! * provided that the above copyright notice appear in all copies and
|
|---|
| 12 | ! * that both that copyright notice and this permission notice appear
|
|---|
| 13 | ! * in supporting documentation. It is provided "as is" without express
|
|---|
| 14 | ! * or implied warranty.
|
|---|
| 15 | ! *
|
|---|
| 16 | !
|
|---|
| 17 | !
|
|---|
| 18 | ! Author(s): Thomas Bretz, 7/2003 <mailto:tbretz@astro.uni-wuerzburg.de>
|
|---|
| 19 | !
|
|---|
| 20 | ! Copyright: MAGIC Software Development, 2003
|
|---|
| 21 | !
|
|---|
| 22 | !
|
|---|
| 23 | \* ======================================================================== */
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 | //////////////////////////////////////////////////////////////////////////////
|
|---|
| 27 | //
|
|---|
| 28 | // MArgs
|
|---|
| 29 | //
|
|---|
| 30 | //////////////////////////////////////////////////////////////////////////////
|
|---|
| 31 | #include "MArgs.h"
|
|---|
| 32 |
|
|---|
| 33 | #include <stdlib.h>
|
|---|
| 34 |
|
|---|
| 35 | #include "MLog.h"
|
|---|
| 36 | #include "MLogManip.h"
|
|---|
| 37 |
|
|---|
| 38 | ClassImp(MArgsEntry);
|
|---|
| 39 | ClassImp(MArgs);
|
|---|
| 40 |
|
|---|
| 41 | using namespace std;
|
|---|
| 42 |
|
|---|
| 43 | void MArgsEntry::Print(const Option_t *o) const
|
|---|
| 44 | {
|
|---|
| 45 | gLog << all << *this << endl;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | MArgs::MArgs(int argc, const char **argv) : fArgc(argc)
|
|---|
| 49 | {
|
|---|
| 50 | fName = argv[0];
|
|---|
| 51 |
|
|---|
| 52 | fArgv = new TList;
|
|---|
| 53 | fArgv->SetOwner();
|
|---|
| 54 |
|
|---|
| 55 | for (int i=1; i<argc; i++)
|
|---|
| 56 | {
|
|---|
| 57 | MArgsEntry &o = *new MArgsEntry(argv[i]);
|
|---|
| 58 | dynamic_cast<TString&>(o) = o.Strip(TString::kBoth);
|
|---|
| 59 | fArgv->Add(&o);
|
|---|
| 60 | }
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | MArgs::~MArgs()
|
|---|
| 64 | {
|
|---|
| 65 | delete fArgv;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | void MArgs::Print(const Option_t *o) const
|
|---|
| 69 | {
|
|---|
| 70 | gLog << all << underline << fName << ":" << endl;
|
|---|
| 71 | fArgv->Print();
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | Int_t MArgs::GetInt(const TString name) const
|
|---|
| 75 | {
|
|---|
| 76 | return atoi(GetString(name));
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | Double_t MArgs::GetFloat(const TString name) const
|
|---|
| 80 | {
|
|---|
| 81 | return atof(GetString(name));
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | TString MArgs::GetString(const TString name) const
|
|---|
| 85 | {
|
|---|
| 86 | TIter Next(fArgv);
|
|---|
| 87 | TString *s = NULL;
|
|---|
| 88 | while ((s=dynamic_cast<TString*>(Next())))
|
|---|
| 89 | if (s->BeginsWith(name))
|
|---|
| 90 | return s->Data()+s->Index(name)+name.Length();
|
|---|
| 91 | return 0;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | Int_t MArgs::GetIntAndRemove(const TString name)
|
|---|
| 95 | {
|
|---|
| 96 | return atoi(GetStringAndRemove(name));
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | Double_t MArgs::GetFloatAndRemove(const TString name)
|
|---|
| 100 | {
|
|---|
| 101 | return atof(GetStringAndRemove(name));
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | TString MArgs::GetStringAndRemove(const TString n)
|
|---|
| 105 | {
|
|---|
| 106 | const TString name = n.Strip(TString::kBoth);
|
|---|
| 107 |
|
|---|
| 108 | TIter Next(fArgv);
|
|---|
| 109 | TString *s = NULL;
|
|---|
| 110 | while ((s=dynamic_cast<TString*>(Next())))
|
|---|
| 111 | if (s->BeginsWith(name))
|
|---|
| 112 | {
|
|---|
| 113 | TString str = s->Data()+s->Index(name)+name.Length();
|
|---|
| 114 | delete fArgv->Remove(dynamic_cast<TObject*>(s));
|
|---|
| 115 | return str;
|
|---|
| 116 | }
|
|---|
| 117 | return 0;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | Int_t MArgs::GetArgumentInt(Int_t i) const
|
|---|
| 121 | {
|
|---|
| 122 | return atoi(GetArgumentStr(i));
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | Float_t MArgs::GetArgumentFloat(Int_t i) const
|
|---|
| 126 | {
|
|---|
| 127 | return atof(GetArgumentStr(i));
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | TString MArgs::GetArgumentStr(Int_t i) const
|
|---|
| 131 | {
|
|---|
| 132 | Int_t num = 0;
|
|---|
| 133 |
|
|---|
| 134 | TIter Next(fArgv);
|
|---|
| 135 | TString *s = NULL;
|
|---|
| 136 | while ((s=dynamic_cast<TString*>(Next())))
|
|---|
| 137 | {
|
|---|
| 138 | if (s->BeginsWith("-"))
|
|---|
| 139 | continue;
|
|---|
| 140 |
|
|---|
| 141 | if (i==num++)
|
|---|
| 142 | return *s;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | return "";
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | Int_t MArgs::GetNumArguments() const
|
|---|
| 149 | {
|
|---|
| 150 | Int_t num = 0;
|
|---|
| 151 |
|
|---|
| 152 | TIter Next(fArgv);
|
|---|
| 153 | TString *s = NULL;
|
|---|
| 154 | while ((s=dynamic_cast<TString*>(Next())))
|
|---|
| 155 | if (!s->BeginsWith("-"))
|
|---|
| 156 | num++;
|
|---|
| 157 |
|
|---|
| 158 | return num;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | Bool_t MArgs::Has(const TString n) const
|
|---|
| 162 | {
|
|---|
| 163 | const TString name = n.Strip(TString::kBoth);
|
|---|
| 164 |
|
|---|
| 165 | TIter Next(fArgv);
|
|---|
| 166 | TString *s = NULL;
|
|---|
| 167 | while ((s=dynamic_cast<TString*>(Next())))
|
|---|
| 168 | if (s->BeginsWith(name))
|
|---|
| 169 | return kTRUE;
|
|---|
| 170 | return kFALSE;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | Bool_t MArgs::HasOption(const TString n) const
|
|---|
| 174 | {
|
|---|
| 175 | const TString name = n.Strip(TString::kBoth);
|
|---|
| 176 |
|
|---|
| 177 | TIter Next(fArgv);
|
|---|
| 178 | TString *s = NULL;
|
|---|
| 179 | while ((s=dynamic_cast<TString*>(Next())))
|
|---|
| 180 | if (s->BeginsWith(name) && s->Length()>name.Length())
|
|---|
| 181 | return kTRUE;
|
|---|
| 182 | return kFALSE;
|
|---|
| 183 | }
|
|---|