]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/BASE/AliHLTConfiguration.cxx
adding huffnam coding base class and data deflater using it (Thorsten)
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTConfiguration.cxx
CommitLineData
3495cce2 1// $Id$
2
4403fb69 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///**************************************************************************
3495cce2 18
4403fb69 19/// @file AliHLTConfiguration.cxx
20/// @author Matthias Richter
21/// @date 2007
22/// @brief HLT configuration description for a single component.
23/// @note The class is used in Offline (AliRoot) context
3a7c0444 24
0c0c9d99 25#if __GNUC__>= 3
3495cce2 26using namespace std;
27#endif
28
a5854ddd 29#include <cerrno>
3495cce2 30#include "AliHLTConfiguration.h"
c38ba6f9 31#include "AliHLTConfigurationHandler.h"
32#include "AliHLTTask.h"
3495cce2 33#include "AliHLTComponent.h"
34#include "AliHLTComponentHandler.h"
35#include <iostream>
a5854ddd 36#include <string>
70ed7d01 37#include "TList.h"
3495cce2 38
b22e91eb 39/** ROOT macro for the implementation of ROOT specific class methods */
3495cce2 40ClassImp(AliHLTConfiguration)
41
42AliHLTConfiguration::AliHLTConfiguration()
85869391 43 :
52c1c164 44 fID(""),
45 fComponent(""),
46 fStringSources(""),
85869391 47 fNofSources(-1),
53feaef5 48 fListSources(),
d174bfc3 49 fListSrcElementIdx(-1),
52c1c164 50 fArguments(""),
85869391 51 fArgc(-1),
032c5e5e 52 fArgv(NULL),
53 fBufferSize(-1)
3495cce2 54{
3a7c0444 55 // see header file for class documentation
56 // or
57 // refer to README to build package
58 // or
59 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
3495cce2 60}
61
032c5e5e 62AliHLTConfiguration::AliHLTConfiguration(const char* id, const char* component, const char* sources,
63 const char* arguments, const char* bufsize)
85869391 64 :
65 fID(id),
66 fComponent(component),
67 fStringSources(sources),
68 fNofSources(-1),
53feaef5 69 fListSources(),
d174bfc3 70 fListSrcElementIdx(-1),
85869391 71 fArguments(arguments),
72 fArgc(-1),
032c5e5e 73 fArgv(NULL),
74 fBufferSize(-1)
3495cce2 75{
70ed7d01 76 // see header file for function documentation
032c5e5e 77 if (bufsize) fBufferSize=ConvertSizeString(bufsize);
3495cce2 78 if (id && component) {
4403fb69 79 if (AliHLTConfigurationHandler::Instance()) {
80 AliHLTConfigurationHandler::Instance()->RegisterConfiguration(this);
85465857 81 } else {
4403fb69 82 AliHLTConfigurationHandler::MissedRegistration(id);
85465857 83 }
3495cce2 84 }
85}
86
fc455fba 87AliHLTConfiguration::AliHLTConfiguration(const AliHLTConfiguration& src)
85869391 88 :
53feaef5 89 TObject(),
90 AliHLTLogging(),
fc455fba 91 fID(src.fID),
92 fComponent(src.fComponent),
93 fStringSources(src.fStringSources),
85869391 94 fNofSources(-1),
53feaef5 95 fListSources(),
d174bfc3 96 fListSrcElementIdx(-1),
fc455fba 97 fArguments(src.fArguments),
85869391 98 fArgc(-1),
032c5e5e 99 fArgv(NULL),
100 fBufferSize(src.fBufferSize)
85869391 101{
70ed7d01 102 // see header file for function documentation
85869391 103}
104
fc455fba 105AliHLTConfiguration& AliHLTConfiguration::operator=(const AliHLTConfiguration& src)
85869391 106{
70ed7d01 107 // see header file for function documentation
d174bfc3 108 if (this==&src) return *this;
109
fc455fba 110 fID=src.fID;
111 fComponent=src.fComponent;
112 fStringSources=src.fStringSources;
113 fNofSources=-1;
114 fArguments=src.fArguments;
115 fArgc=-1;
116 fArgv=NULL;
032c5e5e 117 fBufferSize=src.fBufferSize;
85869391 118 return *this;
119}
120
3495cce2 121AliHLTConfiguration::~AliHLTConfiguration()
122{
70ed7d01 123 // see header file for function documentation
4403fb69 124 if (AliHLTConfigurationHandler::Instance()) {
125 if (AliHLTConfigurationHandler::Instance()->FindConfiguration(fID.Data())!=NULL) {
df1e5419 126 // remove the configuration from the handler if it exists
127 // but DO NOT remove the clone configuration
4403fb69 128 AliHLTConfigurationHandler::Instance()->RemoveConfiguration(this);
85465857 129 }
3495cce2 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 }
a742f6f8 140
141 vector<AliHLTConfiguration*>::iterator element=fListSources.begin();
142 while (element!=fListSources.end()) {
143 fListSources.erase(element);
144 element=fListSources.begin();
145 }
3495cce2 146}
147
70ed7d01 148const char* AliHLTConfiguration::GetName() const
149{
150 // see header file for function documentation
52c1c164 151 if (!fID.IsNull())
152 return fID.Data();
3495cce2 153 return TObject::GetName();
154}
155
156AliHLTConfiguration* AliHLTConfiguration::GetSource(const char* id)
157{
70ed7d01 158 // see header file for function documentation
3495cce2 159 AliHLTConfiguration* pSrc=NULL;
160 if (id) {
161 // first check the current element
d174bfc3 162 if (fListSrcElementIdx>=0 && fListSrcElementIdx<(int)fListSources.size() &&
163 strcmp(id, (fListSources[fListSrcElementIdx])->GetName())==0) {
164 pSrc=fListSources[fListSrcElementIdx];
3495cce2 165 } else {
166 // check the list
167
168 pSrc=GetFirstSource();
169 while (pSrc) {
170 if (strcmp(id, pSrc->GetName())==0)
171 break;
172 pSrc=GetNextSource();
173 }
174 }
175 }
176 return pSrc;
177}
178
d174bfc3 179AliHLTConfiguration* AliHLTConfiguration::GetFirstSource() const
3495cce2 180{
70ed7d01 181 // see header file for function documentation
3495cce2 182 AliHLTConfiguration* pSrc=NULL;
d174bfc3 183 if (fNofSources>0) {
184 const_cast<AliHLTConfiguration*>(this)->fListSrcElementIdx=-1;
185 pSrc=GetNextSource();
3495cce2 186 }
187 return pSrc;
188}
189
d174bfc3 190AliHLTConfiguration* AliHLTConfiguration::GetNextSource() const
3495cce2 191{
70ed7d01 192 // see header file for function documentation
3495cce2 193 AliHLTConfiguration* pSrc=NULL;
194 if (fNofSources>0) {
d174bfc3 195 if (fListSrcElementIdx+1<(int)fListSources.size()) {
196 const_cast<AliHLTConfiguration*>(this)->fListSrcElementIdx++;
197 pSrc=fListSources[fListSrcElementIdx];
198 }
3495cce2 199 }
200 return pSrc;
201}
202
d174bfc3 203int AliHLTConfiguration::SourcesResolved() const
3495cce2 204{
70ed7d01 205 // see header file for function documentation
3495cce2 206 int iResult=0;
d174bfc3 207 if (fNofSources>=0) {
3495cce2 208 iResult=fNofSources==(int)fListSources.size();
209 }
210 return iResult;
211}
212
213int AliHLTConfiguration::InvalidateSource(AliHLTConfiguration* pConf)
214{
70ed7d01 215 // see header file for function documentation
3495cce2 216 int iResult=0;
217 if (pConf) {
218 vector<AliHLTConfiguration*>::iterator element=fListSources.begin();
219 while (element!=fListSources.end()) {
220 if (*element==pConf) {
221 fListSources.erase(element);
d174bfc3 222 fListSrcElementIdx=fListSources.size();
3495cce2 223 // there is no need to re-evaluate until there was a new configuration registered
224 // -> postpone the invalidation, its done in AliHLTConfigurationHandler::RegisterConfiguration
225 //InvalidateSources();
226 break;
227 }
228 element++;
229 }
230 } else {
231 iResult=-EINVAL;
232 }
233 return iResult;
234}
235
d174bfc3 236void AliHLTConfiguration::PrintStatus() const
3495cce2 237{
70ed7d01 238 // see header file for function documentation
85465857 239 HLTLogKeyword("configuration status");
240 HLTMessage("status of configuration \"%s\" (%p)", GetName(), this);
52c1c164 241 if (!fComponent.IsNull()) HLTMessage(" - component: \"%s\"", fComponent.Data());
85465857 242 else HLTMessage(" - component string invalid");
52c1c164 243 if (!fStringSources.IsNull()) HLTMessage(" - sources: \"%s\"", fStringSources.Data());
85465857 244 else HLTMessage(" - no sources");
d174bfc3 245 if (SourcesResolved()!=1)
85465857 246 HLTMessage(" there are unresolved sources");
3495cce2 247 AliHLTConfiguration* pSrc=GetFirstSource();
248 while (pSrc) {
85465857 249 HLTMessage(" source \"%s\" (%p) resolved", pSrc->GetName(), pSrc);
3495cce2 250 pSrc=GetNextSource();
251 }
252}
253
d174bfc3 254void AliHLTConfiguration::Print(const char* option) const
255{
256 // print information
257 if (option && strcmp(option, "status")==0) {
258 PrintStatus();
259 return;
260 }
261 HLTLogKeyword("configuration");
262 HLTMessage("configuration %s: component %s, sources %s, arguments %s",
263 GetName(),
264 GetComponentID(),
265 GetSourceSettings(),
266 GetArgumentSettings()
267 );
268}
269
d489ab09 270int AliHLTConfiguration::GetArguments(const char*** pArgv) const
3495cce2 271{
70ed7d01 272 // see header file for function documentation
3495cce2 273 int iResult=0;
0c0c9d99 274 if (pArgv) {
9ce4bf4a 275 if (fArgc==-1) {
d489ab09 276 if ((iResult=const_cast<AliHLTConfiguration*>(this)->ExtractArguments())<0) {
9ce4bf4a 277 HLTError("error extracting arguments for configuration %s", GetName());
9ce4bf4a 278 }
279 } else if (fArgc<0) {
280 HLTError("previous argument extraction failed");
281 }
282 //HLTDebug("%s fArgc %d", GetName(), fArgc);
0c0c9d99 283 iResult=fArgc;
3495cce2 284 *pArgv=(const char**)fArgv;
285 } else {
9ce4bf4a 286 HLTError("invalid parameter");
3495cce2 287 iResult=-EINVAL;
288 }
289 return iResult;
290}
291
292
293int AliHLTConfiguration::ExtractSources()
294{
70ed7d01 295 // see header file for function documentation
3495cce2 296 int iResult=0;
d174bfc3 297 fNofSources=0; // indicates that the function was called, there are either n or 0 sources
298 fListSources.clear();
4403fb69 299 AliHLTConfigurationHandler* pHandler=AliHLTConfigurationHandler::Instance();
300 if (!pHandler) {
d174bfc3 301 HLTError("global configuration handler not initialized, can not resolve sources");
302 return -EFAULT;
303 }
52c1c164 304 if (!fStringSources.IsNull()) {
3495cce2 305 vector<char*> tgtList;
52c1c164 306 if ((iResult=InterpreteString(fStringSources.Data(), tgtList))>=0) {
3495cce2 307 fNofSources=tgtList.size();
308 vector<char*>::iterator element=tgtList.begin();
85465857 309 while ((element=tgtList.begin())!=tgtList.end()) {
4403fb69 310 AliHLTConfiguration* pConf=pHandler->FindConfiguration(*element);
85465857 311 if (pConf) {
a742f6f8 312 //HLTDebug("configuration %s (%p): source \"%s\" (%p) inserted", GetName(), this, pConf->GetName(), pConf);
85465857 313 fListSources.push_back(pConf);
314 } else {
315 HLTError("can not find source \"%s\"", (*element));
316 iResult=-ENOENT;
317 }
3495cce2 318 delete[] (*element);
319 tgtList.erase(element);
320 }
3495cce2 321 }
322 }
d174bfc3 323 fListSrcElementIdx=-1;
324 return iResult<0?iResult:SourcesResolved();
3495cce2 325}
326
327int AliHLTConfiguration::ExtractArguments()
328{
70ed7d01 329 // see header file for function documentation
3495cce2 330 int iResult=0;
52c1c164 331 if (!fArguments.IsNull()) {
3495cce2 332 vector<char*> tgtList;
333 if ((iResult=InterpreteString(fArguments, tgtList))>=0) {
334 fArgc=tgtList.size();
9ce4bf4a 335 //HLTDebug("configuration %s: extracted %d arguments from \"%s\"", GetName(), fArgc, fArguments);
3495cce2 336 if (fArgc>0) {
337 fArgv = new char*[fArgc];
338 if (fArgv) {
339 vector<char*>::iterator element=tgtList.begin();
340 int i=0;
341 while (element!=tgtList.end()) {
85465857 342 //HLTDebug("assign arguments %d (%s)", i, *element);
3495cce2 343 fArgv[i++]=(*element);
344 element++;
345 }
346 } else {
347 iResult=-ENOMEM;
348 }
349 }
350 }
9ce4bf4a 351 } else {
352 // there are zero arguments
353 fArgc=0;
3495cce2 354 }
d489ab09 355 if (iResult<0) fArgc=iResult;
3495cce2 356 return iResult;
357}
358
4b31e06b 359int AliHLTConfiguration::InterpreteString(const char* arg, vector<char*>& argList)
3495cce2 360{
70ed7d01 361 // see header file for function documentation
3495cce2 362 int iResult=0;
363 if (arg) {
85465857 364 //HLTDebug("interprete \"%s\"", arg);
3495cce2 365 int i=0;
366 int prec=-1;
5f5b708b 367 int bQuote=0;
3495cce2 368 do {
5f5b708b 369 //HLTDebug("%d %x", i, arg[i]);
370 if (arg[i]=='\'' && bQuote==0) {
371 bQuote=1;
372 } else if (arg[i]==0 ||
373 (arg[i]==' ' && bQuote==0) ||
374 (arg[i]=='\'' && bQuote==1)) {
375 bQuote=0;
3495cce2 376 if (prec>=0) {
377 char* pEntry= new char[i-prec+1];
378 if (pEntry) {
379 strncpy(pEntry, &arg[prec], i-prec);
380 pEntry[i-prec]=0; // terminate string
85465857 381 //HLTDebug("create string \"%s\", insert at %d", pEntry, argList.size());
3495cce2 382 argList.push_back(pEntry);
383 } else
384 iResult=-ENOMEM;
385 prec=-1;
386 }
387 } else if (prec==-1) prec=i;
388 } while (arg[i++]!=0 && iResult>=0);
389 } else {
390 iResult=-EINVAL;
391 }
392 return iResult;
393}
394
032c5e5e 395int AliHLTConfiguration::ConvertSizeString(const char* strSize) const
396{
397 // see header file for function documentation
398 int size=0;
399 if (!strSize) return -1;
400
401 char* endptr=NULL;
402 size=strtol(strSize, &endptr, 10);
403 if (size>=0) {
404 if (endptr) {
405 if (endptr==strSize) {
406 HLTWarning("ignoring unrecognized buffer size '%s'", strSize);
407 size=-1;
408 } else if (*endptr==0) {
409 // no unit specifier
410 } else if (*endptr=='k') {
411 size*=1014;
412 } else if (*endptr=='M') {
413 size*=1024*1024;
414 } else {
415 HLTWarning("ignoring buffer size of unknown unit '%c'", endptr[0]);
416 }
417 } else {
418 HLTWarning("ignoring negative buffer size specifier '%s'", strSize);
419 size=-1;
420 }
421 }
422 return size;
423}
424
3495cce2 425int AliHLTConfiguration::FollowDependency(const char* id, TList* pTgtList)
426{
70ed7d01 427 // see header file for function documentation
3495cce2 428 int iResult=0;
429 if (id) {
430 AliHLTConfiguration* pDep=NULL;
431 if ((pDep=GetSource(id))!=NULL) {
432 if (pTgtList) pTgtList->Add(pDep);
433 iResult++;
434 } else {
435 pDep=GetFirstSource();
436 while (pDep && iResult==0) {
437 if ((iResult=pDep->FollowDependency(id, pTgtList))>0) {
438 if (pTgtList) pTgtList->AddFirst(pDep);
439 iResult++;
440 }
441 pDep=GetNextSource();
442 }
443 }
444 } else {
445 iResult=-EINVAL;
446 }
447 return iResult;
448}
449
3495cce2 450