]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/TPCbase/AliTPCConfigParser.cxx
doxy: TPC/TPCbase converted
[u/mrichter/AliRoot.git] / TPC / TPCbase / 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 /// \class AliTPCConfigParser
17 /// \brief  Class for Parsing simple text configuration files
18 ///
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 #include <fstream>
32 //Root includes
33 #include <TObjString.h>
34 #include <TObjArray.h>
35 #include <TString.h>
36 #include <TIterator.h>
37 #include <TList.h>
38 #include <TSystem.h>
39 //AliRoot includes
40
41 //header
42 #include "AliTPCConfigParser.h"
43
44 using std::ifstream;
45
46 AliTPCConfigParser::AliTPCConfigParser() :
47 TObject(),
48 fConfigMap(new TList),
49 fKeyIter(0),
50 fValIter(0)
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   /// copy constructor
64
65   fConfigMap->SetOwner();
66 }
67
68 //_____________________________________________________________________
69 AliTPCConfigParser::AliTPCConfigParser(const char* cfgfile) :
70 TObject(),
71 fConfigMap(new TList),
72 fKeyIter(0),
73 fValIter(0)
74 {
75   /// default constructor using the config file name as input parameter
76
77   fConfigMap->SetOwner();
78   if ( !cfgfile ) return;
79   ParseConfigFileTxt(cfgfile);
80 }
81 //_____________________________________________________________________
82 AliTPCConfigParser& AliTPCConfigParser::operator = (const  AliTPCConfigParser &source)
83 {
84   /// assignment operator
85
86   if (&source == this) return *this;
87   new (this) AliTPCConfigParser(source);
88
89   return *this;
90 }
91 //_____________________________________________________________________
92 AliTPCConfigParser::~AliTPCConfigParser()
93 {
94  /// dtor
95
96   delete fConfigMap;
97   delete fKeyIter;
98   delete fValIter;
99 }
100 //_____________________________________________________________________
101 Int_t AliTPCConfigParser::ParseConfigFileTxt(const char* cfgfile)
102 {
103  /// Function to parse a configuration file
104
105   ResetMap();
106   ifstream file(gSystem->ExpandPathName(cfgfile));
107   if ( !file.is_open() ){
108     Error("ParseConfigFileTxt","File '%s' could not be opened!", cfgfile);
109     return 1;
110   }
111   TString strFile;
112   strFile.ReadFile(file);
113   TObjArray *arr=strFile.Tokenize("\n");
114   if ( !arr ) {
115     file.close();
116     return 2;
117   }
118   TIter nextLine(arr);
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();
132     if (nentries>2){
133       Error("AliTPCConfigParser","ParseConfigFileTxt: Cannot parse line '%s'\n",line.Data());
134       delete arrValues;
135       continue;
136     }
137     TObjArray  *objArr=0x0;
138     if (nentries==2){
139       TObject *objVal=arrValues->At(1);
140       const TString str=objVal->GetName();
141       if (str.Contains(","))
142         objArr=str.Tokenize(",");
143       else{
144         objArr=new TObjArray;
145         objArr->Add(objVal->Clone());
146       }
147       objArr->SetOwner(kTRUE);
148     } else {
149       objArr=new TObjArray;
150     }
151     objArr->SetName(arrValues->At(0)->GetName());
152     fConfigMap->AddLast(objArr);
153     delete arrValues;
154   }
155
156   delete arr;
157   return 0;
158 }
159 //_____________________________________________________________________
160 Float_t AliTPCConfigParser::GetValue(const char *key, UInt_t position)
161 {
162   /// Get value for the speciefied key
163
164   TObject *val=((TObjArray*)fConfigMap->FindObject(key))->At(position);
165   if ( !val ) return -999.;
166   TString sval(((TObjString*)val)->GetString());
167   return sval.Atof();
168 }
169 //_____________________________________________________________________
170 const char* AliTPCConfigParser::GetData(const char *key, UInt_t position)
171 {
172   /// Get value for the speciefied key
173
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();
179 }
180 //_____________________________________________________________________
181 Float_t AliTPCConfigParser::GetValue(const TObject *key, UInt_t position)
182 {
183   /// Get value for the speciefied key
184
185   TObject *val=((TObjArray*)fConfigMap->FindObject(key))->At(position);
186   if ( !val ) return -999.;
187   TString sval(((TObjString*)val)->GetString());
188   return sval.Atof();
189 }
190 //_____________________________________________________________________
191 const char* AliTPCConfigParser::GetData(const TObject *key, UInt_t position)
192 {
193   /// Get value for the speciefied key
194
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();
200 }
201 //_____________________________________________________________________
202 Int_t AliTPCConfigParser::GetNumberOfValues(const char* key) const
203 {
204   /// return the number of values for key
205
206   return ((TObjArray*)fConfigMap->FindObject(key))->GetEntries();
207 }
208 //_____________________________________________________________________
209 Int_t AliTPCConfigParser::GetNumberOfValues(TObject* key) const
210 {
211   /// return the number of values for key
212
213   return ((TObjArray*)fConfigMap->FindObject(key))->GetEntries();
214 }
215 //_____________________________________________________________________
216 TObject* AliTPCConfigParser::NextKey(){
217   if (!fKeyIter) fKeyIter=fConfigMap->MakeIterator();
218   TObject *obj=fKeyIter->Next();
219   if (!obj) {
220     delete fKeyIter;
221     fKeyIter=0;
222   }
223   return obj;
224 }
225 //_____________________________________________________________________
226 TObject* AliTPCConfigParser::NextValue(const char *key){
227   return NextValueIter((TObjArray*)fConfigMap->FindObject(key));
228 }
229 //_____________________________________________________________________
230 TObject* AliTPCConfigParser::NextValue(TObject *key){
231   return NextValueIter((TObjArray*)fConfigMap->FindObject(key));
232 }
233 //_____________________________________________________________________
234 TObject* AliTPCConfigParser::NextValueIter(TObjArray *key){
235   if (!key) return 0;
236   //check if the collection has changed
237   if (fValIter && key!=fValIter->GetCollection()) {
238     delete fValIter;
239     fValIter=0x0;
240   }
241   if (!fValIter) fValIter=key->MakeIterator();
242   TObject *value=fValIter->Next();
243   if (!value) {
244     delete fValIter;
245     fValIter=0;
246   }
247   return value;
248 }
249 //_____________________________________________________________________
250 void AliTPCConfigParser::ResetMap()
251 {
252   /// Reset the map with the configuration values
253
254   fConfigMap->Delete();
255 }