]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/AliExpression.cxx
Fixes for Coverity warnings (A. Mastroserio)
[u/mrichter/AliRoot.git] / STEER / AliExpression.cxx
CommitLineData
a5a091ce 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15
16/* $Id$ */
17
18///////////////////////////////////////////////////////////////////////////////
19// //
20// AliExpression Class // //
21// //
22// Helper class to evaluate the condition expressions in //
51f6d619 23// AliTrigger* classes //
a5a091ce 24// Implements a simple recursive-descent parser //
25// //
26///////////////////////////////////////////////////////////////////////////////
27
28//#include <Riostream.h>
29#include <TString.h>
30#include <TObjString.h>
31#include <TObjArray.h>
32
33#include "AliLog.h"
34#include "AliExpression.h"
35#include "AliTriggerInput.h"
36
37ClassImp( AliExpression )
38
39//______________________________________________________________________________
75e3794b 40AliExpression::AliExpression( TString exp ) :
41 TObject(),
42 fVname(""),
43 fArg1(0x0),
44 fArg2(0x0),
45 fOperator(0)
a5a091ce 46{
47 // Default constructor
48 TObjArray* tokens = Tokenize( exp );
49
50 Int_t i = -1;
51 AliExpression* e = Expression( *tokens, i );
52 // Copy !!!
53 fArg1 = e->fArg1; e->fArg1 = 0;
54 fArg2 = e->fArg2; e->fArg2 = 0;
55 fOperator = e->fOperator;
92c1978f 56 fVname = e->fVname;
a5a091ce 57 delete e;
58 delete tokens;
59}
60
61//______________________________________________________________________________
62AliExpression::~AliExpression()
63{
64 if( fArg1 ) delete fArg1;
65 if( fArg2 ) delete fArg2;
66}
67
68//______________________________________________________________________________
69AliExpression& AliExpression::operator=(const AliExpression& e)
70{
71 // AliExpression assignment operator.
72
73 if( this != &e ) {
74 TObject::operator=(e);
75 fArg1 = e.fArg1;
76 fArg2 = e.fArg2;
77 fOperator = e.fOperator;
92c1978f 78 fVname = e.fVname;
a5a091ce 79 }
80 return *this;
81}
82
83//______________________________________________________________________________
75e3794b 84AliExpression::AliExpression( int op, AliExpression* a, AliExpression* b ) :
85 TObject(),
86 fVname(""),
87 fArg1(a),
88 fArg2(b),
89 fOperator(op)
a5a091ce 90{
91 // Create a new expression
a5a091ce 92}
93
94//______________________________________________________________________________
75e3794b 95AliExpression::AliExpression( int op, AliExpression* a ) :
96 TObject(),
97 fVname(""),
98 fArg1(0),
99 fArg2(a),
100 fOperator(op)
a5a091ce 101{
102 // Create a unary expression.
a5a091ce 103}
104
105//______________________________________________________________________________
51f6d619 106Bool_t AliExpression::Value( const TObjArray &vars )
a5a091ce 107{
108 // Evaluate the expression
92c1978f 109 if ( fArg2 == 0 && fVname.IsNull() ) {
a5a091ce 110 AliError( "Expression undefined." );
111 return kFALSE;
112 }
113
114 switch (fOperator) {
115
116 case kOpOR :
117 return fArg1->Value(vars) || fArg2->Value(vars);
118
119 case kOpAND :
120 return fArg1->Value(vars) && fArg2->Value(vars);
121
122 case kOpNOT :
123 return !(fArg2->Value(vars));
124
92c1978f 125 case 0 :
126 {
127 TObject* dd = vars.FindObject( fVname.Data() );
128 if( dd == NULL ) {
129 AliError( fVname + " is undefined" );
130 return 0;
131 }
132 return ((AliTriggerInput*)dd)->GetValue();
133 }
134
a5a091ce 135 default:
136 AliError( "Illegal operator in expression!");
137
138 }
139 return kFALSE;
140}
141
142
143//______________________________________________________________________________
144TString AliExpression::Unparse() const
145{
146 // Unparse the expression
147
92c1978f 148 TString opVals[4] = { "", "&", "|","!" };
149 if ( fArg2 == 0 && fVname.IsNull() ) {
150 AliError( "Expression undefined." );
151 return "Error";
a5a091ce 152 }
153
92c1978f 154 if( fArg2 == 0 && !fVname.IsNull() ) return fVname;
155
a5a091ce 156 if (fArg1 == 0 && fArg2) {
92c1978f 157 return opVals[fOperator]+fArg2->Unparse();
a5a091ce 158 }
159 return "("+fArg1->Unparse()+" "+opVals[fOperator]+" "+fArg2->Unparse()+")";
160}
161
162//______________________________________________________________________________
163TObjArray* AliExpression::Tokenize( TString str ) const
164{
165 // tokenize the expression
166
167 // Remove spaces
168 TString str1;
169 for( Int_t i=0; i<str.Length(); i++ ) {
170 if( str[i] == ' ' ) continue;
171 str1.Append( str[i] );
172 }
173 // get variable tokens
174 TObjArray* valtok = str1.Tokenize( "!&|()" );
175 // put all variables together
176 Int_t nvt = valtok->GetEntriesFast();
177 TString sumval;
178 for( Int_t i=0; i<nvt; i++ ) {
179 TObjString* val = (TObjString*)valtok->At( i );
180 sumval.Append( val->String() );
181 }
182 // get the operator tokens
183 TObjArray* optok = str1.Tokenize( sumval.Data() );
184 // put all operator in one string
185 TString operators;
186 Int_t nopt = optok->GetEntriesFast();
187 for( Int_t i=0; i<nopt; i++ ) {
188 TObjString* val1 = (TObjString*)optok->At( i );
189 operators.Append( val1->String() );
190 }
191 // add more room to be safe
192 TObjString* blank = new TObjString(" ");
193 operators.Append( " " );
194 valtok->AddLast( blank );
195 // Now put var. and oper. together
196 TObjArray* tokens = new TObjArray( valtok->GetEntriesFast() + operators.Length() );
197 int io = 0,iv = 0;
198 int index = 0;
199 while( 1 ) {
200 TString so = operators[io];
201 int indexO = str1.Index( so, index );
202 TString val2 = ((TObjString*)valtok->At( iv ))->String();
203 int indexV = str1.Index( val2, index );
204 if( (indexO < indexV || indexV < 0) && indexO >=0 ) {
205 tokens->AddLast( new TObjString( so ) );
206 index += so.Length();
207 io++;
208 }
209 if( (indexV < indexO || indexO < 0) && indexV >=0 ) {
210 tokens->AddLast( new TObjString( val2 ) );
211 index += val2.Length();
212 iv++;
213 }
214 if( index >= str1.Length() ) break;
215 }
216
217// Debug -> Print the tokens
218// Int_t nt = tokens->GetEntriesFast();
219// for( Int_t i=0; i<nt; i++ ) {
220// TObjString* val3 = (TObjString*)tokens->At( i );
221// cout << i << " " << val3->String() << endl;
222// }
223 delete valtok;
224 delete optok;
225
226 return tokens;
227}
228
229
230//______________________________________________________________________________
231AliExpression* AliExpression::Element( TObjArray &st, Int_t &i )
232{
233 // create an element
234
235 AliExpression* result = 0;
236
237 Int_t nt = st.GetEntriesFast();
238 TString token = "@";
239 TObjString* valt;
240 // next token
241 if( i < nt-1 ) {
242 i++;
243 valt = (TObjString*)st.At( i );
244 token = valt->String();
245 }
246 // token type
247 char ttok = ( token[0]!='|' && token[0]!='&' &&
248 token[0]!='!' && token[0]!='('&& token[0]!=')') ? 'w' : token[0];
249 switch( ttok ) {
250 case 'w' :
251 result = new AliVariableExpression( token );
252 break;
253 case '(' :
254 result = Expression(st, i);
255 // next token
256 if( i < nt-1 ) {
257 i++;
258 valt = (TObjString*)st.At( i );
259 token = valt->String();
260 }
261 if( token[0] != ')' ) {
262 // i--; // push back
263 AliErrorGeneral( "AliExpression::Element", "Mismatched parenthesis." );
264 delete result;
265 result = new AliExpression;
266 }
267 break;
268 default:
269 i--; // push back
270 AliErrorGeneral( "AliExpression::Element", Form("Unexpected symbol on input. %s", token.Data()) );
a5a091ce 271 result = new AliExpression;
272 }
273 return result;
274}
275
276//______________________________________________________________________________
277AliExpression* AliExpression::Primary( TObjArray &st, Int_t &i )
278{
279 // create a primary
280
281 Int_t nt = st.GetEntriesFast();
282 TString token = "@";
283 TObjString* valt;
284 // next token
285 if( i < nt-1 ) {
286 i++;
287 valt = (TObjString*)st.At( i );
288 token = valt->String();
289 }
290
291 switch (token[0]) {
292 case '!' :
293 return new AliExpression( kOpNOT, Primary( st, i ) );
294 default:
295 i--; // push back
296 return Element( st, i );
297 }
298}
299
300//______________________________________________________________________________
301AliExpression* AliExpression::Expression( TObjArray &st,Int_t &i )
302{
303 // create an expression
304
305 AliExpression* result = 0;
306 Bool_t done = kFALSE;
307 TString token;
308 TObjString* valt;
309
310 static int stack = 0;
311 stack++;
312 Int_t nt = st.GetEntriesFast();
313
314 result = Primary( st, i );
315// cout <<"i "<<i<< "Primary " << result->Unparse() << endl;
316 while (! done) {
317 // next token
318 if( i < nt-1 ) i++;
319 else break;
320 valt = (TObjString*)st.At( i );
321 token = valt->String();
322 switch (token[0]) {
323 case '&' :
324 result = new AliExpression( kOpAND, result, Primary( st, i ) );
325// cout <<"i "<<i<< " Expression AND " << result->Unparse() << endl;
326 break;
327 case '|' :
328 result = new AliExpression( kOpOR, result, Primary( st, i ) );
329// cout <<"i "<<i<< " Expression OR " << result->Unparse() << endl;
330 break;
331 default:
332 done = kTRUE;
333 i--; // push back
334 break;
335 }
336 }
337 stack--;
338 if( stack == 0 && !token.IsNull() && token[0] == ')' ) {
339 AliErrorGeneral( "AliExpression::Expression", "To many closing parenthesis." );
340 delete result;
341 result = new AliExpression;
342 } else
343 if( stack == 0 && i< nt-1 ) {
344 AliErrorGeneral( "AliExpression::Expression", Form( "Unexpected symbol on input. %s", token.Data() ) );
345 delete result;
346 result = new AliExpression;
347 }
348 return result;
349}
350
351////////////////////////////////////////////////////////////////////////////////
352
353ClassImp( AliVariableExpression )
354
355//______________________________________________________________________________
51f6d619 356Bool_t AliVariableExpression::Value( const TObjArray& pgm )
a5a091ce 357{
358 // return the value
359 TObject* dd = pgm.FindObject( fVname.Data() );
360 if( dd == NULL ) {
361 AliError( fVname + " is undefined" );
362 return 0;
363 }
364 return ((AliTriggerInput*)dd)->GetValue();
365}
366