KaliVeda  1.12/06
Heavy-Ion Analysis Toolkit
KVEnv.cpp
Go to the documentation of this file.
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 
10 
11 
12 //________________________________________________________________
13 
14 
15 
22 KVEnv::KVEnv(const KVEnv& obj) : TEnv()
23 {
24  // Copy constructor
25  // This ctor is used to make a copy of an existing object (for example
26  // when a method returns an object), and it is always a good idea to
27  // implement it.
28  // If your class allocates memory in its constructor(s) then it is ESSENTIAL :-)
29 
30  obj.Copy(*this);
31 }
32 
33 
34 
35 
38 
39 KVEnv::KVEnv(const char* name) : TEnv(name)
40 {
41  // Write your code here
42 }
43 
44 
45 
48 
50 {
51  // Destructor
52  fComments.Clear();
53 }
54 
55 
56 
64 
65 void KVEnv::Copy(TObject& obj) const
66 {
67  // This method copies the current state of 'this' object into 'obj'
68  // You should add here any member variables, for example:
69  // (supposing a member variable KVEnv::fToto)
70  // CastedObj.fToto = fToto;
71  // or
72  // CastedObj.SetToto( GetToto() );
73 
74  TEnv::Copy(obj);
75  //KVEnv& CastedObj = (KVEnv&)obj;
76 }
77 
78 
81 
83 {
84  //Copy table of env to this
85  THashList* hl = 0;
86  if ((hl = env.GetTable())) {
87  TIter next(hl);
88  TEnvRec* env_rec = 0;
89  while ((env_rec = (TEnvRec*)next.Next())) {
90  SetValue(env_rec->GetName(), env_rec->GetValue());
91  }
92  }
93 
94 }
95 
96 
97 
103 
104 void KVEnv::AddCommentLine(const Char_t* line)
105 {
106  //Add a comment for the current TEnv
107  //the line will started with "# " prefix and will
108  //be ended with "\n" postfix
109  //
110  fComments.Add(new TNamed(Form("# %s\n", line), ""));
111 
112 }
113 
114 
115 
122 
123 void KVEnv::AddComments(const Char_t* comments)
124 {
125  //Add comments
126  //the comments can contains "\n" character
127  //each line contained in the comments will
128  //begin with "# " prefix and will
129  //be ended with "\n" postfix
130 
131  KVString st(comments);
132  st.Begin("\n");
133  while (!st.End()) {
134  AddCommentLine(st.Next().Data());
135  }
136 }
137 
138 
139 
141 
143 {
144  fComments.Clear();
145 }
146 
147 
148 
152 
154 {
155  //print comments as they will be written
156  //in the file
157  if (fComments.GetEntries() == 0) {
158  printf("No comments defined\n");
159  return;
160  }
161  for (Int_t ii = 0; ii < fComments.GetEntries(); ii += 1) {
162  printf("%s", fComments.At(ii)->GetName());
163  }
164 }
165 
166 
167 
173 
174 Int_t KVEnv::WriteFile(const char* fname, EEnvLevel level)
175 {
176  // Write first comments associated to the current TEnv
177  // Write resourse records to file fname for a certain level. Use
178  // level kEnvAll to write all resources. Returns -1 on case of error,
179  // 0 in case of success.
180 
181  if (!fname || !strlen(fname)) {
182  Error("WriteFile", "no file name specified");
183  return -1;
184  }
185 
186  if (!GetTable()) {
187  Error("WriteFile", "TEnv table is empty");
188  return -1;
189  }
190 
191  FILE* ofp;
192  if ((ofp = fopen(fname, "w"))) {
193  for (Int_t ii = 0; ii < fComments.GetEntries(); ii += 1) {
194  fprintf(ofp, "%s", fComments.At(ii)->GetName());
195  }
196 
197  //for (Int_t jj=0;jj<GetTable()->GetEntries();jj+=1){
198  // TEnvRec *er = (TEnvRec*) GetTable()->At(jj);
199  TIter next(GetTable());
200  TEnvRec* er;
201  while ((er = (TEnvRec*) next()))
202  if (er->GetLevel() == level || level == kEnvAll)
203  fprintf(ofp, "%-40s %s\n", Form("%s:", er->GetName()),
204  er->GetValue());
205  //}
206  fclose(ofp);
207  return 0;
208  }
209 
210  Error("WriteFile", "cannot open %s for writing", fname);
211  return -1;
212 }
213 
214 
int Int_t
ClassImp(KVPartitionList) void KVPartitionList
Initialisation.
char Char_t
EEnvLevel
kEnvAll
char * Form(const char *fmt,...)
Extension of TEnv to allow the writing of comments in the file.
Definition: KVEnv.h:16
void AddCommentLine(const Char_t *line)
Definition: KVEnv.cpp:104
void PrintComments()
Definition: KVEnv.cpp:153
void AddComments(const Char_t *comments)
Definition: KVEnv.cpp:123
virtual ~KVEnv()
Destructor.
Definition: KVEnv.cpp:49
KVEnv(const KVEnv &obj)
Definition: KVEnv.cpp:22
void ClearComments()
Definition: KVEnv.cpp:142
TList fComments
Definition: KVEnv.h:18
void Copy(TObject &obj) const
Definition: KVEnv.cpp:65
void CopyTable(TEnv &env)
Copy table of env to this.
Definition: KVEnv.cpp:82
virtual Int_t WriteFile(const char *fname, EEnvLevel level=kEnvAll)
Definition: KVEnv.cpp:174
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:72
void Begin(TString delim) const
Definition: KVString.cpp:562
Bool_t End() const
Definition: KVString.cpp:625
KVString Next(Bool_t strip_whitespace=kFALSE) const
Definition: KVString.cpp:675
virtual Int_t GetEntries() const
EEnvLevel GetLevel() const
const char * GetName() const
const char * GetValue() const
virtual void SetValue(const char *name, const char *value, EEnvLevel level=kEnvChange, const char *type=nullptr)
THashList * GetTable() const
TObject * Next()
virtual void Add(TObject *obj)
virtual TObject * At(Int_t idx) const
virtual void Clear(Option_t *option="")
virtual const char * GetName() const
virtual void Copy(TObject &object) const
virtual void Error(const char *method, const char *msgfmt,...) const
const char * Data() const
TLine * line