]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTConfiguration.cxx
Minor cleanup of code.
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTConfiguration.cxx
1 // $Id$
2
3 /**************************************************************************
4  * This file is property of and copyright by the ALICE HLT Project        * 
5  * ALICE Experiment at CERN, All rights reserved.                         *
6  *                                                                        *
7  * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
8  *                  for The ALICE HLT Project.                            *
9  *                                                                        *
10  * Permission to use, copy, modify and distribute this software and its   *
11  * documentation strictly for non-commercial purposes is hereby granted   *
12  * without fee, provided that the above copyright notice appears in all   *
13  * copies and that both the copyright notice and this permission notice   *
14  * appear in the supporting documentation. The authors make no claims     *
15  * about the suitability of this software for any purpose. It is          *
16  * provided "as is" without express or implied warranty.                  *
17  **************************************************************************/
18
19 /** @file   AliHLTConfiguration.cxx
20     @author Matthias Richter
21     @date   
22     @brief  Implementation of HLT configurations.
23 */
24
25 // see header file for class documentation
26 // or
27 // refer to README to build package
28 // or
29 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
30
31 #if __GNUC__>= 3
32 using namespace std;
33 #endif
34
35 #include <cerrno>
36 #include "AliHLTConfiguration.h"
37 #include "AliHLTConfigurationHandler.h"
38 #include "AliHLTTask.h"
39 #include "AliHLTComponent.h"
40 #include "AliHLTComponentHandler.h"
41 #include <iostream>
42 #include <string>
43 #include "TList.h"
44
45 /** ROOT macro for the implementation of ROOT specific class methods */
46 ClassImp(AliHLTConfiguration)
47
48 AliHLTConfiguration::AliHLTConfiguration()
49   :
50   fID(""),
51   fComponent(""),
52   fStringSources(""),
53   fNofSources(-1),
54   fListSources(),
55   fListSrcElement(),
56   fArguments(""),
57   fArgc(-1),
58   fArgv(NULL)
59
60   // see header file for class documentation
61   // or
62   // refer to README to build package
63   // or
64   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
65
66   fListSrcElement=fListSources.begin();
67 }
68
69 AliHLTConfiguration::AliHLTConfiguration(const char* id, const char* component, const char* sources, const char* arguments)
70   :
71   fID(id),
72   fComponent(component),
73   fStringSources(sources),
74   fNofSources(-1),
75   fListSources(),
76   fListSrcElement(),
77   fArguments(arguments),
78   fArgc(-1),
79   fArgv(NULL)
80 {
81   // see header file for function documentation
82   fListSrcElement=fListSources.begin();
83   if (id && component) {
84     if (fgConfigurationHandler) {
85       fgConfigurationHandler->RegisterConfiguration(this);
86     } else {
87       HLTError("no configuration handler set, abort registration");
88     }
89   }
90 }
91
92 AliHLTConfiguration::AliHLTConfiguration(const AliHLTConfiguration& src)
93   :
94   TObject(),
95   AliHLTLogging(),
96   fID(src.fID),
97   fComponent(src.fComponent),
98   fStringSources(src.fStringSources),
99   fNofSources(-1),
100   fListSources(),
101   fListSrcElement(),
102   fArguments(src.fArguments),
103   fArgc(-1),
104   fArgv(NULL)
105
106   // see header file for function documentation
107   fListSrcElement=fListSources.begin();
108 }
109
110 AliHLTConfiguration& AliHLTConfiguration::operator=(const AliHLTConfiguration& src)
111
112   // see header file for function documentation
113   fID=src.fID;
114   fComponent=src.fComponent;
115   fStringSources=src.fStringSources;
116   fNofSources=-1;
117   fArguments=src.fArguments;
118   fArgc=-1;
119   fArgv=NULL;
120   return *this;
121 }
122
123 AliHLTConfiguration::~AliHLTConfiguration()
124 {
125   // see header file for function documentation
126   if (fgConfigurationHandler) {
127     if (fgConfigurationHandler->FindConfiguration(fID.Data())!=NULL) {
128       fgConfigurationHandler->RemoveConfiguration(this);
129     }
130   }
131   if (fArgv != NULL) {
132     if (fArgc>0) {
133       for (int i=0; i<fArgc; i++) {
134         delete[] fArgv[i];
135       }
136     }
137     delete[] fArgv;
138     fArgv=NULL;
139   }
140
141   vector<AliHLTConfiguration*>::iterator element=fListSources.begin();
142   while (element!=fListSources.end()) {
143     fListSources.erase(element);
144     element=fListSources.begin();
145   }
146 }
147
148 /* the global configuration handler which is used to automatically register the configuration
149  */
150 AliHLTConfigurationHandler* AliHLTConfiguration::fgConfigurationHandler=NULL;
151
152 int AliHLTConfiguration::GlobalInit(AliHLTConfigurationHandler* pHandler)
153 {
154   // see header file for function documentation
155   int iResult=0;
156   if (fgConfigurationHandler!=NULL) {
157     fgConfigurationHandler->Logging(kHLTLogWarning, "AliHLTConfiguration::GlobalInit", HLT_DEFAULT_LOG_KEYWORD, "configuration handler already initialized, overriding object %p with %p", fgConfigurationHandler, pHandler);
158   }
159   fgConfigurationHandler=pHandler;
160   return iResult;
161 }
162
163 int AliHLTConfiguration::GlobalDeinit(AliHLTConfigurationHandler* pHandler)
164 {
165   // see header file for function documentation
166   int iResult=0;
167   if (pHandler!=NULL && fgConfigurationHandler!=pHandler) {
168     fgConfigurationHandler->Logging(kHLTLogWarning, "AliHLTConfiguration::GlobalDeinit", HLT_DEFAULT_LOG_KEYWORD, "handler %p is not set, skip ...", pHandler);
169     return -EBADF;
170   }
171   fgConfigurationHandler=NULL;
172   return iResult;
173 }
174
175 const char* AliHLTConfiguration::GetName() const 
176 {
177   // see header file for function documentation
178   if (!fID.IsNull())
179     return fID.Data();
180   return TObject::GetName();
181 }
182
183 AliHLTConfiguration* AliHLTConfiguration::GetSource(const char* id)
184 {
185   // see header file for function documentation
186   AliHLTConfiguration* pSrc=NULL;
187   if (id) {
188     // first check the current element
189     if (fListSrcElement!=fListSources.end() && strcmp(id, (*fListSrcElement)->GetName())==0) {
190       pSrc=*fListSrcElement;
191       } else {
192       // check the list
193
194       pSrc=GetFirstSource();
195       while (pSrc) {
196         if (strcmp(id, pSrc->GetName())==0)
197           break;
198         pSrc=GetNextSource();
199       }
200     }
201   }
202   return pSrc;
203 }
204
205 AliHLTConfiguration* AliHLTConfiguration::GetFirstSource()
206 {
207   // see header file for function documentation
208   AliHLTConfiguration* pSrc=NULL;
209   if (fNofSources>=0 || ExtractSources()) {
210     fListSrcElement=fListSources.begin();
211     if (fListSrcElement!=fListSources.end()) pSrc=*fListSrcElement;
212   } 
213   return pSrc;
214 }
215
216 AliHLTConfiguration* AliHLTConfiguration::GetNextSource()
217 {
218   // see header file for function documentation
219   AliHLTConfiguration* pSrc=NULL;
220   if (fNofSources>0) {
221     if (fListSrcElement!=fListSources.end() && (++fListSrcElement)!=fListSources.end()) 
222       pSrc=*fListSrcElement;
223   } 
224   return pSrc;
225 }
226
227 int AliHLTConfiguration::SourcesResolved(int bAuto) 
228 {
229   // see header file for function documentation
230   int iResult=0;
231   if (fNofSources>=0 || bAuto && (iResult=ExtractSources())>=0) {
232     //HLTDebug("fNofSources=%d", fNofSources);
233     //HLTDebug("list size = %d", fListSources.size());
234     iResult=fNofSources==(int)fListSources.size();
235   }
236   return iResult;
237 }
238
239 int AliHLTConfiguration::InvalidateSource(AliHLTConfiguration* pConf)
240 {
241   // see header file for function documentation
242   int iResult=0;
243   if (pConf) {
244     vector<AliHLTConfiguration*>::iterator element=fListSources.begin();
245     while (element!=fListSources.end()) {
246       if (*element==pConf) {
247         fListSources.erase(element);
248         fListSrcElement=fListSources.end();
249         // there is no need to re-evaluate until there was a new configuration registered
250         // -> postpone the invalidation, its done in AliHLTConfigurationHandler::RegisterConfiguration
251         //InvalidateSources();
252         break;
253       }
254       element++;
255     }
256   } else {
257     iResult=-EINVAL;
258   }
259   return iResult;
260 }
261
262 void AliHLTConfiguration::PrintStatus()
263 {
264   // see header file for function documentation
265   HLTLogKeyword("configuration status");
266   HLTMessage("status of configuration \"%s\" (%p)", GetName(), this);
267   if (!fComponent.IsNull()) HLTMessage("  - component: \"%s\"", fComponent.Data());
268   else HLTMessage("  - component string invalid");
269   if (!fStringSources.IsNull()) HLTMessage("  - sources: \"%s\"", fStringSources.Data());
270   else HLTMessage("  - no sources");
271   if (SourcesResolved(1)<=0)
272     HLTMessage("    there are unresolved sources");
273   AliHLTConfiguration* pSrc=GetFirstSource();
274   while (pSrc) {
275     HLTMessage("    source \"%s\" (%p) resolved", pSrc->GetName(), pSrc);
276     pSrc=GetNextSource();
277   }
278 }
279
280 int AliHLTConfiguration::GetArguments(const char*** pArgv)
281 {
282   // see header file for function documentation
283   int iResult=0;
284   if (pArgv) {
285     if (fArgc==-1) {
286       if ((iResult=ExtractArguments())<0) {
287         HLTError("error extracting arguments for configuration %s", GetName());
288         fArgc=-EINVAL;
289       }
290     } else if (fArgc<0) {
291       HLTError("previous argument extraction failed");
292     }
293     //HLTDebug("%s fArgc %d", GetName(), fArgc);
294     iResult=fArgc;
295     *pArgv=(const char**)fArgv;
296   } else {
297     HLTError("invalid parameter");
298     iResult=-EINVAL;
299   }
300   return iResult;
301 }
302
303
304 int AliHLTConfiguration::ExtractSources()
305 {
306   // see header file for function documentation
307   int iResult=0;
308   fNofSources=0;
309   if (!fStringSources.IsNull()) {
310     vector<char*> tgtList;
311     fListSources.clear();
312     if ((iResult=InterpreteString(fStringSources.Data(), tgtList))>=0) {
313       fNofSources=tgtList.size();
314       vector<char*>::iterator element=tgtList.begin();
315       while ((element=tgtList.begin())!=tgtList.end()) {
316         if (fgConfigurationHandler) {
317           AliHLTConfiguration* pConf=fgConfigurationHandler->FindConfiguration(*element);
318           if (pConf) {
319             //HLTDebug("configuration %s (%p): source \"%s\" (%p) inserted", GetName(), this, pConf->GetName(), pConf);
320             fListSources.push_back(pConf);
321           } else {
322             HLTError("can not find source \"%s\"", (*element));
323             iResult=-ENOENT;
324           }
325         } else if (iResult>=0) {
326           iResult=-EFAULT;
327           HLTFatal("global configuration handler not initialized, can not resolve sources");
328         }
329         delete[] (*element);
330         tgtList.erase(element);
331       }
332       fListSrcElement=fListSources.begin();
333     }
334   }
335   return iResult;
336 }
337
338 int AliHLTConfiguration::ExtractArguments()
339 {
340   // see header file for function documentation
341   int iResult=0;
342   if (!fArguments.IsNull()) {
343     vector<char*> tgtList;
344     if ((iResult=InterpreteString(fArguments, tgtList))>=0) {
345       fArgc=tgtList.size();
346       //HLTDebug("configuration %s: extracted %d arguments from \"%s\"", GetName(), fArgc, fArguments);
347       if (fArgc>0) {
348         fArgv = new char*[fArgc];
349         if (fArgv) {
350           vector<char*>::iterator element=tgtList.begin();
351           int i=0;
352           while (element!=tgtList.end()) {
353             //HLTDebug("assign arguments %d (%s)", i, *element);
354             fArgv[i++]=(*element);
355             element++;
356           }
357         } else {
358           iResult=-ENOMEM;
359         }
360       }
361     }
362   } else {
363     // there are zero arguments
364     fArgc=0;
365   }
366   return iResult;
367 }
368
369 int AliHLTConfiguration::InterpreteString(const char* arg, vector<char*>& argList)
370 {
371   // see header file for function documentation
372   int iResult=0;
373   if (arg) {
374     //HLTDebug("interprete \"%s\"", arg);
375     int i=0;
376     int prec=-1;
377     int bQuote=0;
378     do {
379       //HLTDebug("%d %x", i, arg[i]);
380       if (arg[i]=='\'' && bQuote==0) {
381         bQuote=1;
382       } else if (arg[i]==0 || 
383                  (arg[i]==' ' && bQuote==0) ||
384                  (arg[i]=='\'' && bQuote==1)) {
385         bQuote=0;
386         if (prec>=0) {
387           char* pEntry= new char[i-prec+1];
388           if (pEntry) {
389             strncpy(pEntry, &arg[prec], i-prec);
390             pEntry[i-prec]=0; // terminate string
391             //HLTDebug("create string \"%s\", insert at %d", pEntry, argList.size());
392             argList.push_back(pEntry);
393           } else 
394             iResult=-ENOMEM;
395           prec=-1;
396         }
397       } else if (prec==-1) prec=i;
398     } while (arg[i++]!=0 && iResult>=0); 
399   } else {
400     iResult=-EINVAL;
401   }
402   return iResult;
403 }
404
405 int AliHLTConfiguration::FollowDependency(const char* id, TList* pTgtList)
406 {
407   // see header file for function documentation
408   int iResult=0;
409   if (id) {
410     AliHLTConfiguration* pDep=NULL;
411     if ((pDep=GetSource(id))!=NULL) {
412       if (pTgtList) pTgtList->Add(pDep);
413       iResult++;
414     } else {
415       pDep=GetFirstSource();
416       while (pDep && iResult==0) {
417         if ((iResult=pDep->FollowDependency(id, pTgtList))>0) {
418           if (pTgtList) pTgtList->AddFirst(pDep);
419           iResult++;
420         }
421         pDep=GetNextSource();
422       }
423     }
424   } else {
425     iResult=-EINVAL;
426   }
427   return iResult;
428 }
429
430