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 | // This is a helper class for executables to parse command line arguments
|
---|
31 | //
|
---|
32 | //////////////////////////////////////////////////////////////////////////////
|
---|
33 | #include "MArgs.h"
|
---|
34 |
|
---|
35 | #include <stdlib.h>
|
---|
36 |
|
---|
37 | #include "MLog.h"
|
---|
38 | #include "MLogManip.h"
|
---|
39 |
|
---|
40 | ClassImp(MArgsEntry);
|
---|
41 | ClassImp(MArgs);
|
---|
42 |
|
---|
43 | using namespace std;
|
---|
44 |
|
---|
45 | void MArgsEntry::Print(const Option_t *o) const
|
---|
46 | {
|
---|
47 | gLog << all << *this << endl;
|
---|
48 | }
|
---|
49 |
|
---|
50 | // --------------------------------------------------------------------------
|
---|
51 | //
|
---|
52 | // Initializes:
|
---|
53 | // fName: The name of the executable
|
---|
54 | // fArgv: A TList containing all other command line arguments
|
---|
55 | //
|
---|
56 | MArgs::MArgs(int argc, const char **argv) : fArgc(argc)
|
---|
57 | {
|
---|
58 | fName = argv[0];
|
---|
59 |
|
---|
60 | fArgv = new TList;
|
---|
61 | fArgv->SetOwner();
|
---|
62 |
|
---|
63 | for (int i=1; i<argc; i++)
|
---|
64 | {
|
---|
65 | MArgsEntry &o = *new MArgsEntry(argv[i]);
|
---|
66 | dynamic_cast<TString&>(o) = o.Strip(TString::kBoth);
|
---|
67 | fArgv->Add(&o);
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | // --------------------------------------------------------------------------
|
---|
72 | //
|
---|
73 | // Deletes fArgv.
|
---|
74 | //
|
---|
75 | MArgs::~MArgs()
|
---|
76 | {
|
---|
77 | delete fArgv;
|
---|
78 | }
|
---|
79 |
|
---|
80 | // --------------------------------------------------------------------------
|
---|
81 | //
|
---|
82 | // Print all arguments parsed.
|
---|
83 | //
|
---|
84 | void MArgs::Print(const Option_t *o) const
|
---|
85 | {
|
---|
86 | gLog << all << underline << fName << ":" << endl;
|
---|
87 | fArgv->Print();
|
---|
88 | }
|
---|
89 |
|
---|
90 | // --------------------------------------------------------------------------
|
---|
91 | //
|
---|
92 | // Return the Integer corresponding to the command line argument 'name'
|
---|
93 | // eg. executable -argument 5
|
---|
94 | // GetInt("argument") will return 5
|
---|
95 | //
|
---|
96 | Int_t MArgs::GetInt(const TString name) const
|
---|
97 | {
|
---|
98 | return atoi(GetString(name));
|
---|
99 | }
|
---|
100 |
|
---|
101 | // --------------------------------------------------------------------------
|
---|
102 | //
|
---|
103 | // Return the floating point value corresponding to the command line argument
|
---|
104 | // 'name'
|
---|
105 | // eg. executable -argument 5.7
|
---|
106 | // GetFloat("argument") will return 5.7
|
---|
107 | //
|
---|
108 | Double_t MArgs::GetFloat(const TString name) const
|
---|
109 | {
|
---|
110 | return atof(GetString(name));
|
---|
111 | }
|
---|
112 |
|
---|
113 | // --------------------------------------------------------------------------
|
---|
114 | //
|
---|
115 | // Return the TString corresponding to the command line argument 'name'
|
---|
116 | // eg. executable -argument=last
|
---|
117 | // GetString("-argument=") will return "last"
|
---|
118 | //
|
---|
119 | TString MArgs::GetString(const TString name) const
|
---|
120 | {
|
---|
121 | TIter Next(fArgv);
|
---|
122 | TString *s = NULL;
|
---|
123 | while ((s=dynamic_cast<TString*>(Next())))
|
---|
124 | if (s->BeginsWith(name))
|
---|
125 | return s->Data()+s->Index(name)+name.Length();
|
---|
126 | return 0;
|
---|
127 | }
|
---|
128 |
|
---|
129 | // --------------------------------------------------------------------------
|
---|
130 | //
|
---|
131 | // Return the Integer corresponding to the command line argument 'name'
|
---|
132 | // eg. executable -argument5
|
---|
133 | // GetIntAndRemove("-argument") will return 5
|
---|
134 | // and removes the argument from the internal list.
|
---|
135 | //
|
---|
136 | Int_t MArgs::GetIntAndRemove(const TString name)
|
---|
137 | {
|
---|
138 | return atoi(GetStringAndRemove(name));
|
---|
139 | }
|
---|
140 |
|
---|
141 | // --------------------------------------------------------------------------
|
---|
142 | //
|
---|
143 | // Return the floating point value corresponding to the command line argument
|
---|
144 | // 'name'
|
---|
145 | // eg. executable -argument5.7
|
---|
146 | // GetFloatAndRemove("-argument") will return 5.7
|
---|
147 | // and removes the argument from the internal list.
|
---|
148 | //
|
---|
149 | Double_t MArgs::GetFloatAndRemove(const TString name)
|
---|
150 | {
|
---|
151 | return atof(GetStringAndRemove(name));
|
---|
152 | }
|
---|
153 |
|
---|
154 | // --------------------------------------------------------------------------
|
---|
155 | //
|
---|
156 | // Return the TString corresponding to the command line argument 'name'
|
---|
157 | // eg. executable -argument=last
|
---|
158 | // GetStringAndRemove("-argument=") will return "last"
|
---|
159 | // and removes the argument from the internal list.
|
---|
160 | //
|
---|
161 | TString MArgs::GetStringAndRemove(const TString n)
|
---|
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 | {
|
---|
170 | TString str = s->Data()+s->Index(name)+name.Length();
|
---|
171 | delete fArgv->Remove(dynamic_cast<TObject*>(s));
|
---|
172 | return str;
|
---|
173 | }
|
---|
174 | return 0;
|
---|
175 | }
|
---|
176 |
|
---|
177 | // --------------------------------------------------------------------------
|
---|
178 | //
|
---|
179 | // Return the Integer corresponding to the i-th argument. This is ment
|
---|
180 | // for enumerations like
|
---|
181 | // executable 1 7 2
|
---|
182 | // GetArgumentInt(1) will return 7
|
---|
183 | //
|
---|
184 | Int_t MArgs::GetArgumentInt(Int_t i) const
|
---|
185 | {
|
---|
186 | return atoi(GetArgumentStr(i));
|
---|
187 | }
|
---|
188 |
|
---|
189 | // --------------------------------------------------------------------------
|
---|
190 | //
|
---|
191 | // Return the floating point value corresponding to the i-th argument.
|
---|
192 | // This is ment for enumerations like
|
---|
193 | // executable 1.7 7.5 2.3
|
---|
194 | // GetArgumentFloat(1) will return 7.5
|
---|
195 | //
|
---|
196 | Float_t MArgs::GetArgumentFloat(Int_t i) const
|
---|
197 | {
|
---|
198 | return atof(GetArgumentStr(i));
|
---|
199 | }
|
---|
200 |
|
---|
201 | // --------------------------------------------------------------------------
|
---|
202 | //
|
---|
203 | // Return the TString corresponding to the i-th argument.
|
---|
204 | // This is ment for enumerations like
|
---|
205 | // executable file1 file2 file3
|
---|
206 | // GetArgumentStr(1) will return "file2"
|
---|
207 | // Only arguments without a trailing '-' are considered
|
---|
208 | //
|
---|
209 | TString MArgs::GetArgumentStr(Int_t i) const
|
---|
210 | {
|
---|
211 | Int_t num = 0;
|
---|
212 |
|
---|
213 | TIter Next(fArgv);
|
---|
214 | TString *s = NULL;
|
---|
215 | while ((s=dynamic_cast<TString*>(Next())))
|
---|
216 | {
|
---|
217 | if (s->BeginsWith("-"))
|
---|
218 | continue;
|
---|
219 |
|
---|
220 | if (i==num++)
|
---|
221 | return *s;
|
---|
222 | }
|
---|
223 |
|
---|
224 | return "";
|
---|
225 | }
|
---|
226 |
|
---|
227 | // --------------------------------------------------------------------------
|
---|
228 | //
|
---|
229 | // return the number of arguments without a trainling '-'
|
---|
230 | //
|
---|
231 | Int_t MArgs::GetNumArguments() const
|
---|
232 | {
|
---|
233 | Int_t num = 0;
|
---|
234 |
|
---|
235 | TIter Next(fArgv);
|
---|
236 | TString *s = NULL;
|
---|
237 | while ((s=dynamic_cast<TString*>(Next())))
|
---|
238 | if (!s->BeginsWith("-"))
|
---|
239 | num++;
|
---|
240 |
|
---|
241 | return num;
|
---|
242 | }
|
---|
243 |
|
---|
244 | // --------------------------------------------------------------------------
|
---|
245 | //
|
---|
246 | // Checks whether an argument beginning with 'n' is existing, eg:
|
---|
247 | // executable -value5
|
---|
248 | // executable -value
|
---|
249 | // HasOption("-value") will return true in both cases
|
---|
250 | //
|
---|
251 | Bool_t MArgs::Has(const TString n) const
|
---|
252 | {
|
---|
253 | const TString name = n.Strip(TString::kBoth);
|
---|
254 |
|
---|
255 | TIter Next(fArgv);
|
---|
256 | TString *s = NULL;
|
---|
257 | while ((s=dynamic_cast<TString*>(Next())))
|
---|
258 | if (s->BeginsWith(name))
|
---|
259 | return kTRUE;
|
---|
260 | return kFALSE;
|
---|
261 | }
|
---|
262 |
|
---|
263 | // --------------------------------------------------------------------------
|
---|
264 | //
|
---|
265 | // Checks whether an argument beginning with 'n' is exists and a
|
---|
266 | // corresponding option is available, eg.
|
---|
267 | // executable -value5
|
---|
268 | // HasOption("-value") will return true
|
---|
269 | // but:
|
---|
270 | // executable -value
|
---|
271 | // HasOption("-value") will return false
|
---|
272 | //
|
---|
273 | Bool_t MArgs::HasOption(const TString n) const
|
---|
274 | {
|
---|
275 | const TString name = n.Strip(TString::kBoth);
|
---|
276 |
|
---|
277 | TIter Next(fArgv);
|
---|
278 | TString *s = NULL;
|
---|
279 | while ((s=dynamic_cast<TString*>(Next())))
|
---|
280 | if (s->BeginsWith(name) && s->Length()>name.Length())
|
---|
281 | return kTRUE;
|
---|
282 | return kFALSE;
|
---|
283 | }
|
---|