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 
49 
51  : TEnv()
52 {
53  // Copy constructor
54  //
55  // Copies all key-value pairs from other to this
56 
57  CopyTable(other);
58 }
59 
60 
61 
64 
66 {
67  //Copy table of env to this
68  THashList* hl = 0;
69  if ((hl = env.GetTable())) {
70  TIter next(hl);
71  TEnvRec* env_rec = 0;
72  while ((env_rec = (TEnvRec*)next.Next())) {
73  SetValue(env_rec->GetName(), env_rec->GetValue());
74  }
75  }
76 
77 }
78 
79 
80 
86 
87 void KVEnv::AddCommentLine(const Char_t* line)
88 {
89  //Add a comment for the current TEnv
90  //the line will started with "# " prefix and will
91  //be ended with "\n" postfix
92  //
93  fComments.Add(new TNamed(Form("# %s\n", line), ""));
94 
95 }
96 
97 
98 
105 
106 void KVEnv::AddComments(const Char_t* comments)
107 {
108  //Add comments
109  //the comments can contains "\n" character
110  //each line contained in the comments will
111  //begin with "# " prefix and will
112  //be ended with "\n" postfix
113 
114  KVString st(comments);
115  st.Begin("\n");
116  while (!st.End()) {
117  AddCommentLine(st.Next().Data());
118  }
119 }
120 
121 
122 
124 
126 {
127  fComments.Clear();
128 }
129 
130 
131 
135 
137 {
138  //print comments as they will be written
139  //in the file
140  if (fComments.GetEntries() == 0) {
141  printf("No comments defined\n");
142  return;
143  }
144  for (Int_t ii = 0; ii < fComments.GetEntries(); ii += 1) {
145  printf("%s", fComments.At(ii)->GetName());
146  }
147 }
148 
149 
150 
156 
157 Int_t KVEnv::WriteFile(const char* fname, EEnvLevel level)
158 {
159  // Write first comments associated to the current TEnv
160  // Write resourse records to file fname for a certain level. Use
161  // level kEnvAll to write all resources. Returns -1 on case of error,
162  // 0 in case of success.
163 
164  if (!fname || !strlen(fname)) {
165  Error("WriteFile", "no file name specified");
166  return -1;
167  }
168 
169  if (!GetTable()) {
170  Error("WriteFile", "TEnv table is empty");
171  return -1;
172  }
173 
174  FILE* ofp;
175  if ((ofp = fopen(fname, "w"))) {
176  for (Int_t ii = 0; ii < fComments.GetEntries(); ii += 1) {
177  fprintf(ofp, "%s", fComments.At(ii)->GetName());
178  }
179 
180  //for (Int_t jj=0;jj<GetTable()->GetEntries();jj+=1){
181  // TEnvRec *er = (TEnvRec*) GetTable()->At(jj);
182  TIter next(GetTable());
183  TEnvRec* er;
184  while ((er = (TEnvRec*) next()))
185  if (er->GetLevel() == level || level == kEnvAll)
186  fprintf(ofp, "%-40s %s\n", Form("%s:", er->GetName()),
187  er->GetValue());
188  //}
189  fclose(ofp);
190  return 0;
191  }
192 
193  Error("WriteFile", "cannot open %s for writing", fname);
194  return -1;
195 }
196 
197 
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:87
void PrintComments()
Definition: KVEnv.cpp:136
Int_t WriteFile(const char *fname, EEnvLevel level=kEnvAll) override
Definition: KVEnv.cpp:157
void AddComments(const Char_t *comments)
Definition: KVEnv.cpp:106
void ClearComments()
Definition: KVEnv.cpp:125
TList fComments
Definition: KVEnv.h:20
KVEnv(const KVString &name="")
Definition: KVEnv.cpp:25
void CopyTable(TEnv &env)
Copy table of env to this.
Definition: KVEnv.cpp:65
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)