1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
16 /// \class AliTPCConfigParser
17 /// \brief Class for Parsing simple text configuration files
19 /// It produces a TList for the TObjArrays with the name of the Key
20 /// the TObjArray contain the Values, split from kommas, as found in the
21 /// Configutation file.
23 /// The configuration file has a simple structure:
24 /// * Lines starting with a # or empty lines are ignored
25 /// * Key and Value are separated either by a <tab> or <space>es
27 /// Currently the class is used in the TPC DAs to allow an adjustment of
28 /// the most relevant parameters without recompiling the DAs
33 #include <TObjString.h>
34 #include <TObjArray.h>
36 #include <TIterator.h>
42 #include "AliTPCConfigParser.h"
46 AliTPCConfigParser::AliTPCConfigParser() :
48 fConfigMap(new TList),
52 /// default constructor
54 fConfigMap->SetOwner();
56 //_____________________________________________________________________
57 AliTPCConfigParser::AliTPCConfigParser(const AliTPCConfigParser &cfg) :
59 fConfigMap((TList*)cfg.fConfigMap->Clone()),
65 fConfigMap->SetOwner();
68 //_____________________________________________________________________
69 AliTPCConfigParser::AliTPCConfigParser(const char* cfgfile) :
71 fConfigMap(new TList),
75 /// default constructor using the config file name as input parameter
77 fConfigMap->SetOwner();
78 if ( !cfgfile ) return;
79 ParseConfigFileTxt(cfgfile);
81 //_____________________________________________________________________
82 AliTPCConfigParser& AliTPCConfigParser::operator = (const AliTPCConfigParser &source)
84 /// assignment operator
86 if (&source == this) return *this;
87 new (this) AliTPCConfigParser(source);
91 //_____________________________________________________________________
92 AliTPCConfigParser::~AliTPCConfigParser()
100 //_____________________________________________________________________
101 Int_t AliTPCConfigParser::ParseConfigFileTxt(const char* cfgfile)
103 /// Function to parse a configuration file
106 ifstream file(gSystem->ExpandPathName(cfgfile));
107 if ( !file.is_open() ){
108 Error("ParseConfigFileTxt","File '%s' could not be opened!", cfgfile);
112 strFile.ReadFile(file);
113 TObjArray *arr=strFile.Tokenize("\n");
119 while (TObject *l=nextLine()){
120 TString line(((TObjString*)l)->GetString());
121 //remove whitespcaces
122 line.Remove(TString::kBoth,' ');
123 line.Remove(TString::kBoth,'\t');
124 if ( line.BeginsWith("#") || line=="" ) continue;
125 line.ReplaceAll(", ",",");
126 TObjArray *arrValues=line.Tokenize(" \t");
127 //currently only name => Value is supported
128 //and name => 'nothing'
129 //value may be a comma separated list, in which case a TObjArray
130 //of the list will be created and stored as the value
131 Int_t nentries=arrValues->GetEntries();
133 Error("AliTPCConfigParser","ParseConfigFileTxt: Cannot parse line '%s'\n",line.Data());
137 TObjArray *objArr=0x0;
139 TObject *objVal=arrValues->At(1);
140 const TString str=objVal->GetName();
141 if (str.Contains(","))
142 objArr=str.Tokenize(",");
144 objArr=new TObjArray;
145 objArr->Add(objVal->Clone());
147 objArr->SetOwner(kTRUE);
149 objArr=new TObjArray;
151 objArr->SetName(arrValues->At(0)->GetName());
152 fConfigMap->AddLast(objArr);
159 //_____________________________________________________________________
160 Float_t AliTPCConfigParser::GetValue(const char *key, UInt_t position)
162 /// Get value for the speciefied key
164 TObject *val=((TObjArray*)fConfigMap->FindObject(key))->At(position);
165 if ( !val ) return -999.;
166 TString sval(((TObjString*)val)->GetString());
169 //_____________________________________________________________________
170 const char* AliTPCConfigParser::GetData(const char *key, UInt_t position)
172 /// Get value for the speciefied key
174 TObjArray *arr=((TObjArray*)fConfigMap->FindObject(key));
175 if (position>=(UInt_t)(arr->GetEntries())) return "";
176 TObject *val=arr->At(position);
177 if ( !val ) return "";
178 return val->GetName();
180 //_____________________________________________________________________
181 Float_t AliTPCConfigParser::GetValue(const TObject *key, UInt_t position)
183 /// Get value for the speciefied key
185 TObject *val=((TObjArray*)fConfigMap->FindObject(key))->At(position);
186 if ( !val ) return -999.;
187 TString sval(((TObjString*)val)->GetString());
190 //_____________________________________________________________________
191 const char* AliTPCConfigParser::GetData(const TObject *key, UInt_t position)
193 /// Get value for the speciefied key
195 TObjArray *arr=((TObjArray*)fConfigMap->FindObject(key));
196 if (position>=((UInt_t)arr->GetEntries())) return "";
197 TObject *val=arr->At(position);
198 if ( !val ) return "";
199 return val->GetName();
201 //_____________________________________________________________________
202 Int_t AliTPCConfigParser::GetNumberOfValues(const char* key) const
204 /// return the number of values for key
206 return ((TObjArray*)fConfigMap->FindObject(key))->GetEntries();
208 //_____________________________________________________________________
209 Int_t AliTPCConfigParser::GetNumberOfValues(TObject* key) const
211 /// return the number of values for key
213 return ((TObjArray*)fConfigMap->FindObject(key))->GetEntries();
215 //_____________________________________________________________________
216 TObject* AliTPCConfigParser::NextKey(){
217 if (!fKeyIter) fKeyIter=fConfigMap->MakeIterator();
218 TObject *obj=fKeyIter->Next();
225 //_____________________________________________________________________
226 TObject* AliTPCConfigParser::NextValue(const char *key){
227 return NextValueIter((TObjArray*)fConfigMap->FindObject(key));
229 //_____________________________________________________________________
230 TObject* AliTPCConfigParser::NextValue(TObject *key){
231 return NextValueIter((TObjArray*)fConfigMap->FindObject(key));
233 //_____________________________________________________________________
234 TObject* AliTPCConfigParser::NextValueIter(TObjArray *key){
236 //check if the collection has changed
237 if (fValIter && key!=fValIter->GetCollection()) {
241 if (!fValIter) fValIter=key->MakeIterator();
242 TObject *value=fValIter->Next();
249 //_____________________________________________________________________
250 void AliTPCConfigParser::ResetMap()
252 /// Reset the map with the configuration values
254 fConfigMap->Delete();