68 #include "RConfigure.h" 85 static struct BoolNameTable_t {
114 TEnvParser(
TEnv *
e, FILE *
f) : fIfp(f), fEnv(e) { }
115 virtual ~TEnvParser() { }
125 void TEnvParser::Parse()
132 while ((c = fgetc(fIfp)) != EOF) {
138 KeyValue(name, value, type);
180 if (c !=
' ' && c !=
'\t')
193 state = (c ==
':') ? 3 : 4;
197 state = (c ==
')') ? 6 : 5;
217 KeyValue(name, value, type);
227 class TReadEnvParser :
public TEnvParser {
233 TReadEnvParser(
TEnv *
e, FILE *
f,
EEnvLevel l) : TEnvParser(e, f), fLevel(l) { }
235 { fEnv->SetValue(name, value, fLevel, type); }
241 class TWriteEnvParser :
public TEnvParser {
247 TWriteEnvParser(
TEnv *
e, FILE *
f, FILE *of) : TEnvParser(e, f), fOfp(of) { }
258 TEnvRec *er = fEnv->Lookup(name);
263 fprintf(fOfp,
"%s", value.
Data());
274 : fName(n),
fType(t), fLevel(l)
289 if (
fValue != v && !ignoredup)
291 "duplicate entry <%s=%s> for level %d; ignored",
fName.
Data(),
v,
l);
327 char *
v, *vorg =
StrDup(value);
332 while ((s1 = (
char*)strstr(v,
"$("))) {
334 s2 = (
char*)strchr(s1,
')');
341 if (vv) len += strlen(vv);
352 int nch = strlen(v) + len;
353 char *nv =
new char[nch];
356 while ((s1 = (
char*)strstr(v,
"$("))) {
361 s2 = (
char*)strchr(s1,
')');
364 if (vv) strlcat(nv, vv,nch);
369 if (*v) strlcat(nv, v,nch);
396 if (!name || !name[0] || !
gSystem)
436 haveProgName =
kTRUE;
442 aname +=
"."; aname +=
name;
447 aname +=
"."; aname +=
name;
454 if (er == 0 && haveProgName &&
gProgName) {
458 if (er == 0 &&
gROOT) {
459 aname =
gROOT->GetName(); aname +=
"."; aname +=
name;
463 aname =
"*.*."; aname +=
name;
467 aname =
"*."; aname +=
name;
486 char buf2[512], *cp2 = buf2;
488 while (isspace((
int)*cp))
492 if (isdigit((
int)*cp) || *cp ==
'-' || *cp ==
'+')
494 while (isalpha((
int)*cp))
495 *cp2++ = toupper((
int)*cp++);
498 if (strcmp(buf2, bt->fName) == 0)
515 if (val == 0.0 && cp == endptr)
540 if (!fTable)
return 0;
549 if (!opt || !opt[0]) {
554 if (!strcmp(opt,
"global"))
556 if (!strcmp(opt,
"user"))
558 if (!strcmp(opt,
"local"))
571 static const char *lc[] = {
"Global",
"User",
"Local",
"Changed",
"All" };
573 while ((er = (
TEnvRec*) next()))
585 if (!fname || !fname[0]) {
586 Error(
"ReadFile",
"no file name specified");
591 if ((ifp = fopen(fname,
"r"))) {
610 if (!fname || !fname[0]) {
611 Error(
"WriteFile",
"no file name specified");
616 Error(
"WriteFile",
"TEnv table is empty");
621 if ((ofp = fopen(fname,
"w"))) {
624 while ((er = (
TEnvRec*) next()))
632 Error(
"WriteFile",
"cannot open %s for writing", fname);
643 Error(
"Save",
"no resource file name specified");
658 Error(
"SaveLevel",
"no resource file name specified");
663 Error(
"SaveLevel",
"TEnv table is empty");
686 if ((ofp = fopen(
Form(
"%s.new", rootrcdir.
Data()),
"w"))) {
687 ifp = fopen(rootrcdir.
Data(),
"r");
689 ifp = fopen(rootrcdir.
Data(),
"w");
695 if (ifp || (ifp = fopen(rootrcdir.
Data(),
"r"))) {
701 while ((er = (
TEnvRec*) next())) {
706 if (er->
fLevel == level) {
721 Error(
"SaveLevel",
"cannot write to file %s", rootrcdir.
Data());
733 const char *nam =
name;
735 if (name[0] ==
'+') {
742 er->
ChangeValue(value, type, level, append, fIgnoreDup);
744 fTable->Add(
new TEnvRec(nam, value, type, level));
759 SetValue(nm, val, level);
761 SetValue(name,
"1", level);
769 SetValue(name,
Form(
"%d", value));
777 SetValue(name,
Form(
"%g", value));
virtual const char * GetName() const
Returns name of object.
virtual const char * WorkingDirectory()
Return working directory.
const char * Getvalue(const char *name)
Returns the character value for a named resource.
static struct BoolNameTable_t gBoolNames[]
virtual const char * HomeDirectory(const char *userName=0)
Return the user's home directory.
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
The TEnv class reads config files, by default named .rootrc.
void ChangeValue(const char *v, const char *t, EEnvLevel l, Bool_t append=kFALSE, Bool_t ignoredup=kFALSE)
Change the value of a resource.
virtual void SetValue(const char *name, const char *value, EEnvLevel level=kEnvChange, const char *type=0)
Set the value of a resource or create a new resource.
virtual int Rename(const char *from, const char *to)
Rename a file.
virtual ~TEnv()
Delete the resource table.
THashList implements a hybrid collection class consisting of a hash table and a list to store TObject...
virtual void PrintEnv(EEnvLevel level=kEnvAll) const
Print all resources for a certain level (global, user, local, changed).
virtual Int_t WriteFile(const char *fname, EEnvLevel level=kEnvAll)
Write resource records to file fname for a certain level.
void Clear()
Clear string without changing its capacity.
virtual const char * Getenv(const char *env)
Get environment variable.
TString & Append(const char *cs)
Bool_t IgnoreDuplicates(Bool_t ignore)
If set to true, no warnings in case of duplicates are issued.
virtual void Print(Option_t *option="") const
Print all resources or the global, user or local resources separately.
virtual Int_t ReadFile(const char *fname, EEnvLevel level)
Read and parse the resource file for a certain level.
virtual void Save()
Write the resource files for each level.
R__EXTERN const char * gProgName
virtual TObject * FindObject(const char *name) const
Must be redefined in derived classes.
R__EXTERN TSystem * gSystem
friend class TWriteEnvParser
virtual Int_t GetValue(const char *name, Int_t dflt)
Returns the integer value for a resource.
virtual TEnvRec * Lookup(const char *n)
Loop over all resource records and return the one with name.
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
char * Form(const char *fmt,...)
char * StrDup(const char *str)
Duplicate the string str.
Int_t Compare(const TObject *obj) const
Comparison function for resources.
TString ExpandValue(const char *v)
Replace all strings by the value defined in the shell (obtained via TSystem::Getenv()).
static const TString & GetEtcDir()
Get the sysconfig directory in the installation. Static utility function.
friend class TReadEnvParser
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
int CompareTo(const char *cs, ECaseCompare cmp=kExact) const
Compare a string to char *cs2.
virtual void SaveLevel(EEnvLevel level)
Write the resource file for a certain level.
Mother of all ROOT objects.
virtual char * ConcatFileName(const char *dir, const char *name)
Concatenate a directory and a file name. User must delete returned string.
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
const char * Data() const