KaliVeda
Toolkit for HIC analysis
KVEnv.cpp
1 //Created by KVClassFactory on Mon Jan 14 12:23:45 2013
2 //Author: bonnet
3 
4 #include "KVEnv.h"
5 #include "TNamed.h"
6 #include "THashList.h"
7 #include "KVString.h"
8 #include "TSystem.h"
9 
11 
12 
13 
25 KVEnv::KVEnv(const KVString& name) : TEnv(name)
26 {
27  // The TEnv constructor has a major bug: its behaviour changes depending on whether the
28  // current working directory is the user's home directory or not.
29  // If the two are *not* the same, i.e. outside the home directory, the constructor will
30  // try to read the file using just the name as given, after failing to find it in the home
31  // directory: in this case, when an absolute filename is given, the TEnv constructor
32  // succeeds in finding and opening the file. However, if the same code is executed while
33  // in the user's home directory (e.g. user launches 'kaliveda', or 'KaliVedaGUI' or 'KaliVedaSim'
34  // in her home directory), this does not work!
35  //
36  // Here we try to correct this madness.
37 
39  && (!GetTable() || !GetTable()->GetEntries()))
40  ReadFile(name, kEnvUser);
41 }
42 
43 
44 
47 
49 {
50  //Copy table of env to this
51  THashList* hl = 0;
52  if ((hl = env.GetTable())) {
53  TIter next(hl);
54  TEnvRec* env_rec = 0;
55  while ((env_rec = (TEnvRec*)next.Next())) {
56  SetValue(env_rec->GetName(), env_rec->GetValue());
57  }
58  }
59 
60 }
61 
62 
63 
69 
70 void KVEnv::AddCommentLine(const Char_t* line)
71 {
72  //Add a comment for the current TEnv
73  //the line will started with "# " prefix and will
74  //be ended with "\n" postfix
75  //
76  fComments.Add(new TNamed(Form("# %s\n", line), ""));
77 
78 }
79 
80 
81 
88 
89 void KVEnv::AddComments(const Char_t* comments)
90 {
91  //Add comments
92  //the comments can contains "\n" character
93  //each line contained in the comments will
94  //begin with "# " prefix and will
95  //be ended with "\n" postfix
96 
97  KVString st(comments);
98  st.Begin("\n");
99  while (!st.End()) {
100  AddCommentLine(st.Next().Data());
101  }
102 }
103 
104 
105 
107 
109 {
110  fComments.Clear();
111 }
112 
113 
114 
118 
120 {
121  //print comments as they will be written
122  //in the file
123  if (fComments.GetEntries() == 0) {
124  printf("No comments defined\n");
125  return;
126  }
127  for (Int_t ii = 0; ii < fComments.GetEntries(); ii += 1) {
128  printf("%s", fComments.At(ii)->GetName());
129  }
130 }
131 
132 
133 
139 
140 Int_t KVEnv::WriteFile(const char* fname, EEnvLevel level)
141 {
142  // Write first comments associated to the current TEnv
143  // Write resourse records to file fname for a certain level. Use
144  // level kEnvAll to write all resources. Returns -1 on case of error,
145  // 0 in case of success.
146 
147  if (!fname || !strlen(fname)) {
148  Error("WriteFile", "no file name specified");
149  return -1;
150  }
151 
152  if (!GetTable()) {
153  Error("WriteFile", "TEnv table is empty");
154  return -1;
155  }
156 
157  FILE* ofp;
158  if ((ofp = fopen(fname, "w"))) {
159  for (Int_t ii = 0; ii < fComments.GetEntries(); ii += 1) {
160  fprintf(ofp, "%s", fComments.At(ii)->GetName());
161  }
162 
163  //for (Int_t jj=0;jj<GetTable()->GetEntries();jj+=1){
164  // TEnvRec *er = (TEnvRec*) GetTable()->At(jj);
165  TIter next(GetTable());
166  TEnvRec* er;
167  while ((er = (TEnvRec*) next()))
168  if (er->GetLevel() == level || level == kEnvAll)
169  fprintf(ofp, "%-40s %s\n", Form("%s:", er->GetName()),
170  er->GetValue());
171  //}
172  fclose(ofp);
173  return 0;
174  }
175 
176  Error("WriteFile", "cannot open %s for writing", fname);
177  return -1;
178 }
179 
180 
int Int_t
char Char_t
EEnvLevel
kEnvUser
kEnvAll
char name[80]
char * Form(const char *fmt,...)
R__EXTERN TSystem * gSystem
Extension of TEnv to allow the writing of comments in the file.
Definition: KVEnv.h:18
void AddCommentLine(const Char_t *line)
Definition: KVEnv.cpp:70
void PrintComments()
Definition: KVEnv.cpp:119
Int_t WriteFile(const char *fname, EEnvLevel level=kEnvAll) override
Definition: KVEnv.cpp:140
void AddComments(const Char_t *comments)
Definition: KVEnv.cpp:89
void ClearComments()
Definition: KVEnv.cpp:108
TList fComments
Definition: KVEnv.h:20
void CopyTable(TEnv &env)
Copy table of env to this.
Definition: KVEnv.cpp:48
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:73
void Begin(TString delim) const
Definition: KVString.cpp:565
Bool_t End() const
Definition: KVString.cpp:634
KVString Next(Bool_t strip_whitespace=kFALSE) const
Definition: KVString.cpp:695
virtual Int_t GetEntries() const
EEnvLevel GetLevel() const
const char * GetValue() const
const char * GetName() const override
THashList * GetTable() const
virtual void SetValue(const char *name, const char *value, EEnvLevel level=kEnvChange, const char *type=nullptr)
TObject * Next()
void Clear(Option_t *option="") override
void Add(TObject *obj) override
TObject * At(Int_t idx) const override
virtual const char * GetName() const
virtual void Error(const char *method, const char *msgfmt,...) const
const char * Data() const
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
virtual Bool_t IsAbsoluteFileName(const char *dir)
TLine * line
ClassImp(TPyArg)