gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-2191-ga4742ac
a Global And Modular Bsm Inference Tool
plugin_comparators.cpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
10 //
21 
22 #include <sstream>
23 
26 
27 namespace Gambit
28 {
29 
30  namespace Scanner
31  {
32 
33  namespace Plugins
34  {
35 
36  VersionCompareBottom::VersionCompareBottom (const std::string &version) : flag (0x00)
37  {
38  if (version.find_first_not_of(" !()|&=<>") == std::string::npos)
39  {
40  flag = 0x00;
41  setEmpty(true);
42  return;
43  }
44  else
45  {
46  std::string::size_type i;
47  for (i = 0; i < version.length(); i++)
48  {
49  if (version[i] == '<')
50  flag |= LESS;
51  else if (version[i] == '>')
52  flag |= GREATER;
53  else if (version[i] == '=')
54  flag |= EQUAL;
55  else if (version[i] == '!')
56  flag |= NEG;
57  else if (version[i] != ' ' && version[i] != '|' && version[i] != '&')
58  break;
59  }
60 
61  if (i == version.length())
62  {
63  flag = 0x00;
64  return;
65  }
66 
67  if (!bool(flag))
68  flag = EQUAL;
69 
70  std::string::size_type pos = version.find("-", i);
71  std::string number;
72 
73  if (pos != std::string::npos)
74  {
75  flag |= RELEASE;
76  release_version = version.substr(pos + 1);
77  number = version.substr(i, pos - i);
78  }
79  else
80  {
81  number = version.substr(i);
82  }
83 
84  pos = number.find(".");
85  if (pos != std::string::npos)
86  {
87  flag |= MAJOR;
88  major_version = StringToInt(number.substr(0, pos));
89 
90  i = number.find(".", pos + 1);
91  if (i != std::string::npos)
92  {
93  flag |= MINOR;
94  minor_version = StringToInt(number.substr(pos + 1, i - pos - 1));
95 
96  pos = number.find(".", i + 1);
97 
98  if (pos != std::string::npos)
99  {
100  flag |= PATCH;
101  patch_version = StringToInt(number.substr(i + 1, pos - i - 1));
102 
103  if (!bool(flag&RELEASE))
104  {
105  flag |= RELEASE;
106  release_version = number.substr(pos + 1);
107  }
108  }
109  else
110  {
111  flag |= PATCH;
112  patch_version = StringToInt(number.substr(i + 1));
113  }
114  }
115  }
116  }
117 
118  unsigned char comp = (flag << 4);
119  comp >>= 4;
120 
121  if (comp == (EQUAL|GREATER|LESS|NEG) ||
122  comp == (GREATER|LESS|NEG) ||
123  comp == (EQUAL|NEG) ||
124  comp == (NEG))
125  f = neq;
126  else if (comp == (EQUAL|GREATER|LESS) ||
127  comp == (GREATER|LESS) ||
128  comp == (EQUAL))
129  f = eq;
130  else if (comp == (EQUAL|GREATER|NEG) ||
131  comp == (LESS))
132  f = lt;
133  else if (comp == (EQUAL|LESS|NEG) ||
134  comp == (GREATER))
135  f = gt;
136  else if (comp == (LESS|NEG) ||
137  comp == (EQUAL|GREATER))
138  f = gte;
139  else if (comp == (GREATER|NEG) ||
140  comp == (EQUAL|LESS))
141  f = lte;
142  }
143 
145  {
146  if ((bool(flag&MAJOR) && (!f(plugin.major_version, major_version))) ||
147  (bool(flag&MINOR) && (!f(plugin.minor_version, minor_version))) ||
148  (bool(flag&PATCH) && (!f(plugin.patch_version, patch_version))) ||
149  (bool(flag&RELEASE) && plugin.release_version != release_version))
150  {
151  return false;
152  }
153 
154  return true;
155  }
156 
157  VersionCompare::VersionCompare(const std::string &version) : compare1(NULL), compare2(NULL), flag(0x00)
158  {
159  std::string::size_type p_pos = version.find_first_of("(|&");
160 
161  if (version.find_first_not_of(" !()|&=<>") == std::string::npos)
162  {
163  setEmpty(true);
164  }
165  else if (p_pos == std::string::npos)
166  {
167  compare1 = new VersionCompareBottom(version);
168  }
169  else if (version[p_pos] == '|')
170  {
171  flag |= OR;
172  compare1 = new VersionCompare(version.substr(0, p_pos));
173  compare2 = new VersionCompare(version.substr(p_pos + 1));
174  }
175  else if (version[p_pos] == '&')
176  {
177  flag |= AND;
178  compare1 = new VersionCompare(version.substr(0, p_pos));
179  compare2 = new VersionCompare(version.substr(p_pos + 1));
180  }
181  else if (version[p_pos] == '(' && p_pos == version.find_first_not_of(" "))
182  {
183  unsigned int p_num = 1;
184  std::string::size_type pos = p_pos;
185 
186  for (;;)
187  {
188  pos = version.find_first_of("()", pos + 1);
189  if (pos != std::string::npos)
190  {
191  if (version[pos] == '(')
192  {
193  p_num++;
194  }
195  else
196  {
197  p_num--;
198  if (p_num == 0)
199  {
200  compare1 = new VersionCompare(version.substr(p_pos +1, pos - p_pos - 1));
201  pos = version.find_first_of("&|", pos + 1);
202  if (pos == std::string::npos)
203  {
204  }
205  else if (version[pos] == '|')
206  {
207  flag |= OR;
208  compare2 = new VersionCompare(version.substr(pos + 1));
209  }
210  else if (version[pos] == '&')
211  {
212  flag |= AND;
213  compare2 = new VersionCompare(version.substr(pos + 1));
214  }
215 
216  break;
217  }
218  }
219  }
220  else
221  {
222  compare1 = new VersionCompare(version.substr(pos + 1));
223  break;
224  }
225  }
226  }
227  else
228  {
229  scan_err << "Could not parse version string \"" << version << "\"." << scan_end;
230  }
231 
232  if (compare1 != NULL && compare1->isEmpty())
233  {
234  delete compare1;
235  compare1 = NULL;
236  }
237 
238  if (compare2 != NULL && compare2->isEmpty())
239  {
240  delete compare2;
241  compare2 = NULL;
242  }
243 
244  if (compare1 == NULL && compare2 == NULL)
245  setEmpty(true);
246 
247  return;
248  }
249 
251  {
252  if (compare1 == NULL && compare2 == NULL)
253  {
254  return true;
255  }
256  else if (compare1 == NULL && compare2 != NULL)
257  {
258  return (*compare2)(plugin);
259  }
260  else if (compare1 != NULL && compare2 == NULL)
261  {
262  return (*compare1)(plugin);
263  }
264 
265  if (bool(flag&OR)) return (*compare1)(plugin) || (*compare2)(plugin);
266  if (bool(flag&AND)) return (*compare1)(plugin) && (*compare2)(plugin);
267 
268  // None of the previous pieces of code have triggered; something must be wrong.
269  scan_err << "Unknown logical operator in VersionCompare () operator." << scan_end;
270  return false;
271  }
272 
274  {
275  if (compare1 != NULL)
276  delete compare1;
277  if (compare2 != NULL)
278  delete compare2;
279  }
280 
281  }
282 
283  }
284 
285 }
286 
int StringToInt(const std::string &str)
Converts a string to an int.
container info for a specific plugin
std::string release_version
release version
#define scan_err
Defined to macros to output errors in the form: scan_err << "error" << scan_end; scan_warn << "warnin...
Utility Functions for the Gambit Scanner.
#define scan_end
TODO: see if we can use this one:
Definition: Analysis.hpp:33
Advanced comparison utilities for scanner plugins.