Fork me on GitHub

source: git/external/ExRootAnalysis/ExRootConfReader.cc@ fa33983

ImprovedOutputFile Timing dual_readout llp
Last change on this file since fa33983 was cab38f6, checked in by Pavel Demin <pavel.demin@…>, 10 years ago

remove svn tags and fix formatting

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