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

Last change on this file since 2273 was 2267, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 4.0 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//////////////////////////////////////////////////////////////////////////////
31#include "MArgs.h"
32
33#include <stdlib.h>
34
35#include "MLog.h"
36#include "MLogManip.h"
37
38ClassImp(MArgsEntry);
39ClassImp(MArgs);
40
41void MArgsEntry::Print(const Option_t *o) const
42{
43 gLog << all << *this << endl;
44}
45
46MArgs::MArgs(int argc, const char **argv) : fArgc(argc), fArgv()
47{
48 fName = argv[0];
49
50 fArgv.SetOwner();
51
52 for (int i=1; i<argc; i++)
53 {
54 MArgsEntry &o = *new MArgsEntry(argv[i]);
55 dynamic_cast<TString&>(o) = o.Strip(TString::kBoth);
56 fArgv.Add(&o);
57 }
58}
59
60void MArgs::Print(const Option_t *o) const
61{
62 gLog << all << underline << fName << ":" << endl;
63 fArgv.Print();
64}
65
66Int_t MArgs::GetInt(const TString name) const
67{
68 return atoi(GetString(name));
69}
70
71Double_t MArgs::GetFloat(const TString name) const
72{
73 return atof(GetString(name));
74}
75
76TString MArgs::GetString(const TString name) const
77{
78 TIter Next(&fArgv);
79 TString *s = NULL;
80 while ((s=dynamic_cast<TString*>(Next())))
81 if (s->BeginsWith(name))
82 return s->Data()+s->Index(name)+name.Length();
83 return 0;
84}
85
86Int_t MArgs::GetIntAndRemove(const TString name)
87{
88 return atoi(GetStringAndRemove(name));
89}
90
91Double_t MArgs::GetFloatAndRemove(const TString name)
92{
93 return atof(GetStringAndRemove(name));
94}
95
96TString MArgs::GetStringAndRemove(const TString n)
97{
98 const TString name = n.Strip(TString::kBoth);
99
100 TIter Next(&fArgv);
101 TString *s = NULL;
102 while ((s=dynamic_cast<TString*>(Next())))
103 if (s->BeginsWith(name))
104 {
105 TString str = s->Data()+s->Index(name)+name.Length();
106 delete fArgv.Remove(dynamic_cast<TObject*>(s));
107 return str;
108 }
109 return 0;
110}
111
112Int_t MArgs::GetArgumentInt(Int_t i) const
113{
114 return atoi(GetArgumentStr(i));
115}
116
117Float_t MArgs::GetArgumentFloat(Int_t i) const
118{
119 return atof(GetArgumentStr(i));
120}
121
122TString MArgs::GetArgumentStr(Int_t i) const
123{
124 Int_t num = 0;
125
126 TIter Next(&fArgv);
127 TString *s = NULL;
128 while ((s=dynamic_cast<TString*>(Next())))
129 {
130 if (s->BeginsWith("-"))
131 continue;
132
133 if (i==num++)
134 return *s;
135 }
136
137 return "";
138}
139
140Int_t MArgs::GetNumArguments() const
141{
142 Int_t num = 0;
143
144 TIter Next(&fArgv);
145 TString *s = NULL;
146 while ((s=dynamic_cast<TString*>(Next())))
147 if (!s->BeginsWith("-"))
148 num++;
149
150 return num;
151}
152
153Bool_t MArgs::Has(const TString n) const
154{
155 const TString name = n.Strip(TString::kBoth);
156
157 TIter Next(&fArgv);
158 TString *s = NULL;
159 while ((s=dynamic_cast<TString*>(Next())))
160 if (s->BeginsWith(name))
161 return kTRUE;
162 return kFALSE;
163}
164
165Bool_t MArgs::HasOption(const TString n) const
166{
167 const TString name = n.Strip(TString::kBoth);
168
169 TIter Next(&fArgv);
170 TString *s = NULL;
171 while ((s=dynamic_cast<TString*>(Next())))
172 if (s->BeginsWith(name) && s->Length()>name.Length())
173 return kTRUE;
174 return kFALSE;
175}
Note: See TracBrowser for help on using the repository browser.