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 **************************************************************************/
18 // Authors: David Guez, Ivana Hrivnacova, Marion MacCormick; IPN Orsay
20 // Class AliMUONSt1Decoder
21 // -----------------------
22 // A generic set of functions (defined in the <decoder> namespace).
23 // Used to decode formatted strings, eg. a list of integer ranges,
24 // or a list of sub-strings separated by delimiters such as '(','{','[', ... .
26 // (string 1) (string 2) [string3] {string4} [ (string5.1) (string5.2) ]
27 // note : |_____________________|
29 // this is just ONE substring.
30 // Included in AliRoot 2003/01/28
32 #include "AliMUONSt1Decoder.h"
34 //_____________________________________________________________________
35 StringVector decoder::SplitNtuples(const string& s,
36 const string& leftSep,
37 const string& rightSep)
40 // separate substrings in <s>, by using the first level of delimiters
41 // given in the argument
43 // (string 1) (string 2) [string3] {string4} [ (string5.1) (string5.2) ]
44 // returns a list of 5 substrings
49 // " (string5.1) (string5.2) "
52 string::size_type idx = 0;
54 idx = s.find_first_of(leftSep,idx);
55 if (idx != string::npos) {
56 string::size_type sepNum = leftSep.find_first_of(s[idx],0);
57 if (sepNum>=rightSep.length()){
62 string::size_type idx2 = idx+1;
63 while ((count>0) && (idx2<s.length())){
64 if (s[idx2] == leftSep[sepNum]) count++;
65 if (s[idx2] == rightSep[sepNum]) count--;
68 if (count != 0) return ans; // bad format...stop here
69 ans.push_back(s.substr(idx+1,idx2-idx-2));
72 } while (idx != string::npos);
76 //_____________________________________________________________________
77 StringVector decoder::SplitList(const string& s,const string& sep)
79 // split <s> into several substrings, by using any of the delimters in <sep>
80 // Example : str1 ; str2 , str3
81 // gives a list of 3 substrings ("str1", "str2" and "str3") if
82 // the delimiter parameter is ",;"
83 // and a list of 2 substrings ("str1","str2,str3") if
84 // the delimiter parameter is ";"
87 string::size_type i=0,j;
88 while ((j=s.find_first_of(sep,i))!=string::npos){
89 ans.push_back(s.substr(i,j-i));
92 ans.push_back(s.substr(i,s.length()-i));
96 //_____________________________________________________________________
98 decoder::DecodeListRanges(const string& s, const string& sep,
99 const string& rangeSep)
101 // decode <s> as a list of integers
102 // Example: 192/199 ; -10/-7
103 // gives a list of 12 integers:
104 // 192, 193, 194, 195, 196, 197, 198, 199, -10, -9, -8, -7
107 IntPairVector rangeList = DecodeListOfIntRanges(s,sep,rangeSep);
108 for (UInt_t i = 0 ;i<rangeList.size();i++){
109 for (Int_t j=rangeList[i].first;j<=rangeList[i].second;j++){
116 //_____________________________________________________________________
118 decoder::DecodeListOfIntRanges(const string& s, const string& sep,
119 const string& rangeSep)
121 // decodes <s> as a list of int ranges
122 // Example: 192/303 ; -10/-1
123 // gives a list of two int pairs:
124 // pair(192,303) and another pair (-10,-1)
126 string::size_type i=0;
128 if (s.empty()) return ans;
130 StringVector parts = decoder::SplitList(s,sep);
132 for (UInt_t k=0;k<parts.size();k++){
133 i=parts[k].find_first_of(rangeSep);
135 if (i == string::npos) {
136 from = atoi(parts[k].c_str());
139 from=atoi(parts[k].substr(0,i).c_str());
140 to =atoi(parts[k].substr(i+1,parts[k].length()-i-1).c_str());
142 ans.push_back(IntPair(from,to));
147 //_____________________________________________________________________
149 decoder::DecodeListOfFloatRanges(const string& s, const string& sep,
150 const string& rangeSep)
152 // decodes <s> as a list of double (floating point) ranges
153 // Example : 192.33/303.26 ; -10.2/-1.41
154 // gives a list of two double precision floating point (phew!) pairs:
155 // pair (192.33,303.26) and another pair (-10.2,-1.41)
157 string::size_type i=0;
158 DoublePairVector ans;
159 if (s.empty()) return ans;
161 StringVector parts = decoder::SplitList(s,sep);
163 for (UInt_t k=0;k<parts.size();k++){
164 i=parts[k].find_first_of(rangeSep);
166 if (i == string::npos) {
167 from = atof(parts[k].c_str());
170 from=atof(parts[k].substr(0,i).c_str());
171 to =atof(parts[k].substr(i+1,parts[k].length()-i-1).c_str());
173 ans.push_back(DoublePair(from,to));