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

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