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>
42 #include "AliTPCConfigParser.h"
45 AliTPCConfigParser::AliTPCConfigParser() :
47 fConfigMap(new TList),
52 // default constructor
54 fConfigMap->SetOwner();
56 //_____________________________________________________________________
57 AliTPCConfigParser::AliTPCConfigParser(const AliTPCConfigParser &cfg) :
59 fConfigMap((TList*)cfg.fConfigMap->Clone()),
66 fConfigMap->SetOwner();
69 //_____________________________________________________________________
70 AliTPCConfigParser::AliTPCConfigParser(const char* cfgfile) :
72 fConfigMap(new TList),
77 // default constructor using the config file name as input parameter
79 fConfigMap->SetOwner();
80 if ( !cfgfile ) return;
81 ParseConfigFileTxt(cfgfile);
83 //_____________________________________________________________________
84 AliTPCConfigParser& AliTPCConfigParser::operator = (const AliTPCConfigParser &source)
87 // assignment operator
89 if (&source == this) return *this;
90 new (this) AliTPCConfigParser(source);
94 //_____________________________________________________________________
95 AliTPCConfigParser::~AliTPCConfigParser()
104 //_____________________________________________________________________
105 Int_t AliTPCConfigParser::ParseConfigFileTxt(const char* cfgfile)
108 // Function to parse a configuration file
111 ifstream file(cfgfile);
112 if ( !file.is_open() ){
113 Error("ParseConfigFileTxt","File '%s' could not be opened!", cfgfile);
117 strFile.ReadFile(file);
118 TObjArray *arr=strFile.Tokenize("\n");
124 while (TObject *l=nextLine()){
125 TString line(((TObjString*)l)->GetString());
126 //remove whitespcaces
127 line.Remove(TString::kBoth,' ');
128 line.Remove(TString::kBoth,'\t');
129 if ( line.BeginsWith("#") || line=="" ) continue;
130 line.ReplaceAll(", ",",");
131 TObjArray *arrValues=line.Tokenize(" \t");
132 //currently only name => Value is supported
133 //and name => 'nothing'
134 //value may be a comma separated list, in which case a TObjArray
135 //of the list will be created and stored as the value
136 Int_t nentries=arrValues->GetEntries();
138 Error("AliTPCConfigParser","ParseConfigFileTxt: Cannot parse line '%s'\n",line.Data());
142 TObjArray *objArr=0x0;
144 TObject *objVal=arrValues->At(1);
145 const TString &str=((TObjString*)objVal)->GetString();
146 if (str.Contains(","))
147 objArr=str.Tokenize(",");
149 objArr=new TObjArray;
150 objArr->Add(objVal->Clone());
152 objArr->SetOwner(kTRUE);
154 objArr=new TObjArray;
156 objArr->SetName(arrValues->At(0)->GetName());
157 fConfigMap->AddLast(objArr);
164 //_____________________________________________________________________
165 Float_t AliTPCConfigParser::GetValue(const char *key, UInt_t position)
168 //Get value for the speciefied key
170 TObject *val=((TObjArray*)fConfigMap->FindObject(key))->At(position);
171 if ( !val ) return -999.;
172 TString sval(((TObjString*)val)->GetString());
175 //_____________________________________________________________________
176 const char* AliTPCConfigParser::GetData(const char *key, UInt_t position)
179 //Get value for the speciefied key
181 TObject *val=((TObjArray*)fConfigMap->FindObject(key))->At(position);
185 return (((TObjString*)val)->GetString()).Data();
187 //_____________________________________________________________________
188 Float_t AliTPCConfigParser::GetValue(const TObject *key, UInt_t position)
191 //Get value for the speciefied key
193 TObject *val=((TObjArray*)fConfigMap->FindObject(key))->At(position);
194 if ( !val ) return -999.;
195 TString sval(((TObjString*)val)->GetString());
198 //_____________________________________________________________________
199 const char* AliTPCConfigParser::GetData(const TObject *key, UInt_t position)
202 //Get value for the speciefied key
204 TObject *val=((TObjArray*)fConfigMap->FindObject(key))->At(position);
208 return (((TObjString*)val)->GetString()).Data();
210 //_____________________________________________________________________
211 Int_t AliTPCConfigParser::GetNumberOfValues(const char* key) const
214 // return the number of values for key
216 return ((TObjArray*)fConfigMap->FindObject(key))->GetEntries();
218 //_____________________________________________________________________
219 Int_t AliTPCConfigParser::GetNumberOfValues(TObject* key) const
222 // return the number of values for key
224 return ((TObjArray*)fConfigMap->FindObject(key))->GetEntries();
226 //_____________________________________________________________________
227 TObject* AliTPCConfigParser::NextKey(){
228 if (!fKeyIter) fKeyIter=fConfigMap->MakeIterator();
229 TObject *obj=fKeyIter->Next();
236 //_____________________________________________________________________
237 TObject* AliTPCConfigParser::NextValue(const char *key){
238 return NextValueIter((TObjArray*)fConfigMap->FindObject(key));
240 //_____________________________________________________________________
241 TObject* AliTPCConfigParser::NextValue(TObject *key){
242 return NextValueIter((TObjArray*)fConfigMap->FindObject(key));
244 //_____________________________________________________________________
245 TObject* AliTPCConfigParser::NextValueIter(TObjArray *key){
247 //check if the collection has changed
248 if (fValIter && key!=fValIter->GetCollection()) delete fValIter;
249 if (!fValIter) fValIter=key->MakeIterator();
250 TObject *value=fValIter->Next();
257 //_____________________________________________________________________
258 void AliTPCConfigParser::ResetMap()
261 // Reset the map with the configuration values
263 fConfigMap->Delete();