Line data Source code
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 0 : TObject(),
48 0 : fConfigMap(new TList),
49 0 : fKeyIter(0),
50 0 : fValIter(0)
51 0 : {
52 : /// default constructor
53 :
54 0 : fConfigMap->SetOwner();
55 0 : }
56 : //_____________________________________________________________________
57 : AliTPCConfigParser::AliTPCConfigParser(const AliTPCConfigParser &cfg) :
58 0 : TObject(),
59 0 : fConfigMap((TList*)cfg.fConfigMap->Clone()),
60 0 : fKeyIter(0),
61 0 : fValIter(0)
62 0 : {
63 : /// copy constructor
64 :
65 0 : fConfigMap->SetOwner();
66 0 : }
67 :
68 : //_____________________________________________________________________
69 : AliTPCConfigParser::AliTPCConfigParser(const char* cfgfile) :
70 0 : TObject(),
71 0 : fConfigMap(new TList),
72 0 : fKeyIter(0),
73 0 : fValIter(0)
74 0 : {
75 : /// default constructor using the config file name as input parameter
76 :
77 0 : fConfigMap->SetOwner();
78 0 : if ( !cfgfile ) return;
79 0 : ParseConfigFileTxt(cfgfile);
80 0 : }
81 : //_____________________________________________________________________
82 : AliTPCConfigParser& AliTPCConfigParser::operator = (const AliTPCConfigParser &source)
83 : {
84 : /// assignment operator
85 :
86 0 : if (&source == this) return *this;
87 0 : new (this) AliTPCConfigParser(source);
88 :
89 0 : return *this;
90 0 : }
91 : //_____________________________________________________________________
92 : AliTPCConfigParser::~AliTPCConfigParser()
93 0 : {
94 : /// dtor
95 :
96 0 : delete fConfigMap;
97 0 : delete fKeyIter;
98 0 : delete fValIter;
99 0 : }
100 : //_____________________________________________________________________
101 : Int_t AliTPCConfigParser::ParseConfigFileTxt(const char* cfgfile)
102 : {
103 : /// Function to parse a configuration file
104 :
105 0 : ResetMap();
106 0 : ifstream file(gSystem->ExpandPathName(cfgfile));
107 0 : if ( !file.is_open() ){
108 0 : Error("ParseConfigFileTxt","File '%s' could not be opened!", cfgfile);
109 0 : return 1;
110 : }
111 0 : TString strFile;
112 0 : strFile.ReadFile(file);
113 0 : TObjArray *arr=strFile.Tokenize("\n");
114 0 : if ( !arr ) {
115 0 : file.close();
116 0 : return 2;
117 : }
118 0 : TIter nextLine(arr);
119 0 : while (TObject *l=nextLine()){
120 0 : TString line(((TObjString*)l)->GetString());
121 : //remove whitespcaces
122 0 : line.Remove(TString::kBoth,' ');
123 0 : line.Remove(TString::kBoth,'\t');
124 0 : if ( line.BeginsWith("#") || line=="" ) continue;
125 0 : line.ReplaceAll(", ",",");
126 0 : 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 0 : Int_t nentries=arrValues->GetEntries();
132 0 : if (nentries>2){
133 0 : Error("AliTPCConfigParser","ParseConfigFileTxt: Cannot parse line '%s'\n",line.Data());
134 0 : delete arrValues;
135 0 : continue;
136 : }
137 : TObjArray *objArr=0x0;
138 0 : if (nentries==2){
139 0 : TObject *objVal=arrValues->At(1);
140 0 : const TString str=objVal->GetName();
141 0 : if (str.Contains(","))
142 0 : objArr=str.Tokenize(",");
143 : else{
144 0 : objArr=new TObjArray;
145 0 : objArr->Add(objVal->Clone());
146 : }
147 0 : objArr->SetOwner(kTRUE);
148 0 : } else {
149 0 : objArr=new TObjArray;
150 : }
151 0 : objArr->SetName(arrValues->At(0)->GetName());
152 0 : fConfigMap->AddLast(objArr);
153 0 : delete arrValues;
154 0 : }
155 :
156 0 : delete arr;
157 : return 0;
158 0 : }
159 : //_____________________________________________________________________
160 : Float_t AliTPCConfigParser::GetValue(const char *key, UInt_t position)
161 : {
162 : /// Get value for the speciefied key
163 :
164 0 : TObject *val=((TObjArray*)fConfigMap->FindObject(key))->At(position);
165 0 : if ( !val ) return -999.;
166 0 : TString sval(((TObjString*)val)->GetString());
167 0 : return sval.Atof();
168 0 : }
169 : //_____________________________________________________________________
170 : const char* AliTPCConfigParser::GetData(const char *key, UInt_t position)
171 : {
172 : /// Get value for the speciefied key
173 :
174 0 : TObjArray *arr=((TObjArray*)fConfigMap->FindObject(key));
175 0 : if (position>=(UInt_t)(arr->GetEntries())) return "";
176 0 : TObject *val=arr->At(position);
177 0 : if ( !val ) return "";
178 0 : return val->GetName();
179 0 : }
180 : //_____________________________________________________________________
181 : Float_t AliTPCConfigParser::GetValue(const TObject *key, UInt_t position)
182 : {
183 : /// Get value for the speciefied key
184 :
185 0 : TObject *val=((TObjArray*)fConfigMap->FindObject(key))->At(position);
186 0 : if ( !val ) return -999.;
187 0 : TString sval(((TObjString*)val)->GetString());
188 0 : return sval.Atof();
189 0 : }
190 : //_____________________________________________________________________
191 : const char* AliTPCConfigParser::GetData(const TObject *key, UInt_t position)
192 : {
193 : /// Get value for the speciefied key
194 :
195 0 : TObjArray *arr=((TObjArray*)fConfigMap->FindObject(key));
196 0 : if (position>=((UInt_t)arr->GetEntries())) return "";
197 0 : TObject *val=arr->At(position);
198 0 : if ( !val ) return "";
199 0 : return val->GetName();
200 0 : }
201 : //_____________________________________________________________________
202 : Int_t AliTPCConfigParser::GetNumberOfValues(const char* key) const
203 : {
204 : /// return the number of values for key
205 :
206 0 : 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 0 : return ((TObjArray*)fConfigMap->FindObject(key))->GetEntries();
214 : }
215 : //_____________________________________________________________________
216 : TObject* AliTPCConfigParser::NextKey(){
217 0 : if (!fKeyIter) fKeyIter=fConfigMap->MakeIterator();
218 0 : TObject *obj=fKeyIter->Next();
219 0 : if (!obj) {
220 0 : delete fKeyIter;
221 0 : fKeyIter=0;
222 0 : }
223 0 : return obj;
224 : }
225 : //_____________________________________________________________________
226 : TObject* AliTPCConfigParser::NextValue(const char *key){
227 0 : return NextValueIter((TObjArray*)fConfigMap->FindObject(key));
228 : }
229 : //_____________________________________________________________________
230 : TObject* AliTPCConfigParser::NextValue(TObject *key){
231 0 : return NextValueIter((TObjArray*)fConfigMap->FindObject(key));
232 : }
233 : //_____________________________________________________________________
234 : TObject* AliTPCConfigParser::NextValueIter(TObjArray *key){
235 0 : if (!key) return 0;
236 : //check if the collection has changed
237 0 : if (fValIter && key!=fValIter->GetCollection()) {
238 0 : delete fValIter;
239 0 : fValIter=0x0;
240 0 : }
241 0 : if (!fValIter) fValIter=key->MakeIterator();
242 0 : TObject *value=fValIter->Next();
243 0 : if (!value) {
244 0 : delete fValIter;
245 0 : fValIter=0;
246 0 : }
247 : return value;
248 0 : }
249 : //_____________________________________________________________________
250 : void AliTPCConfigParser::ResetMap()
251 : {
252 : /// Reset the map with the configuration values
253 :
254 0 : fConfigMap->Delete();
255 0 : }
|