18 # include <TObjArray.h> 19 # include <TObjString.h> 28 T& str2val(
const char* str,
T& v)
30 std::stringstream s(str);
33 if (str[1] ==
'x' || str[1] ==
'X')
40 bool& str2val(
const char* str,
bool& v)
43 str[0] ==
'y' || str[0] ==
'Y' ||
44 str[0] ==
't' || str[0] ==
'T')
47 str[0] ==
'n' || str[0] ==
'N' ||
48 str[0] ==
'f' || str[0] ==
'F')
106 if (&other ==
this)
return *
this;
148 Error(
"Option::Set",
"Option %s needs an argument",
fName.Data());
216 std::ios::fmtflags oldf = o.setf(std::ios::left);
217 o << std::setw(w) << tmp <<
" " <<
fDescription <<
" [";
220 o <<
"]" << std::endl;
232 std::ios::fmtflags oldf = o.setf(std::ios::left);
233 o << std::setw(w) <<
fName <<
": ";
245 void Store(std::ostream& o,
bool quote=
true)
const 249 o <<
"=" << (quote ?
"\"" :
"") <<
fValue << (quote ?
"\"" :
"");
271 Link() : fPrev(0), fNext(0), fThis(0) {}
273 : fPrev(next ? next->fPrev : 0),
277 if (fPrev) fPrev->
fNext =
this;
278 if (fNext) fNext->
fPrev =
this;
281 if (fPrev) fPrev->
fNext = fNext;
282 if (fNext) fNext->
fPrev = fPrev;
293 if (&o ==
this)
return *
this;
310 : fList(0), fDesc(other.fDesc)
348 if (&other ==
this)
return *
this;
376 if (fList == 0) fList = cur;
377 if (prev) prev->
fNext = cur;
384 std::cout <<
"Link=" << link;
386 std::cout <<
" prev=" << link->
fPrev 387 <<
" next=" << link->
fNext 388 <<
" obj=" << link->
fThis;
390 std::cout <<
" name=" << link->fThis->
fName;
392 std::cout <<std::endl;
403 const Link* cur = fList;
405 while (cur && cur->
fThis) {
428 Warning(
"OptionList::Add",
"Option %s already registered", name.Data());
433 o =
new Option(name, arg, desc, val);
451 n =
new Link(cur, o);
452 if (cur == fList) fList = n;
476 return Add(name,
"", desc,
"");
491 Option* o = Add(name,
"", desc,
"");
492 if (o) o->
Set(def ?
"true" :
"false");
514 return Add(name, arg, desc, Form(
"0x%x", uval));
516 return Add(name, arg, desc, Form(
"%d", val));
536 ULong64_t uval = val;
537 return Add(name, arg, desc, Form(
"0x%llx", uval));
539 return Add(name, arg, desc, Form(
"%lld", val));
556 return Add(name, arg, desc, Form(
"%lg", val));
572 if (fList == cur) fList = cur->
fNext;
587 return (o && o->
IsSet());
615 if (!o)
return false;
618 va_start(ap, format);
634 if (!o)
return false;
690 if (!o)
return def.Data();
720 static char buf[1024];
723 va_start(ap, format);
724 vsnprintf(buf, 1023, format, ap);
762 if (asHex)
Set(name, Form(
"0x%x", val));
763 else Set(name, Form(
"%d", val));
774 ULong64_t uval = val;
775 if (asHex)
Set(name, Form(
"0x%llx", uval));
776 else Set(name, Form(
"%lld", val));
786 Set(name, Form(
"%lg", val));
818 while ((o = static_cast<TObjString*>(next()))) {
822 Int_t eq = s.Index(
"=");
825 val = s(eq+1, s.Length()-eq-1);
832 Warning(
"OptionList::Parse",
"Unknown option: \"%s\"", s.Data());
835 if (opt->HasArg() && val.IsNull()) {
836 Warning(
"OptionList::Parse",
837 "Option %s needs an argument, using default %s",
838 key.Data(), opt->fValue.Data());
856 const Link* cur = fList;
859 nWidth = TMath::Max(nWidth, opt->
NameWidth());
860 aWidth = TMath::Max(aWidth, opt->
ArgWidth());
869 const Int_t max=70)
const 871 if (fDesc.IsNull())
return;
872 Int_t indent= prefix.Length();
875 TObjString* word = 0;
877 o << std::endl << prefix;
878 while ((word = static_cast<TObjString*>(next()))) {
880 if (w.Contains(
"\n")) {
881 Int_t idx = w.Index(
"\n");
882 w.ReplaceAll(
"\n",Form(
"%s\n",prefix.Data()));
884 cur = indent + (w.Length()-idx-1);
887 if (cur + w.Length() > max) {
888 o << std::endl << prefix;
892 cur += w.Length() + 1;
894 if (cur != indent) o << std::endl;
903 void Help(std::ostream& o,
const char* prefix=
" ")
const 905 Int_t nWidth, aWidth;
906 Widest(nWidth, aWidth);
907 if (aWidth > 0) nWidth += aWidth+1;
909 const Link* cur = fList;
913 opt->
Help(o, nWidth);
917 pre.ReplaceAll(
"-",
" ");
918 pre.ReplaceAll(
" ",
"");
919 if (!pre.IsNull()) pre.Append(
" ");
928 void Show(std::ostream& o,
const char* prefix=
" ")
const 930 Int_t nWidth, aWidth;
931 Widest(nWidth, aWidth);
934 const Link* cur = fList;
938 opt->
Show(o, nWidth);
951 void Store(std::ostream& o,
const char* prefix=
"",
952 const char* delim=
",",
bool quote=
true,
953 bool onlySet=
false)
const 955 Int_t nWidth, aWidth;
956 Widest(nWidth, aWidth);
958 const Link* cur = fList;
961 if ((!opt->
HasArg() || onlySet) && !opt->
IsSet()) {
966 opt->
Store(o, quote);
975 static void Test(
const char* opts=
"")
978 l.
Add(
"int",
"NUMBER",
"Integer",
"42");
979 l.
Add(
"float",
"NUMBER",
"Floating point",
"3.14");
980 l.
Add(
"bool",
"Flag");
981 l.
Add(
"hex",
"NUMBER",
"Hexadecimal",
"0xdead");
982 l.
Add(
"string",
"STRING",
"A string",
"Hello, world");
983 l.
Show(std::cout,
"\t");
985 std::cout <<
"Find" << std::endl;
990 std::cout <<
"SetF" << std::endl;
991 l.
SetF(
"float",
"%f", 2.17);
992 l.
Show(std::cout,
"\t");
994 std::cout <<
"GetF" << std::endl;
996 l.
GetF(
"float",
"%f", &f);
997 std::cout <<
"\tf=" << f << std::endl;
998 std::cout <<
"Remove" << std::endl;
1000 l.
Show(std::cout,
"\t");
1002 std::cout <<
"Set" << std::endl;
1004 l.
Set(
"hex", 0xbeef,
true);
1005 l.
Set(
"bool",
"false");
1006 l.
Show(std::cout,
"\t");
1008 std::cout <<
"Copy" << std::endl;
1010 c.
Show(std::cout,
"\t");
1012 std::cout <<
"Parse" << std::endl;
1014 c.
Show(std::cout,
"\t");
1015 std::cout <<
"End of test" << std::endl;
Double_t AsDouble(const TString &name, Double_t def=0) const
void HelpDesc(std::ostream &o, const TString &prefix="", const Int_t max=70) const
Link & operator=(const Link &o)
void Help(std::ostream &o, const char *prefix=" ") const
void DebugLink(const Link *link) const
void Help(std::ostream &o, Int_t w=-1) const
const TString & Get() const
TString format
file names tag, basically the trigger and calorimeter combination
Option * Find(const TString &name) const
Bool_t AsBool(const TString &name) const
Option & operator=(const Option &other)
void Store(std::ostream &o, const char *prefix="", const char *delim=",", bool quote=true, bool onlySet=false) const
Int_t AsInt(const TString &name, Int_t def=0) const
const char * AsString(const TString &name, const TString &def="") const
Option * Add(const TString &name, const TString &arg, const TString &desc, Int_t val, Bool_t asHex=false)
void Store(std::ostream &o, bool quote=true) const
UShort_t T(UShort_t m, UShort_t t)
Long64_t AsLong(const TString &name, Long64_t def=0) const
const TString & Get(const TString &name) const
Bool_t Has(const TString &name) const
Bool_t Parse(const TString &tmp, const TString &delims)
Double_t AsDouble() const
void Set(const TString &name, Double_t val)
Bool_t Parse(const TCollection *opts, Bool_t ignoreUnknown=false)
static void Test(const char *opts="")
Option * Add(const TString &name, const TString &desc, Bool_t def)
void Set(const TString &name, Int_t val, Bool_t asHex=false)
void Set(const TString &name, const TString &value)
void Show(std::ostream &o, Int_t w=-1) const
const TString & AsTString(const TString &name, const TString &def="") const
void SetDescription(const TString &d)
const char * AsString() const
Option * Add(const TString &name, const TString &arg, const TString &desc, const TString &val="")
OptionList(const OptionList &other)
Option(const TString &name, const TString &arg, const TString &description, const TString &value)
void Set(const TString &val)
void Set(const TString &name)
Option * Add(const TString &name, const TString &desc)
Bool_t GetF(const TString &name, const Char_t *format,...) const
OptionList & operator=(const OptionList &other)
Option * Add(const TString &name, const TString &arg, const TString &desc, Double_t val)
void Widest(Int_t &nWidth, Int_t &aWidth) const
void Show(std::ostream &o, const char *prefix=" ") const
Option(const Option &other)
Option * Add(const TString &name, const TString &arg, const TString &desc, Long64_t val, Bool_t asHex=false)
void Set(const TString &name, Long64_t val, Bool_t asHex=false)
void Copy(const OptionList &other)
void SetF(const TString &name, const Char_t *format,...)
void Remove(const TString &name)
Link(Link *next, Option *opt)