]>
Commit | Line | Data |
---|---|---|
ba030c0e | 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 | ||
88cb7938 | 16 | /* $Id$ */ |
ba030c0e | 17 | |
18 | // Authors: David Guez, Ivana Hrivnacova, Marion MacCormick; IPN Orsay | |
19 | // | |
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 '(','{','[', ... . | |
25 | // Example: | |
26 | // (string 1) (string 2) [string3] {string4} [ (string5.1) (string5.2) ] | |
27 | // note : |_____________________| | |
28 | // | | |
29 | // this is just ONE substring. | |
30 | ||
31 | ||
32 | #include "AliMUONSt1Decoder.h" | |
33 | ||
ba030c0e | 34 | //_____________________________________________________________________ |
35 | vector<string> decoder::SplitNtuples(const string& s, | |
36 | const string& leftSep, | |
37 | const string& rightSep) | |
38 | ||
39 | { | |
40 | // separate substrings in <s>, by using the first level of delimiters | |
41 | // given in the argument | |
42 | // Example: | |
43 | // (string 1) (string 2) [string3] {string4} [ (string5.1) (string5.2) ] | |
44 | // returns a list of 5 substrings | |
45 | // "string 1" | |
46 | // "string 2" | |
47 | // "string 3" | |
48 | // "string 4" and | |
49 | // " (string5.1) (string5.2) " | |
50 | // -- | |
51 | vector<string> ans; | |
52 | string::size_type idx = 0; | |
53 | do { | |
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()){ | |
58 | idx++; | |
59 | continue; | |
60 | } | |
61 | int count=1; | |
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--; | |
66 | idx2++; | |
67 | } | |
68 | if (count != 0) return ans; // bad format...stop here | |
69 | ans.push_back(s.substr(idx+1,idx2-idx-2)); | |
70 | idx=idx2; | |
71 | } | |
72 | } while (idx != string::npos); | |
73 | return ans; | |
74 | } | |
75 | ||
76 | //_____________________________________________________________________ | |
77 | vector<string> decoder::SplitList(const string& s,const string& sep) | |
78 | { | |
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 ";" | |
85 | // -- | |
86 | vector<string> ans; | |
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)); | |
90 | i=j+1; | |
91 | } | |
92 | ans.push_back(s.substr(i,s.length()-i)); | |
93 | return ans; | |
94 | } | |
95 | ||
96 | //_____________________________________________________________________ | |
97 | vector<int> | |
98 | decoder::DecodeListRanges(const string& s,const string& sep,const string& rangeSep) | |
99 | { | |
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 | |
104 | // -- | |
105 | vector<int> ans; | |
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++){ | |
109 | ans.push_back(j); | |
110 | } | |
111 | } | |
112 | return ans; | |
113 | } | |
114 | ||
115 | //_____________________________________________________________________ | |
116 | vector< pair <int,int> > | |
117 | decoder::DecodeListOfIntRanges(const string& s,const string& sep,const string& rangeSep) | |
118 | { | |
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) | |
123 | // -- | |
124 | string::size_type i=0; | |
125 | vector< pair <int,int> > ans; | |
126 | if (s.empty()) return ans; | |
127 | ||
128 | vector<string> parts = decoder::SplitList(s,sep); | |
129 | ||
130 | for (unsigned int k=0;k<parts.size();k++){ | |
131 | i=parts[k].find_first_of(rangeSep); | |
132 | int from,to; | |
133 | if (i == string::npos) { | |
134 | from = atoi(parts[k].c_str()); | |
135 | to = from; | |
136 | } else { | |
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()); | |
139 | } | |
140 | ans.push_back(pair<int,int>(from,to)); | |
141 | } | |
142 | return ans; | |
143 | } | |
144 | ||
145 | //_____________________________________________________________________ | |
146 | vector< pair <double,double> > | |
147 | decoder::DecodeListOfFloatRanges(const string& s,const string& sep,const string& rangeSep) | |
148 | { | |
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) | |
153 | // -- | |
154 | string::size_type i=0; | |
155 | vector< pair <double,double> > ans; | |
156 | if (s.empty()) return ans; | |
157 | ||
158 | vector<string> parts = decoder::SplitList(s,sep); | |
159 | ||
160 | for (unsigned int k=0;k<parts.size();k++){ | |
161 | i=parts[k].find_first_of(rangeSep); | |
162 | double from,to; | |
163 | if (i == string::npos) { | |
164 | from = atof(parts[k].c_str()); | |
165 | to = from; | |
166 | } else { | |
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()); | |
169 | } | |
170 | ans.push_back(pair<double,double>(from,to)); | |
171 | } | |
172 | return ans; | |
173 | } |