source: trunk/MagicSoft/Mars/mbase/MArgs.cc@ 2979

Last change on this file since 2979 was 2728, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 10.2 KB
Line 
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// Arguments beginning with a trailing '-' are called 'options'.
33// Arguments without a trailing '-' are considered 'arguments'
34//
35//////////////////////////////////////////////////////////////////////////////
36#include "MArgs.h"
37
38#include <stdlib.h>
39
40#include "MLog.h"
41#include "MLogManip.h"
42
43ClassImp(MArgsEntry);
44ClassImp(MArgs);
45
46using namespace std;
47
48void MArgsEntry::Print(const Option_t *o) const
49{
50 gLog << all << *this << endl;
51}
52
53// --------------------------------------------------------------------------
54//
55// Initializes:
56// fName: The name of the executable
57// fArgv: A TList containing all other command line arguments
58//
59MArgs::MArgs(int argc, char **argv) : fArgc(argc)
60{
61 // FIXME: argv has no const-qualifier to be idetical with
62 // TApplication.
63 fName = argv[0];
64
65 fArgv = new TList;
66 fArgv->SetOwner();
67
68 for (int i=1; i<argc; i++)
69 {
70 MArgsEntry &o = *new MArgsEntry(argv[i]);
71 dynamic_cast<TString&>(o) = o.Strip(TString::kBoth);
72 fArgv->Add(&o);
73 }
74}
75
76// --------------------------------------------------------------------------
77//
78// Deletes fArgv.
79//
80MArgs::~MArgs()
81{
82 delete fArgv;
83}
84
85// --------------------------------------------------------------------------
86//
87// Print everything parsed.
88// Using 'options' as option only 'options' are printed.
89// Using 'arguments' as option only 'arguments' are printed.
90//
91void MArgs::Print(const Option_t *o) const
92{
93 gLog << all << underline << fName << ":" << endl;
94
95 const TString str(o);
96
97 if (!str.CompareTo("options", TString::kIgnoreCase))
98 {
99 TIter Next(fArgv);
100 TString *s = NULL;
101 while ((s=dynamic_cast<TString*>(Next())))
102 if (s->BeginsWith("-"))
103 gLog << *s << endl;
104 return;
105 }
106
107 if (!str.CompareTo("arguments", TString::kIgnoreCase))
108 {
109 TIter Next(fArgv);
110 TString *s = NULL;
111 while ((s=dynamic_cast<TString*>(Next())))
112 if (!s->BeginsWith("-"))
113 gLog << *s << endl;
114 return;
115 }
116
117 fArgv->Print();
118}
119
120// --------------------------------------------------------------------------
121//
122// Return the Integer corresponding to the command line argument 'name'
123// eg. executable -argument 5
124// GetInt("argument") will return 5
125//
126Int_t MArgs::GetInt(const TString name) const
127{
128 return atoi(GetString(name));
129}
130
131// --------------------------------------------------------------------------
132//
133// Return the floating point value corresponding to the command line argument
134// 'name'
135// eg. executable -argument 5.7
136// GetFloat("argument") will return 5.7
137//
138Double_t MArgs::GetFloat(const TString name) const
139{
140 return atof(GetString(name));
141}
142
143// --------------------------------------------------------------------------
144//
145// Return the TString corresponding to the command line argument 'name'
146// eg. executable -argument=last
147// GetString("-argument=") will return "last"
148//
149TString MArgs::GetString(const TString name) const
150{
151 TIter Next(fArgv);
152 TString *s = NULL;
153 while ((s=dynamic_cast<TString*>(Next())))
154 if (s->BeginsWith(name))
155 return s->Data()+s->Index(name)+name.Length();
156 return 0;
157}
158
159// --------------------------------------------------------------------------
160//
161// Return the Integer corresponding to the command line argument 'name'
162// eg. executable -argument5
163// GetIntAndRemove("-argument") will return 5
164// and removes the argument from the internal list.
165//
166Int_t MArgs::GetIntAndRemove(const TString name)
167{
168 return atoi(GetStringAndRemove(name));
169}
170
171// --------------------------------------------------------------------------
172//
173// Return the floating point value corresponding to the command line argument
174// 'name'
175// eg. executable -argument5.7
176// GetFloatAndRemove("-argument") will return 5.7
177// and removes the argument from the internal list.
178//
179Double_t MArgs::GetFloatAndRemove(const TString name)
180{
181 return atof(GetStringAndRemove(name));
182}
183
184// --------------------------------------------------------------------------
185//
186// Return the TString corresponding to the command line argument 'name'
187// eg. executable -argument=last
188// GetStringAndRemove("-argument=") will return "last"
189// and removes the argument from the internal list.
190//
191TString MArgs::GetStringAndRemove(const TString n)
192{
193 const TString name = n.Strip(TString::kBoth);
194
195 TIter Next(fArgv);
196 TString *s = NULL;
197 while ((s=dynamic_cast<TString*>(Next())))
198 if (s->BeginsWith(name))
199 {
200 TString str = s->Data()+s->Index(name)+name.Length();
201 delete fArgv->Remove(dynamic_cast<TObject*>(s));
202 return str;
203 }
204 return 0;
205}
206
207// --------------------------------------------------------------------------
208//
209// Return the Integer corresponding to the i-th argument. This is ment
210// for enumerations like
211// executable 1 7 2
212// GetArgumentInt(1) will return 7
213//
214Int_t MArgs::GetArgumentInt(Int_t i) const
215{
216 return atoi(GetArgumentStr(i));
217}
218
219// --------------------------------------------------------------------------
220//
221// Return the floating point value corresponding to the i-th argument.
222// This is ment for enumerations like
223// executable 1.7 7.5 2.3
224// GetArgumentFloat(1) will return 7.5
225//
226Float_t MArgs::GetArgumentFloat(Int_t i) const
227{
228 return atof(GetArgumentStr(i));
229}
230
231// --------------------------------------------------------------------------
232//
233// Return the TString corresponding to the i-th argument.
234// This is ment for enumerations like
235// executable file1 file2 file3
236// GetArgumentStr(1) will return "file2"
237// Only arguments without a trailing '-' are considered
238//
239TString MArgs::GetArgumentStr(Int_t i) const
240{
241 Int_t num = 0;
242
243 TIter Next(fArgv);
244 TString *s = NULL;
245 while ((s=dynamic_cast<TString*>(Next())))
246 {
247 if (s->BeginsWith("-"))
248 continue;
249
250 if (i==num++)
251 return *s;
252 }
253
254 return "";
255}
256
257// --------------------------------------------------------------------------
258//
259// return the number of arguments without a trainling '-'
260//
261Int_t MArgs::GetNumArguments() const
262{
263 Int_t num = 0;
264
265 TIter Next(fArgv);
266 TString *s = NULL;
267 while ((s=dynamic_cast<TString*>(Next())))
268 if (!s->BeginsWith("-"))
269 num++;
270
271 return num;
272}
273
274// --------------------------------------------------------------------------
275//
276// return the number of arguments with a trainling '-'
277//
278Int_t MArgs::GetNumOptions() const
279{
280 Int_t num = 0;
281
282 TIter Next(fArgv);
283 TString *s = NULL;
284 while ((s=dynamic_cast<TString*>(Next())))
285 if (s->BeginsWith("-"))
286 num++;
287
288 return num;
289}
290
291// --------------------------------------------------------------------------
292//
293// return the total number of entries
294//
295Int_t MArgs::GetNumEntries() const
296{
297 return fArgv->GetSize();
298}
299
300// --------------------------------------------------------------------------
301//
302// Checks whether an argument beginning with 'n' is existing, eg:
303// executable -value5
304// executable -value
305// HasOption("-value") will return true in both cases
306//
307Bool_t MArgs::Has(const TString n) const
308{
309 const TString name = n.Strip(TString::kBoth);
310
311 TIter Next(fArgv);
312 TString *s = NULL;
313 while ((s=dynamic_cast<TString*>(Next())))
314 if (s->BeginsWith(name))
315 return kTRUE;
316 return kFALSE;
317}
318
319// --------------------------------------------------------------------------
320//
321// Checks whether an argument beginning with 'n' is existing, eg:
322// executable -value5
323// HasOption("-value") will return false
324// executable -value
325// HasOption("-value") will return true
326//
327Bool_t MArgs::HasOnly(const TString n) const
328{
329 const TString name = n.Strip(TString::kBoth);
330
331 TIter Next(fArgv);
332 TString *s = NULL;
333 while ((s=dynamic_cast<TString*>(Next())))
334 if (*s==name)
335 return kTRUE;
336 return kFALSE;
337}
338
339// --------------------------------------------------------------------------
340//
341// Checks whether an argument beginning with 'n' is exists and a
342// corresponding option is available, eg.
343// executable -value5
344// HasOption("-value") will return true
345// but:
346// executable -value
347// HasOption("-value") will return false
348//
349Bool_t MArgs::HasOption(const TString n) const
350{
351 const TString name = n.Strip(TString::kBoth);
352
353 TIter Next(fArgv);
354 TString *s = NULL;
355 while ((s=dynamic_cast<TString*>(Next())))
356 if (s->BeginsWith(name) && s->Length()>name.Length())
357 return kTRUE;
358 return kFALSE;
359}
360
361// --------------------------------------------------------------------------
362//
363// Checks whether an argument beginning with 'n' is exists and a
364// corresponding option is available, eg.
365// executable -value5
366// HasOption("-value") will return false
367// but:
368// executable -value
369// HasOption("-value") will return true
370//
371// The argument is removed from the internal list.
372//
373Bool_t MArgs::HasOnlyAndRemove(const TString n)
374{
375 const TString name = n.Strip(TString::kBoth);
376
377 TIter Next(fArgv);
378 TString *s = NULL;
379 while ((s=dynamic_cast<TString*>(Next())))
380 if (*s==name)
381 {
382 delete fArgv->Remove(dynamic_cast<TObject*>(s));
383 return kTRUE;
384 }
385
386 return kFALSE;
387}
Note: See TracBrowser for help on using the repository browser.