]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/AliTPCConfigParser.cxx
Jens Wiechula modifications.
[u/mrichter/AliRoot.git] / TPC / AliTPCConfigParser.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
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  **************************************************************************/
15
16 ///////////////////////////////////////////////////////////////////////////////
17 //                                                                           //
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.                                                      //
22 //                                                                           //
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              //
26 //                                                                           //
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 ///////////////////////////////////////////////////////////////////////////////
30
31
32 #include <fstream>
33 //Root includes
34 #include <TObjString.h>
35 #include <TObjArray.h>
36 #include <TString.h>
37 #include <TIterator.h>
38 #include <TList.h>
39 //AliRoot includes
40
41 //header
42 #include "AliTPCConfigParser.h"
43
44
45 AliTPCConfigParser::AliTPCConfigParser() :
46 TObject(),
47 fConfigMap(new TList),
48 fKeyIter(0),
49 fValIter(0)
50 {
51  //
52  // default constructor
53  //
54   fConfigMap->SetOwner();
55
56 //_____________________________________________________________________
57 AliTPCConfigParser::AliTPCConfigParser(const AliTPCConfigParser &cfg) :
58 TObject(),
59 fConfigMap((TList*)cfg.fConfigMap->Clone()),
60 fKeyIter(0),
61 fValIter(0)
62 {
63   //
64   // copy constructor
65   //
66   fConfigMap->SetOwner();
67 }
68
69 //_____________________________________________________________________
70 AliTPCConfigParser::AliTPCConfigParser(const char* cfgfile) :
71 TObject(),
72 fConfigMap(new TList),
73 fKeyIter(0),
74 fValIter(0)
75 {
76   //
77   // default constructor using the config file name as input parameter
78   //
79   fConfigMap->SetOwner();
80   if ( !cfgfile ) return;
81   ParseConfigFileTxt(cfgfile);
82 }
83 //_____________________________________________________________________
84 AliTPCConfigParser& AliTPCConfigParser::operator = (const  AliTPCConfigParser &source)
85 {
86   //
87   // assignment operator
88   //
89   if (&source == this) return *this;
90   new (this) AliTPCConfigParser(source);
91   
92   return *this;
93 }
94 //_____________________________________________________________________
95 AliTPCConfigParser::~AliTPCConfigParser()
96 {
97  //
98  // dtor
99  //
100   delete fConfigMap;
101   delete fKeyIter;
102   delete fValIter;
103 }
104 //_____________________________________________________________________
105 Int_t AliTPCConfigParser::ParseConfigFileTxt(const char* cfgfile)
106 {
107  //
108  // Function to parse a configuration file
109  //
110   
111   ifstream file(cfgfile);
112   if ( !file.is_open() ){
113     Error("ParseConfigFileTxt","File '%s' could not be opened!", cfgfile);
114     return 1;
115   }
116   TString strFile;
117   strFile.ReadFile(file);
118   TObjArray *arr=strFile.Tokenize("\n");
119   if ( !arr ) {
120     file.close();
121     return 2;
122   }
123   TIter nextLine(arr);
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();
137     if (nentries>2){
138       Error("AliTPCConfigParser","ParseConfigFileTxt: Cannot parse line '%s'\n",line.Data());
139       delete arrValues;
140       continue;
141     }
142     TObjArray  *objArr=0x0;
143     if (nentries==2){
144       TObject *objVal=arrValues->At(1);
145       const TString &str=((TObjString*)objVal)->GetString();
146       if (str.Contains(","))
147         objArr=str.Tokenize(",");
148       else{
149         objArr=new TObjArray;
150         objArr->Add(objVal->Clone());
151       }
152       objArr->SetOwner(kTRUE);
153     } else {
154       objArr=new TObjArray;
155     }
156     objArr->SetName(arrValues->At(0)->GetName());
157     fConfigMap->AddLast(objArr);
158     delete arrValues;
159   }
160   
161   delete arr;
162   return 0;
163 }
164 //_____________________________________________________________________
165 Float_t AliTPCConfigParser::GetValue(const char *key, UInt_t position)
166 {
167   //
168   //Get value for the speciefied key
169   //
170   TObject *val=((TObjArray*)fConfigMap->FindObject(key))->At(position);
171   if ( !val ) return -999.;
172   TString sval(((TObjString*)val)->GetString());
173   return sval.Atof();
174 }
175 //_____________________________________________________________________
176 const char* AliTPCConfigParser::GetData(const char *key, UInt_t position)
177 {
178   //
179   //Get value for the speciefied key
180   //
181   TObject *val=((TObjArray*)fConfigMap->FindObject(key))->At(position);
182   if ( !val ) {
183     return "";
184   }
185   return (((TObjString*)val)->GetString()).Data();
186 }
187 //_____________________________________________________________________
188 Float_t AliTPCConfigParser::GetValue(const TObject *key, UInt_t position)
189 {
190   //
191   //Get value for the speciefied key
192   //
193   TObject *val=((TObjArray*)fConfigMap->FindObject(key))->At(position);
194   if ( !val ) return -999.;
195   TString sval(((TObjString*)val)->GetString());
196   return sval.Atof();
197 }
198 //_____________________________________________________________________
199 const char* AliTPCConfigParser::GetData(const TObject *key, UInt_t position)
200 {
201   //
202   //Get value for the speciefied key
203   //
204   TObject *val=((TObjArray*)fConfigMap->FindObject(key))->At(position);
205   if ( !val ) {
206     return "";
207   }
208   return (((TObjString*)val)->GetString()).Data();
209 }
210 //_____________________________________________________________________
211 Int_t AliTPCConfigParser::GetNumberOfValues(const char* key) const
212 {
213   //
214   // return the number of values for key
215   //
216   return ((TObjArray*)fConfigMap->FindObject(key))->GetEntries();
217 }
218 //_____________________________________________________________________
219 Int_t AliTPCConfigParser::GetNumberOfValues(TObject* key) const
220 {
221   //
222   // return the number of values for key
223   //
224   return ((TObjArray*)fConfigMap->FindObject(key))->GetEntries();
225 }
226 //_____________________________________________________________________
227 TObject* AliTPCConfigParser::NextKey(){
228   if (!fKeyIter) fKeyIter=fConfigMap->MakeIterator();
229   TObject *obj=fKeyIter->Next();
230   if (!obj) {
231     delete fKeyIter;
232     fKeyIter=0;
233   }
234   return obj;
235 }
236 //_____________________________________________________________________
237 TObject* AliTPCConfigParser::NextValue(const char *key){
238   return NextValueIter((TObjArray*)fConfigMap->FindObject(key));
239 }
240 //_____________________________________________________________________
241 TObject* AliTPCConfigParser::NextValue(TObject *key){
242   return NextValueIter((TObjArray*)fConfigMap->FindObject(key));
243 }
244 //_____________________________________________________________________
245 TObject* AliTPCConfigParser::NextValueIter(TObjArray *key){
246   if (!key) return 0;
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();
251   if (!value) {
252     delete fValIter;
253     fValIter=0;
254   }
255   return value;
256 }
257 //_____________________________________________________________________
258 void AliTPCConfigParser::ResetMap()
259 {
260   //
261   // Reset the map with the configuration values
262   //
263   fConfigMap->Delete();
264 }