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"
47 AliTPCConfigParser::AliTPCConfigParser() :
49 fConfigMap(new TList),
54 // default constructor
56 fConfigMap->SetOwner();
58 //_____________________________________________________________________
59 AliTPCConfigParser::AliTPCConfigParser(const AliTPCConfigParser &cfg) :
61 fConfigMap((TList*)cfg.fConfigMap->Clone()),
68 fConfigMap->SetOwner();
71 //_____________________________________________________________________
72 AliTPCConfigParser::AliTPCConfigParser(const char* cfgfile) :
74 fConfigMap(new TList),
79 // default constructor using the config file name as input parameter
81 fConfigMap->SetOwner();
82 if ( !cfgfile ) return;
83 ParseConfigFileTxt(cfgfile);
85 //_____________________________________________________________________
86 AliTPCConfigParser& AliTPCConfigParser::operator = (const AliTPCConfigParser &source)
89 // assignment operator
91 if (&source == this) return *this;
92 new (this) AliTPCConfigParser(source);
96 //_____________________________________________________________________
97 AliTPCConfigParser::~AliTPCConfigParser()
106 //_____________________________________________________________________
107 Int_t AliTPCConfigParser::ParseConfigFileTxt(const char* cfgfile)
110 // Function to parse a configuration file
113 ifstream file(gSystem->ExpandPathName(cfgfile));
114 if ( !file.is_open() ){
115 Error("ParseConfigFileTxt","File '%s' could not be opened!", cfgfile);
119 strFile.ReadFile(file);
120 TObjArray *arr=strFile.Tokenize("\n");
126 while (TObject *l=nextLine()){
127 TString line(((TObjString*)l)->GetString());
128 //remove whitespcaces
129 line.Remove(TString::kBoth,' ');
130 line.Remove(TString::kBoth,'\t');
131 if ( line.BeginsWith("#") || line=="" ) continue;
132 line.ReplaceAll(", ",",");
133 TObjArray *arrValues=line.Tokenize(" \t");
134 //currently only name => Value is supported
135 //and name => 'nothing'
136 //value may be a comma separated list, in which case a TObjArray
137 //of the list will be created and stored as the value
138 Int_t nentries=arrValues->GetEntries();
140 Error("AliTPCConfigParser","ParseConfigFileTxt: Cannot parse line '%s'\n",line.Data());
144 TObjArray *objArr=0x0;
146 TObject *objVal=arrValues->At(1);
147 const TString str=objVal->GetName();
148 if (str.Contains(","))
149 objArr=str.Tokenize(",");
151 objArr=new TObjArray;
152 objArr->Add(objVal->Clone());
154 objArr->SetOwner(kTRUE);
156 objArr=new TObjArray;
158 objArr->SetName(arrValues->At(0)->GetName());
159 fConfigMap->AddLast(objArr);
166 //_____________________________________________________________________
167 Float_t AliTPCConfigParser::GetValue(const char *key, UInt_t position)
170 //Get value for the speciefied key
172 TObject *val=((TObjArray*)fConfigMap->FindObject(key))->At(position);
173 if ( !val ) return -999.;
174 TString sval(((TObjString*)val)->GetString());
177 //_____________________________________________________________________
178 const char* AliTPCConfigParser::GetData(const char *key, UInt_t position)
181 //Get value for the speciefied key
183 TObjArray *arr=((TObjArray*)fConfigMap->FindObject(key));
184 if (position>=(UInt_t)(arr->GetEntries())) return "";
185 TObject *val=arr->At(position);
186 if ( !val ) return "";
187 return val->GetName();
189 //_____________________________________________________________________
190 Float_t AliTPCConfigParser::GetValue(const TObject *key, UInt_t position)
193 //Get value for the speciefied key
195 TObject *val=((TObjArray*)fConfigMap->FindObject(key))->At(position);
196 if ( !val ) return -999.;
197 TString sval(((TObjString*)val)->GetString());
200 //_____________________________________________________________________
201 const char* AliTPCConfigParser::GetData(const TObject *key, UInt_t position)
204 //Get value for the speciefied key
206 TObjArray *arr=((TObjArray*)fConfigMap->FindObject(key));
207 if (position>=((UInt_t)arr->GetEntries())) return "";
208 TObject *val=arr->At(position);
209 if ( !val ) return "";
210 return val->GetName();
212 //_____________________________________________________________________
213 Int_t AliTPCConfigParser::GetNumberOfValues(const char* key) const
216 // return the number of values for key
218 return ((TObjArray*)fConfigMap->FindObject(key))->GetEntries();
220 //_____________________________________________________________________
221 Int_t AliTPCConfigParser::GetNumberOfValues(TObject* key) const
224 // return the number of values for key
226 return ((TObjArray*)fConfigMap->FindObject(key))->GetEntries();
228 //_____________________________________________________________________
229 TObject* AliTPCConfigParser::NextKey(){
230 if (!fKeyIter) fKeyIter=fConfigMap->MakeIterator();
231 TObject *obj=fKeyIter->Next();
238 //_____________________________________________________________________
239 TObject* AliTPCConfigParser::NextValue(const char *key){
240 return NextValueIter((TObjArray*)fConfigMap->FindObject(key));
242 //_____________________________________________________________________
243 TObject* AliTPCConfigParser::NextValue(TObject *key){
244 return NextValueIter((TObjArray*)fConfigMap->FindObject(key));
246 //_____________________________________________________________________
247 TObject* AliTPCConfigParser::NextValueIter(TObjArray *key){
249 //check if the collection has changed
250 if (fValIter && key!=fValIter->GetCollection()) {
254 if (!fValIter) fValIter=key->MakeIterator();
255 TObject *value=fValIter->Next();
262 //_____________________________________________________________________
263 void AliTPCConfigParser::ResetMap()
266 // Reset the map with the configuration values
268 fConfigMap->Delete();