source: trunk/src/ExRootConfReader.cc@ 23

Last change on this file since 23 was 2, checked in by Pavel Demin, 16 years ago

first commit

File size: 8.3 KB
Line 
1
2/** \class ExRootConfReader
3 *
4 * Class handling output ROOT tree
5 *
6 * $Date: 2008-06-04 13:57:54 $
7 * $Revision: 1.1 $
8 *
9 *
10 * \author P. Demin - UCL, Louvain-la-Neuve
11 *
12 */
13
14#include "ExRootAnalysis/ExRootConfReader.h"
15
16#include "tcl/tcl.h"
17
18#include <iostream>
19#include <fstream>
20#include <string>
21
22using namespace std;
23
24static Tcl_ObjCmdProc ModuleObjCmdProc;
25
26//------------------------------------------------------------------------------
27
28ExRootConfReader::ExRootConfReader() :
29 fTclInterp(0)
30{
31 fTclInterp = Tcl_CreateInterp();
32
33 Tcl_CreateObjCommand(fTclInterp, "module", ModuleObjCmdProc, this, 0);
34}
35
36//------------------------------------------------------------------------------
37
38ExRootConfReader::~ExRootConfReader()
39{
40 Tcl_DeleteInterp(fTclInterp);
41}
42
43//------------------------------------------------------------------------------
44
45void ExRootConfReader::ReadFile(const char *fileName)
46{
47/*
48 ifstream infile(fileName);
49 string cmdBuffer = string(istreambuf_iterator<char>(infile), istreambuf_iterator<char>());
50
51 Tcl_Obj *cmdObjPtr = Tcl_NewObj();
52 cmdObjPtr->bytes = const_cast<char *>(cmdBuffer.c_str());
53 cmdObjPtr->length = cmdBuffer.size();
54*/
55
56 ifstream infile(fileName, ios::in | ios::ate);
57 int file_length = infile.tellg();
58 infile.seekg(0, ios::beg);
59 infile.clear();
60 char *cmdBuffer = new char[file_length];
61 infile.read(cmdBuffer, file_length);
62
63 Tcl_Obj *cmdObjPtr = Tcl_NewObj();
64 cmdObjPtr->bytes = cmdBuffer;
65 cmdObjPtr->length = file_length;
66
67 Tcl_IncrRefCount(cmdObjPtr);
68
69 if(Tcl_EvalObj(fTclInterp, cmdObjPtr) != TCL_OK)
70 {
71 cerr << endl;
72 cerr << "** ERROR: cannot read configuration file" << endl;
73 cerr << Tcl_GetStringResult(fTclInterp) << endl;
74 }
75
76 cmdObjPtr->bytes = 0;
77 cmdObjPtr->length = 0;
78
79 Tcl_DecrRefCount(cmdObjPtr);
80
81 delete[] cmdBuffer;
82}
83
84//------------------------------------------------------------------------------
85
86ExRootConfParam ExRootConfReader::GetParam(const char *name)
87{
88 Tcl_Obj *object;
89 Tcl_Obj *variableName = Tcl_NewStringObj(const_cast<char *>(name),-1);
90 object = Tcl_ObjGetVar2(fTclInterp, variableName, 0, TCL_GLOBAL_ONLY);
91 return ExRootConfParam(name, object, fTclInterp);
92}
93
94//------------------------------------------------------------------------------
95
96int ExRootConfReader::GetInt(const char *name, int defaultValue, int index)
97{
98 ExRootConfParam object = GetParam(name);
99 if(index >= 0)
100 {
101 object = object[index];
102 }
103
104 return object.GetInt(defaultValue);
105}
106
107//------------------------------------------------------------------------------
108
109long ExRootConfReader::GetLong(const char *name, long defaultValue, int index)
110{
111 ExRootConfParam object = GetParam(name);
112 if(index >= 0)
113 {
114 object = object[index];
115 }
116
117 return object.GetLong(defaultValue);
118}
119
120//------------------------------------------------------------------------------
121
122double ExRootConfReader::GetDouble(const char *name, double defaultValue, int index)
123{
124 ExRootConfParam object = GetParam(name);
125 if(index >= 0)
126 {
127 object = object[index];
128 }
129
130 return object.GetDouble(defaultValue);
131}
132
133//------------------------------------------------------------------------------
134
135bool ExRootConfReader::GetBool(const char *name, bool defaultValue, int index)
136{
137 ExRootConfParam object = GetParam(name);
138 if(index >= 0)
139 {
140 object = object[index];
141 }
142
143 return object.GetBool(defaultValue);
144}
145
146//------------------------------------------------------------------------------
147
148const char *ExRootConfReader::GetString(const char *name, const char *defaultValue, int index)
149{
150 ExRootConfParam object = GetParam(name);
151 if(index >= 0)
152 {
153 object = object[index];
154 }
155
156 return object.GetString(defaultValue);
157}
158
159//------------------------------------------------------------------------------
160
161void ExRootConfReader::AddModule(const char *className, const char *moduleName)
162{
163 ExRootTaskMap::iterator itMoudles = fModules.find(moduleName);
164
165 if(itMoudles != fModules.end())
166 {
167 cout << "** WARNING: module '" << moduleName << "' is already configured.";
168 cout << " Only first entry will be used." << endl;
169 }
170 else
171 {
172 fModules.insert(make_pair(moduleName, className));
173 cout << "** INFO: adding module ";
174 cout << className << " \t ";
175 cout << moduleName << endl;
176 }
177}
178
179//------------------------------------------------------------------------------
180
181int ModuleObjCmdProc(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
182{
183 if(objc < 3)
184 {
185/*
186 Tcl_SetResult(interp, "wrong # args: should be \"module className moduleName arg ?arg...?\"", 0);
187*/
188 Tcl_WrongNumArgs(interp, 1, objv, "className moduleName ?arg...?");
189 return TCL_ERROR;
190 }
191
192 ExRootConfReader *test = (ExRootConfReader*) clientData;
193
194 // add module to a list of modules to be created
195
196 test->AddModule(Tcl_GetStringFromObj(objv[1], 0), Tcl_GetStringFromObj(objv[2], 0));
197
198 if(objc > 3)
199 {
200 Tcl_Obj *object = Tcl_NewListObj(0, 0);
201 Tcl_ListObjAppendElement(interp, object, Tcl_NewStringObj("namespace", -1));
202 Tcl_ListObjAppendElement(interp, object, Tcl_NewStringObj("eval", -1));
203 Tcl_ListObjAppendList(interp, object, Tcl_NewListObj(objc-2, objv+2));
204
205 return Tcl_GlobalEvalObj(interp, object);
206 }
207
208 return TCL_OK;
209}
210
211//------------------------------------------------------------------------------
212
213ExRootConfParam::ExRootConfParam(const char *name, Tcl_Obj *object, Tcl_Interp *interp) :
214 fName(name), fObject(object), fTclInterp(interp)
215{
216}
217
218//------------------------------------------------------------------------------
219
220int ExRootConfParam::GetInt(int defaultValue)
221{
222 int result = defaultValue;
223 if(fObject && TCL_OK != Tcl_GetIntFromObj(fTclInterp, fObject, &result))
224 {
225 cerr << endl;
226 cerr << "** ERROR: parameter '"<< fName << "' is not an integer." << endl;
227 cerr << "** ERROR: " << fName << " = " << Tcl_GetStringFromObj(fObject, 0) << endl;
228 }
229 return result;
230}
231
232//------------------------------------------------------------------------------
233
234long ExRootConfParam::GetLong(long defaultValue)
235{
236 long result = defaultValue;
237 if(fObject && TCL_OK != Tcl_GetLongFromObj(fTclInterp, fObject, &result))
238 {
239 cerr << endl;
240 cerr << "** ERROR: parameter '"<< fName << "' is not a long integer." << endl;
241 cerr << "** ERROR: " << fName << " = " << Tcl_GetStringFromObj(fObject, 0) << endl;
242 }
243 return result;
244}
245
246//------------------------------------------------------------------------------
247
248double ExRootConfParam::GetDouble(double defaultValue)
249{
250 double result = defaultValue;
251 if(fObject && TCL_OK != Tcl_GetDoubleFromObj(fTclInterp, fObject, &result))
252 {
253 cerr << endl;
254 cerr << "** ERROR: parameter '"<< fName << "' is not a number." << endl;
255 cerr << "** ERROR: " << fName << " = " << Tcl_GetStringFromObj(fObject, 0) << endl;
256 }
257 return result;
258}
259
260//------------------------------------------------------------------------------
261
262bool ExRootConfParam::GetBool(bool defaultValue)
263{
264 int result = defaultValue;
265 if(fObject && TCL_OK != Tcl_GetBooleanFromObj(fTclInterp, fObject, &result))
266 {
267 cerr << endl;
268 cerr << "** ERROR: parameter '"<< fName << "' is not a boolean." << endl;
269 cerr << "** ERROR: " << fName << " = " << Tcl_GetStringFromObj(fObject, 0) << endl;
270 }
271 return result;
272}
273
274//------------------------------------------------------------------------------
275
276const char *ExRootConfParam::GetString(const char *defaultValue)
277{
278 const char *result = defaultValue;
279 if(fObject) result = Tcl_GetStringFromObj(fObject, 0);
280 return result;
281}
282
283//------------------------------------------------------------------------------
284
285int ExRootConfParam::GetSize()
286{
287 int length = 0;
288 if(fObject && TCL_OK != Tcl_ListObjLength(fTclInterp, fObject, &length))
289 {
290 cerr << endl;
291 cerr << "** ERROR: parameter '"<< fName << "' is not a list." << endl;
292 cerr << "** ERROR: " << fName << " = " << Tcl_GetStringFromObj(fObject, 0) << endl;
293 }
294 return length;
295}
296
297//------------------------------------------------------------------------------
298
299ExRootConfParam ExRootConfParam::operator[](int index)
300{
301 Tcl_Obj *object = 0;
302 if(fObject && TCL_OK != Tcl_ListObjIndex(fTclInterp, fObject, index, &object))
303 {
304 cerr << endl;
305 cerr << "** ERROR: parameter '"<< fName << "' is not a list." << endl;
306 cerr << "** ERROR: " << fName << " = " << Tcl_GetStringFromObj(fObject, 0) << endl;
307 }
308 return ExRootConfParam(fName, object, fTclInterp);
309}
310
311
Note: See TracBrowser for help on using the repository browser.