HARPO  5.1.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
HarpoConfigFile.cxx
Go to the documentation of this file.
1 //
2 // File HarpoConfigFile.cxx
3 //
34 #include <cstdlib>
35 #include <cstdio>
36 #include <iostream>
37 #include <getopt.h>
38 #include <libgen.h>
39 
40 #include "HarpoConfigFile.h"
41 #include "HarpoConfig.h"
42 
43 //libconfig
44 #include <libconfig.h++>
45 
46 using namespace libconfig;
47 
50 {
51  fCfgFile = (TString *) NULL;
52  lcfg = new Config();
53 }
54 
57 {
58  if ( fCfgFile != NULL ) delete fCfgFile;
59  delete lcfg;
60 }
61 
62 
63 Bool_t HarpoConfigFile::SetCfgFile(const char *fcfg)
64 {
65  if (fCfgFile != NULL) delete fCfgFile;
66  fCfgFile = new TString(fcfg);
67  std::cout << "Reading Config --> " << *fCfgFile << std::endl;
68  try {
69  lcfg->readFile(fCfgFile->Data());
70  lcfg->setAutoConvert(true); // enable float <-> int assigments
71  }
72  catch (...)
73  {
74  std::cout << "config file read failed" << std::endl;
75  return false;
76  }
77  return true;
78 }
79 
80 Bool_t HarpoConfigFile::Exist(const char * path)
81 {
82  return ((lcfg != NULL) && lcfg->exists(path) );
83 }
84 
85 Bool_t HarpoConfigFile::Lookup(const char * path,Bool_t &val)
86 {
87  if ( Exist(path) ) {
88  lcfg->lookupValue(path,val);
89  return true;
90  } else
91  return false;
92 }
93 
94 Bool_t HarpoConfigFile::Lookup(const char * path,Long64_t &val)
95 {
96  if ( ! Exist(path) ) return false;
97 
98  //std::cout << path<< "Lookup Long64_t in " << val ;
99  if (lcfg->lookupValue(path,val)) {
100  //std::cout << "long " << val << std::endl;
101  return true; // Long value
102  } else {
103  Int_t ival;
104  if (lcfg->lookupValue(path,ival)) {
105  val = ival; // Defined as long not as int
106  //std::cout << "int " << val << std::endl;
107  return true;
108  }
109  }
110  return false;
111 }
112 
113 Bool_t HarpoConfigFile::Lookup(const char * path,ULong64_t &val)
114 {
115  Long64_t lval;
116  // std::cout << path<< "Lookup ULong64_t in " << val ;
117  if (lcfg->lookupValue(path,lval)) {
118  val = (ULong64_t) lval;
119  // std::cout << "long " << val << std::endl;
120  return true; // Long value
121  } else {
122  Int_t ival;
123  if (lcfg->lookupValue(path,ival)) {
124  val = ival; // Defined as long not as int
125  // std::cout << "int " << val << std::endl;
126  return true;
127  }
128  }
129  return false;
130 }
131 
132 Bool_t HarpoConfigFile::Lookup(const char * path,Double_t &val)
133 {
134  if ( Exist(path) ) {
135  lcfg->lookupValue(path,val);
136  return true;
137  } else
138  return false;
139 }
140 
141 Bool_t HarpoConfigFile::Lookup(const char * path,TString &val)
142 {
143  if ( Exist(path) ) {
144  std::string lval;
145  lcfg->lookupValue(path,lval);
146  val = lval;
147  return true;
148  } else
149  return false;
150 }
151 
152 Bool_t HarpoConfigFile::Lookup(const char * path,TString* &val)
153 {
154  if ( Exist(path) ) {
155  std::string lval;
156  lcfg->lookupValue(path,lval);
157  if ( val != NULL ) delete val;
158  val = new TString(lval);
159  return true;
160  } else
161  return false;
162 }
163 
164 Int_t HarpoConfigFile::getSettingLength(const char * path)
165 {
166  if ( Exist(path) ) {
167  Setting &lset = lcfg->lookup(path);
168  return lset.getLength();
169  } else
170  return -1;
171 }
172 
173 Int_t HarpoConfigFile::getSettingType(const char * path)
174 {
175  if ( Exist(path) ) {
176  Setting &lset = lcfg->lookup(path);
177  return lset.getType();
178  } else
179  return -1;
180 }
181 
182 Bool_t HarpoConfigFile::LookupElem(const char * path,UInt_t index,Bool_t &val)
183 {
184  if ( Exist(path) ) {
185  Setting &lset = lcfg->lookup(path);
186  val = lset[index];
187  return true;
188  } else
189  return false;
190 }
191 
192 Bool_t HarpoConfigFile::LookupElem(const char * path,UInt_t index,Long64_t &val)
193 {
194  if ( Exist(path) ) {
195  Setting &lset = lcfg->lookup(path);
196  val = lset[index];
197  return true;
198  } else
199  return false;
200 }
201 
202 Bool_t HarpoConfigFile::LookupElem(const char * path,UInt_t index,ULong64_t &val)
203 {
204  if ( Exist(path) ) {
205  Setting &lset = lcfg->lookup(path);
206  val = lset[index];
207  return true;
208  } else
209  return false;
210 }
211 
212 Bool_t HarpoConfigFile::LookupElem(const char * path,UInt_t index,Double_t &val)
213 {
214  if ( Exist(path) ) {
215  Setting &lset = lcfg->lookup(path);
216  val = lset[index];
217  return true;
218  } else
219  return false;
220 }
221 
222 Bool_t HarpoConfigFile::LookupElem(const char * path,UInt_t index,TString &val)
223 {
224  if ( Exist(path) ) {
225  Setting &lset = lcfg->lookup(path);
226  std::string lval = lset[index];
227  val = lval;
228  return true;
229  } else
230  return false;
231 }
232 
233 Bool_t HarpoConfigFile::LookupElem(const char * path,UInt_t index,TString* &val)
234 {
235  if ( Exist(path) ) {
236  Setting &lset = lcfg->lookup(path);
237  std::string lval = lset[index];
238  // std::cout << "LookupElem " << val << " "
239  // << index << " " << lval << std::endl;
240  if ( val != NULL ) delete val;
241  val = new TString(lval);
242  //std::cout << "New Val " << val << " "
243  // << *val << std::endl;
244  return true;
245  } else
246  return false;
247 }
248 
Bool_t Exist(const char *path)
Bool_t LookupElem(const char *path, UInt_t index, Bool_t &val)
HarpoConfigFile()
Constructor which does basically nothing.
Int_t getSettingLength(const char *path)
Function to deal with arrays or lists.
Int_t getSettingType(const char *path)
Return paramener type as definef in libconfig.h++.
virtual ~HarpoConfigFile()
Destructor:
Bool_t Lookup(const char *path, Bool_t &val)
Lookup function for scalar values.
Bool_t SetCfgFile(const char *fcfg)
Set Name of libconfig configuration file.