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