Fork me on GitHub

source: git/external/ExRootAnalysis/ExRootConfReader.cc@ 239e1d0

ImprovedOutputFile Timing dual_readout llp 3.3.3pre11
Last change on this file since 239e1d0 was 0a297f2, checked in by Pavel Demin <pavel.demin@…>, 9 years ago

replace strcpy and strcat with stringstream in ExRootConfReader.cc

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