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

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