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 purpeateose. It is *
13 : * provided "as is" without express or implied warranty. *
14 : **************************************************************************/
15 :
16 : // $Id$
17 : // $MpId: AliMpHelper.cxx,v 1.5 2006/05/24 13:58:50 ivana Exp $
18 :
19 : #include "AliMpHelper.h"
20 :
21 : #include "TArrayI.h"
22 : #include "TObjArray.h"
23 : #include "TObjString.h"
24 : #include "TString.h"
25 : #include "TMap.h"
26 :
27 : //-----------------------------------------------------------------------------
28 : /// \class AliMpHelper
29 : ///
30 : /// Helper class used to parse mapping files for St345 slats.
31 : ///
32 : /// \author L. Aphecetche
33 : //-----------------------------------------------------------------------------
34 :
35 : /// \cond CLASSIMP
36 18 : ClassImp(AliMpHelper)
37 : /// \endcond
38 :
39 : //_____________________________________________________________________________
40 0 : AliMpHelper::AliMpHelper() : TObject()
41 0 : {
42 : ///
43 : /// Default (empty) ctor.
44 : ///
45 0 : }
46 :
47 : //_____________________________________________________________________________
48 : AliMpHelper::~AliMpHelper()
49 0 : {
50 : ///
51 : /// Dtor.
52 : ///
53 0 : }
54 :
55 : //_____________________________________________________________________________
56 : TMap*
57 : AliMpHelper::Decode(const TString& s)
58 : {
59 : /// \todo add comment
60 :
61 0 : TString ss(s);
62 0 : ss.ToUpper();
63 :
64 0 : TMap* m = new TMap;
65 0 : m->SetOwner(true);
66 :
67 0 : TObjArray* a = ss.Tokenize(";");
68 0 : TIter next(a);
69 : TObjString* o;
70 :
71 0 : while ( ( o = static_cast<TObjString*>(next()) ) )
72 : {
73 0 : TString& os(o->String());
74 0 : TObjArray* b = os.Tokenize("=");
75 0 : if (b->GetEntries()==2)
76 : {
77 0 : m->Add(b->At(0),b->At(1));
78 : }
79 : }
80 0 : delete a;
81 : return m;
82 0 : }
83 :
84 : //_____________________________________________________________________________
85 : Bool_t
86 : AliMpHelper::Decode(const TMap& m, const TString& key, TString& value)
87 : {
88 : /// \todo add comment
89 :
90 0 : TString skey(key);
91 0 : skey.ToUpper();
92 0 : value = "";
93 0 : TPair* p = static_cast<TPair*>(m.FindObject(skey));
94 0 : if (p)
95 : {
96 0 : value = (static_cast<TObjString*>(p->Value()))->String();
97 0 : return kTRUE;
98 : }
99 0 : return kFALSE;
100 0 : }
101 :
102 : //_____________________________________________________________________________
103 : void AliMpHelper::DecodeName(const char* name, char sep, TArrayI& theList)
104 : {
105 : ///
106 : /// From a string of the form "i-j;k;l;m-n" returns an integer array
107 : /// containing all the integers from i to j, then k, l and then from m to
108 : /// n.
109 : ///
110 21576 : theList.Set(0);
111 :
112 10788 : TString str(name);
113 :
114 21576 : if ( str.Length() == 0 )
115 : {
116 : // protection against empty input string.
117 0 : return;
118 : }
119 :
120 : // Get substrings separated by 'sep'
121 32364 : TObjArray* ranges = str.Tokenize(sep);
122 :
123 : // Finally takes each substring (which ought to be a range of the form
124 : // x-y), and decode it into the theList integer vector.
125 103203 : for ( Int_t i = 0; i < ranges->GetEntriesFast(); ++i )
126 : {
127 23613 : int m1;
128 23613 : int m2;
129 23613 : int n;
130 23613 : int incr;
131 47226 : TString& s = ((TObjString*)ranges->At(i))->String();
132 47226 : GetRange(s.Data(),m1,m2,incr,n);
133 23613 : int m = m1;
134 105096 : while ( n > 0 )
135 : {
136 28935 : theList.Set(theList.GetSize()+1);
137 57870 : theList[theList.GetSize()-1] = m;
138 28935 : m += incr;
139 28935 : --n;
140 : }
141 23613 : }
142 :
143 21576 : delete ranges;
144 21576 : }
145 :
146 : //_____________________________________________________________________________
147 : void
148 : AliMpHelper::GetRange(const char* cstr, Int_t& begin, Int_t& end,
149 : Int_t& incr, Int_t& n)
150 : {
151 : ///
152 : /// From a string of the form "m-n" returns a range (begin,end),
153 : /// its ordering (incr=+-1) and its size (abs(begin-end)+1)
154 : ///
155 47226 : TString str(cstr);
156 :
157 23613 : incr = 1;
158 23613 : Ssiz_t pos = str.First('-');
159 23613 : if ( pos < 0 )
160 : {
161 44274 : begin = str.Atoi();
162 22137 : end = -1;
163 22137 : n = 1;
164 22137 : }
165 : else
166 : {
167 2952 : begin = str.Atoi();
168 7380 : end = TString(str(pos+1,str.Length()-pos)).Atoi();
169 1476 : if ( begin > end )
170 : {
171 477 : incr = -1;
172 477 : n = begin-end+1;
173 477 : }
174 : else
175 : {
176 999 : n = end-begin+1;
177 : }
178 : }
179 23613 : }
180 :
181 : //_____________________________________________________________________________
182 : TString AliMpHelper::Normalize(const char* line)
183 : {
184 : ///
185 : /// Remove multiple blanks, and blanks in the begining/end.
186 : ///
187 90382 : TString rv(line);
188 :
189 92236 : if ( rv.Length() <= 0 ) return TString();
190 :
191 111524 : while ( rv[0] == ' ' )
192 : {
193 11498 : rv.Remove(0,1);
194 : }
195 134970 : while ( rv[rv.Length()-1] == ' ' )
196 : {
197 1452 : rv.Remove(rv.Length()-1,1);
198 : }
199 : Ssiz_t i(0);
200 : bool kill = false;
201 2148258 : for ( i = 0; i < rv.Length(); ++i )
202 : {
203 1343644 : if ( rv[i] == ' ' )
204 : {
205 131889 : if (kill)
206 : {
207 38272 : rv.Remove(i,1);
208 38272 : --i;
209 38272 : }
210 : else
211 : {
212 : kill = true;
213 : }
214 : }
215 : else
216 : {
217 : kill = false;
218 : }
219 : }
220 44264 : return rv;
221 45191 : }
|