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

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