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 ///////////////////////////////////////////////////////////////////////////////
18 // 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 //
29 ///////////////////////////////////////////////////////////////////////////////
34 #include <TObjString.h>
35 #include <TObjArray.h>
37 #include <TIterator.h>
43 #include "AliTPCConfigParser.h"
46 AliTPCConfigParser::AliTPCConfigParser() :
48 fConfigMap(new TList),
53 // default constructor
55 fConfigMap->SetOwner();
57 //_____________________________________________________________________
58 AliTPCConfigParser::AliTPCConfigParser(const AliTPCConfigParser &cfg) :
60 fConfigMap((TList*)cfg.fConfigMap->Clone()),
67 fConfigMap->SetOwner();
70 //_____________________________________________________________________
71 AliTPCConfigParser::AliTPCConfigParser(const char* cfgfile) :
73 fConfigMap(new TList),
78 // default constructor using the config file name as input parameter
80 fConfigMap->SetOwner();
81 if ( !cfgfile ) return;
82 ParseConfigFileTxt(cfgfile);
84 //_____________________________________________________________________
85 AliTPCConfigParser& AliTPCConfigParser::operator = (const AliTPCConfigParser &source)
88 // assignment operator
90 if (&source == this) return *this;
91 new (this) AliTPCConfigParser(source);
95 //_____________________________________________________________________
96 AliTPCConfigParser::~AliTPCConfigParser()
105 //_____________________________________________________________________
106 Int_t AliTPCConfigParser::ParseConfigFileTxt(const char* cfgfile)
109 // Function to parse a configuration file
112 ifstream file(gSystem->ExpandPathName(cfgfile));
113 if ( !file.is_open() ){
114 Error("ParseConfigFileTxt","File '%s' could not be opened!", cfgfile);
118 strFile.ReadFile(file);
119 TObjArray *arr=strFile.Tokenize("\n");
125 while (TObject *l=nextLine()){
126 TString line(((TObjString*)l)->GetString());
127 //remove whitespcaces
128 line.Remove(TString::kBoth,' ');
129 line.Remove(TString::kBoth,'\t');
130 if ( line.BeginsWith("#") || line=="" ) continue;
131 line.ReplaceAll(", ",",");
132 TObjArray *arrValues=line.Tokenize(" \t");
133 //currently only name => Value is supported
134 //and name => 'nothing'
135 //value may be a comma separated list, in which case a TObjArray
136 //of the list will be created and stored as the value
137 Int_t nentries=arrValues->GetEntries();
139 Error("AliTPCConfigParser","ParseConfigFileTxt: Cannot parse line '%s'\n",line.Data());
143 TObjArray *objArr=0x0;
145 TObject *objVal=arrValues->At(1);
146 const TString &str=((TObjString*)objVal)->GetString();
147 if (str.Contains(","))
148 objArr=str.Tokenize(",");
150 objArr=new TObjArray;
151 objArr->Add(objVal->Clone());
153 objArr->SetOwner(kTRUE);
155 objArr=new TObjArray;
157 objArr->SetName(arrValues->At(0)->GetName());
158 fConfigMap->AddLast(objArr);
165 //_____________________________________________________________________
166 Float_t AliTPCConfigParser::GetValue(const char *key, UInt_t position)
169 //Get value for the speciefied key
171 TObject *val=((TObjArray*)fConfigMap->FindObject(key))->At(position);
172 if ( !val ) return -999.;
173 TString sval(((TObjString*)val)->GetString());
176 //_____________________________________________________________________
177 const char* AliTPCConfigParser::GetData(const char *key, UInt_t position)
180 //Get value for the speciefied key
182 TObjArray *arr=((TObjArray*)fConfigMap->FindObject(key));
183 if (position>=(UInt_t)(arr->GetEntries())) return "";
184 TObject *val=arr->At(position);
185 if ( !val ) return "";
186 return (((TObjString*)val)->GetString()).Data();
188 //_____________________________________________________________________
189 Float_t AliTPCConfigParser::GetValue(const TObject *key, UInt_t position)
192 //Get value for the speciefied key
194 TObject *val=((TObjArray*)fConfigMap->FindObject(key))->At(position);
195 if ( !val ) return -999.;
196 TString sval(((TObjString*)val)->GetString());
199 //_____________________________________________________________________
200 const char* AliTPCConfigParser::GetData(const TObject *key, UInt_t position)
203 //Get value for the speciefied key
205 TObjArray *arr=((TObjArray*)fConfigMap->FindObject(key));
206 if (position>=((UInt_t)arr->GetEntries())) return "";
207 TObject *val=arr->At(position);
208 if ( !val ) return "";
209 return (((TObjString*)val)->GetString()).Data();
211 //_____________________________________________________________________
212 Int_t AliTPCConfigParser::GetNumberOfValues(const char* key) const
215 // return the number of values for key
217 return ((TObjArray*)fConfigMap->FindObject(key))->GetEntries();
219 //_____________________________________________________________________
220 Int_t AliTPCConfigParser::GetNumberOfValues(TObject* key) const
223 // return the number of values for key
225 return ((TObjArray*)fConfigMap->FindObject(key))->GetEntries();
227 //_____________________________________________________________________
228 TObject* AliTPCConfigParser::NextKey(){
229 if (!fKeyIter) fKeyIter=fConfigMap->MakeIterator();
230 TObject *obj=fKeyIter->Next();
237 //_____________________________________________________________________
238 TObject* AliTPCConfigParser::NextValue(const char *key){
239 return NextValueIter((TObjArray*)fConfigMap->FindObject(key));
241 //_____________________________________________________________________
242 TObject* AliTPCConfigParser::NextValue(TObject *key){
243 return NextValueIter((TObjArray*)fConfigMap->FindObject(key));
245 //_____________________________________________________________________
246 TObject* AliTPCConfigParser::NextValueIter(TObjArray *key){
248 //check if the collection has changed
249 if (fValIter && key!=fValIter->GetCollection()) delete fValIter;
250 if (!fValIter) fValIter=key->MakeIterator();
251 TObject *value=fValIter->Next();
258 //_____________________________________________________________________
259 void AliTPCConfigParser::ResetMap()
262 // Reset the map with the configuration values
264 fConfigMap->Delete();