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.
32 #include "AliMUONSt1Decoder.h"
34 //_____________________________________________________________________
35 vector<string> 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 vector<string> 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,const string& rangeSep)
100 // decode <s> as a list of integers
101 // Example: 192/199 ; -10/-7
102 // gives a list of 12 integers:
103 // 192, 193, 194, 195, 196, 197, 198, 199, -10, -9, -8, -7
106 vector< pair <int,int> > rangeList = DecodeListOfIntRanges(s,sep,rangeSep);
107 for (unsigned int i = 0 ;i<rangeList.size();i++){
108 for (int j=rangeList[i].first;j<=rangeList[i].second;j++){
115 //_____________________________________________________________________
116 vector< pair <int,int> >
117 decoder::DecodeListOfIntRanges(const string& s,const string& sep,const string& rangeSep)
119 // decodes <s> as a list of int ranges
120 // Example: 192/303 ; -10/-1
121 // gives a list of two int pairs:
122 // pair(192,303) and another pair (-10,-1)
124 string::size_type i=0;
125 vector< pair <int,int> > ans;
126 if (s.empty()) return ans;
128 vector<string> parts = decoder::SplitList(s,sep);
130 for (unsigned int k=0;k<parts.size();k++){
131 i=parts[k].find_first_of(rangeSep);
133 if (i == string::npos) {
134 from = atoi(parts[k].c_str());
137 from=atoi(parts[k].substr(0,i).c_str());
138 to =atoi(parts[k].substr(i+1,parts[k].length()-i-1).c_str());
140 ans.push_back(pair<int,int>(from,to));
145 //_____________________________________________________________________
146 vector< pair <double,double> >
147 decoder::DecodeListOfFloatRanges(const string& s,const string& sep,const string& rangeSep)
149 // decodes <s> as a list of double (floating point) ranges
150 // Example : 192.33/303.26 ; -10.2/-1.41
151 // gives a list of two double precision floating point (phew!) pairs:
152 // pair (192.33,303.26) and another pair (-10.2,-1.41)
154 string::size_type i=0;
155 vector< pair <double,double> > ans;
156 if (s.empty()) return ans;
158 vector<string> parts = decoder::SplitList(s,sep);
160 for (unsigned int k=0;k<parts.size();k++){
161 i=parts[k].find_first_of(rangeSep);
163 if (i == string::npos) {
164 from = atof(parts[k].c_str());
167 from=atof(parts[k].substr(0,i).c_str());
168 to =atof(parts[k].substr(i+1,parts[k].length()-i-1).c_str());
170 ans.push_back(pair<double,double>(from,to));